diff options
author | Serdar Sutay <serdar@opscode.com> | 2015-05-21 14:03:31 -0700 |
---|---|---|
committer | Serdar Sutay <serdar@opscode.com> | 2015-05-27 12:52:52 -0700 |
commit | 4220dc5165751947c6504ea9c3b185e13a945bfa (patch) | |
tree | b479b6f86864758a5dfa0b778947e7f70ee296db | |
parent | d8172e646d9fbf43e57bca5e20d0ac352ba9a66a (diff) | |
download | chef-4220dc5165751947c6504ea9c3b185e13a945bfa.tar.gz |
Make sure the audit mode output is reflected both in the logs and in the formatter output.
-rw-r--r-- | lib/chef/audit/audit_reporter.rb | 4 | ||||
-rw-r--r-- | lib/chef/audit/logger.rb | 38 | ||||
-rw-r--r-- | lib/chef/audit/runner.rb | 6 | ||||
-rw-r--r-- | lib/chef/client.rb | 4 | ||||
-rw-r--r-- | lib/chef/event_dispatch/base.rb | 4 | ||||
-rw-r--r-- | lib/chef/formatters/doc.rb | 6 | ||||
-rw-r--r-- | spec/functional/audit/runner_spec.rb | 64 | ||||
-rw-r--r-- | spec/unit/audit/audit_reporter_spec.rb | 8 | ||||
-rw-r--r-- | spec/unit/audit/logger_spec.rb | 22 | ||||
-rw-r--r-- | spec/unit/audit/runner_spec.rb | 4 |
10 files changed, 102 insertions, 58 deletions
diff --git a/lib/chef/audit/audit_reporter.rb b/lib/chef/audit/audit_reporter.rb index b74bf07b8b..d952d8a249 100644 --- a/lib/chef/audit/audit_reporter.rb +++ b/lib/chef/audit/audit_reporter.rb @@ -47,7 +47,7 @@ class Chef @run_status = run_status end - def audit_phase_complete + def audit_phase_complete(audit_output) Chef::Log.debug("Audit Reporter completed successfully without errors.") ordered_control_groups.each do |name, control_group| audit_data.add_control_group(control_group) @@ -58,7 +58,7 @@ class Chef # that runs tests - normal errors are interpreted as EXAMPLE failures and captured. # We still want to send available audit information to the server so we process the # known control groups. - def audit_phase_failed(error) + def audit_phase_failed(error, audit_output) # The stacktrace information has already been logged elsewhere @audit_phase_error = error Chef::Log.debug("Audit Reporter failed.") diff --git a/lib/chef/audit/logger.rb b/lib/chef/audit/logger.rb new file mode 100644 index 0000000000..c5c9f5c8a2 --- /dev/null +++ b/lib/chef/audit/logger.rb @@ -0,0 +1,38 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'stringio' + +class Chef + class Audit + class Logger + def self.puts(message="") + if @buffer.nil? + @buffer = StringIO.new + end + + @buffer.puts(message) + Chef::Log.info(message) + end + + def self.read_buffer + return "" if @buffer.nil? + @buffer.string + end + end + end +end diff --git a/lib/chef/audit/runner.rb b/lib/chef/audit/runner.rb index 801bf5ee58..234d83ab8f 100644 --- a/lib/chef/audit/runner.rb +++ b/lib/chef/audit/runner.rb @@ -16,6 +16,8 @@ # limitations under the License. # +require 'chef/audit/logger' + class Chef class Audit class Runner @@ -115,8 +117,8 @@ class Chef # the output stream to be changed for a formatter once the formatter has # been added. def set_streams - RSpec.configuration.output_stream = Chef::Config[:log_location] - RSpec.configuration.error_stream = Chef::Config[:log_location] + RSpec.configuration.output_stream = Chef::Audit::Logger + RSpec.configuration.error_stream = Chef::Audit::Logger end # Add formatters which we use to diff --git a/lib/chef/client.rb b/lib/chef/client.rb index f3e7722509..bc7ddb180a 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -373,11 +373,11 @@ class Chef audit_exception = Chef::Exceptions::AuditsFailed.new(auditor.num_failed, auditor.num_total) @events.audit_phase_failed(audit_exception) else - @events.audit_phase_complete + @events.audit_phase_complete(Chef::Audit::Logger.read_buffer) end rescue Exception => e Chef::Log.error("Audit phase failed with error message: #{e.message}") - @events.audit_phase_failed(e) + @events.audit_phase_failed(e, Chef::Audit::Logger.read_buffer) audit_exception = e end audit_exception diff --git a/lib/chef/event_dispatch/base.rb b/lib/chef/event_dispatch/base.rb index 93caa62a65..73fe25ec13 100644 --- a/lib/chef/event_dispatch/base.rb +++ b/lib/chef/event_dispatch/base.rb @@ -244,13 +244,13 @@ class Chef end # Called when audit phase successfully finishes - def audit_phase_complete + def audit_phase_complete(audit_output) end # Called if there is an uncaught exception during the audit phase. The audit runner should # be catching and handling errors from the examples, so this is only uncaught errors (like # bugs in our handling code) - def audit_phase_failed(exception) + def audit_phase_failed(exception, audit_output) end # Signifies the start of a `control_group` block with a defined name diff --git a/lib/chef/formatters/doc.rb b/lib/chef/formatters/doc.rb index e63c764cbf..e76a940c38 100644 --- a/lib/chef/formatters/doc.rb +++ b/lib/chef/formatters/doc.rb @@ -179,11 +179,13 @@ class Chef puts_line "Starting audit phase" end - def audit_phase_complete + def audit_phase_complete(audit_output) + puts_line audit_output puts_line "Auditing complete" end - def audit_phase_failed(error) + def audit_phase_failed(error, audit_output) + puts_line audit_output puts_line "" puts_line "Audit phase exception:" indent diff --git a/spec/functional/audit/runner_spec.rb b/spec/functional/audit/runner_spec.rb index 494942889a..aae8fcf582 100644 --- a/spec/functional/audit/runner_spec.rb +++ b/spec/functional/audit/runner_spec.rb @@ -46,22 +46,12 @@ describe Chef::Audit::Runner do RSpec::Core::Sandbox.sandboxed { ex.run } end - before do - Chef::Config[:log_location] = stdout - end - describe "#run" do let(:audits) { {} } let(:run_context) { instance_double(Chef::RunContext, :events => events, :audits => audits) } let(:control_group_name) { "control_group_name" } - it "Correctly runs an empty controls block" do - in_sub_process do - runner.run - end - end - shared_context "passing audit" do let(:audits) do should_pass = lambda do @@ -84,50 +74,40 @@ describe Chef::Audit::Runner do end end - context "there is a single successful control" do - include_context "passing audit" - it "correctly runs" do - in_sub_process do - runner.run - - expect(stdout.string).to match(/1 example, 0 failures/) + describe "log location is stdout" do + before do + allow(Chef::Log).to receive(:info) do |msg| + stdout.puts(msg) end end - end - context "there is a single failing control" do - include_context "failing audit" - it "correctly runs" do + it "Correctly runs an empty controls block" do in_sub_process do runner.run - - expect(stdout.string).to match(/Failure\/Error: expect\(2 - 1\)\.to eq\(0\)/) - expect(stdout.string).to match(/1 example, 1 failure/) - expect(stdout.string).to match(/# control_group_name should fail/) end end - end - describe "log location is a file" do - let(:tmpfile) { Tempfile.new("audit") } - before do - Chef::Config[:log_location] = tmpfile.path - end + context "there is a single successful control" do + include_context "passing audit" + it "correctly runs" do + in_sub_process do + runner.run - after do - tmpfile.close - tmpfile.unlink + expect(stdout.string).to match(/1 example, 0 failures/) + end + end end - include_context "failing audit" - it "correctly runs" do - in_sub_process do - runner.run + context "there is a single failing control" do + include_context "failing audit" + it "correctly runs" do + in_sub_process do + runner.run - contents = tmpfile.read - expect(contents).to match(/Failure\/Error: expect\(2 - 1\)\.to eq\(0\)/) - expect(contents).to match(/1 example, 1 failure/) - expect(contents).to match(/# control_group_name should fail/) + expect(stdout.string).to match(/Failure\/Error: expect\(2 - 1\)\.to eq\(0\)/) + expect(stdout.string).to match(/1 example, 1 failure/) + expect(stdout.string).to match(/# control_group_name should fail/) + end end end end diff --git a/spec/unit/audit/audit_reporter_spec.rb b/spec/unit/audit/audit_reporter_spec.rb index 75c96155da..46c2a96b4c 100644 --- a/spec/unit/audit/audit_reporter_spec.rb +++ b/spec/unit/audit/audit_reporter_spec.rb @@ -314,14 +314,14 @@ EOM it "notifies audit phase finished to debug log" do expect(Chef::Log).to receive(:debug).with(/Audit Reporter completed/) - reporter.audit_phase_complete + reporter.audit_phase_complete("Output from audit mode") end it "collects audit data" do ordered_control_groups.each do |_name, group| expect(audit_data).to receive(:add_control_group).with(group) end - reporter.audit_phase_complete + reporter.audit_phase_complete("Output from audit mode") end end @@ -332,14 +332,14 @@ EOM it "notifies audit phase failed to debug log" do expect(Chef::Log).to receive(:debug).with(/Audit Reporter failed/) - reporter.audit_phase_failed(error) + reporter.audit_phase_failed(error, "Output from audit mode") end it "collects audit data" do ordered_control_groups.each do |_name, group| expect(audit_data).to receive(:add_control_group).with(group) end - reporter.audit_phase_failed(error) + reporter.audit_phase_failed(error, "Output from audit mode") end end diff --git a/spec/unit/audit/logger_spec.rb b/spec/unit/audit/logger_spec.rb new file mode 100644 index 0000000000..ae2cf9ef34 --- /dev/null +++ b/spec/unit/audit/logger_spec.rb @@ -0,0 +1,22 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'spec_helper' + +describe Chef::Audit::Logger do + +end diff --git a/spec/unit/audit/runner_spec.rb b/spec/unit/audit/runner_spec.rb index 0bd4c18388..1de024260f 100644 --- a/spec/unit/audit/runner_spec.rb +++ b/spec/unit/audit/runner_spec.rb @@ -68,8 +68,8 @@ describe Chef::Audit::Runner do in_sub_process do runner.send(:setup) - expect(RSpec.configuration.output_stream).to eq(log_location) - expect(RSpec.configuration.error_stream).to eq(log_location) + expect(RSpec.configuration.output_stream).to eq(Chef::Audit::Logger) + expect(RSpec.configuration.error_stream).to eq(Chef::Audit::Logger) expect(RSpec.configuration.formatters.size).to eq(2) expect(RSpec.configuration.formatters).to include(instance_of(Chef::Audit::AuditEventProxy)) |