diff --git a/lib/puppet/indirector/facts/facter.rb b/lib/puppet/indirector/facts/facter.rb index d704f91fd..e22977568 100644 --- a/lib/puppet/indirector/facts/facter.rb +++ b/lib/puppet/indirector/facts/facter.rb @@ -1,91 +1,91 @@ require 'puppet/node/facts' require 'puppet/indirector/code' class Puppet::Node::Facts::Facter < Puppet::Indirector::Code desc "Retrieve facts from Facter. This provides a somewhat abstract interface between Puppet and Facter. It's only `somewhat` abstract because it always returns the local host's facts, regardless of what you attempt to find." private def self.reload_facter Facter.clear Facter.loadfacts end def self.load_fact_plugins # Add any per-module fact directories to the factpath module_fact_dirs = Puppet.lookup(:current_environment).modulepath.collect do |d| ["lib", "plugins"].map do |subdirectory| Dir.glob("#{d}/*/#{subdirectory}/facter") end end.flatten dirs = module_fact_dirs + Puppet[:factpath].split(File::PATH_SEPARATOR) dirs.uniq.each do |dir| load_facts_in_dir(dir) end end def self.setup_external_facts(request) # Add any per-module fact directories to the factpath external_facts_dirs = [] request.environment.modules.each do |m| if m.has_external_facts? Puppet.info "Loading external facts from #{m.plugin_fact_directory}" external_facts_dirs << m.plugin_fact_directory end end # Add system external fact directory if it exists if File.directory?(Puppet[:pluginfactdest]) external_facts_dirs << Puppet[:pluginfactdest] end # Add to facter config Facter.search_external external_facts_dirs end def self.load_facts_in_dir(dir) return unless FileTest.directory?(dir) Dir.chdir(dir) do Dir.glob("*.rb").each do |file| fqfile = ::File.join(dir, file) begin Puppet.info "Loading facts in #{fqfile}" ::Timeout::timeout(Puppet[:configtimeout]) do - load file + load File.join('.', file) end rescue SystemExit,NoMemoryError raise rescue Exception => detail Puppet.warning "Could not load fact file #{fqfile}: #{detail}" end end end end public def destroy(facts) raise Puppet::DevError, "You cannot destroy facts in the code store; it is only used for getting facts from Facter" end # Look a host's facts up in Facter. def find(request) self.class.setup_external_facts(request) if Puppet.features.external_facts? self.class.reload_facter self.class.load_fact_plugins result = Puppet::Node::Facts.new(request.key, Facter.to_hash) result.add_local_facts Puppet[:stringify_facts] ? result.stringify : result.sanitize result end def save(facts) raise Puppet::DevError, "You cannot save facts to the code store; it is only used for getting facts from Facter" end end diff --git a/spec/unit/indirector/facts/facter_spec.rb b/spec/unit/indirector/facts/facter_spec.rb index cb90fb7c3..cf6dad908 100755 --- a/spec/unit/indirector/facts/facter_spec.rb +++ b/spec/unit/indirector/facts/facter_spec.rb @@ -1,203 +1,203 @@ #! /usr/bin/env ruby require 'spec_helper' require 'puppet/indirector/facts/facter' module PuppetNodeFactsFacter describe Puppet::Node::Facts::Facter do FS = Puppet::FileSystem it "should be a subclass of the Code terminus" do Puppet::Node::Facts::Facter.superclass.should equal(Puppet::Indirector::Code) end it "should have documentation" do Puppet::Node::Facts::Facter.doc.should_not be_nil end it "should be registered with the configuration store indirection" do indirection = Puppet::Indirector::Indirection.instance(:facts) Puppet::Node::Facts::Facter.indirection.should equal(indirection) end it "should have its name set to :facter" do Puppet::Node::Facts::Facter.name.should == :facter end describe "when reloading Facter" do before do @facter_class = Puppet::Node::Facts::Facter Facter.stubs(:clear) Facter.stubs(:load) Facter.stubs(:loadfacts) end it "should clear Facter" do Facter.expects(:clear) @facter_class.reload_facter end it "should load all Facter facts" do Facter.expects(:loadfacts) @facter_class.reload_facter end end end describe Puppet::Node::Facts::Facter do before :each do Puppet::Node::Facts::Facter.stubs(:reload_facter) @facter = Puppet::Node::Facts::Facter.new Facter.stubs(:to_hash).returns({}) @name = "me" @request = stub 'request', :key => @name @environment = stub 'environment' @request.stubs(:environment).returns(@environment) @request.environment.stubs(:modules).returns([]) end describe Puppet::Node::Facts::Facter, " when finding facts" do it "should reset and load facts" do clear = sequence 'clear' Puppet::Node::Facts::Facter.expects(:reload_facter).in_sequence(clear) Puppet::Node::Facts::Facter.expects(:load_fact_plugins).in_sequence(clear) @facter.find(@request) end it "should include external facts when feature is present" do clear = sequence 'clear' Puppet.features.stubs(:external_facts?).returns(:true) Puppet::Node::Facts::Facter.expects(:setup_external_facts).in_sequence(clear) Puppet::Node::Facts::Facter.expects(:reload_facter).in_sequence(clear) Puppet::Node::Facts::Facter.expects(:load_fact_plugins).in_sequence(clear) @facter.find(@request) end it "should return a Facts instance" do @facter.find(@request).should be_instance_of(Puppet::Node::Facts) end it "should return a Facts instance with the provided key as the name" do @facter.find(@request).name.should == @name end it "should return the Facter facts as the values in the Facts instance" do Facter.expects(:to_hash).returns("one" => "two") facts = @facter.find(@request) facts.values["one"].should == "two" end it "should add local facts" do facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:add_local_facts) @facter.find(@request) end it "should convert facts into strings when stringify_facts is true" do Puppet[:stringify_facts] = true facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:stringify) @facter.find(@request) end it "should sanitize facts when stringify_facts is false" do Puppet[:stringify_facts] = false facts = Puppet::Node::Facts.new("foo") Puppet::Node::Facts.expects(:new).returns facts facts.expects(:sanitize) @facter.find(@request) end end describe Puppet::Node::Facts::Facter, " when saving facts" do it "should fail" do proc { @facter.save(@facts) }.should raise_error(Puppet::DevError) end end describe Puppet::Node::Facts::Facter, " when destroying facts" do it "should fail" do proc { @facter.destroy(@facts) }.should raise_error(Puppet::DevError) end end it "should skip files when asked to load a directory" do FileTest.expects(:directory?).with("myfile").returns false Puppet::Node::Facts::Facter.load_facts_in_dir("myfile") end it "should load each ruby file when asked to load a directory" do FileTest.expects(:directory?).with("mydir").returns true Dir.expects(:chdir).with("mydir").yields Dir.expects(:glob).with("*.rb").returns %w{a.rb b.rb} - Puppet::Node::Facts::Facter.expects(:load).with("a.rb") - Puppet::Node::Facts::Facter.expects(:load).with("b.rb") + Puppet::Node::Facts::Facter.expects(:load).with File.join('.', 'a.rb') + Puppet::Node::Facts::Facter.expects(:load).with File.join('.', 'b.rb') Puppet::Node::Facts::Facter.load_facts_in_dir("mydir") end it "should include pluginfactdest when loading external facts", :if => (Puppet.features.external_facts? and not Puppet.features.microsoft_windows?) do Puppet[:pluginfactdest] = "/plugin/dest" @facter.find(@request) Facter.search_external_path.include?("/plugin/dest") end it "should include pluginfactdest when loading external facts", :if => (Puppet.features.external_facts? and Puppet.features.microsoft_windows?) do Puppet[:pluginfactdest] = "/plugin/dest" @facter.find(@request) Facter.search_external_path.include?("C:/plugin/dest") end describe "when loading fact plugins from disk" do let(:one) { File.expand_path("one") } let(:two) { File.expand_path("two") } it "should load each directory in the Fact path" do Puppet[:factpath] = [one, two].join(File::PATH_SEPARATOR) Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with(one) Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with(two) Puppet::Node::Facts::Facter.load_fact_plugins end it "should load all facts from the modules" do Puppet::Node::Facts::Facter.stubs(:load_facts_in_dir) Dir.stubs(:glob).returns [] Dir.expects(:glob).with("#{one}/*/lib/facter").returns %w{oneA oneB} Dir.expects(:glob).with("#{two}/*/lib/facter").returns %w{twoA twoB} Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneA") Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("oneB") Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoA") Puppet::Node::Facts::Facter.expects(:load_facts_in_dir).with("twoB") FS.overlay(FS::MemoryFile.a_directory(one), FS::MemoryFile.a_directory(two)) do Puppet.override(:current_environment => Puppet::Node::Environment.create(:testing, [one, two], "")) do Puppet::Node::Facts::Facter.load_fact_plugins end end end it "should include module plugin facts when present", :if => Puppet.features.external_facts? do mod = Puppet::Module.new("mymodule", "#{one}/mymodule", @request.environment) @request.environment.stubs(:modules).returns([mod]) @facter.find(@request) Facter.search_external_path.include?("#{one}/mymodule/facts.d") end end end end