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
|
require "spec_helper"
require "spec/support/shared/context/client"
# requires platform and platform_version be defined
shared_examples "a completed run" do
include_context "run completed"
it "runs ohai, sets up authentication, loads node state, synchronizes policy, converges, and runs audits" do
# This is what we're testing.
expect(client.run).to be true
# fork is stubbed, so we can see the outcome of the run
expect(node.automatic_attrs[:platform]).to eq(platform)
expect(node.automatic_attrs[:platform_version]).to eq(platform_version)
end
describe "setting node GUID" do
let(:chef_guid_path) { "/tmp/chef_guid" }
let(:chef_guid) { "test-test-test" }
let(:metadata_file) { "data_collector_metadata.json" }
let(:metadata_path) { Pathname.new(File.join(Chef::Config[:file_cache_path], metadata_file)).cleanpath.to_s }
let(:file) { instance_double(File) }
before do
Chef::Config[:chef_guid_path] = chef_guid_path
Chef::Config[:chef_guid] = nil
end
it "loads from the config" do
expect(File).to receive(:exists?).with(chef_guid_path).and_return(true)
expect(File).to receive(:read).with(chef_guid_path).and_return(chef_guid)
client.run
expect(Chef::Config[:chef_guid]).to eql(chef_guid)
expect(node.automatic_attrs[:chef_guid]).to eql(chef_guid)
end
it "loads from the data collector config" do
expect(File).to receive(:exists?).with(chef_guid_path).and_return(false)
expect(Chef::FileCache).to receive(:load).with(metadata_file).and_return("{\"node_uuid\": \"#{chef_guid}\"}")
expect(File).to receive(:open).with(chef_guid_path, "w+").and_yield(file)
expect(file).to receive(:write).with(chef_guid)
client.run
expect(Chef::Config[:chef_guid]).to eql(chef_guid)
expect(node.automatic_attrs[:chef_guid]).to eql(chef_guid)
end
it "creates a new one" do
expect(File).to receive(:exists?).with(chef_guid_path).and_return(false)
expect(File).to receive(:exists?).with(metadata_path).and_return(false)
expect(SecureRandom).to receive(:uuid).and_return(chef_guid).at_least(:once)
# we'll try and write the generated UUID to the data collector too, and that's ok
allow(File).to receive(:open).with(metadata_path, "w", 420)
expect(File).to receive(:open).with(chef_guid_path, "w+").and_yield(file)
expect(file).to receive(:write).with(chef_guid)
client.run
expect(Chef::Config[:chef_guid]).to eql(chef_guid)
expect(node.automatic_attrs[:chef_guid]).to eql(chef_guid)
end
end
end
shared_examples "a completed run with audit failure" do
include_context "run completed"
before do
expect(Chef::Application).to receive(:debug_stacktrace).with an_instance_of(Chef::Exceptions::RunFailedWrappingError)
end
it "converges, runs audits, saves the node and raises the error in a wrapping error" do
expect { client.run }.to raise_error(Chef::Exceptions::RunFailedWrappingError) do |error|
expect(error.wrapped_errors.size).to eq(run_errors.size)
run_errors.each do |run_error|
expect(error.wrapped_errors).to include(run_error)
expect(error.backtrace).to include(*run_error.backtrace)
end
end
# fork is stubbed, so we can see the outcome of the run
expect(node.automatic_attrs[:platform]).to eq(platform)
expect(node.automatic_attrs[:platform_version]).to eq(platform_version)
end
end
shared_examples "a failed run" do
include_context "run failed"
it "skips node save and raises the error in a wrapping error" do
expect { client.run }.to raise_error(Chef::Exceptions::RunFailedWrappingError) do |error|
expect(error.wrapped_errors.size).to eq(run_errors.size)
run_errors.each do |run_error|
expect(error.wrapped_errors).to include(run_error)
expect(error.backtrace).to include(*run_error.backtrace)
end
end
end
end
|