summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/audit/audit_reporter.rb18
-rw-r--r--lib/chef/client.rb13
-rw-r--r--lib/chef/resource_reporter.rb2
-rw-r--r--spec/unit/audit/audit_reporter_spec.rb50
-rw-r--r--spec/unit/client_spec.rb40
5 files changed, 30 insertions, 93 deletions
diff --git a/lib/chef/audit/audit_reporter.rb b/lib/chef/audit/audit_reporter.rb
index 030ae34201..fd3d13dd89 100644
--- a/lib/chef/audit/audit_reporter.rb
+++ b/lib/chef/audit/audit_reporter.rb
@@ -72,7 +72,9 @@ class Chef
end
def run_failed(error)
- post_auditing_data(error)
+ # Audit phase errors are captured when audit_phase_failed gets called.
+ # The error passed here isn't relevant to auditing, so we ignore it.
+ post_auditing_data
end
def control_group_started(name)
@@ -100,7 +102,7 @@ class Chef
private
- def post_auditing_data(error = nil)
+ def post_auditing_data
unless auditing_enabled?
Chef::Log.debug("Audit Reports are disabled. Skipping sending reports.")
return
@@ -118,14 +120,10 @@ class Chef
Chef::Log.debug("Sending audit report (run-id: #{audit_data.run_id})")
run_data = audit_data.to_hash
- if @exception || error
- errors = [@exception, error].uniq.compact
- errors_messages = errors.map do |err|
- msg = "#{err.class.to_s}: #{err.message}"
- msg << "\n#{err.backtrace.join("\n")}" if err.backtrace
- msg
- end
- run_data[:error] = errors_messages.join("\n")
+ if @exception
+ error_info = "#{@exception.class}: #{@exception.message}"
+ error_info << "\n#{@exception.backtrace.join("\n")}" if @exception.backtrace
+ run_data[:error] = error_info
end
Chef::Log.debug "Audit Report:\n#{Chef::JSONCompat.to_json_pretty(run_data)}"
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index bf99f003fb..8203392b39 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -539,16 +539,13 @@ class Chef
end
def wrap_exceptions(converge_error, audit_error)
- if audit_error && !(audit_error.is_a?(Chef::Exceptions::AuditsFailed) && Chef::Config[:audit_as_warning])
- if converge_error
- return Chef::Exceptions::RunFailedWrappingError.new(converge_error, audit_error)
- else
- return Chef::Exceptions::RunFailedWrappingError.new(audit_error)
- end
+ err = if audit_error && !audit_error.is_a?(Chef::Exceptions::AuditsFailed)
+ Chef::Exceptions::RunFailedWrappingError.new(converge_error, audit_error)
elsif converge_error
- return Chef::Exceptions::RunFailedWrappingError.new(converge_error)
+ Chef::Exceptions::RunFailedWrappingError.new(converge_error)
end
- nil
+ err.fill_backtrace if err
+ err
end
end
diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb
index 7829bb4d70..7d13a5a5ce 100644
--- a/lib/chef/resource_reporter.rb
+++ b/lib/chef/resource_reporter.rb
@@ -213,7 +213,7 @@ class Chef
# If we failed before we received the run_started callback, there's not much we can do
# in terms of reporting
if @run_status
- post_reporting_data
+ post_reporting_data
end
end
diff --git a/spec/unit/audit/audit_reporter_spec.rb b/spec/unit/audit/audit_reporter_spec.rb
index 447fd5e357..f2d247644a 100644
--- a/spec/unit/audit/audit_reporter_spec.rb
+++ b/spec/unit/audit/audit_reporter_spec.rb
@@ -242,6 +242,10 @@ EOM
:message => "Audit phase failed with error message: derpderpderp",
:backtrace => ["/path/recipe.rb:57", "/path/library.rb:106"]) }
+ let(:run_error) { double("RunError", :class => "Chef::Exceptions::RunError",
+ :message => "This error shouldn't be reported.",
+ :backtrace => ["fix it", "fix it", "fix it"]) }
+
before do
allow(reporter).to receive(:auditing_enabled?).and_return(true)
allow(reporter).to receive(:run_status).and_return(run_status)
@@ -250,16 +254,11 @@ EOM
end
context "when no prior exception is stored" do
- it "reports the error" do
+ it "reports no 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
+ reporter.run_failed(run_error)
+ expect(run_data).to_not have_key(:error)
end
end
@@ -268,39 +267,16 @@ EOM
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!
+ it "reports the prior error" do
+ expect(rest).to receive(:create_url)
+ expect(rest).to receive(:post)
+ reporter.run_failed(run_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
diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb
index 301f64ddf1..1704bcc461 100644
--- a/spec/unit/client_spec.rb
+++ b/spec/unit/client_spec.rb
@@ -253,24 +253,7 @@ describe Chef::Client do
describe "when audit phase completed with failed controls" do
include_context "audit phase completed with failed controls"
-
- context "with :audit_as_warning true" do
- before do
- Chef::Config[:audit_as_warning] = true
- end
-
- include_examples "a completed run"
- end
-
- context "with :audit_as_warning false" do
- before do
- Chef::Config[:audit_as_warning] = false
- end
-
- include_examples "a failed run" do
- let(:run_errors) { [audit_error] }
- end
- end
+ include_examples "a completed run"
end
end
@@ -294,25 +277,8 @@ describe Chef::Client do
describe "when audit phase completed with failed controls" do
include_context "audit phase completed with failed controls"
-
- context "with :audit_as_warning true" do
- before do
- Chef::Config[:audit_as_warning] = true
- end
-
- include_examples "a failed run" do
- let(:run_errors) { [converge_error] }
- end
- end
-
- context "with :audit_as_warning false" do
- before do
- Chef::Config[:audit_as_warning] = false
- end
-
- include_examples "a failed run" do
- let(:run_errors) { [converge_error, audit_error] }
- end
+ include_examples "a failed run" do
+ let(:run_errors) { [converge_error] }
end
end
end