summaryrefslogtreecommitdiffstats
path: root/spec/unit/agent/fact_handler.rb
blob: b58f55ebc14652a3f9ffa93fa5e22dc072074e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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