diff options
author | tyler-ball <tyleraball@gmail.com> | 2014-12-09 10:22:28 -0800 |
---|---|---|
committer | tyler-ball <tyleraball@gmail.com> | 2014-12-10 09:42:22 -0800 |
commit | 192ed2ecc8820be77ab97b37df5d9276a0390e55 (patch) | |
tree | 1558c36fc119e280a23555acd5de4e277b0fc404 | |
parent | 49e51d3d2b49897129201d8bc559351a8fdcb76b (diff) | |
download | chef-192ed2ecc8820be77ab97b37df5d9276a0390e55.tar.gz |
Rescuing Exception blind was covering up an unexpected error
-rw-r--r-- | lib/chef/exceptions.rb | 2 | ||||
-rw-r--r-- | spec/unit/client_spec.rb | 54 |
2 files changed, 41 insertions, 15 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index ef957ec502..f710266530 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -403,7 +403,7 @@ class Chef attr_reader :wrapped_errors def initialize(*errors) errors = errors.select {|e| !e.nil?} - output = "Found #{errors.size} errors, they are stored in the backtrace\n" + output = "Found #{errors.size} errors, they are stored in the backtrace" @wrapped_errors = errors super output end diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index 8a1246e1f6..de4a328a90 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -192,7 +192,7 @@ describe Chef::Client do let(:http_cookbook_sync) { double("Chef::REST (cookbook sync)") } let(:http_node_save) { double("Chef::REST (node save)") } let(:runner) { double("Chef::Runner") } - let(:audit_runner) { double("Chef::Audit::Runner") } + let(:audit_runner) { instance_double("Chef::Audit::Runner", :failed? => false) } let(:api_client_exists?) { false } @@ -429,9 +429,11 @@ describe Chef::Client do describe "when the audit phase fails" do context "with an exception" do include_context "a client run" do + let(:e) { Exception.new } def stub_for_audit expect(Chef::Audit::Runner).to receive(:new).and_return(audit_runner) - expect(audit_runner).to receive(:run).and_raise(Exception) + expect(audit_runner).to receive(:run).and_raise(e) + expect(Chef::Application).to receive(:debug_stacktrace).with an_instance_of(Chef::Exceptions::RunFailedWrappingError) end def stub_for_run @@ -443,26 +445,50 @@ describe Chef::Client do expect(client).to receive(:run_started) expect(client).to receive(:run_failed) - # --ResourceReporter#run_completed - # updates the server with the resource history - # (has its own tests, so stubbing it here.) - # TODO: What gets called here? - #expect_any_instance_of(Chef::ResourceReporter).to receive(:run_failed) - # --AuditReporter#run_completed - # posts the audit data to server. - # (has its own tests, so stubbing it here.) - # TODO: What gets called here? - #expect_any_instance_of(Chef::Audit::AuditReporter).to receive(:run_failed) + expect_any_instance_of(Chef::ResourceReporter).to receive(:run_failed) + expect_any_instance_of(Chef::Audit::AuditReporter).to receive(:run_failed) end end it "should save the node after converge and raise exception" do - expect{ client.run }.to raise_error(Exception) + expect{ client.run }.to raise_error(Chef::Exceptions::RunFailedWrappingError) do |error| + expect(error.wrapped_errors.size).to eq(1) + expect(error.wrapped_errors[0]).to eq(e) + end end end context "with failed audits" do - skip("because I don't think we've implemented this yet") + include_context "a client run" do + let(:audit_runner) do + instance_double("Chef::Audit::Runner", :run => true, :failed? => true, :num_failed => 1, :num_total => 1) + end + + def stub_for_audit + expect(Chef::Audit::Runner).to receive(:new).and_return(audit_runner) + expect(Chef::Application).to receive(:debug_stacktrace).with an_instance_of(Chef::Exceptions::RunFailedWrappingError) + end + + def stub_for_run + expect_any_instance_of(Chef::RunLock).to receive(:acquire) + expect_any_instance_of(Chef::RunLock).to receive(:save_pid) + expect_any_instance_of(Chef::RunLock).to receive(:release) + + # Post conditions: check that node has been filled in correctly + expect(client).to receive(:run_started) + expect(client).to receive(:run_failed) + + expect_any_instance_of(Chef::ResourceReporter).to receive(:run_failed) + expect_any_instance_of(Chef::Audit::AuditReporter).to receive(:run_failed) + end + end + + it "should save the node after converge and raise exception" do + expect{ client.run }.to raise_error(Chef::Exceptions::RunFailedWrappingError) do |error| + expect(error.wrapped_errors.size).to eq(1) + expect(error.wrapped_errors[0]).to be_instance_of(Chef::Exceptions::AuditsFailed) + end + end end end |