summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <claire@getchef.com>2015-05-08 17:30:33 -0700
committerClaire McQuin <claire@getchef.com>2015-05-21 10:02:02 -0700
commitf521bafc0c3e9f44148c2cf0c1c6d79c4cde1c62 (patch)
treea9443830de2f3c463020785dd358c659af327a19
parent95f43dbbfe0f7408d2a9e7d7f6238b5f99b80603 (diff)
downloadchef-f521bafc0c3e9f44148c2cf0c1c6d79c4cde1c62.tar.gz
Save and report audit phase failure on run_completed or run_failed.
-rw-r--r--lib/chef/audit/audit_reporter.rb11
-rw-r--r--lib/chef/formatters/doc.rb6
-rw-r--r--spec/unit/audit/audit_reporter_spec.rb88
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 7144d00b5d..61fbe98341 100644
--- a/lib/chef/formatters/doc.rb
+++ b/lib/chef/formatters/doc.rb
@@ -184,8 +184,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