summaryrefslogtreecommitdiffstats
path: root/spec/unit/provider/confine_collection_spec.rb
blob: 444281c7756fbfd65936f8745d61d0e616a7b7b6 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/provider/confine_collection'

describe Puppet::Provider::ConfineCollection do
    it "should be able to add confines" do
        Puppet::Provider::ConfineCollection.new("label").should respond_to(:confine)
    end

    it "should require a label at initialization" do
        lambda { Puppet::Provider::ConfineCollection.new }.should raise_error(ArgumentError)
    end

    it "should make its label available" do
        Puppet::Provider::ConfineCollection.new("mylabel").label.should == "mylabel"
    end

    describe "when creating confine instances" do
        it "should create an instance of the named test with the provided values" do
            test_class = mock 'test_class'
            test_class.expects(:new).with(%w{my values}).returns(stub('confine', :label= => nil))
            Puppet::Provider::Confine.expects(:test).with(:foo).returns test_class

            Puppet::Provider::ConfineCollection.new("label").confine :foo => %w{my values}
        end

        it "should copy its label to the confine instance" do
            confine = mock 'confine'
            test_class = mock 'test_class'
            test_class.expects(:new).returns confine
            Puppet::Provider::Confine.expects(:test).returns test_class

            confine.expects(:label=).with("label")

            Puppet::Provider::ConfineCollection.new("label").confine :foo => %w{my values}
        end

        describe "and the test cannot be found" do
            it "should create a Facter test with the provided values and set the name to the test name" do
                confine = Puppet::Provider::Confine.test(:variable).new(%w{my values})
                confine.expects(:name=).with(:foo)
                confine.class.expects(:new).with(%w{my values}).returns confine
                Puppet::Provider::ConfineCollection.new("label").confine(:foo => %w{my values})
            end
        end

        describe "and the 'for_binary' option was provided" do
            it "should mark the test as a binary confine" do
                confine = Puppet::Provider::Confine.test(:exists).new(:bar)
                confine.expects(:for_binary=).with true
                Puppet::Provider::Confine.test(:exists).expects(:new).with(:bar).returns confine
                Puppet::Provider::ConfineCollection.new("label").confine :exists => :bar, :for_binary => true
            end
        end
    end

    it "should be valid if no confines are present" do
        Puppet::Provider::ConfineCollection.new("label").should be_valid
    end

    it "should be valid if all confines pass" do
        c1 = stub 'c1', :valid? => true, :label= => nil
        c2 = stub 'c2', :valid? => true, :label= => nil

        Puppet::Provider::Confine.test(:true).expects(:new).returns(c1)
        Puppet::Provider::Confine.test(:false).expects(:new).returns(c2)

        confiner = Puppet::Provider::ConfineCollection.new("label")
        confiner.confine :true => :bar, :false => :bee

        confiner.should be_valid
    end

    it "should not be valid if any confines fail" do
        c1 = stub 'c1', :valid? => true, :label= => nil
        c2 = stub 'c2', :valid? => false, :label= => nil

        Puppet::Provider::Confine.test(:true).expects(:new).returns(c1)
        Puppet::Provider::Confine.test(:false).expects(:new).returns(c2)

        confiner = Puppet::Provider::ConfineCollection.new("label")
        confiner.confine :true => :bar, :false => :bee

        confiner.should_not be_valid
    end

    describe "when providing a summary" do
        before do
            @confiner = Puppet::Provider::ConfineCollection.new("label")
        end

        it "should return a hash" do
            @confiner.summary.should be_instance_of(Hash)
        end

        it "should return an empty hash if the confiner is valid" do
            @confiner.summary.should == {}
        end

        it "should add each test type's summary to the hash" do
            @confiner.confine :true => :bar, :false => :bee
            Puppet::Provider::Confine.test(:true).expects(:summarize).returns :tsumm
            Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm

            @confiner.summary.should == {:true => :tsumm, :false => :fsumm}
        end

        it "should not include tests that return 0" do
            @confiner.confine :true => :bar, :false => :bee
            Puppet::Provider::Confine.test(:true).expects(:summarize).returns 0
            Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm

            @confiner.summary.should == {:false => :fsumm}
        end

        it "should not include tests that return empty arrays" do
            @confiner.confine :true => :bar, :false => :bee
            Puppet::Provider::Confine.test(:true).expects(:summarize).returns []
            Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm

            @confiner.summary.should == {:false => :fsumm}
        end

        it "should not include tests that return empty hashes" do
            @confiner.confine :true => :bar, :false => :bee
            Puppet::Provider::Confine.test(:true).expects(:summarize).returns({})
            Puppet::Provider::Confine.test(:false).expects(:summarize).returns :fsumm

            @confiner.summary.should == {:false => :fsumm}
        end
    end
end