summaryrefslogtreecommitdiff
path: root/lib/chef/audit
diff options
context:
space:
mode:
authortyler-ball <tyleraball@gmail.com>2014-11-05 11:25:05 -0800
committertyler-ball <tyleraball@gmail.com>2014-12-17 18:47:25 -0800
commit5647ffbafcf22cd54b1edc0b02926aca90b9099c (patch)
treeeca29649eb4ab988796d34aa052903b02007d0f0 /lib/chef/audit
parent9f3390ffe26abbe54b51aee19a78cf2778b9b340 (diff)
downloadchef-5647ffbafcf22cd54b1edc0b02926aca90b9099c.tar.gz
Adding first round of formatter integration - STDOUT doc formatter
Diffstat (limited to 'lib/chef/audit')
-rw-r--r--lib/chef/audit/audit_event_proxy.rb46
-rw-r--r--lib/chef/audit/chef_json_formatter.rb11
-rw-r--r--lib/chef/audit/runner.rb11
3 files changed, 55 insertions, 13 deletions
diff --git a/lib/chef/audit/audit_event_proxy.rb b/lib/chef/audit/audit_event_proxy.rb
new file mode 100644
index 0000000000..1052821946
--- /dev/null
+++ b/lib/chef/audit/audit_event_proxy.rb
@@ -0,0 +1,46 @@
+RSpec::Support.require_rspec_core "formatters/base_text_formatter"
+
+class Chef
+ class Audit
+ class AuditEventProxy < ::RSpec::Core::Formatters::BaseFormatter
+ ::RSpec::Core::Formatters.register self, :example_group_started, :example_group_finished,
+ :example_passed, :example_failed
+
+ # TODO I don't like this, but I don't see another way to pass this in
+ # see configuration.rb#L671 and formatters.rb#L129
+ def self.events=(events)
+ @@events = events
+ end
+
+ def events
+ @@events
+ end
+
+ def initialize(output)
+ super
+ end
+
+ def example_group_started(notification)
+ events.control_group_start(notification.group.description.strip)
+ end
+
+ def example_group_finished(_notification)
+ events.control_group_end
+ end
+
+ def example_passed(passed)
+ events.control_example_success(passed.example.description.strip)
+ end
+
+ def example_failed(failed)
+ events.control_example_failure(failed.example.description.strip, failed.example.execution_result.exception)
+ end
+
+ private
+
+ def example_group_chain
+ example_group.parent_groups.reverse
+ end
+ end
+ end
+end
diff --git a/lib/chef/audit/chef_json_formatter.rb b/lib/chef/audit/chef_json_formatter.rb
index 5dcbdb50b7..50dfbffbb3 100644
--- a/lib/chef/audit/chef_json_formatter.rb
+++ b/lib/chef/audit/chef_json_formatter.rb
@@ -5,7 +5,7 @@ require 'ffi_yajl'
class Chef
class Audit
class ChefJsonFormatter < ::RSpec::Core::Formatters::BaseFormatter
- ::RSpec::Core::Formatters.register self, :example_group_started, :message, :stop, :close, :example_failed
+ ::RSpec::Core::Formatters.register self, :example_group_started, :stop, :close
attr_reader :control_group_data
@@ -25,15 +25,6 @@ class Chef
end
end
- def example_failed(notification)
- e = notification.example.metadata[:execution_result].exception
- raise e unless e.kind_of? ::RSpec::Expectations::ExpectationNotMetError
- end
-
- def message(notification)
- puts "message: #{notification}"
- end
-
def stop(notification)
notification.examples.each do |example|
control_data = build_control_from(example)
diff --git a/lib/chef/audit/runner.rb b/lib/chef/audit/runner.rb
index 0f439a1aa5..1408c67327 100644
--- a/lib/chef/audit/runner.rb
+++ b/lib/chef/audit/runner.rb
@@ -17,12 +17,15 @@
#
require 'chef/audit'
+require 'chef/audit/audit_event_proxy'
require 'chef/config'
class Chef
class Audit
class Runner
+ attr_reader :run_context
+
def initialize(run_context)
@run_context = run_context
end
@@ -57,6 +60,8 @@ class Chef
# for people-friendly output of audit results and json for reporting. Also
# configures expectation frameworks.
def setup
+ # We're setting the output stream, but that will only be used for error situations
+ # Our formatter forwards events to the Chef event message bus
configuration.output_stream = Chef::Config[:log_location]
configuration.error_stream = Chef::Config[:log_location]
@@ -66,8 +71,8 @@ class Chef
end
def add_formatters
- configuration.add_formatter(RSpec::Core::Formatters::DocumentationFormatter)
- configuration.add_formatter(ChefJsonFormatter)
+ configuration.add_formatter(Chef::Audit::AuditEventProxy)
+ Chef::Audit::AuditEventProxy.events = run_context.events
end
# Explicitly disable :should syntax.
@@ -92,7 +97,7 @@ class Chef
# Register each controls group with the world, which will handle
# the ordering of the audits that will be run.
def register_controls_groups
- @run_context.controls_groups.each { |ctls_grp| world.register(ctls_grp) }
+ run_context.controls_groups.each { |ctls_grp| world.register(ctls_grp) }
end
end