File: face_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 (219 lines) | stat: -rw-r--r-- 7,638 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require 'spec_helper'

require 'tmpdir'
require 'puppet/interface'

describe Puppet::Interface::FaceCollection do

  # To prevent conflicts with other specs that use faces, we must save and restore global state.
  # Because there are specs that do 'describe Puppet::Face[...]', we must restore the same objects otherwise
  # the 'subject' of the specs will differ.
  before :all do
    # Save FaceCollection's global state
    faces = described_class.instance_variable_get(:@faces)
    @faces = faces.dup
    faces.each do |k, v|
      @faces[k] = v.dup
    end
    @faces_loaded = described_class.instance_variable_get(:@loaded)

    # Save the already required face files
    @required = []
    $".each do |path|
      @required << path if path =~ /face\/.*\.rb$/
    end

    # Save Autoload's global state
    @loaded = Puppet::Util::Autoload.instance_variable_get(:@loaded).dup
  end

  after :all do
    # Restore global state
    described_class.instance_variable_set :@faces, @faces
    described_class.instance_variable_set :@loaded, @faces_loaded
    $".delete_if { |path| path =~ /face\/.*\.rb$/ }
    @required.each { |path| $".push path unless $".include? path }
    Puppet::Util::Autoload.instance_variable_set(:@loaded, @loaded)
  end

  before :each do
    # Before each test, clear the faces
    subject.instance_variable_get(:@faces).clear
    subject.instance_variable_set(:@loaded, false)
    Puppet::Util::Autoload.instance_variable_get(:@loaded).clear
    $".delete_if { |path| path =~ /face\/.*\.rb$/ }
  end

  describe "::[]" do
    before :each do
      subject.instance_variable_get("@faces")[:foo][SemanticPuppet::Version.parse('0.0.1')] = 10
    end

    it "should return the face with the given name" do
      expect(subject["foo", '0.0.1']).to eq(10)
    end

    it "should attempt to load the face if it isn't found" do
      expect(subject).to receive(:require).once.with('puppet/face/bar')
      expect(subject).to receive(:require).once.with('puppet/face/0.0.1/bar')
      subject["bar", '0.0.1']
    end

    it "should attempt to load the default face for the specified version :current" do
      expect(subject).to receive(:require).with('puppet/face/fozzie')
      subject['fozzie', :current]
    end

    it "should return true if the face specified is registered" do
      subject.instance_variable_get("@faces")[:foo][SemanticPuppet::Version.parse('0.0.1')] = 10
      expect(subject["foo", '0.0.1']).to eq(10)
    end

    it "should attempt to require the face if it is not registered" do
      expect(subject).to receive(:require) do |file|
        subject.instance_variable_get("@faces")[:bar][SemanticPuppet::Version.parse('0.0.1')] = true
        expect(file).to eq('puppet/face/bar')
      end

      expect(subject["bar", '0.0.1']).to be_truthy
    end

    it "should return false if the face is not registered" do
      allow(subject).to receive(:require).and_return(true)
      expect(subject["bar", '0.0.1']).to be_falsey
    end

    it "should return false if the face file itself is missing" do
      num_calls = 0
      allow(subject).to receive(:require) do
        num_calls += 1
        if num_calls == 1
          raise LoadError.new('no such file to load -- puppet/face/bar')
        else
          raise LoadError.new('no such file to load -- puppet/face/0.0.1/bar')
        end
      end

      expect(subject["bar", '0.0.1']).to be_falsey
    end

    it "should register the version loaded by `:current` as `:current`" do
      expect(subject).to receive(:require) do |file|
        subject.instance_variable_get("@faces")[:huzzah]['2.0.1'] = :huzzah_face
        expect(file).to eq('puppet/face/huzzah')
      end
      subject["huzzah", :current]
      expect(subject.instance_variable_get("@faces")[:huzzah][:current]).to eq(:huzzah_face)
    end

    context "with something on disk" do
      it "should register the version loaded from `puppet/face/{name}` as `:current`" do
        expect(subject["huzzah", '2.0.1']).to be
        expect(subject["huzzah", :current]).to be
        expect(Puppet::Face[:huzzah, '2.0.1']).to eq(Puppet::Face[:huzzah, :current])
      end

      it "should index :current when the code was pre-required" do
        expect(subject.instance_variable_get("@faces")[:huzzah]).not_to be_key :current
        require 'puppet/face/huzzah'
        expect(subject[:huzzah, :current]).to be_truthy
      end
    end

    it "should not cause an invalid face to be enumerated later" do
      expect(subject[:there_is_no_face, :current]).to be_falsey
      expect(subject.faces).not_to include :there_is_no_face
    end
  end

  describe "::get_action_for_face" do
    it "should return an action on the current face" do
      expect(Puppet::Face::FaceCollection.get_action_for_face(:huzzah, :bar, :current)).
        to be_an_instance_of Puppet::Interface::Action
    end

    it "should return an action on an older version of a face" do
      action = Puppet::Face::FaceCollection.
        get_action_for_face(:huzzah, :obsolete, :current)

      expect(action).to be_an_instance_of Puppet::Interface::Action
      expect(action.face.version).to eq(SemanticPuppet::Version.parse('1.0.0'))
    end

    it "should load the full older version of a face" do
      action = Puppet::Face::FaceCollection.
        get_action_for_face(:huzzah, :obsolete, :current)

      expect(action.face.version).to eq(SemanticPuppet::Version.parse('1.0.0'))
      expect(action.face).to be_action :obsolete_in_core
    end

    it "should not add obsolete actions to the current version" do
      action = Puppet::Face::FaceCollection.
        get_action_for_face(:huzzah, :obsolete, :current)

      expect(action.face.version).to eq(SemanticPuppet::Version.parse('1.0.0'))
      expect(action.face).to be_action :obsolete_in_core

      current = Puppet::Face[:huzzah, :current]
      expect(current.version).to eq(SemanticPuppet::Version.parse('2.0.1'))
      expect(current).not_to be_action :obsolete_in_core
      expect(current).not_to be_action :obsolete
    end
  end

  describe "::register" do
    it "should store the face by name" do
      face = Puppet::Face.new(:my_face, '0.0.1')
      subject.register(face)
      expect(subject.instance_variable_get("@faces")).to eq({
        :my_face => { face.version => face }
      })
    end
  end

  describe "::underscorize" do
    faulty = [1, "23foo", "#foo", "$bar", "sturm und drang", :"sturm und drang"]
    valid  = {
      "Foo"       => :foo,
      :Foo        => :foo,
      "foo_bar"   => :foo_bar,
      :foo_bar    => :foo_bar,
      "foo-bar"   => :foo_bar,
      :"foo-bar"  => :foo_bar,
      "foo_bar23" => :foo_bar23,
      :foo_bar23  => :foo_bar23,
    }

    valid.each do |input, expect|
      it "should map #{input.inspect} to #{expect.inspect}" do
        result = subject.underscorize(input)
        expect(result).to eq(expect)
      end
    end

    faulty.each do |input|
      it "should fail when presented with #{input.inspect} (#{input.class})" do
        expect { subject.underscorize(input) }.
          to raise_error ArgumentError, /not a valid face name/
      end
    end
  end

  context "faulty faces" do
    before :each do
      $:.unshift "#{PuppetSpec::FIXTURE_DIR}/faulty_face"
    end

    after :each do
      $:.delete_if {|x| x == "#{PuppetSpec::FIXTURE_DIR}/faulty_face"}
    end

    it "should not die if a face has a syntax error" do
      expect(subject.faces).to be_include :help
      expect(subject.faces).not_to be_include :syntax
      expect(@logs).not_to be_empty
      expect(@logs.first.message).to match(/syntax error/)
    end
  end
end