diff options
author | sersut <serdar@opscode.com> | 2013-07-11 21:38:26 +0300 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2013-07-11 21:38:26 +0300 |
commit | 232f137a8b64863c125bf95e5d0229668e8eac86 (patch) | |
tree | b83f38db2ae8a855992061970e8798d3f46f0ade /spec/unit/resource_reporter_spec.rb | |
parent | c9f21f2ae307a7956d3a3b8777a52658e246d6b2 (diff) | |
parent | 98c11727c25db302550e4332bae1ef2a7de6fac8 (diff) | |
download | chef-232f137a8b64863c125bf95e5d0229668e8eac86.tar.gz |
Merge branch 'master' into 11-stable
Diffstat (limited to 'spec/unit/resource_reporter_spec.rb')
-rw-r--r-- | spec/unit/resource_reporter_spec.rb | 215 |
1 files changed, 114 insertions, 101 deletions
diff --git a/spec/unit/resource_reporter_spec.rb b/spec/unit/resource_reporter_spec.rb index 51f4ba7c19..d9f8172539 100644 --- a/spec/unit/resource_reporter_spec.rb +++ b/spec/unit/resource_reporter_spec.rb @@ -38,11 +38,18 @@ describe Chef::ResourceReporter do @rest_client = mock("Chef::REST (mock)") @rest_client.stub!(:post_rest).and_return(true) @resource_reporter = Chef::ResourceReporter.new(@rest_client) + @run_id = @resource_reporter.run_id @new_resource = Chef::Resource::File.new("/tmp/a-file.txt") @new_resource.cookbook_name = "monkey" @cookbook_version = mock("Cookbook::Version", :version => "1.2.3") @new_resource.stub!(:cookbook_version).and_return(@cookbook_version) @current_resource = Chef::Resource::File.new("/tmp/a-file.txt") + @start_time = Time.new + @end_time = Time.new + 20 + @events = Chef::EventDispatch::Dispatcher.new + @run_context = Chef::RunContext.new(@node, {}, @events) + @run_status = Chef::RunStatus.new(@node, @events) + Time.stub!(:now).and_return(@start_time, @end_time) end context "when first created" do @@ -80,23 +87,23 @@ describe Chef::ResourceReporter do context "when chef fails" do before do - @rest_client.stub!(:create_url).and_return("reports/nodes/spitfire/runs/ABC123"); + @rest_client.stub!(:create_url).and_return("reports/nodes/spitfire/runs/#{@run_id}"); @rest_client.stub!(:raw_http_request).and_return({"result"=>"ok"}); - @rest_client.stub!(:post_rest).and_return({"uri"=>"https://example.com/reports/nodes/spitfire/runs/ABC123"}); + @rest_client.stub!(:post_rest).and_return({"uri"=>"https://example.com/reports/nodes/spitfire/runs/#{@run_id}"}); - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) end context "before converging any resources" do before do + @resource_reporter.run_started(@run_status) @exception = Exception.new @resource_reporter.run_failed(@exception) end - + it "sets the run status to 'failure'" do @resource_reporter.status.should == "failure" end - + it "keeps the exception data" do @resource_reporter.exception.should == @exception end @@ -117,13 +124,13 @@ describe Chef::ResourceReporter do it "collects the desired state of the resource" do update_record = @resource_reporter.updated_resources.first - update_record.new_resource.should == @new_resource + update_record.new_resource.should == @new_resource end end # TODO: make sure a resource that is skipped because of `not_if` doesn't # leave us in a bad state. - + context "once the a resource's current state is loaded" do before do @resource_reporter.resource_action_start(@new_resource, :create) @@ -151,12 +158,12 @@ describe Chef::ResourceReporter do it "collects the updated resource" do @resource_reporter.should have(1).updated_resources end - + it "collects the old state of the resource" do - update_record = @resource_reporter.updated_resources.first + update_record = @resource_reporter.updated_resources.first update_record.current_resource.should == @current_resource end - + it "collects the new state of the resource" do update_record = @resource_reporter.updated_resources.first update_record.new_resource.should == @new_resource @@ -170,19 +177,19 @@ describe Chef::ResourceReporter do @resource_reporter.resource_failed(@next_new_resource, :create, @exception) @resource_reporter.resource_completed(@next_new_resource) end - + it "collects the desired state of the failed resource" do failed_resource_update = @resource_reporter.updated_resources.last failed_resource_update.new_resource.should == @next_new_resource end - + it "does not have the current state of the failed resource" do failed_resource_update = @resource_reporter.updated_resources.last failed_resource_update.current_resource.should be_nil end end end - + # Some providers, such as RemoteDirectory and some LWRPs use other # resources for their implementation. These should be hidden from reporting # since we only care about the top-level resource and not the sub-resources @@ -213,7 +220,7 @@ describe Chef::ResourceReporter do @resource_reporter.resource_updated(@new_resource, :create) @resource_reporter.resource_completed(@new_resource) end - + it "does not collect data about the nested resource" do @resource_reporter.should have(1).updated_resources end @@ -226,16 +233,16 @@ describe Chef::ResourceReporter do @resource_reporter.resource_failed(@new_resource, :create, @exception) @resource_reporter.resource_completed(@new_resource) end - + it "collects the resource as an updated resource" do @resource_reporter.should have(1).updated_resources end - + it "collects the desired state of the resource" do update_record = @resource_reporter.updated_resources.first update_record.new_resource.should == @new_resource end - + it "collects the current state of the resource" do update_record = @resource_reporter.updated_resources.first update_record.current_resource.should == @current_resource @@ -248,13 +255,13 @@ describe Chef::ResourceReporter do describe "when generating a report for the server" do before do - @rest_client.stub!(:create_url).and_return("reports/nodes/spitfire/runs/ABC123"); + @rest_client.stub!(:create_url).and_return("reports/nodes/spitfire/runs/#{@run_id}"); @rest_client.stub!(:raw_http_request).and_return({"result"=>"ok"}); - @rest_client.stub!(:post_rest).and_return({"uri"=>"https://example.com/reports/nodes/spitfire/runs/ABC123"}); - - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @rest_client.stub!(:post_rest).and_return({"uri"=>"https://example.com/reports/nodes/spitfire/runs/#{@run_id}"}); + + @resource_reporter.run_started(@run_status) end - + context "for a successful client run" do before do # TODO: add inputs to generate expected output. @@ -291,6 +298,7 @@ describe Chef::ResourceReporter do @resource_reporter.resource_current_state_loaded(@new_resource, :create, @current_resource) @resource_reporter.resource_updated(@new_resource, :create) @resource_reporter.resource_completed(@new_resource) + @run_status.stop_clock @report = @resource_reporter.prepare_run_data @first_update_report = @report["resources"].first end @@ -357,8 +365,14 @@ describe Chef::ResourceReporter do it "includes the run_list" do @report.should have_key("run_list") - @report["run_list"].should == @node.run_list.to_json + @report["run_list"].should == @run_status.node.run_list.to_json end + + it "includes the end_time" do + @report.should have_key("end_time") + @report["end_time"].should == @run_status.end_time.to_s + end + end context "for an unsuccessful run" do @@ -399,10 +413,63 @@ describe Chef::ResourceReporter do end + context "when new_resource does not have a cookbook_name" do + before do + @bad_resource = Chef::Resource::File.new("/tmp/a-file.txt") + @bad_resource.cookbook_name = nil + + @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) + @resource_reporter.resource_completed(@bad_resource) + @run_status.stop_clock + @report = @resource_reporter.prepare_run_data + @first_update_report = @report["resources"].first + end + + it "includes an updated resource's initial state" do + @first_update_report["before"].should == @current_resource.state + end + + it "includes an updated resource's final state" do + @first_update_report["after"].should == @new_resource.state + end + + it "includes the resource's name" do + @first_update_report["name"].should == @new_resource.name + end + + it "includes the resource's id attribute" do + @first_update_report["id"].should == @new_resource.identity + end + + it "includes the elapsed time for the resource to converge" do + # TODO: API takes integer number of milliseconds as a string. This + # should be an int. + @first_update_report.should have_key("duration") + @first_update_report["duration"].to_i.should be_within(100).of(0) + end + + it "includes the action executed by the resource" do + # TODO: rename as "action" + @first_update_report["result"].should == "create" + end + + it "does not include a cookbook name for the resource" do + @first_update_report.should_not have_key("cookbook_name") + end + + it "does not include a cookbook version for the resource" do + @first_update_report.should_not have_key("cookbook_version") + end + end + end describe "when updating resource history on the server" do before do + @resource_reporter.run_started(@run_status) + @run_status.start_clock end context "when the server does not support storing resource history" do @@ -411,24 +478,26 @@ describe Chef::ResourceReporter do @response = Net::HTTPNotFound.new("a response body", "404", "Not Found") @error = Net::HTTPServerException.new("404 message", @response) @rest_client.should_receive(:post_rest). - with("reports/nodes/spitfire/runs", {:action => :begin}). + 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 it "assumes the feature is not enabled" do - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) @resource_reporter.reporting_enabled?.should be_false end it "does not send a resource report to the server" do - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) @rest_client.should_not_receive(:post_rest) @resource_reporter.run_completed(@node) end it "prints an error about the 404" do Chef::Log.should_receive(:debug).with(/404/) - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) end end @@ -439,24 +508,25 @@ describe Chef::ResourceReporter do @response = Net::HTTPInternalServerError.new("a response body", "500", "Internal Server Error") @error = Net::HTTPServerException.new("500 message", @response) @rest_client.should_receive(:post_rest). - with("reports/nodes/spitfire/runs", {:action => :begin}). + 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 it "assumes the feature is not enabled" do - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) @resource_reporter.reporting_enabled?.should be_false end it "does not send a resource report to the server" do - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) @rest_client.should_not_receive(:post_rest) @resource_reporter.run_completed(@node) end it "prints an error about the error" do Chef::Log.should_receive(:info).with(/500/) - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) end end @@ -468,7 +538,8 @@ describe Chef::ResourceReporter do @response = Net::HTTPInternalServerError.new("a response body", "500", "Internal Server Error") @error = Net::HTTPServerException.new("500 message", @response) @rest_client.should_receive(:post_rest). - with("reports/nodes/spitfire/runs", {:action => :begin}). + 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 @@ -479,23 +550,23 @@ describe Chef::ResourceReporter do it "fails the run and prints an message about the error" do Chef::Log.should_receive(:error).with(/500/) lambda { - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) }.should raise_error(Net::HTTPServerException) end end context "after creating the run history document" do before do - response = {"uri"=>"https://example.com/reports/nodes/spitfire/runs/ABC123"} + response = {"uri"=>"https://example.com/reports/nodes/spitfire/runs/@run_id"} @rest_client.should_receive(:post_rest). - with("reports/nodes/spitfire/runs", {:action => :begin}). + 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.node_load_completed(@node, :expanded_run_list, :config) + @resource_reporter.run_started(@run_status) end it "creates a run document on the server at the start of the run" do - @resource_reporter.run_id.should == "ABC123" + @resource_reporter.run_id.should == @run_id end it "updates the run document with resource updates at the end of the run" do @@ -504,19 +575,22 @@ describe Chef::ResourceReporter do @resource_reporter.resource_current_state_loaded(@new_resource, :create, @current_resource) @resource_reporter.resource_updated(@new_resource, :create) + @resource_reporter.stub!(:end_time).and_return(@end_time) @expected_data = @resource_reporter.prepare_run_data post_url = "https://chef_server/example_url" response = {"result"=>"ok"} @rest_client.should_receive(:create_url). - with("reports/nodes/spitfire/runs/ABC123"). + with("reports/nodes/spitfire/runs/#{@run_id}"). ordered. and_return(post_url) @rest_client.should_receive(:raw_http_request).ordered do |method, url, headers, data| method.should eq(:POST) url.should eq(post_url) - headers.should eq({'Content-Encoding' => 'gzip'}) + headers.should 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 data.should eq(@expected_data.to_json) @@ -526,66 +600,5 @@ describe Chef::ResourceReporter do @resource_reporter.run_completed(@node) end end - - context "after creating the run history document when summary_only is set to true" do - before do - response = {"uri"=>"https://example.com/reports/nodes/spitfire/runs/ABC123", "summary_only"=>"true"} - @rest_client.should_receive(:post_rest). - with("reports/nodes/spitfire/runs", {:action => :begin}). - and_return(response) - - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) - end - - it "enables summary only reporting" do - @resource_reporter.summary_only.should == "true" - end - - it "updates the run document with resource updates at the end of the run" do - # update some resources... - @resource_reporter.resource_action_start(@new_resource, :create) - @resource_reporter.resource_current_state_loaded(@new_resource, :create, @current_resource) - @resource_reporter.resource_updated(@new_resource, :create) - - post_url = "reports/nodes/spitfire/runs/ABC123" - response = {"result"=>"ok"} - - @rest_client.should_receive(:post_rest).ordered do |url, data| - url.should eq(post_url) - data.should have_key("action") - data["action"].should == "end" - data.should have_key("status") - data.should have_key("resources") - data["resources"].should == [] - data.should have_key("total_res_count") - data["total_res_count"].should == "1" - data.should have_key("data") - data["data"].should == {} - data.should have_key("updated_res_count") - data["updated_res_count"].should == "0" - data.should have_key("post_size") - response - end - - @resource_reporter.run_completed(@node) - end - end - - context "after creating the run history document when summary_only is set to false" do - before do - response = {"uri"=>"https://example.com/reports/nodes/spitfire/runs/ABC123", "summary_only"=>"false"} - @rest_client.should_receive(:post_rest). - with("reports/nodes/spitfire/runs", {:action => :begin}). - and_return(response) - - @resource_reporter.node_load_completed(@node, :expanded_run_list, :config) - end - - it "disables summary_only reporting" do - @resource_reporter.summary_only.should == "false" - end - end - end - end |