summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2017-04-03 13:34:47 -0700
committerGitHub <noreply@github.com>2017-04-03 13:34:47 -0700
commitdfbbf953708ced1d745542306b6aa26ed2dbeaf2 (patch)
treead9b3a589fa9573c6a2c522ecb1a4025896bbf63
parent361f5a54b02e455c059dfabe3e18620857827f2d (diff)
parentfee12cf34e0f4ce48842df00711fe86bb9d02fb3 (diff)
downloadchef-dfbbf953708ced1d745542306b6aa26ed2dbeaf2.tar.gz
Merge pull request #5973 from chef/jjh/policy_collector_fields
Add policy_name and policy_group indexes to converge message sent to …
-rw-r--r--lib/chef/data_collector/messages.rb2
-rw-r--r--spec/unit/data_collector/messages_spec.rb137
2 files changed, 139 insertions, 0 deletions
diff --git a/lib/chef/data_collector/messages.rb b/lib/chef/data_collector/messages.rb
index c0683534a9..ca854957d1 100644
--- a/lib/chef/data_collector/messages.rb
+++ b/lib/chef/data_collector/messages.rb
@@ -73,6 +73,8 @@ class Chef
"resources" => reporter_data[:resources].map(&:report_data),
"run_id" => run_status.run_id,
"run_list" => run_status.node.run_list.for_json,
+ "policy_name" => run_status.node.policy_name,
+ "policy_group" => run_status.node.policy_group,
"start_time" => run_status.start_time.utc.iso8601,
"end_time" => run_status.end_time.utc.iso8601,
"source" => collector_source,
diff --git a/spec/unit/data_collector/messages_spec.rb b/spec/unit/data_collector/messages_spec.rb
index 5c6ec8736c..f5df85a988 100644
--- a/spec/unit/data_collector/messages_spec.rb
+++ b/spec/unit/data_collector/messages_spec.rb
@@ -116,6 +116,141 @@ describe Chef::DataCollector::Messages do
deprecations
}
end
+ let(:optional_fields) { %w{error policy_group policy_name} }
+
+ before do
+ allow(run_status).to receive(:exception).and_return(nil)
+ end
+
+ it "is not missing any required fields" do
+ missing_fields = required_fields.select do |key|
+ !Chef::DataCollector::Messages.run_end_message(reporter_data).key?(key)
+ end
+ expect(missing_fields).to eq([])
+ end
+
+ it "does not have any extra fields" do
+ extra_fields = Chef::DataCollector::Messages.run_end_message(reporter_data).keys.select do |key|
+ !required_fields.include?(key) && !optional_fields.include?(key)
+ end
+ expect(extra_fields).to eq([])
+ end
+
+ it "only includes updated resources in its count" do
+ message = Chef::DataCollector::Messages.run_end_message(reporter_data)
+ expect(message["total_resource_count"]).to eq(2)
+ expect(message["updated_resource_count"]).to eq(1)
+ end
+ end
+
+ context "when the run was not successful" do
+ let(:required_fields) do
+ %w{
+ chef_server_fqdn
+ entity_uuid
+ id
+ end_time
+ error
+ expanded_run_list
+ message_type
+ message_version
+ node
+ node_name
+ organization_name
+ resources
+ run_id
+ run_list
+ source
+ start_time
+ status
+ total_resource_count
+ updated_resource_count
+ deprecations
+ }
+ end
+ let(:optional_fields) { %w{policy_group policy_name} }
+
+ before do
+ allow(run_status).to receive(:exception).and_return(RuntimeError.new("an error happened"))
+ end
+
+ it "is not missing any required fields" do
+ missing_fields = required_fields.select do |key|
+ !Chef::DataCollector::Messages.run_end_message(reporter_data).key?(key)
+ end
+ expect(missing_fields).to eq([])
+ end
+
+ it "does not have any extra fields" do
+ extra_fields = Chef::DataCollector::Messages.run_end_message(reporter_data).keys.select do |key|
+ !required_fields.include?(key) && !optional_fields.include?(key)
+ end
+ expect(extra_fields).to eq([])
+ end
+ end
+ end
+
+ describe "#run_end_message in policy mode" do
+ let(:node) { Chef::Node.new }
+ let(:run_status) { Chef::RunStatus.new(node, Chef::EventDispatch::Dispatcher.new) }
+ let(:report1) { double("report1", report_data: { "status" => "updated" }) }
+ let(:report2) { double("report2", report_data: { "status" => "skipped" }) }
+ let(:reporter_data) do
+ {
+ run_status: run_status,
+ resources: [report1, report2],
+ }
+ end
+
+ before do
+ allow(run_status).to receive(:start_time).and_return(Time.now)
+ allow(run_status).to receive(:end_time).and_return(Time.now)
+ node.policy_group = "test"
+ node.policy_name = "policy-test"
+ end
+
+ it "includes a valid node object in the payload" do
+ message = Chef::DataCollector::Messages.run_end_message(reporter_data)
+ expect(message["node"]).to be_an_instance_of(Chef::Node)
+ end
+
+ it "returns a sane JSON representation of the node object" do
+ node.chef_environment = "my_test_environment"
+ node.run_list.add("recipe[my_test_cookbook::default]")
+ message = FFI_Yajl::Parser.parse(Chef::DataCollector::Messages.run_end_message(reporter_data).to_json)
+
+ expect(message["node"]["chef_environment"]).to eq("my_test_environment")
+ expect(message["node"]["run_list"]).to eq(["recipe[my_test_cookbook::default]"])
+ expect(message["node"]["policy_name"]).to eq("policy-test")
+ expect(message["node"]["policy_group"]).to eq("test")
+ end
+
+ context "when the run was successful" do
+ let(:required_fields) do
+ %w{
+ chef_server_fqdn
+ entity_uuid
+ id
+ end_time
+ expanded_run_list
+ message_type
+ message_version
+ node
+ node_name
+ organization_name
+ resources
+ run_id
+ run_list
+ source
+ start_time
+ status
+ total_resource_count
+ updated_resource_count
+ deprecations
+ policy_name
+ policy_group
+ }
+ end
let(:optional_fields) { %w{error} }
before do
@@ -166,6 +301,8 @@ describe Chef::DataCollector::Messages do
total_resource_count
updated_resource_count
deprecations
+ policy_name
+ policy_group
}
end
let(:optional_fields) { [] }