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
|
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
class SnitchyProvider < Chef::Provider
def self.all_actions_called
@all_actions_called ||= []
end
def self.action_called(action)
all_actions_called << action
end
def self.clear_action_record
@all_actions_called = nil
end
def load_current_resource
true
end
def action_first_action
@new_resource.updated_by_last_action(true)
self.class.action_called(:first)
end
def action_second_action
@new_resource.updated_by_last_action(true)
self.class.action_called(:second)
end
def action_third_action
@new_resource.updated_by_last_action(true)
self.class.action_called(:third)
end
end
describe Chef::Runner do
before(:each) do
@node = Chef::Node.new
@node.name "latte"
@node.platform "mac_os_x"
@node.platform_version "10.5.1"
@run_context = Chef::RunContext.new(@node, Chef::CookbookCollection.new({}))
@first_resource = Chef::Resource::Cat.new("loulou1", @run_context)
@run_context.resource_collection << @first_resource
Chef::Platform.set(
:resource => :cat,
:provider => Chef::Provider::SnakeOil
)
@runner = Chef::Runner.new(@run_context)
end
it "should pass each resource in the collection to a provider" do
@run_context.resource_collection.should_receive(:execute_each_resource).once
@runner.converge
end
it "should use the provider specified by the resource (if it has one)" do
provider = Chef::Provider::Easy.new(@run_context.resource_collection[0], @run_context)
# Expect provider to be called twice, because will fall back to old provider lookup
@run_context.resource_collection[0].should_receive(:provider).twice.and_return(Chef::Provider::Easy)
Chef::Provider::Easy.should_receive(:new).once.and_return(provider)
@runner.converge
end
it "should use the platform provider if it has one" do
Chef::Platform.should_receive(:find_provider_for_node).once.and_return(Chef::Provider::SnakeOil)
@runner.converge
end
it "should run the action for each resource" do
Chef::Platform.should_receive(:find_provider_for_node).once.and_return(Chef::Provider::SnakeOil)
provider = Chef::Provider::SnakeOil.new(@run_context.resource_collection[0], @run_context)
provider.should_receive(:action_sell).once.and_return(true)
Chef::Provider::SnakeOil.should_receive(:new).once.and_return(provider)
@runner.converge
end
it "should raise exceptions as thrown by a provider" do
provider = Chef::Provider::SnakeOil.new(@run_context.resource_collection[0], @run_context)
Chef::Provider::SnakeOil.stub!(:new).once.and_return(provider)
provider.stub!(:action_sell).once.and_raise(ArgumentError)
lambda { @runner.converge }.should raise_error(ArgumentError)
end
it "should not raise exceptions thrown by providers if the resource has ignore_failure set to true" do
@run_context.resource_collection[0].stub!(:ignore_failure).and_return(true)
provider = Chef::Provider::SnakeOil.new(@run_context.resource_collection[0], @run_context)
Chef::Provider::SnakeOil.stub!(:new).once.and_return(provider)
provider.stub!(:action_sell).once.and_raise(ArgumentError)
lambda { @runner.converge }.should_not raise_error(ArgumentError)
end
it "should retry with the specified delay if retries are specified" do
@first_resource.retries 3
provider = Chef::Provider::SnakeOil.new(@run_context.resource_collection[0], @run_context)
Chef::Provider::SnakeOil.stub!(:new).once.and_return(provider)
provider.stub!(:action_sell).and_raise(ArgumentError)
@runner.should_receive(:sleep).with(2).exactly(3).times
lambda { @runner.converge }.should raise_error(ArgumentError)
end
it "should execute immediate actions on changed resources" do
notifying_resource = Chef::Resource::Cat.new("peanut", @run_context)
notifying_resource.action = :purr # only action that will set updated on the resource
@run_context.resource_collection << notifying_resource
@first_resource.action = :nothing # won't be updated unless notified by other resource
notifying_resource.notifies(:purr, @first_resource, :immediately)
@runner.converge
@first_resource.should be_updated
end
it "should follow a chain of actions" do
@first_resource.action = :nothing
middle_resource = Chef::Resource::Cat.new("peanut", @run_context)
middle_resource.action = :nothing
@run_context.resource_collection << middle_resource
middle_resource.notifies(:purr, @first_resource, :immediately)
last_resource = Chef::Resource::Cat.new("snuffles", @run_context)
last_resource.action = :purr
@run_context.resource_collection << last_resource
last_resource.notifies(:purr, middle_resource, :immediately)
@runner.converge
last_resource.should be_updated # by action(:purr)
middle_resource.should be_updated # by notification from last_resource
@first_resource.should be_updated # by notification from middle_resource
end
it "should execute delayed actions on changed resources" do
@first_resource.action = :nothing
second_resource = Chef::Resource::Cat.new("peanut", @run_context)
second_resource.action = :purr
@run_context.resource_collection << second_resource
second_resource.notifies(:purr, @first_resource, :delayed)
@runner.converge
@first_resource.should be_updated
end
it "does not duplicate delayed notifications" do
SnitchyProvider.clear_action_record
Chef::Platform.set(
:resource => :cat,
:provider => SnitchyProvider
)
@first_resource.action = :nothing
second_resource = Chef::Resource::Cat.new("peanut", @run_context)
second_resource.action = :first_action
@run_context.resource_collection << second_resource
third_resource = Chef::Resource::Cat.new("snickers", @run_context)
third_resource.action = :first_action
@run_context.resource_collection << third_resource
second_resource.notifies(:second_action, @first_resource, :delayed)
second_resource.notifies(:third_action, @first_resource, :delayed)
third_resource.notifies(:second_action, @first_resource, :delayed)
third_resource.notifies(:third_action, @first_resource, :delayed)
@runner.converge
# resources 2 and 3 call :first_action in the course of normal resource
# execution, and schedule delayed actions :second and :third on the first
# resource. The duplicate actions should "collapse" to a single notification
# and order should be preserved.
SnitchyProvider.all_actions_called.should == [:first, :first, :second, :third]
end
it "executes delayed notifications in the order they were declared" do
SnitchyProvider.clear_action_record
Chef::Platform.set(
:resource => :cat,
:provider => SnitchyProvider
)
@first_resource.action = :nothing
second_resource = Chef::Resource::Cat.new("peanut", @run_context)
second_resource.action = :first_action
@run_context.resource_collection << second_resource
third_resource = Chef::Resource::Cat.new("snickers", @run_context)
third_resource.action = :first_action
@run_context.resource_collection << third_resource
second_resource.notifies(:second_action, @first_resource, :delayed)
second_resource.notifies(:second_action, @first_resource, :delayed)
third_resource.notifies(:third_action, @first_resource, :delayed)
third_resource.notifies(:third_action, @first_resource, :delayed)
@runner.converge
SnitchyProvider.all_actions_called.should == [:first, :first, :second, :third]
end
it "does not fire notifications if the resource was not updated by the last action executed" do
# REGRESSION TEST FOR CHEF-1452
SnitchyProvider.clear_action_record
Chef::Platform.set(
:resource => :cat,
:provider => SnitchyProvider
)
@first_resource.action = :first_action
second_resource = Chef::Resource::Cat.new("peanut", @run_context)
second_resource.action = :nothing
@run_context.resource_collection << second_resource
third_resource = Chef::Resource::Cat.new("snickers", @run_context)
third_resource.action = :nothing
@run_context.resource_collection << third_resource
@first_resource.notifies(:second_action, second_resource, :immediately)
second_resource.notifies(:third_action, third_resource, :immediately)
@runner.converge
# All of the resources should only fire once:
SnitchyProvider.all_actions_called.should == [:first, :second, :third]
# all of the resources should be marked as updated for reporting purposes
@first_resource.should be_updated
second_resource.should be_updated
third_resource.should be_updated
end
it "should check a resource's only_if and not_if if notified by another resource" do
@first_resource.action = :nothing
only_if_called_times = 0
@first_resource.only_if {only_if_called_times += 1; true}
not_if_called_times = 0
@first_resource.not_if {not_if_called_times += 1; false}
second_resource = Chef::Resource::Cat.new("carmel", @run_context)
@run_context.resource_collection << second_resource
second_resource.notifies(:purr, @first_resource, :delayed)
second_resource.action = :purr
# hits only_if first time when the resource is run in order, second on notify
@runner.converge
only_if_called_times.should == 2
not_if_called_times.should == 2
end
end
|