|
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | # you may not use this file except in compliance with the License. |
8 | 8 | # You may obtain a copy of the License at |
9 | | -# |
| 9 | +# |
10 | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
11 | | -# |
| 11 | +# |
12 | 12 | # Unless required by applicable law or agreed to in writing, software |
13 | 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
14 | 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
40 | 40 | Chef::FileCache.stub!(:has_key).and_return(false) |
41 | 41 | Chef::FileCache.stub!(:move_to).and_return(true) |
42 | 42 | Chef::FileCache.stub!(:load).and_return("monkeypoop") |
| 43 | + |
| 44 | + Chef::REST.stub!(:new).and_return(@rest) |
43 | 45 | end |
44 | 46 |
|
45 | 47 | describe "action_create" do |
|
51 | 53 | after do |
52 | 54 | Chef::Config[:solo] = false |
53 | 55 | end |
54 | | - |
| 56 | + |
55 | 57 | it "should load the correct file from the FileCache" do |
56 | 58 | Chef::Config[:file_cache_path] = '/var/chef' |
57 | 59 | @provider.stub!(:find_preferred_file).and_return('/var/chef/site-cookbooks/joe/templates/default/joe.erb') |
58 | 60 | 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 |
60 | 62 | end |
61 | 63 | end |
62 | | - |
63 | | - def do_action_create |
64 | | - Chef::REST.stub!(:new).and_return(@rest) |
65 | | - @provider.action_create |
66 | | - end |
67 | | - |
| 64 | + |
68 | 65 | it "should get the template based on the resources source value" do |
69 | 66 | @rest.should_receive(:get_rest).with(@resource.source, true).and_return(@tempfile) |
70 | | - do_action_create |
| 67 | + @provider.action_create |
71 | 68 | end |
72 | | - |
| 69 | + |
73 | 70 | it "should use the cookbook name if defined in the template resource" do |
74 | 71 | @resource.cookbook "jane" |
75 | 72 | @resource.source "template.erb" |
76 | 73 | @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 |
78 | 75 | end |
79 | | - |
| 76 | + |
80 | 77 | 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 |
84 | 79 | @resource.checksum.should == "0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924aa" |
85 | 80 | end |
86 | | - |
| 81 | + |
87 | 82 | it "should not copy the tempfile to the real file if the checksums match" do |
88 | 83 | FileUtils.should_not_receive(:cp) |
89 | | - do_action_create |
| 84 | + @provider.action_create |
90 | 85 | end |
91 | | - |
| 86 | + |
92 | 87 | it "should copy the tempfile to the real file if the checksums do not match" do |
93 | 88 | @provider.stub!(:checksum).and_return("0fd012fdc96e96f8f7cf2046522a54aed0ce470224513e45da6bc1a17a4924ab") |
94 | 89 | FileUtils.should_receive(:mv).with("/tmp/foo", CHEF_SPEC_DATA + '/templates/seattle.txt').once |
95 | 90 | @provider.stub!(:backup).and_return(true) |
96 | | - do_action_create |
| 91 | + @provider.action_create |
97 | 92 | end |
98 | | - |
| 93 | + |
99 | 94 | it "should set the owner if provided" do |
100 | 95 | @resource.owner("adam") |
101 | 96 | @provider.should_receive(:set_owner).and_return(true) |
102 | | - do_action_create |
| 97 | + @provider.action_create |
103 | 98 | end |
104 | | - |
| 99 | + |
105 | 100 | it "should set the group if provided" do |
106 | 101 | @resource.group("adam") |
107 | 102 | @provider.should_receive(:set_group).and_return(true) |
108 | | - do_action_create |
| 103 | + @provider.action_create |
109 | 104 | end |
110 | | - |
| 105 | + |
111 | 106 | it "should set the mode if provided" do |
112 | 107 | @resource.mode(0676) |
113 | 108 | @provider.should_receive(:set_mode).and_return(true) |
114 | | - do_action_create |
| 109 | + @provider.action_create |
115 | 110 | end |
116 | | - |
| 111 | + |
117 | 112 | it "should build a checksum of the file in the cache (assuming it exists)" do |
118 | 113 | Chef::FileCache.stub!(:has_key?).and_return(true) |
119 | 114 | Chef::FileCache.stub!(:load).and_return("/some/path") |
120 | 115 | @provider.should_receive(:checksum).with("/some/path") |
121 | | - do_action_create |
| 116 | + @provider.action_create |
122 | 117 | end |
123 | | - |
| 118 | + |
124 | 119 | it "should not update the filecache if the template has not been modified on the server" do |
125 | 120 | error_response = mock("Net::HTTPNotModified", { :kind_of? => true }) |
126 | 121 | @rest.stub!(:get_rest).and_raise(Net::HTTPRetriableError.new("foo", error_response)) |
127 | 122 | Chef::FileCache.should_not_receive(:move_to) |
128 | | - do_action_create |
| 123 | + @provider.action_create |
129 | 124 | end |
130 | | - |
| 125 | + |
131 | 126 | it "should raise an exception if we get a Net::HTTPRetriableError that is not from a NotModified response" do |
132 | 127 | error_response = mock("Net::HTTPNotModified", { :kind_of? => false }) |
133 | 128 | @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) |
135 | 130 | end |
136 | | - |
| 131 | + |
137 | 132 | it "should populate the template_cache as true after rendering once" do |
138 | | - do_action_create |
| 133 | + @provider.action_create |
139 | 134 | @node.run_state[:template_cache]["#{@resource.cookbook_name}_#{@resource.source}"].should eql(true) |
140 | 135 | end |
141 | | - |
| 136 | + |
142 | 137 | it "should not update the FileCache for the template on the second pass" do |
143 | | - do_action_create |
| 138 | + @provider.action_create |
144 | 139 | Chef::FileCache.should_not_receive(:move_to) |
145 | 140 | @tempfile = StringIO.new |
146 | 141 | @tempfile.stub!(:path).and_return("/tmp/foo") |
147 | 142 | Tempfile.stub!(:open).and_yield(@tempfile) |
148 | | - do_action_create |
| 143 | + @provider.action_create |
149 | 144 | end |
150 | 145 | end |
151 | | - |
| 146 | + |
152 | 147 | describe "action_create_if_missing" do |
153 | | - |
| 148 | + |
154 | 149 | it "should not call action_create if the new resources path exists" do |
155 | 150 | File.stub!(:exists?).and_return(true) |
156 | 151 | @provider.should_not_receive(:action_create) |
@@ -179,6 +174,6 @@ def do_action_create |
179 | 174 | @provider.generate_url('default/something', "templates").should eql("cookbooks/daft/templates?id=default/something&platform=monkey&version=1.0&fqdn=monkeynode&node_name=latte") |
180 | 175 | end |
181 | 176 | end |
182 | | - |
| 177 | + |
183 | 178 | end |
184 | 179 |
|
0 commit comments