diff options
Diffstat (limited to 'spec/unit/data_collector_spec.rb')
-rw-r--r-- | spec/unit/data_collector_spec.rb | 107 |
1 files changed, 83 insertions, 24 deletions
diff --git a/spec/unit/data_collector_spec.rb b/spec/unit/data_collector_spec.rb index 78b0aaf97d..fb47fe3012 100644 --- a/spec/unit/data_collector_spec.rb +++ b/spec/unit/data_collector_spec.rb @@ -20,6 +20,7 @@ require "spec_helper" require "chef/data_collector" +require "chef/resource_builder" describe Chef::DataCollector do describe ".register_reporter?" do @@ -155,6 +156,21 @@ describe Chef::DataCollector::Reporter do let(:reporter) { described_class.new } let(:run_status) { Chef::RunStatus.new(Chef::Node.new, Chef::EventDispatch::Dispatcher.new) } + describe '#run_context_setup' do + it "creates resource reports for each resource in the collection" do + resource1 = double("resource1", action: [:create]) + resource2 = double("resource2", action: [:enable, :start]) + resource_collection = double("resource_collection", all_resources: [resource1, resource2]) + run_context = double("run_context", resource_collection: resource_collection) + + expect(reporter).to receive(:create_resource_report).with(resource1, :create) + expect(reporter).to receive(:create_resource_report).with(resource2, :enable) + expect(reporter).to receive(:create_resource_report).with(resource2, :start) + + reporter.run_context_setup(run_context) + end + end + describe '#run_started' do before do allow(reporter).to receive(:update_run_status) @@ -197,6 +213,7 @@ describe Chef::DataCollector::Reporter do let(:new_resource) { double("new_resource") } let(:action) { double("action") } let(:current_resource) { double("current_resource") } + let(:resource_report) { double("resource_report") } context "when resource is a nested resource" do it "does not update the resource report" do @@ -207,14 +224,13 @@ describe Chef::DataCollector::Reporter do end context "when resource is not a nested resource" do - it "updates the resource report" do + it "finds the resource report and stores it as the current one" do allow(reporter).to receive(:nested_resource?).and_return(false) - expect(Chef::DataCollector::ResourceReport).to receive(:new).with( - new_resource, - action, - current_resource) - .and_return("resource_report") - expect(reporter).to receive(:update_current_resource_report).with("resource_report") + expect(reporter).to receive(:find_or_create_resource_report) + .with(new_resource, action) + .and_return(resource_report) + expect(resource_report).to receive(:current_resource=).with(current_resource) + expect(reporter).to receive(:update_current_resource_report).with(resource_report) reporter.resource_current_state_loaded(new_resource, action, current_resource) end end @@ -256,7 +272,7 @@ describe Chef::DataCollector::Reporter do before do allow(reporter).to receive(:nested_resource?) - allow(reporter).to receive(:current_resource_report).and_return(resource_report) + allow(reporter).to receive(:find_or_create_resource_report).and_return(resource_report) allow(resource_report).to receive(:skipped) end @@ -269,13 +285,12 @@ describe Chef::DataCollector::Reporter do end context "when the resource is not a nested resource" do - it "updates the resource report" do + it "finds the correct resource report and stores it as the current one" do allow(reporter).to receive(:nested_resource?).and_return(false) - expect(Chef::DataCollector::ResourceReport).to receive(:new).with( - new_resource, - action) - .and_return("resource_report") - expect(reporter).to receive(:update_current_resource_report).with("resource_report") + expect(reporter).to receive(:find_or_create_resource_report) + .with(new_resource, action) + .and_return(resource_report) + expect(reporter).to receive(:update_current_resource_report).with(resource_report) reporter.resource_skipped(new_resource, action, conditional) end @@ -349,15 +364,15 @@ describe Chef::DataCollector::Reporter do let(:resource_report) { double("resource_report") } before do - allow(reporter).to receive(:add_completed_resource) allow(reporter).to receive(:update_current_resource_report) + allow(reporter).to receive(:current_resource_report) allow(resource_report).to receive(:finish) end context "when there is no current resource report" do - it "does not add the updated resource" do + it "does not touch the current resource report" do allow(reporter).to receive(:current_resource_report).and_return(nil) - expect(reporter).not_to receive(:add_completed_resource) + expect(reporter).not_to receive(:update_current_resource_report) reporter.resource_completed(new_resource) end end @@ -368,9 +383,9 @@ describe Chef::DataCollector::Reporter do end context "when the resource is a nested resource" do - it "does not add the updated resource" do + it "does not mark the resource as finished" do allow(reporter).to receive(:nested_resource?).with(new_resource).and_return(true) - expect(reporter).not_to receive(:add_completed_resource) + expect(resource_report).not_to receive(:finish) reporter.resource_completed(new_resource) end end @@ -385,11 +400,6 @@ describe Chef::DataCollector::Reporter do reporter.resource_completed(new_resource) end - it "adds the resource to the updated resource list" do - expect(reporter).to receive(:add_completed_resource).with(resource_report) - reporter.resource_completed(new_resource) - end - it "nils out the current resource report" do expect(reporter).to receive(:update_current_resource_report).with(nil) reporter.resource_completed(new_resource) @@ -499,4 +509,53 @@ describe Chef::DataCollector::Reporter do end end end + + describe '#find_or_create_resource_report' do + let(:node) { Chef::Node.new } + let(:run_context) { Chef::RunContext.new(node, nil, nil) } + + let(:resource1) do + Chef::ResourceBuilder.new( + type: :file, + name: "test1", + run_context: run_context) + .build + end + + let(:resource2) do + Chef::ResourceBuilder.new( + type: :file, + name: "test2", + run_context: run_context) + .build { action :delete } + end + + let(:resource3) do + Chef::ResourceBuilder.new( + type: :service, + name: "test3", + run_context: run_context) + .build + end + + let(:report1) { Chef::DataCollector::ResourceReport.new(resource1, resource1.action.first) } + let(:report2) { Chef::DataCollector::ResourceReport.new(resource2, resource2.action.first) } + let(:report3) { Chef::DataCollector::ResourceReport.new(resource3, resource3.action.first) } + + context "when a matching resource report exists" do + it "returns the resource report" do + allow(reporter).to receive(:all_resource_reports).and_return([report1, report2, report3]) + expect(reporter).not_to receive(:create_resource_report) + expect(reporter.send(:find_or_create_resource_report, resource1, resource1.action.first)).to eq(report1) + end + end + + context "when a matching resource report does not exist" do + it "creates a new resource report and returns it" do + allow(reporter).to receive(:all_resource_reports).and_return([report1, report2]) + expect(reporter).to receive(:create_resource_report).with(resource3, resource3.action.first).and_return("report_for_resource3") + expect(reporter.send(:find_or_create_resource_report, resource3, resource3.action.first)).to eq("report_for_resource3") + end + end + end end |