-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookbook_steps.rb
More file actions
312 lines (263 loc) · 12.6 KB
/
Copy pathcookbook_steps.rb
File metadata and controls
312 lines (263 loc) · 12.6 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Christopher Walters (<cw@opscode.com>)
# Author:: Tim Hinderliter (<tim@opscode.com>)
# Copyright:: Copyright (c) 2008, 2010 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 'chef/cookbook/file_system_file_vendor'
def compare_manifests(manifest1, manifest2)
Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
next unless manifest1[segment]
manifest2.should have_key(segment)
manifest2_records_by_path = manifest2[segment].inject({}) {|memo,manifest2_record| memo[manifest2_record[:path]] = manifest2_record; memo}
manifest1[segment].each do |manifest1_record|
path = manifest1_record[:path]
manifest2_records_by_path.should have_key(path)
manifest1_record.should == manifest2_records_by_path[path]
end
end
end
Before do
save_cookbook_path = Chef::Config[:cookbook_path]
FileUtils.mkdir "#{datadir}/cookbooks_not_uploaded_at_feature_start/testcookbook_invalid_empty" unless File.exist?("#{datadir}/cookbooks_not_uploaded_at_feature_start/testcookbook_invalid_empty")
Chef::Config[:cookbook_path] = File.join(datadir, "cookbooks_not_uploaded_at_feature_start")
Chef::Cookbook::FileVendor.on_create {|manifest| Chef::Cookbook::FileSystemFileVendor.new(manifest) }
@cookbook_loader_not_uploaded_at_feature_start = Chef::CookbookLoader.new
Chef::Config[:cookbook_path] = save_cookbook_path
end
Given /^a local cookbook repository$/ do
Dir.mkdir(File.join(tmpdir, 'cookbooks_dir'))
Dir.mkdir(File.join(tmpdir, 'cookbooks_dir', 'cookbooks'))
Dir.mkdir(File.join(tmpdir, 'cookbooks_dir', 'config'))
system("cp #{datadir}/Rakefile #{tmpdir}/cookbooks_dir")
system("cp -r #{datadir}/config/* #{tmpdir}/cookbooks_dir/config")
system("cp -r #{datadir}/cookbooks/* #{tmpdir}/cookbooks_dir/cookbooks")
cleanup_dirs << "#{tmpdir}/cookbooks_dir"
end
Given /^a local cookbook named '(.+)'$/ do |cb|
Dir.mkdir(File.join(tmpdir, 'cookbooks_dir'))
Dir.mkdir(File.join(tmpdir, 'cookbooks_dir', 'cookbooks'))
Dir.mkdir(File.join(tmpdir, 'cookbooks_dir', 'config'))
system("cp #{datadir}/Rakefile #{tmpdir}/cookbooks_dir")
system("cp -r #{datadir}/config/* #{tmpdir}/cookbooks_dir/config")
system("cp -r #{datadir}/cookbooks/#{cb} #{tmpdir}/cookbooks_dir/cookbooks")
cleanup_dirs << "#{tmpdir}/cookbooks_dir"
end
Given "I upload the cookbook" do
cookbook_name, recipe_name = recipe.split('::')
shell_out!("#{KNIFE_CMD} cookbook upload -c #{KNIFE_CONFIG} -a -o #{INTEGRATION_COOKBOOKS}")
end
When /^I run the task to generate cookbook metadata for '(.+)'$/ do |cb|
self.cookbook = cb
When('I run the task to generate cookbook metadata')
end
When /^I run the task to generate cookbook metadata$/ do
to_run = "#{KNIFE_CMD} cookbook metadata"
if cookbook
to_run += " #{cookbook}"
else
to_run += " -a"
end
to_run += " -o #{File.join(tmpdir, 'cookbooks_dir', 'cookbooks')}"
Dir.chdir(File.join(tmpdir, 'cookbooks_dir', 'cookbooks')) do
self.status = Chef::Mixin::Command.popen4(to_run) do |p, i, o, e|
self.stdout = o.gets(nil)
self.stderr = o.gets(nil)
end
end
end
#####
# Cookbook upload/download-specific steps
#####
When /^I create a versioned cookbook(?: named '(.*?)')?(?: versioned '(.*?)')? with '(.*?)'$/ do |request_name, request_version, cookbook_name|
cookbook = @cookbook_loader_not_uploaded_at_feature_start[cookbook_name]
raise ArgumentError, "no such cookbook in cookbooks_not_uploaded_at_feature_start: #{cookbook_name}" unless cookbook
begin
self.api_response = rest.put_rest("/cookbooks/#{request_name}/#{request_version}", cookbook)
self.inflated_response = api_response
rescue => e
self.exception = e
end
end
# The argument handling in the above step defn isn't working for me, so dup city.
# :/
When "I create a cookbook named '$cookbook_name' with only the metadata file" do |cookbook_name|
cookbook = @cookbook_loader_not_uploaded_at_feature_start[cookbook_name.to_sym]
raise ArgumentError, "no such cookbook in cookbooks_not_uploaded_at_feature_start: #{cookbook_name}" unless cookbook
begin
self.api_response = rest.put_rest("/cookbooks/#{cookbook_name}/1.0.0", cookbook)
self.inflated_response = api_response
rescue => e
Chef::Log.debug("Caught exception #{e} from HTTP request")
self.exception = e
end
end
When /^I create a sandbox named '(.+)' for cookbook '([^\']+)'(?: minus files '(.+)')?$/ do |sandbox_name, cookbook_name, filenames_to_exclude|
cookbook = @cookbook_loader_not_uploaded_at_feature_start[cookbook_name]
raise ArgumentError, "no such cookbook in cookbooks_not_uploaded_at_feature_start: #{cookbook_name}" unless cookbook
if filenames_to_exclude
filenames_to_exclude = filenames_to_exclude.split(",").inject({}) { |memo, filename| memo[filename] = 1; memo }
else
filenames_to_exclude = Hash.new
end
# add all the checksums from the given cookbook into the sandbox.
checksums = Hash.new
Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
next unless cookbook.manifest[segment]
cookbook.manifest[segment].each do |manifest_record|
# include the checksum, unless it was included in the filenames to exclude
checksums[manifest_record[:checksum]] = nil unless filenames_to_exclude.has_key?(manifest_record[:path])
end
end
sandbox = {
:checksums => checksums
}
begin
self.api_response = self.inflated_response = nil
self.exception = nil
self.inflated_response = rest.post_rest('/sandboxes', sandbox)
self.sandbox_url = self.inflated_response['uri']
@stash['sandbox_response'] = self.inflated_response
rescue
Chef::Log.debug("Caught exception in sandbox create (POST) request: #{$!.message}: #{$!.backtrace.join("\n")}")
self.exception = $!
end
end
Then /^I upload a file named '(.+)' from cookbook '(.+)' to the sandbox/ do |path, cookbook_name|
cookbook = @cookbook_loader_not_uploaded_at_feature_start[cookbook_name]
raise ArgumentError, "no such cookbook in cookbooks_not_uploaded_at_feature_start: #{cookbook_name}" unless cookbook
if path =~ /([^\/]+)\/(.+)/
segment, path_no_segment = $1, $2
else
segment = :root_files
path_no_segment = path
end
if cookbook.manifest[segment]
manifest_record = cookbook.manifest[segment].find {|manifest_record| manifest_record[:path] == path }
end
raise ArgumentError, "no such file in cookbooks_not_uploaded_at_feature_start/#{cookbook_name}: #{path}" unless manifest_record
full_path = File.join(datadir, "cookbooks_not_uploaded_at_feature_start", cookbook_name, path)
begin
url = @stash['sandbox_response']['checksums'][manifest_record[:checksum]]['url']
upload_to_sandbox(full_path, manifest_record[:checksum], url)
rescue
Chef::Log.debug("Caught exception in cookbook/sandbox checksum upload (PUT) request: #{$!.message}: #{$!.backtrace.join("\n")}")
self.exception = $!
end
end
# Shortcut for uploading a whole cookbook based on data in the
# cookbooks_not_uploaded_at_feature_start directory
Then /I fully upload a sandboxed cookbook (force-)?named '([^\']+)' versioned '([^\']+)' with '(.+)'/ do |forced, request_name, request_version, cookbook_name|
cookbook = @cookbook_loader_not_uploaded_at_feature_start[cookbook_name]
raise ArgumentError, "no such cookbook in cookbooks_not_uploaded_at_feature_start: #{cookbook_name}" unless cookbook
# If they said 'force-named', we will reach into the cookbook and change its
# name. This is to get around the fact that CookbookLoader uses the
# directory name as the cookbook name. This is super awesome right here.
if forced == "force-"
# If the paths contain the name of the old cookbook name, change it to the
# new cookbook name.
Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
next unless cookbook.manifest[segment]
cookbook.manifest[segment].each do |manifest_record|
if manifest_record[:path] =~ /^(.+)\/#{cookbook.name}\/(.+)$/
manifest_record[:path] = "#{$1}/#{request_name}/#{$2}"
end
end
end
cookbook.name = request_name
cookbook.manifest[:cookbook_name] = request_name
cookbook.manifest[:name] = "#{cookbook.manifest[:cookbook_name]}-#{cookbook.manifest[:version]}"
end
When "I create a sandbox named 'sandbox1' for cookbook '#{cookbook_name}'"
Then "the inflated responses key 'uri' should match '^http://.+/sandboxes/[^\/]+$'"
Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
next unless cookbook.manifest[segment]
cookbook.manifest[segment].each do |manifest_record|
full_path = File.join(datadir, "cookbooks_not_uploaded_at_feature_start", cookbook_name, manifest_record[:path])
begin
url = @stash['sandbox_response']['checksums'][manifest_record[:checksum]]['url']
upload_to_sandbox(full_path, manifest_record[:checksum], url)
rescue
Chef::Log.debug("Caught exception in cookbook/sandbox checksum upload (PUT) request: #{$!.message}: #{$!.backtrace.join("\n")}")
self.exception = $!
end
Then "the response code should be '200'"
end
end
When "I commit the sandbox"
Then "I should not get an exception"
When "I create a versioned cookbook named '#{request_name}' versioned '#{request_version}' with '#{cookbook_name}'"
Then "I should not get an exception"
end
When /I download the cookbook manifest for '(.+)' version '(.+)'$/ do |cookbook_name, cookbook_version|
self.api_response = self.inflated_response = self.exception = nil
When "I 'GET' to the path '/cookbooks/#{cookbook_name}/#{cookbook_version}'"
@downloaded_cookbook = self.inflated_response
end
Then /the downloaded cookbook manifest contents should match '(.+)'$/ do |cookbook_name|
expected_cookbook = @cookbook_loader_not_uploaded_at_feature_start[cookbook_name]
raise ArgumentError, "no such cookbook in cookbooks_not_uploaded_at_feature_start: #{cookbook_name}" unless expected_cookbook
downloaded_cookbook_manifest = Mash.new(@downloaded_cookbook.manifest)
downloaded_cookbook_manifest.delete("uri")
# remove the uri's from the manifest records
Chef::CookbookVersion::COOKBOOK_SEGMENTS.each do |segment|
next unless downloaded_cookbook_manifest[segment]
downloaded_cookbook_manifest[segment].each do |downloaded_manifest_record|
downloaded_manifest_record.delete("url")
end
end
# ensure that each file expected (from the cookbook on disk) was downloaded,
# and then do the opposite.
begin
compare_manifests(expected_cookbook.manifest, downloaded_cookbook_manifest)
compare_manifests(downloaded_cookbook_manifest, expected_cookbook.manifest)
rescue
pp({:expected_cookbook_manifest => expected_cookbook.manifest})
pp({:downloaded_cookbook_manifest => downloaded_cookbook_manifest})
raise
end
end
When /I download the file '([^\']+)' from the downloaded cookbook manifest/ do |path|
raise "no @downloaded_cookbook" unless @downloaded_cookbook
# TODO: timh, 2010-5-26: Cookbook really should have a "get me a file by its
# path" method.
if path =~ /^([^\/]+)\/(.+)$/
segment, path_in_segment = $1, $2
else
segment = :root_files
path_in_segment = path
end
raise "no such file #{path}" unless @downloaded_cookbook.manifest[segment]
found_manifest_record = @downloaded_cookbook.manifest[segment].find {|manifest_record| manifest_record[:path] == path}
raise "no such file #{path}" unless found_manifest_record
begin
cookbook_name = @downloaded_cookbook.name
cookbook_version = @downloaded_cookbook.version
checksum = found_manifest_record[:checksum]
self.api_response = nil
self.inflated_response = nil
self.exception = nil
url = found_manifest_record[:url]
downloaded_cookbook_file = rest.get_rest(url, true)
@downloaded_cookbook_file_contents = IO.read(downloaded_cookbook_file.path)
rescue
self.exception = $!
end
end
Then /^the downloaded cookbook file contents should match the pattern '(.+)'$/ do |pattern|
raise "no @downloaded_cookbook_file_contents" unless @downloaded_cookbook_file_contents
@downloaded_cookbook_file_contents.should =~ /#{pattern}/
end