summaryrefslogtreecommitdiffstats
path: root/spec/unit/transaction.rb
blob: abb362123c205320ce17c8fca81064068103144e (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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/env ruby

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

require 'puppet/transaction'

describe Puppet::Transaction do
    describe "when evaluating a resource" do
        before do
            @transaction = Puppet::Transaction.new(Puppet::Resource::Catalog.new)
            @transaction.stubs(:eval_children_and_apply_resource)
            @transaction.stubs(:skip?).returns false

            @resource = stub("resource")
        end

        it "should check whether the resource should be skipped" do
            @transaction.expects(:skip?).with(@resource).returns false

            @transaction.eval_resource(@resource)
        end

        it "should eval and apply children" do
            @transaction.expects(:eval_children_and_apply_resource).with(@resource)

            @transaction.eval_resource(@resource)
        end

        it "should process events" do
            @transaction.expects(:process_events).with(@resource)

            @transaction.eval_resource(@resource)
        end

        describe "and the resource should be skipped" do
            before do
                @transaction.expects(:skip?).with(@resource).returns true
            end

            it "should increment the 'skipped' count" do
                @transaction.eval_resource(@resource)
                @transaction.resourcemetrics[:skipped].should == 1
            end
        end
    end

    describe "when applying changes" do
        before do
            @transaction = Puppet::Transaction.new(Puppet::Resource::Catalog.new)
            @transaction.stubs(:queue_event)

            @resource = stub 'resource'
            @property = stub 'property', :is_to_s => "is", :should_to_s => "should"

            @event = stub 'event', :status => "success"
            @change = stub 'change', :property => @property, :changed= => nil, :forward => @event, :is => "is", :should => "should"
        end

        it "should apply each change" do
            c1 = stub 'c1', :property => @property, :changed= => nil
            c1.expects(:forward).returns @event
            c2 = stub 'c2', :property => @property, :changed= => nil
            c2.expects(:forward).returns @event

            @transaction.apply_changes(@resource, [c1, c2])
        end

        it "should queue the events from each change" do
            c1 = stub 'c1', :forward => stub("event1", :status => "success"), :property => @property, :changed= => nil
            c2 = stub 'c2', :forward => stub("event2", :status => "success"), :property => @property, :changed= => nil

            @transaction.expects(:queue_event).with(@resource, c1.forward)
            @transaction.expects(:queue_event).with(@resource, c2.forward)

            @transaction.apply_changes(@resource, [c1, c2])
        end

        it "should store the change in the transaction's change list" do
            @transaction.apply_changes(@resource, [@change])
            @transaction.changes.should include(@change)
        end

        it "should increment the number of applied resources" do
            @transaction.apply_changes(@resource, [@change])
            @transaction.resourcemetrics[:applied].should == 1
        end

        describe "and a change fails" do
            before do
                @event.stubs(:status).returns "failure"
            end

            it "should increment the failures" do
                @transaction.apply_changes(@resource, [@change])
                @transaction.should be_any_failed
            end

            it "should queue the event" do
                @transaction.expects(:queue_event).with(@resource, @event)
                @transaction.apply_changes(@resource, [@change])
            end
        end
    end

    describe "when queueing events" do
        before do
            @transaction = Puppet::Transaction.new(Puppet::Resource::Catalog.new)

            @resource = stub("resource", :self_refresh? => false, :deleting => false)

            @graph = stub 'graph', :matching_edges => [], :resource => @resource
            @transaction.stubs(:relationship_graph).returns @graph

            @event = Puppet::Transaction::Event.new(:foo, @resource)
        end

        it "should store each event in its event list" do
            @transaction.queue_event(@resource, @event)

            @transaction.events.should include(@event)
        end

        it "should queue events for the target and callback of any matching edges" do
            edge1 = stub("edge1", :callback => :c1, :source => stub("s1"), :target => stub("t1", :c1 => nil))
            edge2 = stub("edge2", :callback => :c2, :source => stub("s2"), :target => stub("t2", :c2 => nil))

            @graph.expects(:matching_edges).with { |events, resource| events == [@event] }.returns [edge1, edge2]

            @transaction.expects(:queue_event_for_resource).with(@resource, edge1.target, edge1.callback, @event)
            @transaction.expects(:queue_event_for_resource).with(@resource, edge2.target, edge2.callback, @event)

            @transaction.queue_event(@resource, @event)
        end

        it "should queue events for the changed resource if the resource is self-refreshing and not being deleted" do
            @graph.stubs(:matching_edges).returns []

            @resource.expects(:self_refresh?).returns true
            @resource.expects(:deleting?).returns false
            @transaction.expects(:queue_event_for_resource).with(@resource, @resource, :refresh, @event)

            @transaction.queue_event(@resource, @event)
        end

        it "should not queue events for the changed resource if the resource is not self-refreshing" do
            @graph.stubs(:matching_edges).returns []

            @resource.expects(:self_refresh?).returns false
            @resource.stubs(:deleting?).returns false
            @transaction.expects(:queue_event_for_resource).never

            @transaction.queue_event(@resource, @event)
        end

        it "should not queue events for the changed resource if the resource is being deleted" do
            @graph.stubs(:matching_edges).returns []

            @resource.expects(:self_refresh?).returns true
            @resource.expects(:deleting?).returns true
            @transaction.expects(:queue_event_for_resource).never

            @transaction.queue_event(@resource, @event)
        end

        it "should ignore edges that don't have a callback" do
            edge1 = stub("edge1", :callback => :nil, :source => stub("s1"), :target => stub("t1", :c1 => nil))

            @graph.expects(:matching_edges).returns [edge1]

            @transaction.expects(:queue_event_for_resource).never

            @transaction.queue_event(@resource, @event)
        end

        it "should ignore targets that don't respond to the callback" do
            edge1 = stub("edge1", :callback => :c1, :source => stub("s1"), :target => stub("t1"))

            @graph.expects(:matching_edges).returns [edge1]

            @transaction.expects(:queue_event_for_resource).never

            @transaction.queue_event(@resource, @event)
        end
    end

    describe "when queueing events for a resource" do
        before do
            @transaction = Puppet::Transaction.new(Puppet::Resource::Catalog.new)
        end

        it "should do nothing if no events are queued" do
            @transaction.queued_events(stub("target")) { |callback, events| raise "should never reach this" }
        end

        it "should yield the callback and events for each callback" do
            target = stub("target")

            2.times do |i|
                @transaction.queue_event_for_resource(stub("source", :info => nil), target, "callback#{i}", ["event#{i}"])
            end

            @transaction.queued_events(target) { |callback, events| }
        end

        it "should use the source to log that it's scheduling a refresh of the target" do
            target = stub("target")
            source = stub 'source'
            source.expects(:info)

            @transaction.queue_event_for_resource(source, target, "callback", ["event"])

            @transaction.queued_events(target) { |callback, events| }
        end
    end

    describe "when processing events for a given resource" do
        before do
            @transaction = Puppet::Transaction.new(Puppet::Resource::Catalog.new)
            @transaction.stubs(:queue_event)

            @resource = stub 'resource', :notice => nil
            @event = Puppet::Transaction::Event.new(:event, @resource)
        end

        it "should call the required callback once for each set of associated events" do
            @transaction.expects(:queued_events).with(@resource).multiple_yields([:callback1, [@event]], [:callback2, [@event]])

            @resource.expects(:callback1)
            @resource.expects(:callback2)

            @transaction.process_events(@resource)
        end

        it "should update the 'restarted' metric" do
            @transaction.expects(:queued_events).with(@resource).yields(:callback1, [@event])

            @resource.stubs(:callback1)

            @transaction.process_events(@resource)

            @transaction.resourcemetrics[:restarted].should == 1
        end

        it "should queue a 'restarted' event with the resource as the source" do
            @transaction.expects(:queued_events).with(@resource).yields(:callback1, [@event])

            @resource.stubs(:callback1)

            @transaction.expects(:queue_event).with do |resource, event|
                event.name == :restarted and resource == @resource
            end

            @transaction.process_events(@resource)
        end

        it "should log that it restarted" do
            @transaction.expects(:queued_events).with(@resource).yields(:callback1, [@event])

            @resource.stubs(:callback1)

            @resource.expects(:notice).with { |msg| msg.include?("Triggered 'callback1'") }

            @transaction.process_events(@resource)
        end

        describe "and the events include a noop event" do
            before do
                @event.stubs(:name).returns :noop
                @transaction.expects(:queued_events).with(@resource).yields(:callback1, [@event])
            end

            it "should log" do
                @resource.expects(:notice).with { |msg| msg.include?("Would have triggered 'callback1'") }

                @transaction.process_events(@resource)
            end

            it "should not call the callback" do
                @resource.expects(:callback1).never

                @transaction.process_events(@resource)
            end

            it "should queue a new noop event" do
                @transaction.expects(:queue_event).with { |resource, event| event.name == :noop and resource == @resource }

                @transaction.process_events(@resource)
            end
        end

        describe "and the callback fails" do
            before do
                @resource.expects(:callback1).raises "a failure"
                @resource.stubs(:err)

                @transaction.expects(:queued_events).yields(:callback1, [@event])
            end

            it "should log but not fail" do
                @resource.expects(:err)

                lambda { @transaction.process_events(@resource) }.should_not raise_error
            end

            it "should update the 'failed_restarts' metric" do
                @transaction.process_events(@resource)
                @transaction.resourcemetrics[:failed_restarts].should == 1
            end

            it "should not queue a 'restarted' event" do
                @transaction.expects(:queue_event).never
                @transaction.process_events(@resource)
            end

            it "should not increase the restarted resource count" do
                @transaction.process_events(@resource)
                @transaction.resourcemetrics[:restarted].should == 0
            end
        end
    end

    describe "when initializing" do
        it "should accept a catalog and set an instance variable for it" do
            catalog = stub 'catalog', :vertices => []

            trans = Puppet::Transaction.new(catalog)
            trans.catalog.should == catalog
        end
    end

    describe "when generating resources" do
        it "should finish all resources" do
            generator = stub 'generator', :depthfirst? => true, :tags => []
            resource = stub 'resource', :tag => nil

            @catalog = Puppet::Resource::Catalog.new
            @transaction = Puppet::Transaction.new(@catalog)

            generator.expects(:generate).returns [resource]

            @catalog.expects(:add_resource).yields(resource)

            resource.expects(:finish)

            @transaction.generate_additional_resources(generator, :generate)
        end

        it "should skip generated resources that conflict with existing resources" do
            generator = mock 'generator', :tags => []
            resource = stub 'resource', :tag => nil

            @catalog = Puppet::Resource::Catalog.new
            @transaction = Puppet::Transaction.new(@catalog)

            generator.expects(:generate).returns [resource]

            @catalog.expects(:add_resource).raises(Puppet::Resource::Catalog::DuplicateResourceError.new("foo"))

            resource.expects(:finish).never
            resource.expects(:info) # log that it's skipped

            @transaction.generate_additional_resources(generator, :generate).should be_empty
        end

        it "should copy all tags to the newly generated resources" do
            child = stub 'child'
            generator = stub 'resource', :tags => ["one", "two"]

            @catalog = Puppet::Resource::Catalog.new
            @transaction = Puppet::Transaction.new(@catalog)

            generator.stubs(:generate).returns [child]
            @catalog.stubs(:add_resource)

            child.expects(:tag).with("one", "two")

            @transaction.generate_additional_resources(generator, :generate)
        end
    end

    describe "when skipping a resource" do
        before :each do
            @resource = stub_everything 'res'
            @catalog = Puppet::Resource::Catalog.new
            @transaction = Puppet::Transaction.new(@catalog)
        end

        it "should skip resource with missing tags" do
            @transaction.stubs(:missing_tags?).returns(true)
            @transaction.skip?(@resource).should be_true
        end

        it "should ask the resource if it's tagged with any of the tags" do
            tags = ['one', 'two']
            @transaction.stubs(:ignore_tags?).returns(false)
            @transaction.stubs(:tags).returns(tags)

            @resource.expects(:tagged?).with(*tags).returns(true)

            @transaction.missing_tags?(@resource).should be_false
        end

        it "should skip not scheduled resources" do
            @transaction.stubs(:scheduled?).returns(false)
            @transaction.skip?(@resource).should be_true
        end

        it "should skip resources with failed dependencies" do
            @transaction.stubs(:failed_dependencies?).returns(false)
            @transaction.skip?(@resource).should be_true
        end

        it "should skip virtual resource" do
            @resource.stubs(:virtual?).returns true
            @transaction.skip?(@resource).should be_true
        end
    end

    describe "when adding metrics to a report" do
        before do
            @catalog = Puppet::Resource::Catalog.new
            @transaction = Puppet::Transaction.new(@catalog)

            @report = stub 'report', :newmetric => nil, :time= => nil
        end

        [:resources, :time, :changes].each do |metric|
            it "should add times for '#{metric}'" do
                @report.expects(:newmetric).with { |m, v| m == metric }
                @transaction.add_metrics_to_report(@report)
            end
        end

        it "should set the transaction time to the current time" do
            Time.expects(:now).returns "now"
            @report.expects(:time=).with("now")
            @transaction.add_metrics_to_report(@report)
        end
    end

    describe "when prefetching" do
        it "should match resources by name, not title" do
            @catalog = Puppet::Resource::Catalog.new
            @transaction = Puppet::Transaction.new(@catalog)

            # Have both a title and name
            resource = Puppet::Type.type(:sshkey).create :title => "foo", :name => "bar", :type => :dsa, :key => "eh"
            @catalog.add_resource resource

            resource.provider.class.expects(:prefetch).with("bar" => resource)

            @transaction.prefetch
        end
    end
end

describe Puppet::Transaction, " when determining tags" do
    before do
        @config = Puppet::Resource::Catalog.new
        @transaction = Puppet::Transaction.new(@config)
    end

    it "should default to the tags specified in the :tags setting" do
        Puppet.expects(:[]).with(:tags).returns("one")
        @transaction.tags.should == %w{one}
    end

    it "should split tags based on ','" do
        Puppet.expects(:[]).with(:tags).returns("one,two")
        @transaction.tags.should == %w{one two}
    end

    it "should use any tags set after creation" do
        Puppet.expects(:[]).with(:tags).never
        @transaction.tags = %w{one two}
        @transaction.tags.should == %w{one two}
    end

    it "should always convert assigned tags to an array" do
        @transaction.tags = "one::two"
        @transaction.tags.should == %w{one::two}
    end

    it "should accept a comma-delimited string" do
        @transaction.tags = "one, two"
        @transaction.tags.should == %w{one two}
    end

    it "should accept an empty string" do
        @transaction.tags = ""
        @transaction.tags.should == []
    end
end