summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/shared/context/client.rb1
-rw-r--r--spec/support/shared/examples/client.rb50
2 files changed, 51 insertions, 0 deletions
diff --git a/spec/support/shared/context/client.rb b/spec/support/shared/context/client.rb
index 19ce82fa15..d92ab3d999 100644
--- a/spec/support/shared/context/client.rb
+++ b/spec/support/shared/context/client.rb
@@ -162,6 +162,7 @@ shared_context "a client run" do
Chef::Config[:cache_path] = windows? ? 'C:\chef' : "/var/chef"
Chef::Config[:why_run] = false
Chef::Config[:audit_mode] = :enabled
+ Chef::Config[:chef_guid] = "default-guid"
stub_const("Chef::Client::STDOUT_FD", stdout)
stub_const("Chef::Client::STDERR_FD", stderr)
diff --git a/spec/support/shared/examples/client.rb b/spec/support/shared/examples/client.rb
index 3c13cd767e..8ad9f6189a 100644
--- a/spec/support/shared/examples/client.rb
+++ b/spec/support/shared/examples/client.rb
@@ -14,6 +14,56 @@ shared_examples "a completed run" do
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