diff options
author | Thom May <thom@may.lt> | 2018-03-21 16:49:46 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-21 16:49:46 +0000 |
commit | f52b31f01cd4d11e503aa73aa614d5ccbb00b8f7 (patch) | |
tree | cc86e8b9e91a6317084f29af488733a28f373ea8 /spec | |
parent | 26a5e48aa7f447beaee016b0b54ee2076c308dda (diff) | |
parent | 38f11a01425445c161f1dfd2c17ceacbaabf6341 (diff) | |
download | chef-f52b31f01cd4d11e503aa73aa614d5ccbb00b8f7.tar.gz |
Merge pull request #7016 from chef/tm/chef_guid
Save the node's UUID as an attribute
Diffstat (limited to 'spec')
-rw-r--r-- | spec/support/shared/context/client.rb | 1 | ||||
-rw-r--r-- | spec/support/shared/examples/client.rb | 50 | ||||
-rw-r--r-- | spec/unit/client_spec.rb | 1 | ||||
-rw-r--r-- | spec/unit/data_collector/messages/helpers_spec.rb | 9 | ||||
-rw-r--r-- | spec/unit/node_spec.rb | 8 |
5 files changed, 68 insertions, 1 deletions
diff --git a/spec/support/shared/context/client.rb b/spec/support/shared/context/client.rb index 5acae86acf..4b90fcaedd 100644 --- a/spec/support/shared/context/client.rb +++ b/spec/support/shared/context/client.rb @@ -167,6 +167,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_rest_clean stub_for_register 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 diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index d92b2e2358..001be10e0b 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -560,7 +560,6 @@ EOM expect { client.node_name }.to raise_error(Chef::Exceptions::CannotDetermineNodeName) end end - end describe "always attempt to run handlers" do diff --git a/spec/unit/data_collector/messages/helpers_spec.rb b/spec/unit/data_collector/messages/helpers_spec.rb index a241bda699..a2c6753003 100644 --- a/spec/unit/data_collector/messages/helpers_spec.rb +++ b/spec/unit/data_collector/messages/helpers_spec.rb @@ -124,8 +124,16 @@ describe Chef::DataCollector::Messages::Helpers do end describe "#node_uuid" do + context "when the node UUID is available in Chef::Config" do + it "returns the configured value" do + Chef::Config[:chef_guid] = "configured_uuid" + expect(TestMessage.node_uuid).to eq("configured_uuid") + end + end + context "when the node UUID can be read" do it "returns the read-in node UUID" do + Chef::Config[:chef_guid] = nil allow(TestMessage).to receive(:read_node_uuid).and_return("read_uuid") expect(TestMessage.node_uuid).to eq("read_uuid") end @@ -133,6 +141,7 @@ describe Chef::DataCollector::Messages::Helpers do context "when the node UUID cannot be read" do it "generated a new node UUID" do + Chef::Config[:chef_guid] = nil allow(TestMessage).to receive(:read_node_uuid).and_return(nil) allow(TestMessage).to receive(:generate_node_uuid).and_return("generated_uuid") expect(TestMessage.node_uuid).to eq("generated_uuid") diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb index df85b79fc5..c9f3d7d1d2 100644 --- a/spec/unit/node_spec.rb +++ b/spec/unit/node_spec.rb @@ -953,6 +953,14 @@ describe Chef::Node do expect(node.automatic_attrs[:platform_version]).to eq("23.42") end + it "sets the chef guid attribute correctly" do + guid = Chef::Config[:chef_guid] + Chef::Config[:chef_guid] = "test-guid-guid" + node.consume_external_attrs(@ohai_data, {}) + expect(node.automatic_attrs[:chef_guid]).to eq("test-guid-guid") + Chef::Config[:chef_guid] = guid + end + it "consumes the run list from provided json attributes" do node.consume_external_attrs(@ohai_data, { "run_list" => ["recipe[unicorn]"] }) expect(node.run_list).to eq(["recipe[unicorn]"]) |