summaryrefslogtreecommitdiff
path: root/spec/integration/knife/deps_spec.rb
blob: 8b4d71906b2a4ccfdd8652a12d690f57be1dea5c (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
#
# Author:: John Keiser (<jkeiser@opscode.com>)
# Copyright:: Copyright (c) 2013 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 'support/shared/integration/integration_helper'
require 'support/shared/context/config'
require 'chef/knife/deps'

describe 'knife deps', :workstation do
  include IntegrationSupport
  include KnifeSupport

  context 'local' do
    when_the_repository 'has a role with no run_list' do
      before { file 'roles/starring.json', {} }
      it 'knife deps reports no dependencies' do
        knife('deps /roles/starring.json').should_succeed "/roles/starring.json\n"
      end
    end

    when_the_repository 'has a role with a default run_list' do
      before do
        file 'roles/starring.json', { 'run_list' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) }
        file 'roles/minor.json', {}
        file 'cookbooks/quiche/metadata.rb', 'name "quiche"'
        file 'cookbooks/quiche/recipes/default.rb', ''
        file 'cookbooks/soup/metadata.rb', 'name "soup"'
        file 'cookbooks/soup/recipes/chicken.rb', ''
      end
      it 'knife deps reports all dependencies' do
        knife('deps /roles/starring.json').should_succeed <<EOM
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
EOM
      end
    end

    when_the_repository 'has a role with an env_run_list' do
      before do
        file 'roles/starring.json', { 'env_run_lists' => { 'desert' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) } }
        file 'roles/minor.json', {}
        file 'cookbooks/quiche/metadata.rb', 'name "quiche"'
        file 'cookbooks/quiche/recipes/default.rb', ''
        file 'cookbooks/soup/metadata.rb', 'name "soup"'
        file 'cookbooks/soup/recipes/chicken.rb', ''
      end
      it 'knife deps reports all dependencies' do
        knife('deps /roles/starring.json').should_succeed <<EOM
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
EOM
      end
    end

    when_the_repository 'has a node with no environment or run_list' do
      before { file 'nodes/mort.json', {} }
      it 'knife deps reports just the node' do
        knife('deps /nodes/mort.json').should_succeed "/nodes/mort.json\n"
      end
    end
    when_the_repository 'has a node with an environment' do
      before do
        file 'environments/desert.json', {}
        file 'nodes/mort.json', { 'chef_environment' => 'desert' }
      end
      it 'knife deps reports just the node' do
        knife('deps /nodes/mort.json').should_succeed "/environments/desert.json\n/nodes/mort.json\n"
      end
    end
    when_the_repository 'has a node with roles and recipes in its run_list' do
      before do
        file 'roles/minor.json', {}
        file 'cookbooks/quiche/metadata.rb', 'name "quiche"'
        file 'cookbooks/quiche/recipes/default.rb', ''
        file 'cookbooks/soup/metadata.rb', 'name "soup"'
        file 'cookbooks/soup/recipes/chicken.rb', ''
        file 'nodes/mort.json', { 'run_list' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) }
      end
      it 'knife deps reports just the node' do
        knife('deps /nodes/mort.json').should_succeed <<EOM
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/nodes/mort.json
EOM
      end
    end
    when_the_repository 'has a cookbook with no dependencies' do
      before do
        file 'cookbooks/quiche/metadata.rb', 'name "quiche"'
        file 'cookbooks/quiche/recipes/default.rb', ''
      end
      it 'knife deps reports just the cookbook' do
        knife('deps /cookbooks/quiche').should_succeed "/cookbooks/quiche\n"
      end
    end
    when_the_repository 'has a cookbook with dependencies' do
      before do
        file 'cookbooks/kettle/metadata.rb', 'name "kettle"'
        file 'cookbooks/quiche/metadata.rb', 'name "quiche"
depends "kettle"'
        file 'cookbooks/quiche/recipes/default.rb', ''
      end
      it 'knife deps reports just the cookbook' do
        knife('deps /cookbooks/quiche').should_succeed "/cookbooks/kettle\n/cookbooks/quiche\n"
      end
    end
    when_the_repository 'has a data bag' do
      before { file 'data_bags/bag/item.json', {} }
      it 'knife deps reports just the data bag' do
        knife('deps /data_bags/bag/item.json').should_succeed "/data_bags/bag/item.json\n"
      end
    end
    when_the_repository 'has an environment' do
      before { file 'environments/desert.json', {} }
      it 'knife deps reports just the environment' do
        knife('deps /environments/desert.json').should_succeed "/environments/desert.json\n"
      end
    end
    when_the_repository 'has a deep dependency tree' do
      before do
        file 'roles/starring.json', { 'run_list' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) }
        file 'roles/minor.json', {}
        file 'cookbooks/quiche/metadata.rb', 'name "quiche"'
        file 'cookbooks/quiche/recipes/default.rb', ''
        file 'cookbooks/soup/metadata.rb', 'name "soup"'
        file 'cookbooks/soup/recipes/chicken.rb', ''
        file 'environments/desert.json', {}
        file 'nodes/mort.json', { 'chef_environment' => 'desert', 'run_list' => [ 'role[starring]' ] }
        file 'nodes/bart.json', { 'run_list' => [ 'role[minor]' ] }
      end

      it 'knife deps reports all dependencies' do
        knife('deps /nodes/mort.json').should_succeed <<EOM
/environments/desert.json
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
/nodes/mort.json
EOM
      end
      it 'knife deps * reports all dependencies of all things' do
        knife('deps /nodes/*').should_succeed <<EOM
/roles/minor.json
/nodes/bart.json
/environments/desert.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
/nodes/mort.json
EOM
      end
      it 'knife deps a b reports all dependencies of a and b' do
        knife('deps /nodes/bart.json /nodes/mort.json').should_succeed <<EOM
/roles/minor.json
/nodes/bart.json
/environments/desert.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
/nodes/mort.json
EOM
      end
      it 'knife deps --tree /* shows dependencies in a tree' do
        knife('deps --tree /nodes/*').should_succeed <<EOM
/nodes/bart.json
  /roles/minor.json
/nodes/mort.json
  /environments/desert.json
  /roles/starring.json
    /roles/minor.json
    /cookbooks/quiche
    /cookbooks/soup
EOM
      end
      it 'knife deps --tree --no-recurse shows only the first level of dependencies' do
        knife('deps --tree --no-recurse /nodes/*').should_succeed <<EOM
/nodes/bart.json
  /roles/minor.json
/nodes/mort.json
  /environments/desert.json
  /roles/starring.json
EOM
      end
    end

    context 'circular dependencies' do
      when_the_repository 'has cookbooks with circular dependencies' do
        before do
          file 'cookbooks/foo/metadata.rb', 'name "foo"
depends "bar"'
          file 'cookbooks/bar/metadata.rb', 'name "bar"
depends "baz"'
          file 'cookbooks/baz/metadata.rb', 'name "baz"
depends "foo"'
          file 'cookbooks/self/metadata.rb', 'name "self"
depends "self"'
        end

        it 'knife deps prints each once' do
          knife('deps /cookbooks/foo /cookbooks/self').should_succeed <<EOM
/cookbooks/baz
/cookbooks/bar
/cookbooks/foo
/cookbooks/self
EOM
        end
        it 'knife deps --tree prints each once' do
          knife('deps --tree /cookbooks/foo /cookbooks/self').should_succeed <<EOM
/cookbooks/foo
  /cookbooks/bar
    /cookbooks/baz
      /cookbooks/foo
/cookbooks/self
  /cookbooks/self
EOM
        end
      end
      when_the_repository 'has roles with circular dependencies' do
        before do
          file 'roles/foo.json', { 'run_list' => [ 'role[bar]' ] }
          file 'roles/bar.json', { 'run_list' => [ 'role[baz]' ] }
          file 'roles/baz.json', { 'run_list' => [ 'role[foo]' ] }
          file 'roles/self.json', { 'run_list' => [ 'role[self]' ] }
        end
        it 'knife deps prints each once' do
          knife('deps /roles/foo.json /roles/self.json').should_succeed <<EOM
/roles/baz.json
/roles/bar.json
/roles/foo.json
/roles/self.json
EOM
        end
        it 'knife deps --tree prints each once' do
          knife('deps --tree /roles/foo.json /roles/self.json') do
            stdout.should == "/roles/foo.json\n  /roles/bar.json\n    /roles/baz.json\n      /roles/foo.json\n/roles/self.json\n  /roles/self.json\n"
            stderr.should == "WARNING: No knife configuration file found\n"
          end
        end
      end
    end

    context 'missing objects' do
      when_the_repository 'is empty' do
        it 'knife deps /blah reports an error' do
          knife('deps /blah').should_fail(
            :exit_code => 2,
            :stdout => "/blah\n",
            :stderr => "ERROR: /blah: No such file or directory\n"
          )
        end
        it 'knife deps /roles/x.json reports an error' do
          knife('deps /roles/x.json').should_fail(
            :exit_code => 2,
            :stdout => "/roles/x.json\n",
            :stderr => "ERROR: /roles/x.json: No such file or directory\n"
          )
        end
        it 'knife deps /nodes/x.json reports an error' do
          knife('deps /nodes/x.json').should_fail(
            :exit_code => 2,
            :stdout => "/nodes/x.json\n",
            :stderr => "ERROR: /nodes/x.json: No such file or directory\n"
          )
        end
        it 'knife deps /environments/x.json reports an error' do
          knife('deps /environments/x.json').should_fail(
            :exit_code => 2,
            :stdout => "/environments/x.json\n",
            :stderr => "ERROR: /environments/x.json: No such file or directory\n"
          )
        end
        it 'knife deps /cookbooks/x reports an error' do
          knife('deps /cookbooks/x').should_fail(
            :exit_code => 2,
            :stdout => "/cookbooks/x\n",
            :stderr => "ERROR: /cookbooks/x: No such file or directory\n"
          )
        end
        it 'knife deps /data_bags/bag/item reports an error' do
          knife('deps /data_bags/bag/item').should_fail(
            :exit_code => 2,
            :stdout => "/data_bags/bag/item\n",
            :stderr => "ERROR: /data_bags/bag/item: No such file or directory\n"
          )
        end
      end
      when_the_repository 'is missing a dependent cookbook' do
        before do
          file 'roles/starring.json', { 'run_list' => [ 'recipe[quiche]'] }
        end
        it 'knife deps reports the cookbook, along with an error' do
          knife('deps /roles/starring.json').should_fail(
            :exit_code => 2,
            :stdout => "/cookbooks/quiche\n/roles/starring.json\n",
            :stderr => "ERROR: /cookbooks/quiche: No such file or directory\n"
          )
        end
      end
      when_the_repository 'is missing a dependent environment' do
        before do
          file 'nodes/mort.json', { 'chef_environment' => 'desert' }
        end
        it 'knife deps reports the environment, along with an error' do
          knife('deps /nodes/mort.json').should_fail(
            :exit_code => 2,
            :stdout => "/environments/desert.json\n/nodes/mort.json\n",
            :stderr => "ERROR: /environments/desert.json: No such file or directory\n"
          )
        end
      end
      when_the_repository 'is missing a dependent role' do
        before do
          file 'roles/starring.json', { 'run_list' => [ 'role[minor]'] }
        end
        it 'knife deps reports the role, along with an error' do
          knife('deps /roles/starring.json').should_fail(
            :exit_code => 2,
            :stdout => "/roles/minor.json\n/roles/starring.json\n",
            :stderr => "ERROR: /roles/minor.json: No such file or directory\n"
          )
        end
      end
    end
    context 'invalid objects' do
      when_the_repository 'is empty' do
        it 'knife deps / reports itself only' do
          knife('deps /').should_succeed("/\n")
        end
        it 'knife deps /roles reports an error' do
          knife('deps /roles').should_fail(
            :exit_code => 2,
            :stderr => "ERROR: /roles: No such file or directory\n",
            :stdout => "/roles\n"
          )
        end
      end
      when_the_repository 'has a data bag' do
        before { file 'data_bags/bag/item.json', '' }
        it 'knife deps /data_bags/bag shows no dependencies' do
          knife('deps /data_bags/bag').should_succeed("/data_bags/bag\n")
        end
      end
      when_the_repository 'has a cookbook' do
        before { file 'cookbooks/blah/metadata.rb', 'name "blah"' }
        it 'knife deps on a cookbook file shows no dependencies' do
          knife('deps /cookbooks/blah/metadata.rb').should_succeed(
            "/cookbooks/blah/metadata.rb\n"
          )
        end
      end
    end
  end

  context 'remote' do
    include_context "default config options"

    when_the_chef_server 'has a role with no run_list' do
      before { role 'starring', {} }
      it 'knife deps reports no dependencies' do
        knife('deps --remote /roles/starring.json').should_succeed "/roles/starring.json\n"
      end
    end

    when_the_chef_server 'has a role with a default run_list' do
      before do
        role 'starring', { 'run_list' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) }
        role 'minor', {}
        cookbook 'quiche', '1.0.0', { 'metadata.rb' => %Q{name "quiche"\nversion "1.0.0"\n}, 'recipes' => { 'default.rb' => '' } }
        cookbook 'soup', '1.0.0', { 'metadata.rb' => %Q{name "soup"\nversion "1.0.0"\n}, 'recipes' => { 'chicken.rb' => '' } }
      end
      it 'knife deps reports all dependencies' do
        knife('deps --remote /roles/starring.json').should_succeed <<EOM
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
EOM
      end
    end

    when_the_chef_server 'has a role with an env_run_list' do
      before do
        role 'starring', { 'env_run_lists' => { 'desert' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) } }
        role 'minor', {}
        cookbook 'quiche', '1.0.0', { 'metadata.rb' => %Q{name "quiche"\nversion "1.0.0"\n}, 'recipes' => { 'default.rb' => '' } }
        cookbook 'soup', '1.0.0', { 'metadata.rb' =>   %Q{name "soup"\nversion "1.0.0"\n}, 'recipes' => { 'chicken.rb' => '' } }
      end
      it 'knife deps reports all dependencies' do
        knife('deps --remote /roles/starring.json').should_succeed <<EOM
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
EOM
      end
    end

    when_the_chef_server 'has a node with no environment or run_list' do
      before { node 'mort', {} }
      it 'knife deps reports just the node' do
        knife('deps --remote /nodes/mort.json').should_succeed "/nodes/mort.json\n"
      end
    end
    when_the_chef_server 'has a node with an environment' do
      before do
        environment 'desert', {}
        node 'mort', { 'chef_environment' => 'desert' }
      end
      it 'knife deps reports just the node' do
        knife('deps --remote /nodes/mort.json').should_succeed "/environments/desert.json\n/nodes/mort.json\n"
      end
    end
    when_the_chef_server 'has a node with roles and recipes in its run_list' do
      before do
        role 'minor', {}
        cookbook 'quiche', '1.0.0', { 'metadata.rb' => %Q{name "quiche"\nversion "1.0.0"\n}, 'recipes' => { 'default.rb' => '' } }
        cookbook 'soup', '1.0.0', { 'metadata.rb' =>   %Q{name "soup"\nversion "1.0.0"\n}, 'recipes' => { 'chicken.rb' => '' } }
        node 'mort', { 'run_list' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) }
      end
      it 'knife deps reports just the node' do
        knife('deps --remote /nodes/mort.json').should_succeed <<EOM
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/nodes/mort.json
EOM
      end
    end
    when_the_chef_server 'has a cookbook with no dependencies' do
      before do
        cookbook 'quiche', '1.0.0', { 'metadata.rb' => %Q{name "quiche"\nversion "1.0.0"\n}, 'recipes' => { 'default.rb' => '' } }
      end
      it 'knife deps reports just the cookbook' do
        knife('deps --remote /cookbooks/quiche').should_succeed "/cookbooks/quiche\n"
      end
    end
    when_the_chef_server 'has a cookbook with dependencies' do
      before do
        cookbook 'kettle', '1.0.0', { 'metadata.rb' => %Q{name "kettle"\nversion "1.0.0"\n} }
        cookbook 'quiche', '1.0.0', { 'metadata.rb' => 'name "quiche"
depends "kettle"', 'recipes' => { 'default.rb' => '' } }
      end
      it 'knife deps reports the cookbook and its dependencies' do
        knife('deps --remote /cookbooks/quiche').should_succeed "/cookbooks/kettle\n/cookbooks/quiche\n"
      end
    end
    when_the_chef_server 'has a data bag' do
      before { data_bag 'bag', { 'item' => {} } }
      it 'knife deps reports just the data bag' do
        knife('deps --remote /data_bags/bag/item.json').should_succeed "/data_bags/bag/item.json\n"
      end
    end
    when_the_chef_server 'has an environment' do
      before { environment 'desert', {} }
      it 'knife deps reports just the environment' do
        knife('deps --remote /environments/desert.json').should_succeed "/environments/desert.json\n"
      end
    end
    when_the_chef_server 'has a deep dependency tree' do
      before do
        role 'starring', { 'run_list' => %w(role[minor] recipe[quiche] recipe[soup::chicken]) }
        role 'minor', {}
        cookbook 'quiche', '1.0.0', { 'metadata.rb' => %Q{name "quiche"\nversion "1.0.0"\n}, 'recipes' => { 'default.rb' => '' } }
        cookbook 'soup', '1.0.0', { 'metadata.rb' =>   %Q{name "soup"\nversion "1.0.0"\n}, 'recipes' => { 'chicken.rb' => '' } }
        environment 'desert', {}
        node 'mort', { 'chef_environment' => 'desert', 'run_list' => [ 'role[starring]' ] }
        node 'bart', { 'run_list' => [ 'role[minor]' ] }
      end

      it 'knife deps reports all dependencies' do
        knife('deps --remote /nodes/mort.json').should_succeed <<EOM
/environments/desert.json
/roles/minor.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
/nodes/mort.json
EOM
      end
      it 'knife deps * reports all dependencies of all things' do
        knife('deps --remote /nodes/*').should_succeed <<EOM
/roles/minor.json
/nodes/bart.json
/environments/desert.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
/nodes/mort.json
EOM
      end
      it 'knife deps a b reports all dependencies of a and b' do
        knife('deps --remote /nodes/bart.json /nodes/mort.json').should_succeed <<EOM
/roles/minor.json
/nodes/bart.json
/environments/desert.json
/cookbooks/quiche
/cookbooks/soup
/roles/starring.json
/nodes/mort.json
EOM
      end
      it 'knife deps --tree /* shows dependencies in a tree' do
        knife('deps --remote --tree /nodes/*').should_succeed <<EOM
/nodes/bart.json
  /roles/minor.json
/nodes/mort.json
  /environments/desert.json
  /roles/starring.json
    /roles/minor.json
    /cookbooks/quiche
    /cookbooks/soup
EOM
      end
      it 'knife deps --tree --no-recurse shows only the first level of dependencies' do
        knife('deps --remote --tree --no-recurse /nodes/*').should_succeed <<EOM
/nodes/bart.json
  /roles/minor.json
/nodes/mort.json
  /environments/desert.json
  /roles/starring.json
EOM
      end
    end

    context 'circular dependencies' do
      when_the_chef_server 'has cookbooks with circular dependencies' do
        before do
          cookbook 'foo', '1.0.0', { 'metadata.rb'  => 'name "foo"
depends "bar"' }
          cookbook 'bar', '1.0.0', { 'metadata.rb'  => 'name "bar"
depends "baz"' }
          cookbook 'baz', '1.0.0', { 'metadata.rb'  => 'name "baz"
depends "foo"' }
          cookbook 'self', '1.0.0', { 'metadata.rb' => 'name "self"
depends "self"' }
        end
        it 'knife deps prints each once' do
          knife('deps --remote /cookbooks/foo /cookbooks/self').should_succeed <<EOM
/cookbooks/baz
/cookbooks/bar
/cookbooks/foo
/cookbooks/self
EOM
        end
        it 'knife deps --tree prints each once' do
          knife('deps --remote --tree /cookbooks/foo /cookbooks/self').should_succeed <<EOM
/cookbooks/foo
  /cookbooks/bar
    /cookbooks/baz
      /cookbooks/foo
/cookbooks/self
  /cookbooks/self
EOM
        end
      end
      when_the_chef_server 'has roles with circular dependencies' do
        before do
          role 'foo', { 'run_list' => [ 'role[bar]' ] }
          role 'bar', { 'run_list' => [ 'role[baz]' ] }
          role 'baz', { 'run_list' => [ 'role[foo]' ] }
          role 'self', { 'run_list' => [ 'role[self]' ] }
        end
        it 'knife deps prints each once' do
          knife('deps --remote /roles/foo.json /roles/self.json').should_succeed <<EOM
/roles/baz.json
/roles/bar.json
/roles/foo.json
/roles/self.json
EOM
        end
        it 'knife deps --tree prints each once' do
          knife('deps --remote --tree /roles/foo.json /roles/self.json') do
            stdout.should == "/roles/foo.json\n  /roles/bar.json\n    /roles/baz.json\n      /roles/foo.json\n/roles/self.json\n  /roles/self.json\n"
            stderr.should == "WARNING: No knife configuration file found\n"
          end
        end
      end
    end

    context 'missing objects' do
      when_the_chef_server 'is empty' do
        it 'knife deps /blah reports an error' do
          knife('deps --remote /blah').should_fail(
            :exit_code => 2,
            :stdout => "/blah\n",
            :stderr => "ERROR: /blah: No such file or directory\n"
          )
        end
        it 'knife deps /roles/x.json reports an error' do
          knife('deps --remote /roles/x.json').should_fail(
            :exit_code => 2,
            :stdout => "/roles/x.json\n",
            :stderr => "ERROR: /roles/x.json: No such file or directory\n"
          )
        end
        it 'knife deps /nodes/x.json reports an error' do
          knife('deps --remote /nodes/x.json').should_fail(
            :exit_code => 2,
            :stdout => "/nodes/x.json\n",
            :stderr => "ERROR: /nodes/x.json: No such file or directory\n"
          )
        end
        it 'knife deps /environments/x.json reports an error' do
          knife('deps --remote /environments/x.json').should_fail(
            :exit_code => 2,
            :stdout => "/environments/x.json\n",
            :stderr => "ERROR: /environments/x.json: No such file or directory\n"
          )
        end
        it 'knife deps /cookbooks/x reports an error' do
          knife('deps --remote /cookbooks/x').should_fail(
            :exit_code => 2,
            :stdout => "/cookbooks/x\n",
            :stderr => "ERROR: /cookbooks/x: No such file or directory\n"
          )
        end
        it 'knife deps /data_bags/bag/item reports an error' do
          knife('deps --remote /data_bags/bag/item').should_fail(
            :exit_code => 2,
            :stdout => "/data_bags/bag/item\n",
            :stderr => "ERROR: /data_bags/bag/item: No such file or directory\n"
          )
        end
      end
      when_the_chef_server 'is missing a dependent cookbook' do
        before do
          role 'starring', { 'run_list' => [ 'recipe[quiche]'] }
        end
        it 'knife deps reports the cookbook, along with an error' do
          knife('deps --remote /roles/starring.json').should_fail(
            :exit_code => 2,
            :stdout => "/cookbooks/quiche\n/roles/starring.json\n",
            :stderr => "ERROR: /cookbooks/quiche: No such file or directory\n"
          )
        end
      end
      when_the_chef_server 'is missing a dependent environment' do
        before do
          node 'mort', { 'chef_environment' => 'desert' }
        end
        it 'knife deps reports the environment, along with an error' do
          knife('deps --remote /nodes/mort.json').should_fail(
            :exit_code => 2,
            :stdout => "/environments/desert.json\n/nodes/mort.json\n",
            :stderr => "ERROR: /environments/desert.json: No such file or directory\n"
          )
        end
      end
      when_the_chef_server 'is missing a dependent role' do
        before do
          role 'starring', { 'run_list' => [ 'role[minor]'] }
        end
        it 'knife deps reports the role, along with an error' do
          knife('deps --remote /roles/starring.json').should_fail(
            :exit_code => 2,
            :stdout => "/roles/minor.json\n/roles/starring.json\n",
            :stderr => "ERROR: /roles/minor.json: No such file or directory\n"
          )
        end
      end
    end
    context 'invalid objects' do
      when_the_chef_server 'is empty' do
        it 'knife deps / reports an error' do
          knife('deps --remote /').should_succeed("/\n")
        end
        it 'knife deps /roles reports an error' do
          knife('deps --remote /roles').should_succeed("/roles\n")
        end
      end
      when_the_chef_server 'has a data bag' do
        before { data_bag 'bag', { 'item' => {} } }
        it 'knife deps /data_bags/bag shows no dependencies' do
          knife('deps --remote /data_bags/bag').should_succeed("/data_bags/bag\n")
        end
      end
      when_the_chef_server 'has a cookbook' do
        before do
          cookbook 'blah', '1.0.0', { 'metadata.rb' => 'name "blah"' }
        end
        it 'knife deps on a cookbook file shows no dependencies' do
          knife('deps --remote /cookbooks/blah/metadata.rb').should_succeed(
            "/cookbooks/blah/metadata.rb\n"
          )
        end
      end
    end
  end

  it 'knife deps --no-recurse reports an error' do
    knife('deps --no-recurse /').should_fail("ERROR: --no-recurse requires --tree\n")
  end
end