knife-inspect 0.8.0 → 0.9.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.
Files changed (41) hide show
  1. checksums.yaml +5 -13
  2. data/.travis.yml +3 -2
  3. data/HISTORY.md +12 -0
  4. data/README.md +10 -0
  5. data/knife-inspect.gemspec +2 -2
  6. data/lib/chef/knife/cookbook_inspect.rb +4 -27
  7. data/lib/chef/knife/data_bag_inspect.rb +7 -3
  8. data/lib/chef/knife/environment_inspect.rb +4 -16
  9. data/lib/chef/knife/inspect.rb +6 -5
  10. data/lib/chef/knife/role_inspect.rb +4 -16
  11. data/lib/health_inspector.rb +11 -10
  12. data/lib/health_inspector/checklists/base.rb +30 -27
  13. data/lib/health_inspector/checklists/cookbooks.rb +44 -52
  14. data/lib/health_inspector/checklists/data_bag_items.rb +11 -12
  15. data/lib/health_inspector/checklists/data_bags.rb +5 -6
  16. data/lib/health_inspector/checklists/environments.rb +8 -9
  17. data/lib/health_inspector/checklists/roles.rb +6 -7
  18. data/lib/health_inspector/color.rb +18 -19
  19. data/lib/health_inspector/context.rb +1 -1
  20. data/lib/health_inspector/pairing.rb +27 -32
  21. data/lib/health_inspector/runner.rb +29 -0
  22. data/lib/health_inspector/version.rb +1 -1
  23. data/spec/chef-repo/data_bags/data_bag_one/one.json +4 -0
  24. data/spec/chef-repo/data_bags/data_bag_two/two.json +1 -0
  25. data/spec/chef/knife/cookbook_inspect_spec.rb +10 -0
  26. data/spec/chef/knife/data_bag_inspect_spec.rb +79 -0
  27. data/spec/chef/knife/environment_inspect_spec.rb +10 -0
  28. data/spec/chef/knife/inspect_spec.rb +51 -0
  29. data/spec/chef/knife/role_inspect_spec.rb +10 -0
  30. data/spec/health_inspector/checklists/cookbook_spec.rb +23 -21
  31. data/spec/health_inspector/checklists/cookbooks_spec.rb +14 -12
  32. data/spec/health_inspector/checklists/data_bag_item_spec.rb +3 -3
  33. data/spec/health_inspector/checklists/data_bag_items_spec.rb +96 -10
  34. data/spec/health_inspector/checklists/data_bag_spec.rb +3 -3
  35. data/spec/health_inspector/checklists/data_bags_spec.rb +28 -11
  36. data/spec/health_inspector/checklists/environment_spec.rb +6 -7
  37. data/spec/health_inspector/checklists/environments_spec.rb +9 -11
  38. data/spec/health_inspector/checklists/role_spec.rb +3 -3
  39. data/spec/health_inspector/checklists/roles_spec.rb +9 -11
  40. data/spec/spec_helper.rb +122 -56
  41. metadata +37 -26
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'chef/knife/environment_inspect'
3
+
4
+ RSpec.describe Chef::Knife::EnvironmentInspect do
5
+ it_behaves_like 'a knife inspect runner' do
6
+ let :checklist do
7
+ HealthInspector::Checklists::Environments
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+ require 'chef/knife/inspect'
3
+
4
+ RSpec.describe Chef::Knife::Inspect do
5
+ let :knife_inspect do
6
+ described_class.new
7
+ end
8
+
9
+ it 'inherits from Chef::Knife' do
10
+ expect(knife_inspect).to be_a Chef::Knife
11
+ end
12
+
13
+ describe '#run' do
14
+ subject do
15
+ knife_inspect.run
16
+ end
17
+
18
+ context 'when all the checklists pass' do
19
+ it 'runs all the check lists and exits with 0' do
20
+ { HealthInspector::Checklists::Cookbooks => true,
21
+ HealthInspector::Checklists::DataBags => true,
22
+ HealthInspector::Checklists::DataBagItems => true,
23
+ HealthInspector::Checklists::Environments => true,
24
+ HealthInspector::Checklists::Roles => true }.each do |checklist, status|
25
+ expect(checklist).to receive(:run).and_return status
26
+ end
27
+
28
+ expect(knife_inspect).to receive(:exit).with true
29
+
30
+ subject
31
+ end
32
+ end
33
+
34
+ context 'when one or more checklists fail' do
35
+ it 'runs all the check lists and exits with 1' do
36
+ { HealthInspector::Checklists::Cookbooks => true,
37
+ HealthInspector::Checklists::DataBags => false,
38
+ HealthInspector::Checklists::DataBagItems => true,
39
+ HealthInspector::Checklists::Environments => true,
40
+ HealthInspector::Checklists::Roles => true }.each do |checklist, status|
41
+ expect(checklist).to receive(:run).and_return status
42
+ end
43
+
44
+ expect(knife_inspect).to receive(:exit).with false
45
+
46
+ subject
47
+ end
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+ require 'chef/knife/role_inspect'
3
+
4
+ RSpec.describe Chef::Knife::RoleInspect do
5
+ it_behaves_like 'a knife inspect runner' do
6
+ let :checklist do
7
+ HealthInspector::Checklists::Roles
8
+ end
9
+ end
10
+ end
@@ -1,41 +1,43 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::Cookbook do
4
- let(:pairing) { described_class.new(health_inspector_context, :name => "dummy") }
3
+ RSpec.describe HealthInspector::Checklists::Cookbook do
4
+ let(:pairing) do
5
+ described_class.new(health_inspector_context, :name => 'dummy')
6
+ end
5
7
 
6
- it "should detect if an item does not exist locally" do
7
- pairing.server = "0.0.1"
8
+ it 'detects if an item does not exist locally' do
9
+ pairing.server = '0.0.1'
8
10
  pairing.local = nil
9
11
  pairing.validate
10
12
 
11
- pairing.errors.should_not be_empty
12
- pairing.errors.first.should == "exists on server but not locally"
13
+ expect(pairing.errors).not_to be_empty
14
+ expect(pairing.errors.first).to eq('exists on server but not locally')
13
15
  end
14
16
 
15
- it "should detect if an item does not exist on server" do
17
+ it 'detects if an item does not exist on server' do
16
18
  pairing.server = nil
17
- pairing.local = "0.0.1"
19
+ pairing.local = '0.0.1'
18
20
  pairing.validate
19
21
 
20
- pairing.errors.should_not be_empty
21
- pairing.errors.first.should == "exists locally but not on server"
22
+ expect(pairing.errors).not_to be_empty
23
+ expect(pairing.errors.first).to eq('exists locally but not on server')
22
24
  end
23
25
 
24
- it "should detect if an item is different" do
25
- pairing.server = "0.0.1"
26
- pairing.local = "0.0.2"
26
+ it 'detects if an item is different' do
27
+ pairing.server = '0.0.1'
28
+ pairing.local = '0.0.2'
27
29
  pairing.validate
28
30
 
29
- pairing.errors.should_not be_empty
30
- pairing.errors.first.should == "chef server has 0.0.1 but local version is 0.0.2"
31
+ expect(pairing.errors).not_to be_empty
32
+ expect(pairing.errors.first).to eq('chef server has 0.0.1 but local version is 0.0.2')
31
33
  end
32
34
 
33
- it "should detect if an item is the same" do
34
- pairing.should_receive(:validate_changes_on_the_server_not_in_the_repo)
35
- pairing.server = "0.0.1"
36
- pairing.local = "0.0.1"
35
+ it 'detects if an item is the same' do
36
+ expect(pairing).to receive(:validate_changes_on_the_server_not_in_the_repo)
37
+ pairing.server = '0.0.1'
38
+ pairing.local = '0.0.1'
37
39
  pairing.validate
38
40
 
39
- pairing.errors.should be_empty
41
+ expect(pairing.errors).to be_empty
40
42
  end
41
- end
43
+ end
@@ -1,35 +1,37 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::Cookbooks do
3
+ RSpec.describe HealthInspector::Checklists::Cookbooks do
4
4
  let :checklist do
5
5
  described_class.new(nil)
6
6
  end
7
7
 
8
8
  before do
9
- expect(HealthInspector::Context).to receive(:new).with(nil).
10
- and_return health_inspector_context
9
+ expect(HealthInspector::Context).to receive(:new).with(nil)
10
+ .and_return health_inspector_context
11
11
  end
12
12
 
13
13
  describe '#server_items' do
14
14
  it 'returns a list of roles from the chef server' do
15
- health_inspector_context.stub_chain(:rest, :get_rest).
16
- with('/cookbooks').and_return({
17
- 'cookbook_one' => { 'versions' => [{ 'version' => '1.0.0' }] },
18
- 'cookbook_two' => { 'versions' => [{ 'version' => '0.0.1' }] }
19
- })
20
- expect(checklist.server_items).to eq({
15
+ allow(health_inspector_context).to receive(:rest).and_return(double)
16
+ expect(health_inspector_context.rest).to receive(:get_rest)
17
+ .with('/cookbooks')
18
+ .and_return(
19
+ 'cookbook_one' => { 'versions' => [{ 'version' => '1.0.0' }] },
20
+ 'cookbook_two' => { 'versions' => [{ 'version' => '0.0.1' }] }
21
+ )
22
+ expect(checklist.server_items).to eq(
21
23
  'cookbook_one' => Chef::Version.new('1.0.0'),
22
24
  'cookbook_two' => Chef::Version.new('0.0.1')
23
- })
25
+ )
24
26
  end
25
27
  end
26
28
 
27
29
  describe '#local_items' do
28
30
  it 'returns a list of roles from the chef repo' do
29
- expect(checklist.local_items).to eq({
31
+ expect(checklist.local_items).to eq(
30
32
  'cookbook_one' => Chef::Version.new('1.0.0'),
31
33
  'cookbook_two' => Chef::Version.new('0.0.1')
32
- })
34
+ )
33
35
  end
34
36
  end
35
37
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::DataBagItem do
4
- it_behaves_like "a chef model"
5
- it_behaves_like "a chef model that can be represented in json"
3
+ RSpec.describe HealthInspector::Checklists::DataBagItem do
4
+ it_behaves_like 'a chef model'
5
+ it_behaves_like 'a chef model that can be represented in json'
6
6
  end
@@ -1,31 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::DataBagItems do
3
+ RSpec.describe HealthInspector::Checklists::DataBagItems do
4
4
  let :checklist do
5
5
  described_class.new(nil)
6
6
  end
7
7
 
8
8
  before do
9
- expect(HealthInspector::Context).to receive(:new).with(nil).
10
- and_return health_inspector_context
9
+ expect(HealthInspector::Context).to receive(:new).with(nil)
10
+ .and_return health_inspector_context
11
11
  end
12
12
 
13
13
  describe '#server_items' do
14
14
  it 'returns a list of data bag items from the chef server' do
15
- expect(Chef::DataBag).to receive(:list).and_return({
15
+ expect(Chef::DataBag).to receive(:list).and_return(
16
16
  'data_bag_one' => 'url',
17
17
  'data_bag_two' => 'url',
18
18
  'data_bag_from_subdir' => 'url'
19
- })
19
+ )
20
20
  { 'data_bag_one' => 'one',
21
21
  'data_bag_two' => 'two',
22
22
  'data_bag_from_subdir' => 'three' }.each do |data_bag, item|
23
- expect(Chef::DataBag).to receive(:load).with(data_bag).
24
- and_return({ item => 'url' })
23
+ expect(Chef::DataBag).to receive(:load).with(data_bag)
24
+ .and_return item => 'url'
25
25
  end
26
- expect(checklist.server_items.sort).to eq [
27
- 'data_bag_from_subdir/three', 'data_bag_one/one', 'data_bag_two/two'
28
- ]
26
+ expect(checklist.server_items.sort)
27
+ .to eq %w(data_bag_from_subdir/three data_bag_one/one data_bag_two/two)
29
28
  end
30
29
  end
31
30
 
@@ -36,4 +35,91 @@ describe HealthInspector::Checklists::DataBagItems do
36
35
  ]
37
36
  end
38
37
  end
38
+
39
+ describe '#load_item(name)' do
40
+ let :name do
41
+ 'data_bag_one/one'
42
+ end
43
+
44
+ let :item do
45
+ checklist.load_item name
46
+ end
47
+
48
+ it 'instanciates a DataBagItem pairing for that item' do
49
+ expect(checklist).to receive(:load_item_from_local)
50
+ .with(name).and_return name
51
+ expect(checklist).to receive(:load_item_from_server)
52
+ .with(name).and_return name
53
+
54
+ expect(item).to be_a(HealthInspector::Checklists::DataBagItem)
55
+ expect(item.name).to eq(name)
56
+ expect(item.server).to eq(name)
57
+ end
58
+ end
59
+
60
+ describe '#load_item_from_local(name)' do
61
+ context 'when the data bag item exists' do
62
+ let :name do
63
+ 'data_bag_one/one'
64
+ end
65
+
66
+ it 'returns a parsed version of the data bag item' do
67
+ expect(checklist.load_item_from_local(name))
68
+ .to eq('id' => 'one', 'some' => 'key')
69
+ end
70
+ end
71
+
72
+ context 'when the data bag item does not exist' do
73
+ let :name do
74
+ 'data_bag_one/non_existent'
75
+ end
76
+
77
+ it 'returns nil' do
78
+ expect(checklist.load_item_from_local(name)).to be_nil
79
+ end
80
+ end
81
+
82
+ context 'when the data bag item is not valid json' do
83
+ let :name do
84
+ 'data_bag_two/two'
85
+ end
86
+
87
+ it 'returns nil' do
88
+ expect(checklist.load_item_from_local(name)).to be_nil
89
+ end
90
+ end
91
+ end
92
+
93
+ describe '#load_item_from_server(name)' do
94
+ context 'when the data bag item exists' do
95
+ let :name do
96
+ 'data_bag_one/one'
97
+ end
98
+
99
+ let :data_bag_item do
100
+ double(:raw_data => { 'id' => 'one', 'some' => 'key' })
101
+ end
102
+
103
+ it 'returns a parsed version of the data bag item' do
104
+ expect(Chef::DataBagItem).to receive(:load).with('data_bag_one', 'one')
105
+ .and_return(data_bag_item)
106
+ expect(checklist.load_item_from_server(name))
107
+ .to eq('id' => 'one', 'some' => 'key')
108
+ end
109
+ end
110
+
111
+ context 'when the data bag item does not exist' do
112
+ let :name do
113
+ 'data_bag_one/non_existent'
114
+ end
115
+
116
+ it 'returns nil' do
117
+ expect(Chef::DataBagItem).to receive(:load)
118
+ .with('data_bag_one', 'non_existent')
119
+ .and_raise(Net::HTTPServerException.new('404 "Object Not Found"',
120
+ 'response'))
121
+ expect(checklist.load_item_from_server(name)).to be_nil
122
+ end
123
+ end
124
+ end
39
125
  end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::DataBag do
4
- it_behaves_like "a chef model"
5
- end
3
+ RSpec.describe HealthInspector::Checklists::DataBag do
4
+ it_behaves_like 'a chef model'
5
+ end
@@ -1,33 +1,50 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::DataBags do
3
+ RSpec.describe HealthInspector::Checklists::DataBags do
4
4
  let :checklist do
5
5
  described_class.new(nil)
6
6
  end
7
7
 
8
8
  before do
9
- expect(HealthInspector::Context).to receive(:new).with(nil).
10
- and_return health_inspector_context
9
+ expect(HealthInspector::Context).to receive(:new).with(nil)
10
+ .and_return health_inspector_context
11
11
  end
12
12
 
13
13
  describe '#server_items' do
14
14
  it 'returns a list of data bags from the chef server' do
15
- expect(Chef::DataBag).to receive(:list).and_return({
15
+ expect(Chef::DataBag).to receive(:list).and_return(
16
16
  'data_bag_one' => 'url',
17
17
  'data_bag_two' => 'url',
18
18
  'data_bag_from_subdir' => 'url'
19
- })
20
- expect(checklist.server_items.sort).to eq [
21
- 'data_bag_from_subdir', 'data_bag_one', 'data_bag_two'
22
- ]
19
+ )
20
+ expect(checklist.server_items.sort)
21
+ .to eq %w(data_bag_from_subdir data_bag_one data_bag_two)
23
22
  end
24
23
  end
25
24
 
26
25
  describe '#local_items' do
27
26
  it 'returns a list of data bags from the chef repo' do
28
- expect(checklist.local_items.sort).to eq [
29
- 'data_bag_from_subdir', 'data_bag_one', 'data_bag_two'
30
- ]
27
+ expect(checklist.local_items.sort)
28
+ .to eq %w(data_bag_from_subdir data_bag_one data_bag_two)
29
+ end
30
+ end
31
+
32
+ describe '#load_item(name)' do
33
+ let :name do
34
+ 'data bag'
35
+ end
36
+
37
+ let :item do
38
+ checklist.load_item name
39
+ end
40
+
41
+ it 'instanciates a DataBag pairing for that item' do
42
+ expect(checklist).to receive(:server_items).and_return ['data bag']
43
+ expect(checklist).to receive(:local_items).and_return ['data bag']
44
+ expect(item).to be_a(HealthInspector::Checklists::DataBag)
45
+ expect(item.name).to eq(name)
46
+ expect(item.server).to eq(true)
47
+ expect(item.local).to eq(true)
31
48
  end
32
49
  end
33
50
  end
@@ -1,18 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::Environment do
3
+ RSpec.describe HealthInspector::Checklists::Environment do
4
4
  let(:pairing) { described_class.new(health_inspector_context) }
5
5
 
6
- it_behaves_like "a chef model"
7
- it_behaves_like "a chef model that can be represented in json"
6
+ it_behaves_like 'a chef model'
7
+ it_behaves_like 'a chef model that can be represented in json'
8
8
 
9
- it "should ignore _default environment if it only exists on server" do
10
- pairing.name = "_default"
9
+ it 'ignores _default environment if it only exists on server' do
10
+ pairing.name = '_default'
11
11
  pairing.server = {}
12
12
  pairing.local = nil
13
13
  pairing.validate
14
14
 
15
- pairing.errors.should be_empty
15
+ expect(pairing.errors).to be_empty
16
16
  end
17
-
18
17
  end
@@ -1,33 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe HealthInspector::Checklists::Environments do
3
+ RSpec.describe HealthInspector::Checklists::Environments do
4
4
  let :checklist do
5
5
  described_class.new(nil)
6
6
  end
7
7
 
8
8
  before do
9
- expect(HealthInspector::Context).to receive(:new).with(nil).
10
- and_return health_inspector_context
9
+ expect(HealthInspector::Context).to receive(:new).with(nil)
10
+ .and_return health_inspector_context
11
11
  end
12
12
 
13
13
  describe '#server_items' do
14
14
  it 'returns a list of environments from the chef server' do
15
- expect(Chef::Environment).to receive(:list).and_return({
15
+ expect(Chef::Environment).to receive(:list).and_return(
16
16
  'environment_one' => 'url',
17
17
  'environment_two' => 'url',
18
18
  'environment_from_subdir' => 'url'
19
- })
20
- expect(checklist.server_items.sort).to eq [
21
- 'environment_from_subdir', 'environment_one', 'environment_two'
22
- ]
19
+ )
20
+ expect(checklist.server_items.sort)
21
+ .to eq %w(environment_from_subdir environment_one environment_two)
23
22
  end
24
23
  end
25
24
 
26
25
  describe '#local_items' do
27
26
  it 'returns a list of environments from the chef repo' do
28
- expect(checklist.local_items.sort).to eq [
29
- 'environment_from_subdir', 'environment_one', 'environment_two'
30
- ]
27
+ expect(checklist.local_items.sort)
28
+ .to eq %w(environment_from_subdir environment_one environment_two)
31
29
  end
32
30
  end
33
31
  end