diff options
author | Claire McQuin <claire@getchef.com> | 2015-05-08 17:30:33 -0700 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2015-05-27 14:16:23 -0400 |
commit | 9890e02b3bc16ddc2416497ad39acc40afe98c32 (patch) | |
tree | 36155fd108e16b599b1f5a7fcc8786e3452cd710 | |
parent | 67b9fdc307fd0a7480cc4f11c18540763ec43b09 (diff) | |
download | chef-9890e02b3bc16ddc2416497ad39acc40afe98c32.tar.gz |
Save and report audit phase failure on run_completed or run_failed.
-rw-r--r-- | lib/chef/audit/audit_reporter.rb | 11 | ||||
-rw-r--r-- | lib/chef/formatters/doc.rb | 6 | ||||
-rw-r--r-- | spec/unit/audit/audit_reporter_spec.rb | 88 |
3 files changed, 91 insertions, 14 deletions
diff --git a/lib/chef/audit/audit_reporter.rb b/lib/chef/audit/audit_reporter.rb index a4f84ed7eb..8268de3aa7 100644 --- a/lib/chef/audit/audit_reporter.rb +++ b/lib/chef/audit/audit_reporter.rb @@ -34,6 +34,7 @@ class Chef @rest_client = rest_client # Ruby 1.9.3 and above "enumerate their values in the order that the corresponding keys were inserted." @ordered_control_groups = Hash.new + @exception = nil end def run_context @@ -59,6 +60,7 @@ class Chef # known control groups. def audit_phase_failed(error) # The stacktrace information has already been logged elsewhere + @exception = error Chef::Log.debug("Audit Reporter failed.") ordered_control_groups.each do |name, control_group| audit_data.add_control_group(control_group) @@ -116,8 +118,10 @@ class Chef Chef::Log.debug("Sending audit report (run-id: #{audit_data.run_id})") run_data = audit_data.to_hash - if error - run_data[:error] = "#{error.class.to_s}: #{error.message}\n#{error.backtrace.join("\n")}" + if @exception || error + errors = [@exception, error].uniq.compact + errors_messages = errors.map { |err| "#{err.class.to_s}: #{err.message}\n#{err.backtrace.join("\n")}" } + run_data[:error] = errors_messages.join("\n") end Chef::Log.debug "Audit Report:\n#{Chef::JSONCompat.to_json_pretty(run_data)}" @@ -164,6 +168,9 @@ class Chef time.utc.iso8601.to_s end + def error_message_for_run_data(error) + "#{err.class.to_s}: #{err.message}\n#{err.backtrace.join("\n")}" + end end end end diff --git a/lib/chef/formatters/doc.rb b/lib/chef/formatters/doc.rb index 945bc71f2f..e63c764cbf 100644 --- a/lib/chef/formatters/doc.rb +++ b/lib/chef/formatters/doc.rb @@ -188,8 +188,10 @@ class Chef puts_line "Audit phase exception:" indent puts_line "#{error.message}" - error.backtrace.each do |l| - puts_line l + if error.backtrace + error.backtrace.each do |l| + puts_line l + end end end diff --git a/spec/unit/audit/audit_reporter_spec.rb b/spec/unit/audit/audit_reporter_spec.rb index 4bf889510a..447fd5e357 100644 --- a/spec/unit/audit/audit_reporter_spec.rb +++ b/spec/unit/audit/audit_reporter_spec.rb @@ -88,6 +88,29 @@ describe Chef::Audit::AuditReporter do reporter.run_completed(node) end + context "when audit phase failed" do + + let(:audit_error) { double("AuditError", :class => "Chef::Exceptions::AuditError", + :message => "Audit phase failed with error message: derpderpderp", + :backtrace => ["/path/recipe.rb:57", "/path/library.rb:106"]) } + + before do + reporter.instance_variable_set(:@exception, audit_error) + end + + it "reports an error" do + reporter.run_completed(node) + expect(run_data).to have_key(:error) + expect(run_data).to have_key(:error) + expect(run_data[:error]).to eq <<-EOM.strip! +Chef::Exceptions::AuditError: Audit phase failed with error message: derpderpderp +/path/recipe.rb:57 +/path/library.rb:106 +EOM + end + + end + context "when unable to post to server" do let(:error) do @@ -215,9 +238,9 @@ describe Chef::Audit::AuditReporter do let(:audit_data) { Chef::Audit::AuditData.new(node.name, run_id) } let(:run_data) { audit_data.to_hash } - let(:error) { double("AuditError", :class => "Chef::Exception::AuditError", - :message => "Well that certainly didn't work", - :backtrace => ["line 0", "line 1", "line 2"]) } + let(:audit_error) { double("AuditError", :class => "Chef::Exceptions::AuditError", + :message => "Audit phase failed with error message: derpderpderp", + :backtrace => ["/path/recipe.rb:57", "/path/library.rb:106"]) } before do allow(reporter).to receive(:auditing_enabled?).and_return(true) @@ -226,15 +249,60 @@ describe Chef::Audit::AuditReporter do allow(audit_data).to receive(:to_hash).and_return(run_data) end - it "adds the error information to the reported data" do - expect(rest).to receive(:create_url) - expect(rest).to receive(:post) - reporter.run_failed(error) - expect(run_data).to have_key(:error) - expect(run_data[:error]).to eq "Chef::Exception::AuditError: Well that certainly didn't work\n" + - "line 0\nline 1\nline 2" + context "when no prior exception is stored" do + it "reports the error" do + expect(rest).to receive(:create_url) + expect(rest).to receive(:post) + reporter.run_failed(audit_error) + expect(run_data).to have_key(:error) + expect(run_data[:error]).to eq <<-EOM.strip! +Chef::Exceptions::AuditError: Audit phase failed with error message: derpderpderp +/path/recipe.rb:57 +/path/library.rb:106 +EOM + end end + context "when some prior exception is stored" do + before do + reporter.instance_variable_set(:@exception, audit_error) + end + + context "when the error is the same as the prior exception" do + it "reports one error" do + expect(rest).to receive(:create_url) + expect(rest).to receive(:post) + reporter.run_failed(audit_error) + expect(run_data).to have_key(:error) + expect(run_data[:error]).to eq <<-EOM.strip! +Chef::Exceptions::AuditError: Audit phase failed with error message: derpderpderp +/path/recipe.rb:57 +/path/library.rb:106 +EOM + end + end + + context "when the error is not the same as the prior exception" do + let(:converge_error) { double("ConvergeError", :class => "Chef::Exceptions::ConvergeError", + :message => "Welp converge failed that's a bummer", + :backtrace => ["/path/recipe.rb:14", "/path/library.rb:5"]) } + + it "reports both errors" do + expect(rest).to receive(:create_url) + expect(rest).to receive(:post) + reporter.run_failed(converge_error) + expect(run_data).to have_key(:error) + expect(run_data[:error]).to eq <<-EOM.strip! +Chef::Exceptions::AuditError: Audit phase failed with error message: derpderpderp +/path/recipe.rb:57 +/path/library.rb:106 +Chef::Exceptions::ConvergeError: Welp converge failed that's a bummer +/path/recipe.rb:14 +/path/library.rb:5 +EOM + end + end + end end shared_context "audit data" do |