Skip to content

Commit 205ef9d

Browse files
committed
cleanup template provider spec
1 parent ff81392 commit 205ef9d

1 file changed

Lines changed: 36 additions & 41 deletions

File tree

‎chef/spec/unit/provider/template_spec.rb‎

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -40,6 +40,8 @@
4040
Chef::FileCache.stub!(:has_key).and_return(false)
4141
Chef::FileCache.stub!(:move_to).and_return(true)
4242
Chef::FileCache.stub!(:load).and_return("monkeypoop")
43+
44+
Chef::REST.stub!(:new).and_return(@rest)
4345
end
4446

4547
describe "action_create" do
@@ -51,106 +53,99 @@
5153
after do
5254
Chef::Config[:solo] = false
5355
end
54-
56+
5557
it "should load the correct file from the FileCache" do
5658
Chef::Config[:file_cache_path] = '/var/chef'
5759
@provider.stub!(:find_preferred_file).and_return('/var/chef/site-cookbooks/joe/templates/default/joe.erb')
5860
Chef::FileCache.should_receive(:load).with('site-cookbooks/joe/templates/default/joe.erb').and_return('joe template')
59-
do_action_create
61+
@provider.action_create
6062
end
6163
end
62-
63-
def do_action_create
64-
Chef::REST.stub!(:new).and_return(@rest)
65-
@provider.action_create
66-
end
67-
64+
6865
it "should get the template based on the resources source value" do
6966
@rest.should_receive(:get_rest).with(@resource.source, true).and_return(@tempfile)
70-
do_action_create
67+
@provider.action_create
7168
end
72-
69+
7370
it "should use the cookbook name if defined in the template resource" do
7471
@resource.cookbook "jane"
7572
@resource.source "template.erb"
7673
@provider.should_receive(:fetch_template_via_rest).with("cookbooks/jane/templates/default/template.erb", "jane_template.erb")
77-
do_action_create
74+
@provider.action_create
7875
end
79-
76+
8077
it "should set the checksum of the new resource to the value of the returned template" do
81-
#@resource.should_receive(:checksum).with("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa").once
82-
#@resource.should_receive(:checksum).twice
83-
do_action_create
78+
@provider.action_create
8479
@resource.checksum.should == "0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa"
8580
end
86-
81+
8782
it "should not copy the tempfile to the real file if the checksums match" do
8883
FileUtils.should_not_receive(:cp)
89-
do_action_create
84+
@provider.action_create
9085
end
91-
86+
9287
it "should copy the tempfile to the real file if the checksums do not match" do
9388
@provider.stub!(:checksum).and_return("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924ab")
9489
FileUtils.should_receive(:mv).with("/tmp/foo", CHEF_SPEC_DATA + '/templates/seattle.txt').once
9590
@provider.stub!(:backup).and_return(true)
96-
do_action_create
91+
@provider.action_create
9792
end
98-
93+
9994
it "should set the owner if provided" do
10095
@resource.owner("adam")
10196
@provider.should_receive(:set_owner).and_return(true)
102-
do_action_create
97+
@provider.action_create
10398
end
104-
99+
105100
it "should set the group if provided" do
106101
@resource.group("adam")
107102
@provider.should_receive(:set_group).and_return(true)
108-
do_action_create
103+
@provider.action_create
109104
end
110-
105+
111106
it "should set the mode if provided" do
112107
@resource.mode(0676)
113108
@provider.should_receive(:set_mode).and_return(true)
114-
do_action_create
109+
@provider.action_create
115110
end
116-
111+
117112
it "should build a checksum of the file in the cache (assuming it exists)" do
118113
Chef::FileCache.stub!(:has_key?).and_return(true)
119114
Chef::FileCache.stub!(:load).and_return("/some/path")
120115
@provider.should_receive(:checksum).with("/some/path")
121-
do_action_create
116+
@provider.action_create
122117
end
123-
118+
124119
it "should not update the filecache if the template has not been modified on the server" do
125120
error_response = mock("Net::HTTPNotModified", { :kind_of? => true })
126121
@rest.stub!(:get_rest).and_raise(Net::HTTPRetriableError.new("foo", error_response))
127122
Chef::FileCache.should_not_receive(:move_to)
128-
do_action_create
123+
@provider.action_create
129124
end
130-
125+
131126
it "should raise an exception if we get a Net::HTTPRetriableError that is not from a NotModified response" do
132127
error_response = mock("Net::HTTPNotModified", { :kind_of? => false })
133128
@rest.stub!(:get_rest).and_raise(Net::HTTPRetriableError.new("foo", error_response))
134-
lambda { do_action_create }.should raise_error(Net::HTTPRetriableError)
129+
lambda { @provider.action_create }.should raise_error(Net::HTTPRetriableError)
135130
end
136-
131+
137132
it "should populate the template_cache as true after rendering once" do
138-
do_action_create
133+
@provider.action_create
139134
@node.run_state[:template_cache]["#{@resource.cookbook_name}_#{@resource.source}"].should eql(true)
140135
end
141-
136+
142137
it "should not update the FileCache for the template on the second pass" do
143-
do_action_create
138+
@provider.action_create
144139
Chef::FileCache.should_not_receive(:move_to)
145140
@tempfile = StringIO.new
146141
@tempfile.stub!(:path).and_return("/tmp/foo")
147142
Tempfile.stub!(:open).and_yield(@tempfile)
148-
do_action_create
143+
@provider.action_create
149144
end
150145
end
151-
146+
152147
describe "action_create_if_missing" do
153-
148+
154149
it "should not call action_create if the new resources path exists" do
155150
File.stub!(:exists?).and_return(true)
156151
@provider.should_not_receive(:action_create)
@@ -179,6 +174,6 @@ def do_action_create
179174
@provider.generate_url('default/something', "templates").should eql("cookbooks/daft/templates?id=default/something&platform=monkey&version=1.0&fqdn=monkeynode&node_name=latte")
180175
end
181176
end
182-
177+
183178
end
184179

0 commit comments

Comments
 (0)