diff options
-rw-r--r-- | lib/chef/audit.rb | 64 | ||||
-rw-r--r-- | lib/chef/dsl/audit.rb | 15 |
2 files changed, 72 insertions, 7 deletions
diff --git a/lib/chef/audit.rb b/lib/chef/audit.rb new file mode 100644 index 0000000000..6fd067448b --- /dev/null +++ b/lib/chef/audit.rb @@ -0,0 +1,64 @@ +# +# 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/config' + +class Chef + class Audit + + def initialize + @configuration = RSpec::Core::Configuration.new + @world = RSpec::Core::World.new(@configuration) + @runner = RSpec::Core::Runner.new(nil, @configuration, @world) + end + + def setup + @configuration.output_stream = Chef::Config[:log_location] + @configuration.error_stream = Chef::Config[:log_location] + + configure_formatters + configure_expectation_frameworks + end + + private + # Adds formatters to RSpec. + # By default, two formatters are added: one for outputting readable text + # of audits run and one for sending JSON data back to reporting. + def configure_formatters + # TODO (future): We should allow for an audit-mode formatter config option + # and use this formatter as default/fallback if none is specified. + @configuration.add_formatter(RSpec::Core::Formatters::DocumentationFormatter) + # TODO: Add JSON formatter for audit reporting to analytics. + end + + def configure_expectation_frameworks + @configuration.expect_with(:rspec) do |config| + # :should is deprecated in RSpec 3+ and we have chosen to explicitly disable + # it in audits. If :should is used in an audit, this will cause the audit to + # fail with message "undefined method `should`" rather than print a deprecation + # message. + config.syntax = :expect + end + + #TODO: serverspec? + end + + end +end diff --git a/lib/chef/dsl/audit.rb b/lib/chef/dsl/audit.rb index a90f7f66e0..24c1bb0464 100644 --- a/lib/chef/dsl/audit.rb +++ b/lib/chef/dsl/audit.rb @@ -21,11 +21,11 @@ class Chef module DSL module Audit - # List of `controls` example groups to be executed + # List of `control` example groups to be executed @example_groups = nil - # Adds the control_group and block (containing controls to execute) to the runner's list of pending examples - def control_group(group_name, &group_block) + # Adds the controls group and block (containing controls to execute) to the runner's list of pending examples + def controls(group_name, &group_block) puts "entered group named #{group_name}" @example_groups = [] @@ -36,14 +36,15 @@ class Chef # TODO add the @example_groups list to the runner for later execution p @example_groups - # Reset this to nil so we can tell if a `controls` message is sent outside a `control_group` block - # Prevents defining then un-defining the `controls` singleton method + # Reset this to nil so we can tell if a `control` message is sent outside a `controls` block + # Prevents defining then un-defining the `control` singleton method + # TODO this does not prevent calling `control` inside `control` @example_groups = nil end - def controls(*args, &control_block) + def control(*args, &control_block) if @example_groups.nil? - raise "Cannot define a `controls` unless inside a `control_group`" + raise "Cannot define a `control` unless inside a `controls` block" end example_name = args[0] |