Codebase list facter / 2255abe
(#7670) Never fail to find a fact that is present With the previous behavior, any fact which depended on another fact in a file not matching its name would fail to properly load the required fact, resulting in missing, incorrect, or inconsistent values. For instance, the operatingsystem fact depends on the lsbdistid fact found in lsb.rb. The first time the operatingsystem fact is requested, it requires lsb.rb, and so the required fact is loaded first. But if Facter is subsequently cleared and the operatingsystem fact requested again, the require will not occur, and the fact will not be automatically loaded because it doesn't match its filename. Now if a fact is requested and can't be found, we will attempt to load all the facts to find it. Such an approach appears heavy-handed, but in the case where we want a particular fact, this is the only way to make sure we've found it. In the case where we eventually want other facts, we are conveniently eagerly loading them. Paired-With: Jacob Helwig <jacob@puppetlabs.com> Nick Lewis 12 years ago
6 changed file(s) with 17 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
6565 def fact(name)
6666 name = canonize(name)
6767
68 # Try to load the fact if necessary
6869 loader.load(name) unless @facts[name]
6970
70 return @facts[name]
71 # Try HARDER
72 loader.load_all unless @facts[name]
73
74 @facts[name]
7175 end
7276
7377 # Flush all cached values.
1717
1818 # Ensure that we don't accidentally cache between test cases.
1919 config.before :each do
20 Facter::Util::Loader.any_instance.stubs(:load_all)
2021 Facter.clear
2122 end
2223 end
22 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
33
44 require 'facter'
5 require 'facter/util/ip'
56
67 describe "Per Interface IP facts" do
7 before do
8 Facter.loadfacts
9 end
10
118 it "should replace the ':' in an interface list with '_'" do
129 # So we look supported
1310 Facter.fact(:kernel).stubs(:value).returns("SunOS")
1411
15 Facter::Util::IP.expects(:get_interfaces).returns %w{eth0:1 eth1:2}
12 Facter::Util::IP.stubs(:get_interfaces).returns %w{eth0:1 eth1:2}
1613 Facter.fact(:interfaces).value.should == %{eth0_1,eth1_2}
1714 end
1815 end
55
66 describe "Memory facts" do
77 before do
8 Facter.loadfacts
8 # We need these facts loaded, but they belong to a file with a
9 # different name, so load the file explicitly.
10 Facter.collection.loader.load(:memory)
911 end
1012
1113 after do
3333
3434 it "should identify Oracle VM as OVS" do
3535 Facter.fact(:kernel).stubs(:value).returns("Linux")
36 Facter.stubs(:value).with(:lsbdistid).returns(nil)
3637 FileTest.stubs(:exists?).returns false
3738
3839 FileTest.expects(:exists?).with("/etc/ovs-release").returns true
4344
4445 it "should identify VMWare ESX" do
4546 Facter.fact(:kernel).stubs(:value).returns("Linux")
47 Facter.stubs(:value).with(:lsbdistid).returns(nil)
4648 FileTest.stubs(:exists?).returns false
4749
4850 FileTest.expects(:exists?).with("/etc/vmware-release").returns true
1818
1919
2020 describe Facter::Util::Loader do
21 before :each do
22 Facter::Util::Loader.any_instance.unstub(:load_all)
23 end
24
2125 def with_env(values)
2226 old = {}
2327 values.each do |var, value|