librarian-chef-nochef 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,449 @@
1
+ require 'pathname'
2
+ require 'securerandom'
3
+
4
+ require 'librarian'
5
+ require 'librarian/helpers'
6
+ require 'librarian/error'
7
+ require 'librarian/action/resolve'
8
+ require 'librarian/action/install'
9
+ require 'librarian/action/update'
10
+ require 'librarian/chef'
11
+
12
+ require 'support/project_path'
13
+
14
+ module Librarian
15
+ module Chef
16
+ module Source
17
+ describe Git do
18
+
19
+ let(:project_path) { ::Support::ProjectPath.project_path }
20
+ let(:tmp_path) { project_path.join("tmp/spec/integration/chef/source/git") }
21
+ after { tmp_path.rmtree if tmp_path && tmp_path.exist? }
22
+
23
+ let(:cookbooks_path) { tmp_path.join("cookbooks") }
24
+
25
+ # depends on repo_path being defined in each context
26
+ let(:env) { Environment.new(:project_path => repo_path) }
27
+
28
+ context "a single dependency with a git source" do
29
+
30
+ let(:sample_path) { tmp_path.join("sample") }
31
+ let(:sample_metadata) do
32
+ Helpers.strip_heredoc(<<-METADATA)
33
+ version "0.6.5"
34
+ METADATA
35
+ end
36
+
37
+ let(:first_sample_path) { cookbooks_path.join("first-sample") }
38
+ let(:first_sample_metadata) do
39
+ Helpers.strip_heredoc(<<-METADATA)
40
+ version "3.2.1"
41
+ METADATA
42
+ end
43
+
44
+ let(:second_sample_path) { cookbooks_path.join("second-sample") }
45
+ let(:second_sample_metadata) do
46
+ Helpers.strip_heredoc(<<-METADATA)
47
+ version "4.3.2"
48
+ METADATA
49
+ end
50
+
51
+ before do
52
+ sample_path.rmtree if sample_path.exist?
53
+ sample_path.mkpath
54
+ sample_path.join("metadata.rb").open("wb") { |f| f.write(sample_metadata) }
55
+ Dir.chdir(sample_path) do
56
+ `git init`
57
+ `git config user.name "Simba"`
58
+ `git config user.email "simba@savannah-pride.gov"`
59
+ `git add metadata.rb`
60
+ `git commit -m "Initial commit."`
61
+ end
62
+
63
+ cookbooks_path.rmtree if cookbooks_path.exist?
64
+ cookbooks_path.mkpath
65
+ first_sample_path.mkpath
66
+ first_sample_path.join("metadata.rb").open("wb") { |f| f.write(first_sample_metadata) }
67
+ second_sample_path.mkpath
68
+ second_sample_path.join("metadata.rb").open("wb") { |f| f.write(second_sample_metadata) }
69
+ Dir.chdir(cookbooks_path) do
70
+ `git init`
71
+ `git config user.name "Simba"`
72
+ `git config user.email "simba@savannah-pride.gov"`
73
+ `git add .`
74
+ `git commit -m "Initial commit."`
75
+ end
76
+ end
77
+
78
+ context "resolving" do
79
+ let(:repo_path) { tmp_path.join("repo/resolve") }
80
+ before do
81
+ repo_path.rmtree if repo_path.exist?
82
+ repo_path.mkpath
83
+ repo_path.join("cookbooks").mkpath
84
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
85
+ #!/usr/bin/env ruby
86
+ cookbook "sample", :git => #{sample_path.to_s.inspect}
87
+ CHEFFILE
88
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
89
+ end
90
+
91
+ context "the resolve" do
92
+ it "should not raise an exception" do
93
+ expect { Action::Resolve.new(env).run }.to_not raise_error
94
+ end
95
+ end
96
+
97
+ context "the results" do
98
+ before { Action::Resolve.new(env).run }
99
+
100
+ it "should create the lockfile" do
101
+ repo_path.join("Cheffile.lock").should exist
102
+ end
103
+
104
+ it "should not attempt to install the sample cookbok" do
105
+ repo_path.join("cookbooks/sample").should_not exist
106
+ end
107
+ end
108
+ end
109
+
110
+ context "installing" do
111
+ let(:repo_path) { tmp_path.join("repo/install") }
112
+ before do
113
+ repo_path.rmtree if repo_path.exist?
114
+ repo_path.mkpath
115
+ repo_path.join("cookbooks").mkpath
116
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
117
+ #!/usr/bin/env ruby
118
+ cookbook "sample", :git => #{sample_path.to_s.inspect}
119
+ CHEFFILE
120
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
121
+
122
+ Action::Resolve.new(env).run
123
+ end
124
+
125
+ context "the install" do
126
+ it "should not raise an exception" do
127
+ expect { Action::Install.new(env).run }.to_not raise_error
128
+ end
129
+ end
130
+
131
+ context "the results" do
132
+ before { Action::Install.new(env).run }
133
+
134
+ it "should create the lockfile" do
135
+ repo_path.join("Cheffile.lock").should exist
136
+ end
137
+
138
+ it "should create the directory for the cookbook" do
139
+ repo_path.join("cookbooks/sample").should exist
140
+ end
141
+
142
+ it "should copy the cookbook files into the cookbook directory" do
143
+ repo_path.join("cookbooks/sample/metadata.rb").should exist
144
+ end
145
+ end
146
+ end
147
+
148
+ context "resolving and and separately installing" do
149
+ let(:repo_path) { tmp_path.join("repo/resolve-install") }
150
+ before do
151
+ repo_path.rmtree if repo_path.exist?
152
+ repo_path.mkpath
153
+ repo_path.join("cookbooks").mkpath
154
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
155
+ #!/usr/bin/env ruby
156
+ cookbook "sample", :git => #{sample_path.to_s.inspect}
157
+ CHEFFILE
158
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
159
+
160
+ Action::Resolve.new(env).run
161
+ repo_path.join("tmp").rmtree if repo_path.join("tmp").exist?
162
+ end
163
+
164
+ context "the install" do
165
+ it "should not raise an exception" do
166
+ expect { Action::Install.new(env).run }.to_not raise_error
167
+ end
168
+ end
169
+
170
+ context "the results" do
171
+ before { Action::Install.new(env).run }
172
+
173
+ it "should create the directory for the cookbook" do
174
+ repo_path.join("cookbooks/sample").should exist
175
+ end
176
+
177
+ it "should copy the cookbook files into the cookbook directory" do
178
+ repo_path.join("cookbooks/sample/metadata.rb").should exist
179
+ end
180
+ end
181
+ end
182
+
183
+ context "resolving, changing, and resolving" do
184
+ let(:repo_path) { tmp_path.join("repo/resolve-update") }
185
+ before do
186
+ repo_path.rmtree if repo_path.exist?
187
+ repo_path.mkpath
188
+ repo_path.join("cookbooks").mkpath
189
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
190
+ git #{cookbooks_path.to_s.inspect}
191
+ cookbook "first-sample"
192
+ CHEFFILE
193
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
194
+ Action::Resolve.new(env).run
195
+
196
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
197
+ git #{cookbooks_path.to_s.inspect}
198
+ cookbook "first-sample"
199
+ cookbook "second-sample"
200
+ CHEFFILE
201
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
202
+ end
203
+
204
+ context "the second resolve" do
205
+ it "should not raise an exception" do
206
+ expect { Action::Resolve.new(env).run }.to_not raise_error
207
+ end
208
+ end
209
+ end
210
+
211
+ end
212
+
213
+ context "with a path" do
214
+
215
+ let(:git_path) { tmp_path.join("big-git-repo") }
216
+ let(:sample_path) { git_path.join("buttercup") }
217
+ let(:sample_metadata) do
218
+ Helpers.strip_heredoc(<<-METADATA)
219
+ version "0.6.5"
220
+ METADATA
221
+ end
222
+
223
+ before do
224
+ git_path.rmtree if git_path.exist?
225
+ git_path.mkpath
226
+ sample_path.mkpath
227
+ sample_path.join("metadata.rb").open("wb") { |f| f.write(sample_metadata) }
228
+ Dir.chdir(git_path) do
229
+ `git init`
230
+ `git config user.name "Simba"`
231
+ `git config user.email "simba@savannah-pride.gov"`
232
+ `git add .`
233
+ `git commit -m "Initial commit."`
234
+ end
235
+ end
236
+
237
+ context "if no path option is given" do
238
+ let(:repo_path) { tmp_path.join("repo/resolve") }
239
+ before do
240
+ repo_path.rmtree if repo_path.exist?
241
+ repo_path.mkpath
242
+ repo_path.join("cookbooks").mkpath
243
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
244
+ #!/usr/bin/env ruby
245
+ cookbook "sample",
246
+ :git => #{git_path.to_s.inspect}
247
+ CHEFFILE
248
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
249
+ end
250
+
251
+ it "should not resolve" do
252
+ expect{ Action::Resolve.new(env).run }.to raise_error
253
+ end
254
+ end
255
+
256
+ context "if the path option is wrong" do
257
+ let(:repo_path) { tmp_path.join("repo/resolve") }
258
+ before do
259
+ repo_path.rmtree if repo_path.exist?
260
+ repo_path.mkpath
261
+ repo_path.join("cookbooks").mkpath
262
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
263
+ #!/usr/bin/env ruby
264
+ cookbook "sample",
265
+ :git => #{git_path.to_s.inspect},
266
+ :path => "jelly"
267
+ CHEFFILE
268
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
269
+ end
270
+
271
+ it "should not resolve" do
272
+ expect{ Action::Resolve.new(env).run }.to raise_error
273
+ end
274
+ end
275
+
276
+ context "if the path option is right" do
277
+ let(:repo_path) { tmp_path.join("repo/resolve") }
278
+ before do
279
+ repo_path.rmtree if repo_path.exist?
280
+ repo_path.mkpath
281
+ repo_path.join("cookbooks").mkpath
282
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
283
+ #!/usr/bin/env ruby
284
+ cookbook "sample",
285
+ :git => #{git_path.to_s.inspect},
286
+ :path => "buttercup"
287
+ CHEFFILE
288
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
289
+ end
290
+
291
+ context "the resolve" do
292
+ it "should not raise an exception" do
293
+ expect { Action::Resolve.new(env).run }.to_not raise_error
294
+ end
295
+ end
296
+
297
+ context "the results" do
298
+ before { Action::Resolve.new(env).run }
299
+
300
+ it "should create the lockfile" do
301
+ repo_path.join("Cheffile.lock").should exist
302
+ end
303
+ end
304
+ end
305
+
306
+ end
307
+
308
+ context "missing a metadata" do
309
+ let(:git_path) { tmp_path.join("big-git-repo") }
310
+ let(:repo_path) { tmp_path.join("repo/resolve") }
311
+ before do
312
+ git_path.rmtree if git_path.exist?
313
+ git_path.mkpath
314
+ Dir.chdir(git_path) do
315
+ `git init`
316
+ `git config user.name "Simba"`
317
+ `git config user.email "simba@savannah-pride.gov"`
318
+ `touch not-a-metadata`
319
+ `git add .`
320
+ `git commit -m "Initial commit."`
321
+ end
322
+ repo_path.rmtree if repo_path.exist?
323
+ repo_path.mkpath
324
+ repo_path.join("cookbooks").mkpath
325
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
326
+ cookbook "sample",
327
+ :git => #{git_path.to_s.inspect}
328
+ CHEFFILE
329
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
330
+ end
331
+
332
+ context "the resolve" do
333
+ it "should raise an exception" do
334
+ expect { Action::Resolve.new(env).run }.to raise_error
335
+ end
336
+
337
+ it "should explain the problem" do
338
+ expect { Action::Resolve.new(env).run }.
339
+ to raise_error(Error, /no metadata file found/i)
340
+ end
341
+ end
342
+
343
+ context "the results" do
344
+ before { Action::Resolve.new(env).run rescue nil }
345
+
346
+ it "should not create the lockfile" do
347
+ repo_path.join("Cheffile.lock").should_not exist
348
+ end
349
+
350
+ it "should not create the directory for the cookbook" do
351
+ repo_path.join("cookbooks/sample").should_not exist
352
+ end
353
+ end
354
+ end
355
+
356
+ context "when upstream updates" do
357
+ let(:git_path) { tmp_path.join("upstream-updates-repo") }
358
+ let(:repo_path) { tmp_path.join("repo/resolve-with-upstream-updates") }
359
+
360
+ let(:sample_metadata) do
361
+ Helpers.strip_heredoc(<<-METADATA)
362
+ version "0.6.5"
363
+ METADATA
364
+ end
365
+ before do
366
+
367
+ # set up the git repo as normal, but let's also set up a release-stable branch
368
+ # from which our Cheffile will only pull stable releases
369
+ git_path.rmtree if git_path.exist?
370
+ git_path.mkpath
371
+ git_path.join("metadata.rb").open("w+b"){|f| f.write(sample_metadata)}
372
+
373
+ Dir.chdir(git_path) do
374
+ `git init`
375
+ `git config user.name "Simba"`
376
+ `git config user.email "simba@savannah-pride.gov"`
377
+ `git add metadata.rb`
378
+ `git commit -m "Initial Commit."`
379
+ `git checkout -b some-branch --quiet`
380
+ `echo 'hi' > some-file`
381
+ `git add some-file`
382
+ `git commit -m 'Some File.'`
383
+ `git checkout master --quiet`
384
+ end
385
+
386
+ # set up the chef repo as normal, except the Cheffile points to the release-stable
387
+ # branch - we expect when the upstream copy of that branch is changed, then we can
388
+ # fetch & merge those changes when we update
389
+ repo_path.rmtree if repo_path.exist?
390
+ repo_path.mkpath
391
+ repo_path.join("cookbooks").mkpath
392
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
393
+ cookbook "sample",
394
+ :git => #{git_path.to_s.inspect},
395
+ :ref => "some-branch"
396
+ CHEFFILE
397
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
398
+ Action::Resolve.new(env).run
399
+
400
+ # change the upstream copy of that branch: we expect to be able to pull the latest
401
+ # when we re-resolve
402
+ Dir.chdir(git_path) do
403
+ `git checkout some-branch --quiet`
404
+ `echo 'ho' > some-other-file`
405
+ `git add some-other-file`
406
+ `git commit -m 'Some Other File.'`
407
+ `git checkout master --quiet`
408
+ end
409
+ end
410
+
411
+ let(:metadata_file) { repo_path.join("cookbooks/sample/metadata.rb") }
412
+ let(:old_code_file) { repo_path.join("cookbooks/sample/some-file") }
413
+ let(:new_code_file) { repo_path.join("cookbooks/sample/some-other-file") }
414
+
415
+ context "when updating not a cookbook from that source" do
416
+ before do
417
+ Action::Update.new(env).run
418
+ end
419
+
420
+ it "should pull the tip from upstream" do
421
+ Action::Install.new(env).run
422
+
423
+ metadata_file.should exist #sanity
424
+ old_code_file.should exist #sanity
425
+
426
+ new_code_file.should_not exist # the assertion
427
+ end
428
+ end
429
+
430
+ context "when updating a cookbook from that source" do
431
+ before do
432
+ Action::Update.new(env, :names => %w(sample)).run
433
+ end
434
+
435
+ it "should pull the tip from upstream" do
436
+ Action::Install.new(env).run
437
+
438
+ metadata_file.should exist #sanity
439
+ old_code_file.should exist #sanity
440
+
441
+ new_code_file.should exist # the assertion
442
+ end
443
+ end
444
+ end
445
+
446
+ end
447
+ end
448
+ end
449
+ end
@@ -0,0 +1,215 @@
1
+ require 'pathname'
2
+ require 'json'
3
+ require 'webmock'
4
+
5
+ require 'librarian'
6
+ require 'librarian/helpers'
7
+ require 'librarian/action/resolve'
8
+ require 'librarian/action/install'
9
+ require 'librarian/chef'
10
+
11
+ require 'support/project_path'
12
+
13
+ module Librarian
14
+ module Chef
15
+ module Source
16
+ describe Site do
17
+
18
+ include WebMock::API
19
+
20
+ let(:project_path) { ::Support::ProjectPath.project_path }
21
+ let(:tmp_path) { project_path.join("tmp/spec/integration/chef/source/site") }
22
+ after { tmp_path.rmtree if tmp_path && tmp_path.exist? }
23
+ let(:sample_path) { tmp_path.join("sample") }
24
+ let(:sample_metadata) do
25
+ Helpers.strip_heredoc(<<-METADATA)
26
+ version "0.6.5"
27
+ METADATA
28
+ end
29
+
30
+ let(:api_url) { "http://site.cookbooks.com" }
31
+
32
+ let(:sample_index_data) do
33
+ {
34
+ "name" => "sample",
35
+ "versions" => [
36
+ "#{api_url}/cookbooks/sample/versions/0_6_5"
37
+ ]
38
+ }
39
+ end
40
+ let(:sample_0_6_5_data) do
41
+ {
42
+ "version" => "0.6.5",
43
+ "file" => "#{api_url}/cookbooks/sample/versions/0_6_5/file.tar.gz"
44
+ }
45
+ end
46
+ let(:sample_0_6_5_package) do
47
+ s = StringIO.new
48
+ z = Zlib::GzipWriter.new(s, Zlib::NO_COMPRESSION)
49
+ t = Archive::Tar::Minitar::Output.new(z)
50
+ t.tar.add_file_simple("sample/metadata.rb", :mode => 0700,
51
+ :size => sample_metadata.bytesize){|io| io.write(sample_metadata)}
52
+ t.close
53
+ z.close unless z.closed?
54
+ s.string
55
+ end
56
+
57
+ # depends on repo_path being defined in each context
58
+ let(:env) { Environment.new(:project_path => repo_path) }
59
+
60
+ before do
61
+ stub_request(:get, "#{api_url}/cookbooks/sample").
62
+ to_return(:body => JSON.dump(sample_index_data))
63
+
64
+ stub_request(:get, "#{api_url}/cookbooks/sample/versions/0_6_5").
65
+ to_return(:body => JSON.dump(sample_0_6_5_data))
66
+
67
+ stub_request(:get, "#{api_url}/cookbooks/sample/versions/0_6_5/file.tar.gz").
68
+ to_return(:body => sample_0_6_5_package)
69
+ end
70
+
71
+ after do
72
+ WebMock.reset!
73
+ end
74
+
75
+ context "a single dependency with a site source" do
76
+
77
+ context "resolving" do
78
+ let(:repo_path) { tmp_path.join("repo/resolve") }
79
+ before do
80
+ repo_path.rmtree if repo_path.exist?
81
+ repo_path.mkpath
82
+ repo_path.join("cookbooks").mkpath
83
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
84
+ #!/usr/bin/env ruby
85
+ cookbook "sample", :site => #{api_url.inspect}
86
+ CHEFFILE
87
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
88
+ end
89
+
90
+ context "the resolve" do
91
+ it "should not raise an exception" do
92
+ expect { Action::Resolve.new(env).run }.to_not raise_error
93
+ end
94
+ end
95
+
96
+ context "the results" do
97
+ before { Action::Resolve.new(env).run }
98
+
99
+ it "should create the lockfile" do
100
+ repo_path.join("Cheffile.lock").should exist
101
+ end
102
+
103
+ it "should not attempt to install the cookbok" do
104
+ repo_path.join("cookbooks/sample").should_not exist
105
+ end
106
+ end
107
+ end
108
+
109
+ context "intalling" do
110
+ let(:repo_path) { tmp_path.join("repo/install") }
111
+ before do
112
+ repo_path.rmtree if repo_path.exist?
113
+ repo_path.mkpath
114
+ repo_path.join("cookbooks").mkpath
115
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
116
+ #!/usr/bin/env ruby
117
+ cookbook "sample", :site => #{api_url.inspect}
118
+ CHEFFILE
119
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
120
+
121
+ Action::Resolve.new(env).run
122
+ end
123
+
124
+ context "the install" do
125
+ it "should not raise an exception" do
126
+ expect { Action::Install.new(env).run }.to_not raise_error
127
+ end
128
+ end
129
+
130
+ context "the results" do
131
+ before { Action::Install.new(env).run }
132
+
133
+ it "should create the lockfile" do
134
+ repo_path.join("Cheffile.lock").should exist
135
+ end
136
+
137
+ it "should create a directory for the cookbook" do
138
+ repo_path.join("cookbooks/sample").should exist
139
+ end
140
+
141
+ it "should copy the cookbook files into the cookbook directory" do
142
+ repo_path.join("cookbooks/sample/metadata.rb").should exist
143
+ end
144
+ end
145
+ end
146
+
147
+ context "resolving and separately installing" do
148
+ let(:repo_path) { tmp_path.join("repo/resolve-install") }
149
+ before do
150
+ repo_path.rmtree if repo_path.exist?
151
+ repo_path.mkpath
152
+ repo_path.join("cookbooks").mkpath
153
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
154
+ #!/usr/bin/env ruby
155
+ cookbook "sample", :site => #{api_url.inspect}
156
+ CHEFFILE
157
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
158
+
159
+ Action::Resolve.new(env).run
160
+ repo_path.join("tmp").rmtree if repo_path.join("tmp").exist?
161
+ end
162
+
163
+ context "the install" do
164
+ it "should not raise an exception" do
165
+ expect { Action::Install.new(env).run }.to_not raise_error
166
+ end
167
+ end
168
+
169
+ context "the results" do
170
+ before { Action::Install.new(env).run }
171
+
172
+ it "should create a directory for the cookbook" do
173
+ repo_path.join("cookbooks/sample").should exist
174
+ end
175
+
176
+ it "should copy the cookbook files into the cookbook directory" do
177
+ repo_path.join("cookbooks/sample/metadata.rb").should exist
178
+ end
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ context "when the repo path has a space" do
185
+
186
+ let(:repo_path) { tmp_path.join("repo/with extra spaces/resolve") }
187
+
188
+ before do
189
+ repo_path.rmtree if repo_path.exist?
190
+ repo_path.mkpath
191
+ repo_path.join("cookbooks").mkpath
192
+
193
+ cheffile = Helpers.strip_heredoc(<<-CHEFFILE)
194
+ #!/usr/bin/env ruby
195
+ cookbook "sample", :site => #{api_url.inspect}
196
+ CHEFFILE
197
+ repo_path.join("Cheffile").open("wb") { |f| f.write(cheffile) }
198
+ end
199
+
200
+ after do
201
+ repo_path.rmtree
202
+ end
203
+
204
+ context "the resolution" do
205
+ it "should not raise an exception" do
206
+ expect { Action::Resolve.new(env).run }.to_not raise_error
207
+ end
208
+ end
209
+
210
+ end
211
+
212
+ end
213
+ end
214
+ end
215
+ end