diff options
-rw-r--r-- | lib/facter.rb | 47 | ||||
-rw-r--r-- | tests/tc_simple.rb | 68 |
2 files changed, 109 insertions, 6 deletions
diff --git a/lib/facter.rb b/lib/facter.rb index 8415f81..a246845 100644 --- a/lib/facter.rb +++ b/lib/facter.rb @@ -55,14 +55,26 @@ FACTERVERSION = '1.1.4' # Return a fact object by name. If you use this, you still have to call # 'value' on it to retrieve the actual value. def Facter.[](name) - @@facts[name.to_s.downcase] + name = name.to_s.downcase + unless @@facts.include?(name) + # Try autoloading the fact. + begin + require "facter/#{name}" + unless @@facts.include?(name) + warn "Loaded facter/#{name} but fact was not added" + end + rescue LoadError + # Just ignore it + end + end + @@facts[name] end # Add a resolution mechanism for a named fact. This does not distinguish # between adding a new fact and adding a new way to resolve a fact. def Facter.add(name, &block) fact = nil - dcname = name.downcase + dcname = name.to_s.downcase if @@facts.include?(dcname) fact = @@facts[dcname] @@ -93,6 +105,14 @@ FACTERVERSION = '1.1.4' end } end + + def method_missing(name, *args) + if fact = self[name] + return fact.value + else + super + end + end end def Facter.value(name) @@ -146,6 +166,14 @@ FACTERVERSION = '1.1.4' return @@facts.keys end + # Return a hash of all of our facts. + def Facter.to_hash + self.inject({}) do |h, ary| + h[ary[0]] = ary[1] + h + end + end + # Compare one value to another. def <=>(other) return self.value <=> other @@ -158,7 +186,7 @@ FACTERVERSION = '1.1.4' # Create a new fact, with no resolution mechanisms. def initialize(name) - @name = name.downcase + @name = name.downcase if name.is_a? String if @@facts.include?(@name) raise ArgumentError, "A fact named %s already exists" % name else @@ -520,6 +548,19 @@ FACTERVERSION = '1.1.4' #tag("operatingsystem","SunOS") end + Facter.add("Architecture") do + tag("operatingsystem","Debian") + setcode do + model = Facter.hardwaremodel + case model + when 'x86_64': "amd64" + when /(i[3456]86|pentium)/: "i386" + else + model + end + end + end + Facter.add("CfKey") do setcode do value = nil diff --git a/tests/tc_simple.rb b/tests/tc_simple.rb index efc7d21..8510fcf 100644 --- a/tests/tc_simple.rb +++ b/tests/tc_simple.rb @@ -15,6 +15,8 @@ end class TestFacter < Test::Unit::TestCase def setup Facter.load + + @tmpfiles = [] end def teardown @@ -22,9 +24,11 @@ class TestFacter < Test::Unit::TestCase Facter.reset Facter.flush - #if ! @oldhandles.empty? - # $stdin, $stdout, $stderr = @oldhandles - #end + @tmpfiles.each do |file| + if FileTest.exists?(file) + system("rm -rf %s" % file) + end + end end def test_version @@ -262,4 +266,62 @@ class TestFacter < Test::Unit::TestCase assert(fact.ldapname, "Fact %s has no ldapname" % name) } end + + def test_hash + hash = nil + assert_nothing_raised { + hash = Facter.to_hash + } + + assert_instance_of(Hash, hash) + + hash.each do |name, value| + assert_instance_of(String, name) + assert_instance_of(String, value) + end + end + + # Verify we can call retrieve facts as methods + def test_factfunction + val = nil + assert_nothing_raised { + val = Facter.operatingsystem + } + + assert_equal(Facter["operatingsystem"].value, val) + + assert_raise(NoMethodError) { Facter.nosuchfact } + end + + # Verify we can autoload facts. + def test_autoloading + dir = "/tmp/facterloading" + @tmpfiles << dir + Dir.mkdir(dir) + Dir.mkdir(File.join(dir, "facter")) + $: << dir + + # Make sure we don't have a value right now. + assert_raise(NoMethodError) do + Facter.autoloadfact + end + assert_nil(Facter["autoloadfact"]) + + val = "autoloadedness" + File.open(File.join(dir, "facter", "autoloadfact.rb"), "w") do |file| + file.puts %{ +Facter.add("AutoloadFact") do + setcode { "#{val}" } +end +} + end + + ret = nil + assert_nothing_raised do + ret = Facter.autoloadfact + end + assert_equal(val, ret, "Got incorrect value for autoloaded fact") + assert_equal(val, Facter["autoloadfact"].value, + "Got incorrect value for autoloaded fact") + end end |