Skip to content

Commit d03717e

Browse files
committed
Updating specs for the template provider, reflecting new caching philosophy
1 parent 89fcfe8 commit d03717e

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

chef/spec/unit/provider/template_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
File.stub!(:read).and_return("monkeypoop")
2727
@rest.stub!(:get_rest).and_return(@tempfile)
2828
@resource = Chef::Resource::Template.new("seattle")
29+
@resource.cookbook_name = "foo"
2930
@resource.path(File.join(File.dirname(__FILE__), "..", "..", "data", "seattle.txt"))
3031
@resource.source("http://foo")
3132
@node = Chef::Node.new
@@ -35,6 +36,9 @@
3536
@provider.current_resource = @resource.clone
3637
@provider.current_resource.checksum("dad86c61eea237932f201009e5431609")
3738
FileUtils.stub!(:cp).and_return(true)
39+
Chef::FileCache.stub!(:has_key).and_return(false)
40+
Chef::FileCache.stub!(:move_to).and_return(true)
41+
Chef::FileCache.stub!(:load).and_return("monkeypoop")
3842
end
3943

4044
def do_action_create
@@ -82,6 +86,50 @@ def do_action_create
8286
@provider.should_receive(:set_mode).and_return(true)
8387
do_action_create
8488
end
89+
90+
it "should build a checksum of the file in the cache (assuming it exists)" do
91+
Chef::FileCache.stub!(:has_key?).and_return(true)
92+
Chef::FileCache.stub!(:load).and_return("/some/path")
93+
@provider.should_receive(:checksum).with("/some/path")
94+
do_action_create
95+
end
96+
97+
it "should not update the filecache if the template has not been modified on the server" do
98+
error_response = mock("Net::HTTPNotModified", { :kind_of? => true })
99+
@rest.stub!(:get_rest).and_raise(Net::HTTPRetriableError.new("foo", error_response))
100+
Chef::FileCache.should_not_receive(:move_to)
101+
do_action_create
102+
end
103+
104+
it "should raise an exception if we get a Net::HTTPRetriableError that is not from a NotModified response" do
105+
error_response = mock("Net::HTTPNotModified", { :kind_of? => false })
106+
@rest.stub!(:get_rest).and_raise(Net::HTTPRetriableError.new("foo", error_response))
107+
lambda { do_action_create }.should raise_error(Net::HTTPRetriableError)
108+
end
109+
110+
end
111+
112+
describe Chef::Provider::Template, "action_create_if_missing" do
113+
before(:each) do
114+
@resource = Chef::Resource::Template.new("seattle")
115+
@resource.cookbook_name = "daft"
116+
@resource.path(File.join(File.dirname(__FILE__), "..", "..", "data", "seattle.txt"))
117+
@node = Chef::Node.new
118+
@node.name "latte"
119+
@provider = Chef::Provider::Template.new(@node, @resource)
120+
end
121+
122+
it "should not call action_create if the new resources path exists" do
123+
File.stub!(:exists?).and_return(true)
124+
@provider.should_not_receive(:action_create)
125+
@provider.action_create_if_missing
126+
end
127+
128+
it "should call action create if the new resource path does not exist" do
129+
File.stub!(:exists?).and_return(false)
130+
@provider.should_receive(:action_create).and_return(true)
131+
@provider.action_create_if_missing
132+
end
85133
end
86134

87135
describe Chef::Provider::Template, "generate_url" do

0 commit comments

Comments
 (0)