summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Hudson <jhudson@chef.io>2017-03-30 17:15:44 -0600
committerJosh Hudson <jhudson@chef.io>2017-03-30 17:45:14 -0600
commit3e94e03785ac01a1953ab20a150ac05fdd3ab1cd (patch)
tree7a9f1e6417c873840c6aea71eb67f2181a941c3f
parent089eb5ede5b67bb9ea53356234e38eb716b4c523 (diff)
downloadchef-3e94e03785ac01a1953ab20a150ac05fdd3ab1cd.tar.gz
Add unit tests for run_end message in policy mode. Also some random cookstyle cleanup in the file.
Signed-off-by: Josh Hudson <jhudson@chef.io>
-rw-r--r--spec/unit/data_collector/messages_spec.rb203
1 files changed, 170 insertions, 33 deletions
diff --git a/spec/unit/data_collector/messages_spec.rb b/spec/unit/data_collector/messages_spec.rb
index 5c6ec8736c..d1feafaef8 100644
--- a/spec/unit/data_collector/messages_spec.rb
+++ b/spec/unit/data_collector/messages_spec.rb
@@ -17,15 +17,15 @@
# limitations under the License.
#
-require "spec_helper"
-require "ffi_yajl"
-require "chef/data_collector/messages/helpers"
+require 'spec_helper'
+require 'ffi_yajl'
+require 'chef/data_collector/messages/helpers'
describe Chef::DataCollector::Messages do
- describe "#run_start_message" do
+ describe '#run_start_message' do
let(:run_status) { Chef::RunStatus.new(Chef::Node.new, Chef::EventDispatch::Dispatcher.new) }
let(:required_fields) do
- %w{
+ %w(
chef_server_fqdn
entity_uuid
id
@@ -36,7 +36,7 @@ describe Chef::DataCollector::Messages do
run_id
source
start_time
- }
+ )
end
let(:optional_fields) { [] }
@@ -44,7 +44,7 @@ describe Chef::DataCollector::Messages do
allow(run_status).to receive(:start_time).and_return(Time.now)
end
- it "is not missing any required fields" do
+ it 'is not missing any required fields' do
missing_fields = required_fields.select do |key|
!Chef::DataCollector::Messages.run_start_message(run_status).key?(key)
end
@@ -52,7 +52,7 @@ describe Chef::DataCollector::Messages do
expect(missing_fields).to eq([])
end
- it "does not have any extra fields" do
+ it 'does not have any extra fields' do
extra_fields = Chef::DataCollector::Messages.run_start_message(run_status).keys.select do |key|
!required_fields.include?(key) && !optional_fields.include?(key)
end
@@ -61,11 +61,140 @@ describe Chef::DataCollector::Messages do
end
end
- describe "#run_end_message" do
+ describe '#run_end_message' 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(: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)
+ 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]'])
+ 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
+ )
+ 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,
@@ -76,25 +205,29 @@ describe Chef::DataCollector::Messages do
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
+ 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)
+ 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]")
+ 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']['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
+ context 'when the run was successful' do
let(:required_fields) do
- %w{
+ %w(
chef_server_fqdn
entity_uuid
id
@@ -114,38 +247,40 @@ describe Chef::DataCollector::Messages do
total_resource_count
updated_resource_count
deprecations
- }
+ policy_name
+ policy_group
+ )
end
- let(:optional_fields) { %w{error} }
+ let(:optional_fields) { %w(error) }
before do
allow(run_status).to receive(:exception).and_return(nil)
end
- it "is not missing any required fields" do
+ 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
+ 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
+ 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)
+ 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
+ context 'when the run was not successful' do
let(:required_fields) do
- %w{
+ %w(
chef_server_fqdn
entity_uuid
id
@@ -166,22 +301,24 @@ describe Chef::DataCollector::Messages do
total_resource_count
updated_resource_count
deprecations
- }
+ policy_name
+ policy_group
+ )
end
let(:optional_fields) { [] }
before do
- allow(run_status).to receive(:exception).and_return(RuntimeError.new("an error happened"))
+ allow(run_status).to receive(:exception).and_return(RuntimeError.new('an error happened'))
end
- it "is not missing any required fields" do
+ 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
+ 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