summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaire McQuin <mcquin@users.noreply.github.com>2014-11-04 10:02:32 -0800
committerClaire McQuin <mcquin@users.noreply.github.com>2014-11-04 10:02:32 -0800
commit76cf26b62bb224cf5fa24e17b49c00a70f282fa6 (patch)
tree6eec2839d30e478ca7faa76f1bb175cb703db58e
parentd6687d195091ab67a00ac5d61ededbb7958f32ad (diff)
parent461d5221d9fd78c2748ac74e8fe175e632307ad6 (diff)
downloadchef-76cf26b62bb224cf5fa24e17b49c00a70f282fa6.tar.gz
Merge pull request #2329 from opscode/runner
Audit-mode runner
-rw-r--r--lib/chef/audit.rb22
-rw-r--r--lib/chef/audit/runner.rb93
2 files changed, 115 insertions, 0 deletions
diff --git a/lib/chef/audit.rb b/lib/chef/audit.rb
new file mode 100644
index 0000000000..1e3668396f
--- /dev/null
+++ b/lib/chef/audit.rb
@@ -0,0 +1,22 @@
+#
+# Author:: Claire McQuin (<claire@getchef.com>)
+# 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 'rspec/core'
+
+require 'chef/dsl/audit'
+require 'chef/audit/chef_json_formatter'
diff --git a/lib/chef/audit/runner.rb b/lib/chef/audit/runner.rb
new file mode 100644
index 0000000000..baf4520466
--- /dev/null
+++ b/lib/chef/audit/runner.rb
@@ -0,0 +1,93 @@
+#
+# Author:: Claire McQuin (<claire@getchef.com>)
+# 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 'chef/audit'
+require 'chef/config'
+
+class Chef
+ class Audit
+ class Runner
+
+ def initialize(run_context)
+ @run_context = run_context
+ end
+
+ def run
+ setup
+ register_controls_groups
+
+ # The first parameter passed to RSpec::Core::Runner.new
+ # is an instance of RSpec::Core::ConfigurationOptions, which is
+ # responsible for processing command line options passed through rspec.
+ # This then gets merged with the configuration. We'll just communicate
+ # directly with the Configuration here.
+ audit_runner = RSpec::Core::Runner.new(nil, configuration, world)
+ audit_runner.run_specs(world.ordered_example_groups)
+ end
+
+ private
+
+ # RSpec configuration and world objects are heavy, so let's wait until
+ # we actually need them.
+ def configuration
+ @configuration ||= RSpec::Core::Configuration.new
+ end
+
+ def world
+ @world ||= RSpec::Core::World.new(configuration)
+ end
+
+ # Configure audits before run.
+ # Sets up where output and error streams should stream to, adds formatters
+ # for people-friendly output of audit results and json for reporting. Also
+ # configures expectation frameworks.
+ def setup
+ configuration.output_stream = Chef::Config[:log_location]
+ configuration.error_stream = Chef::Config[:log_location]
+
+ add_formatters
+ disable_should_syntax
+ end
+
+ def add_formatters
+ configuration.add_formatter(RSpec::Core::Formatters::DocumentationFormatter)
+ configuration.add_formatter(ChefJsonFormatter)
+ end
+
+ # Explicitly disable :should syntax.
+ #
+ # :should is deprecated in RSpec 3 and we have chosen to explicitly disable it
+ # in audits. If :should is used in an audit, the audit will fail with error
+ # message "undefined method `:should`" rather than issue a deprecation warning.
+ #
+ # This can be removed when :should is fully removed from RSpec.
+ def disable_should_syntax
+ configuration.expect_with :rspec do |c|
+ c.syntax = :expect
+ end
+ end
+
+ # 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) }
+ end
+
+ end
+ end
+end