From d20d3aca4e6cc947322c347ba6b7a0f6260576b6 Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Fri, 14 Nov 2014 15:40:24 -0800 Subject: Make sure we don't close the output_stream after running rspec. --- lib/chef/audit/audit_reporter.rb | 23 +++++++---------------- lib/chef/audit/rspec_formatter.rb | 19 +++++++++++++++++++ lib/chef/audit/runner.rb | 1 - lib/chef/client.rb | 8 +++----- lib/chef/config.rb | 1 + lib/chef/monologger.rb | 2 -- lib/chef/resource_reporter.rb | 3 ++- lib/chef/run_context.rb | 13 +++++++++---- 8 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 lib/chef/audit/rspec_formatter.rb (limited to 'lib/chef') diff --git a/lib/chef/audit/audit_reporter.rb b/lib/chef/audit/audit_reporter.rb index b1c9d30bfc..a671ce2221 100644 --- a/lib/chef/audit/audit_reporter.rb +++ b/lib/chef/audit/audit_reporter.rb @@ -53,15 +53,9 @@ class Chef post_auditing_data end - # If the audit phase failed, its because there was some kind of error in the framework - # that runs tests - normal errors are interpreted as EXAMPLE failures and captured. def audit_phase_failed(error) - # The stacktrace information has already been logged elsewhere - Chef::Log.error("Audit Reporter failed - sending error to server with available example information") - ordered_control_groups.each do |name, control_group| - audit_data.add_control_group(control_group) - end - post_auditing_data(error) + # TODO + raise error end def control_group_started(name) @@ -87,16 +81,13 @@ class Chef private - def post_auditing_data(error = nil) + def post_auditing_data if auditing_enabled? - audit_history_url = "controls" - Chef::Log.info("Sending audit report (run-id: #{audit_data.run_id})") + node_name = audit_data.node_name + run_id = audit_data.run_id + audit_history_url = "audits/nodes/#{node_name}/runs/#{run_id}" + Chef::Log.info("Sending audit report (run-id: #{run_id})") run_data = audit_data.to_hash - - if error - run_data[:error] = "#{error.class.to_s}: #{error.message}\n#{error.backtrace.join("\n")}" - end - Chef::Log.debug run_data.inspect compressed_data = encode_gzip(Chef::JSONCompat.to_json(run_data)) Chef::Log.debug("Sending compressed audit data...") diff --git a/lib/chef/audit/rspec_formatter.rb b/lib/chef/audit/rspec_formatter.rb new file mode 100644 index 0000000000..990c1cd780 --- /dev/null +++ b/lib/chef/audit/rspec_formatter.rb @@ -0,0 +1,19 @@ +require 'rspec/core' + +class Chef + class Audit + class RspecFormatter < RSpec::Core::Formatters::DocumentationFormatter + RSpec::Core::Formatters.register self, :close + + # @api public + # + # Invoked at the very end, `close` allows the formatter to clean + # up resources, e.g. open streams, etc. + # + # @param _notification [NullNotification] (Ignored) + def close(_notification) + # Normally Rspec closes the streams it's given. We don't want it for Chef. + end + end + end +end diff --git a/lib/chef/audit/runner.rb b/lib/chef/audit/runner.rb index 4059741359..e20c8b3810 100644 --- a/lib/chef/audit/runner.rb +++ b/lib/chef/audit/runner.rb @@ -71,7 +71,6 @@ class Chef configuration.backtrace_exclusion_patterns.push(Regexp.new("/Users".gsub("/", File::SEPARATOR))) configuration.backtrace_exclusion_patterns.push(Regexp.new("(eval)")) configuration.color = Chef::Config[:color] - configuration.expose_dsl_globally = false add_formatters disable_should_syntax diff --git a/lib/chef/client.rb b/lib/chef/client.rb index b27a2b693d..8cadd43878 100644 --- a/lib/chef/client.rb +++ b/lib/chef/client.rb @@ -330,7 +330,6 @@ class Chef runner.converge @events.converge_complete rescue Exception => e - Chef::Log.error("Converge failed with error message #{e.message}") @events.converge_failed(e) converge_exception = e end @@ -351,16 +350,15 @@ class Chef converge_exception end + # TODO are failed audits going to raise exceptions, or only be handled by the reporters? def run_audits(run_context) audit_exception = nil begin @events.audit_phase_start(run_status) - Chef::Log.info("Starting audit phase") auditor = Chef::Audit::Runner.new(run_context) auditor.run @events.audit_phase_complete rescue Exception => e - Chef::Log.error("Audit phase failed with error message #{e.message}") @events.audit_phase_failed(e) audit_exception = e end @@ -441,8 +439,8 @@ class Chef run_context = setup_run_context - converge_error = converge_and_save(run_context) unless (Chef::Config[:audit_mode] == true) - audit_error = run_audits(run_context) unless (Chef::Config[:audit_mode] == false) + converge_error = converge_and_save(run_context) + audit_error = run_audits(run_context) if converge_error || audit_error e = Chef::Exceptions::RunFailedWrappingError.new(converge_error, audit_error) diff --git a/lib/chef/config.rb b/lib/chef/config.rb index 4b83a0eca3..be31be937a 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -320,6 +320,7 @@ class Chef default :ez, false default :enable_reporting, true default :enable_reporting_url_fatals, false + default :audit_mode, nil # Policyfile is an experimental feature where a node gets its run list and # cookbook version set from a single document on the server instead of diff --git a/lib/chef/monologger.rb b/lib/chef/monologger.rb index 464b21bdd3..f7d226f82e 100644 --- a/lib/chef/monologger.rb +++ b/lib/chef/monologger.rb @@ -1,5 +1,4 @@ require 'logger' - require 'pp' #== MonoLogger @@ -89,4 +88,3 @@ class MonoLogger < Logger end - diff --git a/lib/chef/resource_reporter.rb b/lib/chef/resource_reporter.rb index 1816fc857d..a673f4aa58 100644 --- a/lib/chef/resource_reporter.rb +++ b/lib/chef/resource_reporter.rb @@ -20,7 +20,8 @@ # require 'uri' -require 'securerandom' +require 'zlib' +require 'chef/monkey_patches/securerandom' require 'chef/event_dispatch/base' class Chef diff --git a/lib/chef/run_context.rb b/lib/chef/run_context.rb index 0999ae57c1..41fd11e6eb 100644 --- a/lib/chef/run_context.rb +++ b/lib/chef/run_context.rb @@ -18,6 +18,7 @@ # limitations under the License. require 'chef/resource_collection' +require 'chef/provider_resolver' require 'chef/cookbook_version' require 'chef/node' require 'chef/role' @@ -53,6 +54,9 @@ class Chef # The list of control groups to execute during the audit phase attr_accessor :controls_groups + # Chef::ProviderResolver for this run + attr_accessor :provider_resolver + # A Hash containing the immediate notifications triggered by resources # during the converge phase of the chef run. attr_accessor :immediate_notification_collection @@ -87,6 +91,7 @@ class Chef @node.run_context = self @cookbook_compiler = nil + @provider_resolver = Chef::ProviderResolver.new(@node) end # Triggers the compile phase of the chef run. Implemented by @@ -104,7 +109,7 @@ class Chef if nr.instance_of?(Chef::Resource) @immediate_notification_collection[nr.name] << notification else - @immediate_notification_collection[nr.declared_key] << notification + @immediate_notification_collection[nr.to_s] << notification end end @@ -115,7 +120,7 @@ class Chef if nr.instance_of?(Chef::Resource) @delayed_notification_collection[nr.name] << notification else - @delayed_notification_collection[nr.declared_key] << notification + @delayed_notification_collection[nr.to_s] << notification end end @@ -123,7 +128,7 @@ class Chef if resource.instance_of?(Chef::Resource) return @immediate_notification_collection[resource.name] else - return @immediate_notification_collection[resource.declared_key] + return @immediate_notification_collection[resource.to_s] end end @@ -131,7 +136,7 @@ class Chef if resource.instance_of?(Chef::Resource) return @delayed_notification_collection[resource.name] else - return @delayed_notification_collection[resource.declared_key] + return @delayed_notification_collection[resource.to_s] end end -- cgit v1.2.1