diff options
Diffstat (limited to 'spec/unit/resource_reporter_spec.rb')
| -rw-r--r-- | spec/unit/resource_reporter_spec.rb | 70 |
1 files changed, 29 insertions, 41 deletions
diff --git a/spec/unit/resource_reporter_spec.rb b/spec/unit/resource_reporter_spec.rb index 5517865c8e..1e749ed500 100644 --- a/spec/unit/resource_reporter_spec.rb +++ b/spec/unit/resource_reporter_spec.rb @@ -44,7 +44,7 @@ describe Chef::ResourceReporter do @new_resource.cookbook_name = @cookbook_name @cookbook_version = double("Cookbook::Version", :version => "1.2.3") allow(@new_resource).to receive(:cookbook_version).and_return(@cookbook_version) - @current_resource = Chef::Resource::File.new("/tmp/a-file.txt") + @current_resource = Chef::Resource::File.new("/tmp/a-file.txt") @start_time = Time.new @end_time = Time.new + 20 @events = Chef::EventDispatch::Dispatcher.new @@ -58,7 +58,6 @@ describe Chef::ResourceReporter do end context "when first created" do - it "has no updated resources" do expect(@resource_reporter.updated_resources.size).to eq(0) end @@ -76,11 +75,9 @@ describe Chef::ResourceReporter do # @resource_reporter.error_descriptions.should be_empty # @resource_reporter.should have(0).error_descriptions end - end context "after the chef run completes" do - before do end @@ -92,9 +89,8 @@ describe Chef::ResourceReporter do context "when chef fails" do before do - allow(@rest_client).to receive(:raw_request).and_return({"result"=>"ok"}); - allow(@rest_client).to receive(:post).and_return({"uri"=>"https://example.com/reports/nodes/spitfire/runs/#{@run_id}"}); - + allow(@rest_client).to receive(:raw_request).and_return({ "result" => "ok" }); + allow(@rest_client).to receive(:post).and_return({ "uri" => "https://example.com/reports/nodes/spitfire/runs/#{@run_id}" }); end context "before converging any resources" do @@ -201,12 +197,12 @@ describe Chef::ResourceReporter do context "and a nested resource is updated" do before do @implementation_resource = Chef::Resource::CookbookFile.new("/preseed-file.txt") - @resource_reporter.resource_action_start(@implementation_resource , :create) - @resource_reporter.resource_current_state_loaded(@implementation_resource, :create, @implementation_resource) - @resource_reporter.resource_updated(@implementation_resource, :create) - @resource_reporter.resource_completed(@implementation_resource) - @resource_reporter.resource_updated(@new_resource, :create) - @resource_reporter.resource_completed(@new_resource) + @resource_reporter.resource_action_start(@implementation_resource , :create) + @resource_reporter.resource_current_state_loaded(@implementation_resource, :create, @implementation_resource) + @resource_reporter.resource_updated(@implementation_resource, :create) + @resource_reporter.resource_completed(@implementation_resource) + @resource_reporter.resource_updated(@new_resource, :create) + @resource_reporter.resource_completed(@new_resource) end it "does not collect data about the nested resource" do @@ -252,15 +248,13 @@ describe Chef::ResourceReporter do expect(update_record.current_resource).to eq(@current_resource) end end - end end describe "when generating a report for the server" do - before do - allow(@rest_client).to receive(:raw_request).and_return({"result"=>"ok"}); - allow(@rest_client).to receive(:post).and_return({"uri"=>"https://example.com/reports/nodes/spitfire/runs/#{@run_id}"}); + allow(@rest_client).to receive(:raw_request).and_return({ "result" => "ok" }); + allow(@rest_client).to receive(:post).and_return({ "uri" => "https://example.com/reports/nodes/spitfire/runs/#{@run_id}" }); @resource_reporter.run_started(@run_status) end @@ -293,9 +287,9 @@ describe Chef::ResourceReporter do context "the new_resource name and id are hashes" do before do @bad_resource = Chef::Resource::File.new("/tmp/filename_as_hash.txt") - allow(@bad_resource).to receive(:name).and_return({:foo=>:bar}) - allow(@bad_resource).to receive(:identity).and_return({:foo=>:bar}) - allow(@bad_resource).to receive(:path).and_return({:foo=>:bar}) + allow(@bad_resource).to receive(:name).and_return({ :foo => :bar }) + allow(@bad_resource).to receive(:identity).and_return({ :foo => :bar }) + allow(@bad_resource).to receive(:path).and_return({ :foo => :bar }) @resource_reporter.resource_action_start(@bad_resource, :create) @resource_reporter.resource_current_state_loaded(@bad_resource, :create, @current_resource) @resource_reporter.resource_updated(@bad_resource, :create) @@ -435,7 +429,6 @@ describe Chef::ResourceReporter do expect(@report).to have_key("end_time") expect(@report["end_time"]).to eq(@run_status.end_time.to_s) end - end context "when the resource is a File" do @@ -464,9 +457,8 @@ describe Chef::ResourceReporter do end context "for an unsuccessful run" do - before do - @backtrace = ["foo.rb:1 in `foo!'","bar.rb:2 in `bar!","'baz.rb:3 in `baz!'"] + @backtrace = ["foo.rb:1 in `foo!'", "bar.rb:2 in `bar!", "'baz.rb:3 in `baz!'"] @node = Chef::Node.new @node.name("spitfire") @exception = ArgumentError.new @@ -496,9 +488,8 @@ describe Chef::ResourceReporter do it "includes the error inspector output in the event data" do expect(@report["data"]["exception"]).to have_key("description") - expect(@report["data"]["exception"]["description"]).to include({"title"=>"Error expanding the run_list:", "sections"=>[{"Unexpected Error:" => "ArgumentError: Object not found"}]}) + expect(@report["data"]["exception"]["description"]).to include({ "title" => "Error expanding the run_list:", "sections" => [{ "Unexpected Error:" => "ArgumentError: Object not found" }] }) end - end context "when new_resource does not have a cookbook_name" do @@ -578,7 +569,6 @@ describe Chef::ResourceReporter do expect(@first_update_report["after"]).to eq({}) end end - end describe "when updating resource history on the server" do @@ -593,9 +583,9 @@ describe Chef::ResourceReporter do @response = Net::HTTPNotFound.new("a response body", "404", "Not Found") @error = Net::HTTPServerException.new("404 message", @response) expect(@rest_client).to receive(:post). - with("reports/nodes/spitfire/runs", {:action => :start, :run_id => @run_id, - :start_time => @start_time.to_s}, - {"X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION}). + with("reports/nodes/spitfire/runs", { :action => :start, :run_id => @run_id, + :start_time => @start_time.to_s }, + { "X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION }). and_raise(@error) end @@ -614,7 +604,6 @@ describe Chef::ResourceReporter do expect(Chef::Log).to receive(:debug).with(/404/) @resource_reporter.run_started(@run_status) end - end context "when the server returns a 500 to the client" do @@ -623,8 +612,8 @@ describe Chef::ResourceReporter do @response = Net::HTTPInternalServerError.new("a response body", "500", "Internal Server Error") @error = Net::HTTPServerException.new("500 message", @response) expect(@rest_client).to receive(:post). - with("reports/nodes/spitfire/runs", {:action => :start, :run_id => @run_id, :start_time => @start_time.to_s}, - {"X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION}). + with("reports/nodes/spitfire/runs", { :action => :start, :run_id => @run_id, :start_time => @start_time.to_s }, + { "X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION }). and_raise(@error) end @@ -653,8 +642,8 @@ describe Chef::ResourceReporter do @response = Net::HTTPInternalServerError.new("a response body", "500", "Internal Server Error") @error = Net::HTTPServerException.new("500 message", @response) expect(@rest_client).to receive(:post). - with("reports/nodes/spitfire/runs", {:action => :start, :run_id => @run_id, :start_time => @start_time.to_s}, - {"X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION}). + with("reports/nodes/spitfire/runs", { :action => :start, :run_id => @run_id, :start_time => @start_time.to_s }, + { "X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION }). and_raise(@error) end @@ -672,10 +661,10 @@ describe Chef::ResourceReporter do context "after creating the run history document" do before do - response = {"uri"=>"https://example.com/reports/nodes/spitfire/runs/@run_id"} + response = { "uri" => "https://example.com/reports/nodes/spitfire/runs/@run_id" } expect(@rest_client).to receive(:post). - with("reports/nodes/spitfire/runs", {:action => :start, :run_id => @run_id, :start_time => @start_time.to_s}, - {"X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION}). + with("reports/nodes/spitfire/runs", { :action => :start, :run_id => @run_id, :start_time => @start_time.to_s }, + { "X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION }). and_return(response) @resource_reporter.run_started(@run_status) end @@ -693,12 +682,12 @@ describe Chef::ResourceReporter do allow(@resource_reporter).to receive(:end_time).and_return(@end_time) @expected_data = @resource_reporter.prepare_run_data - response = {"result"=>"ok"} + response = { "result" => "ok" } expect(@rest_client).to receive(:raw_request).ordered do |method, url, headers, data| expect(method).to eq(:POST) - expect(headers).to eq({"Content-Encoding" => "gzip", - "X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION, + expect(headers).to eq({ "Content-Encoding" => "gzip", + "X-Ops-Reporting-Protocol-Version" => Chef::ResourceReporter::PROTOCOL_VERSION, },) data_stream = Zlib::GzipReader.new(StringIO.new(data)) data = data_stream.read @@ -743,7 +732,6 @@ describe Chef::ResourceReporter do expect(Chef::Log).to receive(:error).with(/test socket error/) @resource_reporter.post_reporting_data - end it "should raise if an unkwown error happens" do |
