summaryrefslogtreecommitdiffstats
path: root/spec/unit/agent/fact_handler.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/agent/fact_handler.rb')
-rwxr-xr-xspec/unit/agent/fact_handler.rb117
1 files changed, 117 insertions, 0 deletions
diff --git a/spec/unit/agent/fact_handler.rb b/spec/unit/agent/fact_handler.rb
new file mode 100755
index 000000000..b58f55ebc
--- /dev/null
+++ b/spec/unit/agent/fact_handler.rb
@@ -0,0 +1,117 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+require 'puppet/agent'
+require 'puppet/agent/fact_handler'
+
+class FactHandlerTester
+ include Puppet::Agent::FactHandler
+end
+
+describe Puppet::Agent::FactHandler do
+ before do
+ @facthandler = FactHandlerTester.new
+ end
+
+ it "should have a method for downloading fact plugins" do
+ @facthandler.should respond_to(:download_fact_plugins)
+ end
+
+ it "should have a boolean method for determining whether fact plugins should be downloaded" do
+ @facthandler.should respond_to(:download_fact_plugins?)
+ end
+
+ it "should download fact plugins when :factsync is true" do
+ Puppet.settings.expects(:value).with(:factsync).returns true
+ @facthandler.should be_download_fact_plugins
+ end
+
+ it "should not download fact plugins when :factsync is false" do
+ Puppet.settings.expects(:value).with(:factsync).returns false
+ @facthandler.should_not be_download_fact_plugins
+ end
+
+ it "should not download fact plugins when downloading is disabled" do
+ Puppet::Agent::Downloader.expects(:new).never
+ @facthandler.expects(:download_fact_plugins?).returns false
+ @facthandler.download_fact_plugins
+ end
+
+ it "should use an Agent Downloader, with the name, source, destination, and ignore set correctly, to download fact plugins when downloading is enabled" do
+ downloader = mock 'downloader'
+
+ Puppet.settings.expects(:value).with(:factsource).returns "fsource"
+ Puppet.settings.expects(:value).with(:factdest).returns "fdest"
+ Puppet.settings.expects(:value).with(:factsignore).returns "fignore"
+
+ Puppet::Agent::Downloader.expects(:new).with("fact", "fsource", "fdest", "fignore").returns downloader
+
+ downloader.expects(:evaluate)
+
+ @facthandler.expects(:download_fact_plugins?).returns true
+ @facthandler.download_fact_plugins
+ end
+
+ it "should have a method for uploading facts" do
+ @facthandler.should respond_to(:upload_facts)
+ end
+
+ it "should reload Facter and find local facts when asked to upload facts" do
+ @facthandler.expects(:reload_facter)
+
+ Puppet.settings.expects(:value).with(:certname).returns "myhost"
+ Puppet::Node::Facts.expects(:find).with("myhost")
+
+ @facthandler.upload_facts
+ end
+
+ describe "when reloading Facter" do
+ before do
+ Facter.stubs(:clear)
+ Facter.stubs(:load)
+ Facter.stubs(:loadfacts)
+ end
+
+ it "should clear Facter" do
+ Facter.expects(:clear)
+ @facthandler.reload_facter
+ end
+
+ it "should load all Facter facts" do
+ Facter.expects(:loadfacts)
+ @facthandler.reload_facter
+ end
+
+ it "should load all Puppet Fact plugins" do
+ @facthandler.expects(:load_fact_plugins)
+ @facthandler.reload_facter
+ end
+ end
+
+ it "should load each directory in the Fact path when loading fact plugins" do
+ Puppet.settings.expects(:value).with(:factpath).returns("one%stwo" % File::PATH_SEPARATOR)
+
+ @facthandler.expects(:load_facts_in_dir).with("one")
+ @facthandler.expects(:load_facts_in_dir).with("two")
+
+ @facthandler.load_fact_plugins
+ end
+
+ it "should skip files when asked to load a directory" do
+ FileTest.expects(:directory?).with("myfile").returns false
+
+ @facthandler.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}
+
+ @facthandler.expects(:load).with("a.rb")
+ @facthandler.expects(:load).with("b.rb")
+
+ @facthandler.load_facts_in_dir("mydir")
+ end
+end