Skip to content

Commit 93e4526

Browse files
committed
We no longer inherit the action of the prior resource - fixes CHEF-92
1 parent 0003f14 commit 93e4526

4 files changed

Lines changed: 305 additions & 248 deletions

File tree

chef/lib/chef/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def load_prior_resource
6464
prior_resource = @collection.lookup(self.to_s)
6565
Chef::Log.debug("Setting #{self.to_s} to the state of the prior #{self.to_s}")
6666
prior_resource.instance_variables.each do |iv|
67-
unless iv == "@source_line"
67+
unless iv == "@source_line" || iv == "@action"
6868
self.instance_variable_set(iv, prior_resource.instance_variable_get(iv))
6969
end
7070
end

chef/lib/chef/resource_collection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ def <<(*args)
4141
args.flatten.each do |a|
4242
is_chef_resource(a)
4343
@resources << a
44-
@resources_by_name[a.to_s] = @resources.length - 1
44+
@resources_by_name[a.to_s] = @resources.length - 1
4545
end
4646
end
4747

4848
def push(*args)
4949
args.flatten.each do |a|
5050
is_chef_resource(a)
5151
@resources.push(a)
52-
@resources_by_name[a.to_s] = @resources.length - 1
52+
@resources_by_name[a.to_s] = @resources.length - 1
5353
end
5454
end
5555

chef/spec/unit/resource_collection_spec.rb

Lines changed: 131 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -25,149 +25,170 @@
2525
@resource = Chef::Resource::ZenMaster.new("makoto")
2626
end
2727

28-
it "should return a Chef::ResourceCollection" do
29-
@rc.should be_kind_of(Chef::ResourceCollection)
30-
end
31-
32-
it "should accept Chef::Resources through [index]" do
33-
lambda { @rc[0] = @resource }.should_not raise_error
34-
lambda { @rc[0] = "string" }.should raise_error
28+
describe "initialize" do
29+
it "should return a Chef::ResourceCollection" do
30+
@rc.should be_kind_of(Chef::ResourceCollection)
31+
end
3532
end
3633

37-
it "should accept Chef::Resources through pushing" do
38-
lambda { @rc.push(@resource) }.should_not raise_error
39-
lambda { @rc.push("string") }.should raise_error
34+
describe "[]" do
35+
it "should accept Chef::Resources through [index]" do
36+
lambda { @rc[0] = @resource }.should_not raise_error
37+
lambda { @rc[0] = "string" }.should raise_error
38+
end
39+
40+
it "should allow you to fetch Chef::Resources by position" do
41+
@rc[0] = @resource
42+
@rc[0].should eql(@resource)
43+
end
4044
end
4145

42-
it "should allow you to fetch Chef::Resources by position" do
43-
@rc[0] = @resource
44-
@rc[0].should eql(@resource)
46+
describe "push" do
47+
it "should accept Chef::Resources through pushing" do
48+
lambda { @rc.push(@resource) }.should_not raise_error
49+
lambda { @rc.push("string") }.should raise_error
50+
end
4551
end
4652

47-
it "should accept the << operator" do
48-
lambda { @rc << @resource }.should_not raise_error
53+
describe "<<" do
54+
it "should accept the << operator" do
55+
lambda { @rc << @resource }.should_not raise_error
56+
end
4957
end
5058

51-
it "should allow you to iterate over every resource in the collection" do
52-
load_up_resources
53-
results = Array.new
54-
lambda {
55-
@rc.each do |r|
56-
results << r.name
57-
end
58-
}.should_not raise_error
59-
results.each_index do |i|
60-
case i
61-
when 0
62-
results[i].should eql("dog")
63-
when 1
64-
results[i].should eql("cat")
65-
when 2
66-
results[i].should eql("monkey")
59+
describe "each" do
60+
it "should allow you to iterate over every resource in the collection" do
61+
load_up_resources
62+
results = Array.new
63+
lambda {
64+
@rc.each do |r|
65+
results << r.name
66+
end
67+
}.should_not raise_error
68+
results.each_index do |i|
69+
case i
70+
when 0
71+
results[i].should eql("dog")
72+
when 1
73+
results[i].should eql("cat")
74+
when 2
75+
results[i].should eql("monkey")
76+
end
6777
end
6878
end
6979
end
7080

71-
it "should allow you to iterate over every resource by index" do
72-
load_up_resources
73-
results = Array.new
74-
lambda {
75-
@rc.each_index do |i|
76-
results << @rc[i].name
77-
end
78-
}.should_not raise_error()
79-
results.each_index do |i|
80-
case i
81-
when 0
82-
results[i].should eql("dog")
83-
when 1
84-
results[i].should eql("cat")
85-
when 2
86-
results[i].should eql("monkey")
81+
describe "each_index" do
82+
it "should allow you to iterate over every resource by index" do
83+
load_up_resources
84+
results = Array.new
85+
lambda {
86+
@rc.each_index do |i|
87+
results << @rc[i].name
88+
end
89+
}.should_not raise_error()
90+
results.each_index do |i|
91+
case i
92+
when 0
93+
results[i].should eql("dog")
94+
when 1
95+
results[i].should eql("cat")
96+
when 2
97+
results[i].should eql("monkey")
98+
end
8799
end
88100
end
89101
end
90102

91-
it "should allow you to find resources by name via lookup" do
92-
zmr = Chef::Resource::ZenMaster.new("dog")
93-
@rc << zmr
94-
@rc.lookup(zmr.to_s).should eql(zmr)
103+
describe "lookup" do
104+
it "should allow you to find resources by name via lookup" do
105+
zmr = Chef::Resource::ZenMaster.new("dog")
106+
@rc << zmr
107+
@rc.lookup(zmr.to_s).should eql(zmr)
95108

96-
zmr = Chef::Resource::ZenMaster.new("cat")
97-
@rc[0] = zmr
98-
@rc.lookup(zmr).should eql(zmr)
109+
zmr = Chef::Resource::ZenMaster.new("cat")
110+
@rc[0] = zmr
111+
@rc.lookup(zmr).should eql(zmr)
99112

100-
zmr = Chef::Resource::ZenMaster.new("monkey")
101-
@rc.push(zmr)
102-
@rc.lookup(zmr).should eql(zmr)
103-
end
113+
zmr = Chef::Resource::ZenMaster.new("monkey")
114+
@rc.push(zmr)
115+
@rc.lookup(zmr).should eql(zmr)
116+
end
104117

105-
it "should raise an exception if you send something strange to lookup" do
106-
lambda { @rc.lookup(:symbol) }.should raise_error(ArgumentError)
107-
end
118+
it "should raise an exception if you send something strange to lookup" do
119+
lambda { @rc.lookup(:symbol) }.should raise_error(ArgumentError)
120+
end
108121

109-
it "should raise an exception if it cannot find a resource with lookup" do
110-
lambda { @rc.lookup("zen_master[dog]") }.should raise_error(ArgumentError)
122+
it "should raise an exception if it cannot find a resource with lookup" do
123+
lambda { @rc.lookup("zen_master[dog]") }.should raise_error(ArgumentError)
124+
end
111125
end
126+
127+
describe "resources" do
112128

113-
it "should find a resource by symbol and name (:zen_master => monkey)" do
114-
load_up_resources
115-
@rc.resources(:zen_master => "monkey").name.should eql("monkey")
116-
end
129+
it "should find a resource by symbol and name (:zen_master => monkey)" do
130+
load_up_resources
131+
@rc.resources(:zen_master => "monkey").name.should eql("monkey")
132+
end
117133

118-
it "should find a resource by symbol and array of names (:zen_master => [a,b])" do
119-
load_up_resources
120-
results = @rc.resources(:zen_master => [ "monkey", "dog" ])
121-
results.length.should eql(2)
122-
check_by_names(results, "monkey", "dog")
123-
end
134+
it "should find a resource by symbol and array of names (:zen_master => [a,b])" do
135+
load_up_resources
136+
results = @rc.resources(:zen_master => [ "monkey", "dog" ])
137+
results.length.should eql(2)
138+
check_by_names(results, "monkey", "dog")
139+
end
124140

125-
it "should find resources of multiple kinds (:zen_master => a, :file => b)" do
126-
load_up_resources
127-
results = @rc.resources(:zen_master => "monkey", :file => "something")
128-
results.length.should eql(2)
129-
check_by_names(results, "monkey", "something")
130-
end
141+
it "should find resources of multiple kinds (:zen_master => a, :file => b)" do
142+
load_up_resources
143+
results = @rc.resources(:zen_master => "monkey", :file => "something")
144+
results.length.should eql(2)
145+
check_by_names(results, "monkey", "something")
146+
end
131147

132-
it "should find a resource by string zen_master[a]" do
133-
load_up_resources
134-
@rc.resources("zen_master[monkey]").name.should eql("monkey")
135-
end
148+
it "should find a resource by string zen_master[a]" do
149+
load_up_resources
150+
@rc.resources("zen_master[monkey]").name.should eql("monkey")
151+
end
136152

137-
it "should find resources by strings of zen_master[a,b]" do
138-
load_up_resources
139-
results = @rc.resources("zen_master[monkey,dog]")
140-
results.length.should eql(2)
141-
check_by_names(results, "monkey", "dog")
142-
end
153+
it "should find resources by strings of zen_master[a,b]" do
154+
load_up_resources
155+
results = @rc.resources("zen_master[monkey,dog]")
156+
results.length.should eql(2)
157+
check_by_names(results, "monkey", "dog")
158+
end
143159

144-
it "should find resources of multiple types by strings of zen_master[a]" do
145-
load_up_resources
146-
results = @rc.resources("zen_master[monkey]", "file[something]")
147-
results.length.should eql(2)
148-
check_by_names(results, "monkey", "something")
149-
end
160+
it "should find resources of multiple types by strings of zen_master[a]" do
161+
load_up_resources
162+
results = @rc.resources("zen_master[monkey]", "file[something]")
163+
results.length.should eql(2)
164+
check_by_names(results, "monkey", "something")
165+
end
150166

151-
it "should raise an exception if you pass a bad name to resources" do
152-
lambda { @rc.resources("michael jackson") }.should raise_error(ArgumentError)
153-
end
167+
it "should raise an exception if you pass a bad name to resources" do
168+
lambda { @rc.resources("michael jackson") }.should raise_error(ArgumentError)
169+
end
154170

155-
it "should raise an exception if you pass something other than a string or hash to resource" do
156-
lambda { @rc.resources([Array.new]) }.should raise_error(ArgumentError)
171+
it "should raise an exception if you pass something other than a string or hash to resource" do
172+
lambda { @rc.resources([Array.new]) }.should raise_error(ArgumentError)
173+
end
157174
end
158175

159-
it "should serialize to json" do
160-
json = @rc.to_json
161-
json.should =~ /json_class/
162-
json.should =~ /instance_vars/
176+
describe "to_json" do
177+
it "should serialize to json" do
178+
json = @rc.to_json
179+
json.should =~ /json_class/
180+
json.should =~ /instance_vars/
181+
end
163182
end
164183

165-
it "should deserialize itself from json" do
166-
@rc << @resource
167-
json = @rc.to_json
168-
s_rc = JSON.parse(json)
169-
s_rc.should be_a_kind_of(Chef::ResourceCollection)
170-
s_rc[0].name.should eql(@resource.name)
184+
describe "self.from_json" do
185+
it "should deserialize itself from json" do
186+
@rc << @resource
187+
json = @rc.to_json
188+
s_rc = JSON.parse(json)
189+
s_rc.should be_a_kind_of(Chef::ResourceCollection)
190+
s_rc[0].name.should eql(@resource.name)
191+
end
171192
end
172193

173194
def check_by_names(results, *names)

0 commit comments

Comments
 (0)