summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2006-05-23 15:14:11 +0000
committerluke <luke@1f5c1d6a-bddf-0310-8f58-fc49e503516a>2006-05-23 15:14:11 +0000
commit22bd24b2a2d29431320f67ebbb93d36fbae1c9ba (patch)
treef67d901857a16afb36c245b63095d3b953b5fb34
parentfe782b9596ee8b727f0b91e08318f27a99ab5250 (diff)
downloadfacter-22bd24b2a2d29431320f67ebbb93d36fbae1c9ba.tar.gz
facter-22bd24b2a2d29431320f67ebbb93d36fbae1c9ba.tar.xz
facter-22bd24b2a2d29431320f67ebbb93d36fbae1c9ba.zip
Added "architecture" fact, added the ability to autoload facts from separate files, and added the ability to retrieve fact values via a method for each fact.
git-svn-id: http://reductivelabs.com/svn/facter/trunk@101 1f5c1d6a-bddf-0310-8f58-fc49e503516a
-rw-r--r--lib/facter.rb47
-rw-r--r--tests/tc_simple.rb68
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