File: confine_collection_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (132 lines) | stat: -rw-r--r-- 5,007 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

require 'puppet/confine_collection'

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

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

  it "should make its label available" do
    expect(Puppet::ConfineCollection.new("mylabel").label).to eq("mylabel")
  end

  describe "when creating confine instances" do
    it "should create an instance of the named test with the provided values" do
      test_class = double('test_class')
      expect(test_class).to receive(:new).with(%w{my values}).and_return(double('confine', :label= => nil))
      expect(Puppet::Confine).to receive(:test).with(:foo).and_return(test_class)

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

    it "should copy its label to the confine instance" do
      confine = double('confine')
      test_class = double('test_class')
      expect(test_class).to receive(:new).and_return(confine)
      expect(Puppet::Confine).to receive(:test).and_return(test_class)

      expect(confine).to receive(:label=).with("label")

      Puppet::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::Confine.test(:variable).new(%w{my values})
        expect(confine).to receive(:name=).with(:foo)
        expect(confine.class).to receive(:new).with(%w{my values}).and_return(confine)
        Puppet::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::Confine.test(:exists).new(:bar)
        expect(confine).to receive(:for_binary=).with(true)
        expect(Puppet::Confine.test(:exists)).to receive(:new).with(:bar).and_return(confine)
        Puppet::ConfineCollection.new("label").confine :exists => :bar, :for_binary => true
      end
    end
  end

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

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

    expect(Puppet::Confine.test(:true)).to receive(:new).and_return(c1)
    expect(Puppet::Confine.test(:false)).to receive(:new).and_return(c2)

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

    expect(confiner).to be_valid
  end

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

    expect(Puppet::Confine.test(:true)).to receive(:new).and_return(c1)
    expect(Puppet::Confine.test(:false)).to receive(:new).and_return(c2)

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

    expect(confiner).not_to be_valid
  end

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

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

    it "should return an empty hash if the confiner is valid" do
      expect(@confiner.summary).to eq({})
    end

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

      expect(@confiner.summary).to eq({:true => :tsumm, :false => :fsumm})
    end

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

      expect(@confiner.summary).to eq({:false => :fsumm})
    end

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

      expect(@confiner.summary).to eq({:false => :fsumm})
    end

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

      expect(@confiner.summary).to eq({:false => :fsumm})
    end
  end
end