File: base_context_spec.rb

package info (click to toggle)
ruby-puppet-resource-api 1.8.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,228 kB
  • sloc: ruby: 9,519; sh: 4; makefile: 2
file content (378 lines) | stat: -rw-r--r-- 15,831 bytes parent folder | download
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Puppet::ResourceApi::BaseContext do
  class TestContext < described_class
    attr_reader :last_level, :last_message
    def send_log(log, msg)
      @last_level = log
      @last_message = msg
    end
  end

  subject(:context) do
    TestContext.new(definition)
  end

  let(:definition_hash) do
    {
      name: 'some_resource',
      desc: 'a test resource',
      attributes: {
        name: { type: 'String', desc: 'message' },
      },
      features: feature_support,
    }
  end
  let(:definition) { Puppet::ResourceApi::TypeDefinition.new(definition_hash) }
  let(:feature_support) { [] }

  it { expect { described_class.new(nil) }.to raise_error ArgumentError, %r{BaseContext requires definition to be a child of Puppet::ResourceApi::BaseTypeDefinition} }
  describe 'legacy hash definition support' do
    let(:definition) { definition_hash }

    it { expect { context }.not_to raise_error }
    it { expect(context.type.name).to eq 'some_resource' }
  end

  describe '#failed?' do
    it('defaults to false') { is_expected.not_to be_failed }
  end

  describe '#warning(msg)' do
    it 'outputs the message' do
      context.warning('message')
      expect(context.last_message).to eq 'some_resource: message'
    end
    it 'outputs at the correct level' do
      context.warning('message')
      expect(context.last_level).to eq :warning
    end
  end

  describe '#warning(titles, msg)' do
    it 'formats no titles correctly' do
      context.warning([], 'message')
      expect(context.last_message).to eq 'some_resource: message'
    end
    it 'formats an empty title correctly' do
      context.warning('', 'message')
      expect(context.last_message).to eq 'some_resource[]: message'
    end
    it 'formats a single title' do
      context.warning('a', 'message')
      expect(context.last_message).to eq 'some_resource[a]: message'
    end
    it 'formats multiple titles' do
      context.warning(%w[a b], 'message')
      expect(context.last_message).to eq 'some_resource[a, b]: message'
    end
  end

  describe '#warning(msg1, msg2, msg3, ...)' do
    it 'outputs all passed messages' do
      context.warning('msg1', 'msg2', 'msg3')
      expect(context.last_message).to eq 'msg1, msg2, msg3'
    end
  end

  [:creating, :updating, :deleting].each do |method|
    describe "##{method}(title, &block)" do
      it 'outputs the start and stop messages' do
        allow(context).to receive(:send_log)
        expect(context).to receive(:send_log).with(:debug, %r{some_title.*#{method.to_s}.*start}i)
        expect(context).to receive(:send_log).with(:notice, %r{some_title.*#{method.to_s}.*finished}i)
        context.send(method, 'some_title') {}
      end

      it 'logs completion time' do
        allow(context).to receive(:send_log).with(:debug, anything)
        expect(context).to receive(:send_log).with(:notice, %r{finished in [0-9]*\.[0-9]* seconds}i)
        context.send(method, 'timed_resource') {}
      end

      it 'does not leak state between invocations' do
        context.send(method, 'resource_one') {}
        expect(context).to receive(:send_log).with(:debug, %r{resource_two.*#{method.to_s}.*start}i)
        expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
        context.send(method, 'resource_two') {}
      end

      context 'when a StandardError is raised' do
        it 'swallows the exception' do
          expect {
            context.send(method, 'bad_resource') { raise StandardError, 'Bad Resource!' }
          }.not_to raise_error
        end

        it 'logs an error' do
          allow(context).to receive(:send_log)
          expect(context).to receive(:send_log).with(:err, %r{bad_resource.*#{method.to_s}.*failed.*reasons}i)
          context.send(method, 'bad_resource') { raise StandardError, 'Reasons' }
        end

        it 'does not leak state into next invocation' do
          context.send(method, 'resource_one') { raise StandardError, 'Bad Resource!' }
          expect(context).to receive(:send_log).with(:debug, %r{resource_two.*#{method.to_s}.*start}i)
          expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
          context.send(method, 'resource_two') {}
        end

        it 'marks the context as failed' do
          context.send(method, 'resource_one') { raise StandardError, 'Bad Resource!' }
          expect(context).to be_failed
        end
      end

      context 'when an Exception that is not StandardError is raised' do
        it 'raises the exception' do
          expect {
            context.send(method, 'total_failure') { raise LoadError, 'Disk Read Error' }
          }.to raise_error(LoadError, 'Disk Read Error')
        end

        it 'does not leak state into next invocation' do
          expect {
            context.send(method, 'resource_one') { raise LoadError, 'Uh oh' }
          }.to raise_error(LoadError, 'Uh oh')
          expect(context).to receive(:send_log).with(:debug, %r{resource_two.*#{method.to_s}.*start}i)
          expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
          context.send(method, 'resource_two') {}
        end
      end
    end
  end

  describe '#failing(titles, &block)' do
    it 'logs a debug start message' do
      allow(context).to receive(:send_log)
      expect(context).to receive(:send_log).with(:debug, %r{\[Thing\[one\], Thing\[two\]\].*failing.*start}i)
      context.failing(['Thing[one]', 'Thing[two]']) {}
    end

    it 'logs a warning on completion' do
      allow(context).to receive(:send_log)
      expect(context).to receive(:send_log).with(:warning, %r{\[Thing\[one\], Thing\[two\]\].*failing.*finished}i)
      context.failing(['Thing[one]', 'Thing[two]']) {}
    end

    it 'logs completion time' do
      allow(context).to receive(:send_log)
      expect(context).to receive(:send_log).with(:warning, %r{finished failing in [0-9]*\.[0-9]* seconds}i)
      context.failing(['Thing[one]', 'Thing[two]']) {}
    end

    it 'does not leak state between invocations' do
      context.failing('resource_one') {}
      expect(context).to receive(:send_log).with(:debug, %r{resource_two.*failing.*start}i)
      expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
      context.failing('resource_two') {}
    end

    context 'when a StandardError is raised' do
      it 'swallows the exception' do
        expect {
          context.failing('bad_resource') { raise StandardError, 'Bad Resource!' }
        }.not_to raise_error
      end

      it 'logs an error' do
        allow(context).to receive(:send_log)
        expect(context).to receive(:send_log).with(:err, %r{some_resource.*failing.*failed.*reasons}i)
        context.failing('bad_resource') { raise StandardError, 'Reasons' }
      end

      it 'does not leak state into next invocation' do
        context.failing('resource_one') { raise StandardError, 'Bad Resource!' }
        expect(context).to receive(:send_log).with(:debug, %r{resource_two.*failing.*start}i)
        expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
        context.failing('resource_two') {}
      end
    end

    context 'when an Exception that is not StandardError is raised' do
      it 'raises the exception' do
        expect {
          context.failing('total_failure') { raise LoadError, 'Disk Read Error' }
        }.to raise_error(LoadError, 'Disk Read Error')
      end

      it 'does not leak state into next invocation' do
        expect {
          context.failing('resource_one') { raise LoadError, 'Uh oh' }
        }.to raise_error(LoadError, 'Uh oh')
        expect(context).to receive(:send_log).with(:debug, %r{resource_two.*failing.*start}i)
        expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
        context.failing('resource_two') {}
      end
    end
  end

  [:created, :updated, :deleted].each do |method|
    describe "##{method}(titles, message: '#{method.to_s.capitalize}')" do
      it 'logs the action at :notice level' do
        expect(context).to receive(:send_log).with(:notice, %r{#{method.to_s.capitalize}: \[\"Thing\[one\]\", \"Thing\[two\]\"\]}i)
        context.send(method, ['Thing[one]', 'Thing[two]'])
      end

      it 'logs a custom message if provided' do
        expect(context).to receive(:send_log).with(:notice, %r{My provider did the action: \[\"Thing\[one\]\", \"Thing\[two\]\"\]}i)
        context.send(method, ['Thing[one]', 'Thing[two]'], message: 'My provider did the action')
      end
    end
  end

  describe '#failed(titles, message: \'Failed\')' do
    it 'logs the action at :err level' do
      expect(context).to receive(:send_log).with(:err, %r{\[Thing\[one\], Thing\[two\]\].*failed})
      context.failed(['Thing[one]', 'Thing[two]'])
    end

    it 'logs a custom message if provided' do
      expect(context).to receive(:send_log).with(:err, %r{\[Thing\[one\], Thing\[two\]\].*My provider is really sorry})
      context.failed(['Thing[one]', 'Thing[two]'], message: 'My provider is really sorry')
    end
  end

  describe '#processed(title, is, should)' do
    it 'logs the successful change of attributes' do
      expect(context).to receive(:send_log).with(:notice, %r{Processed Thing\[one\] from {:ensure=>"absent"} to {:ensure=>"present", :name=>"thing one"}})
      context.processed('Thing[one]', { ensure: 'absent' }, { ensure: 'present', name: 'thing one' })
    end

    it 'raises if multiple titles are passed' do
      expect { context.processed(['Thing[one]', 'Thing[two'], { foo: 'bar' }, { foo: 'baz' })  }.to raise_error('processed only accepts a single resource title')
    end
  end

  describe '#processing(title, is, should, message: \'Processing\', &block)' do
    it 'raises if multiple titles are passed' do
      expect { context.processing(['Thing[one]', 'Thing[two'], { foo: 'bar' }, { foo: 'baz' }) { puts 'Doing it' } }.to raise_error('processing only accepts a single resource title')
    end

    it 'logs the start message' do
      allow(context).to receive(:send_log)
      expect(context).to receive(:send_log).with(:debug, %r{starting processing of.*some_title.*}i)
      context.processing('some_title', { ensure: 'absent' }, { name: 'some_title', ensure: 'present' }) {}
    end

    it 'logs a succesful change' do
      allow(context).to receive(:send_log)
      expect(context).to receive(:send_log).with(:notice, %r{Finished processing some_title in .* seconds: \{:name=>\"some_title\", :ensure=>\"present\"\}}i)
      context.processing('some_title', { ensure: 'absent' }, { name: 'some_title', ensure: 'present' }) {}
    end

    it 'logs completion time' do
      allow(context).to receive(:send_log).with(:debug, anything)
      expect(context).to receive(:send_log).with(:notice, %r{finished processing some_title in [0-9]*\.[0-9]* seconds}i)
      context.processing('some_title', { ensure: 'absent' }, ensure: 'present') {}
    end

    it 'logs failure if the block raises an exception' do
      allow(context).to receive(:send_log).with(:debug, anything)
      expect(context).to receive(:send_log).with(:err, %r{failed processing some_title after [0-9]*\.[0-9]* seconds: salt the fries}i)
      expect { context.processing('some_title', { ensure: 'absent' }, ensure: 'present') { raise StandardError, 'salt the fries' } }.not_to raise_error
    end

    it 'does not leak state between invocations' do
      context.processing('resource_one', { ensure: 'absent' }, ensure: 'present') {}
      expect(context).to receive(:send_log).with(:debug, %r{processing of resource_two}i)
      expect(context).not_to receive(:send_log).with(anything, %r{.*resource_one.*})
      context.processing('resource_two', { ensure: 'absent' }, ensure: 'present') {}
    end
  end

  describe 'attribute_changed(title, attribute, is, should, message: nil)' do
    it 'logs string values with quotes' do
      expect(context).to receive(:send_log).with(:notice, %r{Thing\[foo\]: attribute 'config_file' changed from '/tmp/foo.cfg' to '/tmp/bar.cfg'}i)
      context.attribute_changed('Thing[foo]', 'config_file', '/tmp/foo.cfg', '/tmp/bar.cfg')
    end

    it 'logs number values without quotes' do
      expect(context).to receive(:send_log).with(:notice, %r{attribute 'height' changed from 5 to 6$}i)
      context.attribute_changed('Thing[foo]', 'height', 5, 6)
    end

    it 'logs a nil should value as \'nil\'' do
      expect(context).to receive(:send_log).with(:notice, %r{attribute 'height' changed from 5 to nil$}i)
      context.attribute_changed('Thing[foo]', 'height', 5, nil)
    end

    it 'logs a nil is value as \'nil\'' do
      expect(context).to receive(:send_log).with(:notice, %r{attribute 'height' changed from nil to 6$}i)
      context.attribute_changed('Thing[foo]', 'height', nil, 6)
    end

    it 'logs with a message if one is passed' do
      expect(context).to receive(:send_log).with(:notice, %r{attribute 'height' changed from 5 to 6: something interesting$}i)
      context.attribute_changed('Thing[foo]', 'height', 5, 6, message: 'something interesting')
    end

    it 'raises if multiple titles are passed' do
      expect { context.attribute_changed(['Thing[one]', 'Thing[two]'], 'room', 'clean', 'messy') }.to raise_error('attribute_changed only accepts a single resource title')
    end
  end

  describe '#format_seconds' do
    it 'returns 6 decimal points for a number less than 1' do
      expect(described_class.new(definition).send(:format_seconds, 0.000136696)).to eq('0.000137')
    end

    it 'returns 2 decimal places for a number greater than 1' do
      expect(described_class.new(definition).send(:format_seconds, 123.45678)).to eq('123.46')
      expect(described_class.new(definition).send(:format_seconds, 1_234_567.89)).to eq('1234567.89')
      expect(described_class.new(definition).send(:format_seconds, 123_456)).to eq('123456.00')
    end
  end

  describe '#log_exception(exception, message:, trace:)' do
    let(:exception) do
      ex = ArgumentError.new('x')
      ex.set_backtrace %w[a b c]
      ex
    end

    context 'without tracing' do
      it 'creates a log message' do
        context.log_exception(exception, message: 'message', trace: false)
        expect(context.last_message).to eq 'some_resource: message: x'
      end
    end

    context 'with tracing' do
      it 'creates a log message containing the backtrace' do
        context.log_exception(exception, message: 'message', trace: true)
        expect(context.last_message).to eq "some_resource: message: x\na\nb\nc"
      end
    end
  end

  describe '#device' do
    it { expect { described_class.new(definition).device }.to raise_error RuntimeError, %r{Received device\(\) on an unprepared BaseContext\. Use a PuppetContext instead} }
  end

  describe '#transport' do
    it { expect { described_class.new(definition).transport }.to raise_error RuntimeError, %r{No transport available\.} }
  end

  describe '#send_log' do
    it { expect { described_class.new(definition).send_log(nil, nil) }.to raise_error RuntimeError, %r{Received send_log\(\) on an unprepared BaseContext\. Use IOContext, or PuppetContext instead} }
  end

  describe '#feature_support?' do
    it {
      expect(Puppet::Util::Log).to receive(:create).with(level: :warning, source: 'Puppet', message: match(%r{context.feature_support\? is deprecated. Please use context.type.feature\? instead\.}))
      context.feature_support?('anything')
    }
  end

  describe '#reset_failed' do
    it 'resets the failure state' do
      context.failing('bad_resource') { raise StandardError, 'Bad Resource!' }
      expect(context).to be_failed
      context.reset_failed
      expect(context).not_to be_failed
    end
  end
end