summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortyler-ball <tyleraball@gmail.com>2014-10-30 17:34:37 -0700
committertyler-ball <tyleraball@gmail.com>2014-10-30 17:34:37 -0700
commitd8624671c53e522310ec616aa693632b667e6395 (patch)
tree6560adf9fb9a47a303791c4aa72a29a4cca5c11b
parent51ea3cb190bcfa7c7f8e498cfcc9d1197a294332 (diff)
downloadchef-d8624671c53e522310ec616aa693632b667e6395.tar.gz
Creating our own example group class to simplify adding examples to the spec runner
-rw-r--r--lib/chef/audit/chef_example_group.rb10
-rw-r--r--lib/chef/client.rb56
-rw-r--r--lib/chef/dsl/audit.rb34
-rw-r--r--lib/chef/event_dispatch/base.rb16
-rw-r--r--lib/chef/exceptions.rb2
5 files changed, 75 insertions, 43 deletions
diff --git a/lib/chef/audit/chef_example_group.rb b/lib/chef/audit/chef_example_group.rb
new file mode 100644
index 0000000000..cd874d57b7
--- /dev/null
+++ b/lib/chef/audit/chef_example_group.rb
@@ -0,0 +1,10 @@
+require 'rspec/core'
+
+class Chef
+ class Audit
+ class ChefExampleGroup < ::RSpec::Core::ExampleGroup
+ # Can encompass tests in a `control` block or `describe` block
+ define_example_group_method :control
+ end
+ end
+end
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index 4f37bd0ee3..7fde1eb12e 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -295,6 +295,7 @@ class Chef
end
# We now have the client key, and should use it from now on.
@rest = Chef::REST.new(config[:chef_server_url], client_name, config[:client_key])
+ # TODO register this where we register all other event listeners
@resource_reporter = Chef::ResourceReporter.new(@rest)
@events.register(@resource_reporter)
rescue Exception => e
@@ -307,18 +308,35 @@ class Chef
# Converges the node.
#
# === Returns
- # true:: Always returns true
+ # The thrown exception, if there was one. If this returns nil the converge was successful.
def converge(run_context)
- @events.converge_start(run_context)
- Chef::Log.debug("Converging node #{node_name}")
- @runner = Chef::Runner.new(run_context)
- runner.converge
- @events.converge_complete
- true
- rescue Exception
- # TODO: should this be a separate #converge_failed(exception) method?
- @events.converge_complete
- raise
+ converge_exception = nil
+ catch(:end_client_run_early) do
+ begin
+ @events.converge_start(run_context)
+ Chef::Log.debug("Converging node #{node_name}")
+ @runner = Chef::Runner.new(run_context)
+ runner.converge
+ @events.converge_complete
+ rescue Exception => e
+ @events.converge_failed(e)
+ converge_exception = e
+ end
+ end
+ converge_exception
+ end
+
+ def run_audits(run_context)
+ audit_exception = nil
+ begin
+ @events.audit_start(run_context)
+ # TODO
+ @events.audit_complete
+ rescue Exception => e
+ @events.audit_failed(e)
+ audit_exception = e
+ end
+ audit_exception
end
# Expands the run list. Delegates to the policy_builder.
@@ -396,11 +414,17 @@ class Chef
run_context = setup_run_context
- catch(:end_client_run_early) do
- converge(run_context)
+ converge_exception = converge(run_context)
+ if converge_exception
+
+ else
+ save_updated_node
end
- save_updated_node
+ audit_exception = run_audits(run_context)
+ if audit_exception
+
+ end
run_status.stop_clock
Chef::Log.info("Chef Run complete in #{run_status.elapsed_time} seconds")
@@ -411,6 +435,10 @@ class Chef
Chef::Platform::Rebooter.reboot_if_needed!(node)
true
+
+ # TODO get rid of resuce here, push down into sub-methods, clean up this method, return exceptions and raise new
+ # exception wrapping all known exceptions. sub-method should do all their own event reporting.
+
rescue Exception => e
# CHEF-3336: Send the error first in case something goes wrong below and we don't know why
Chef::Log.debug("Re-raising exception: #{e.class} - #{e.message}\n#{e.backtrace.join("\n ")}")
diff --git a/lib/chef/dsl/audit.rb b/lib/chef/dsl/audit.rb
index 24c1bb0464..fee89c02ca 100644
--- a/lib/chef/dsl/audit.rb
+++ b/lib/chef/dsl/audit.rb
@@ -15,44 +15,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-require 'rspec/core'
+#require 'chef/audit'
+require 'chef/audit/chef_example_group'
class Chef
module DSL
module Audit
- # List of `control` example groups to be executed
- @example_groups = nil
-
# 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 = []
-
- if group_block
- yield
- end
+ raise ::Chef::Exceptions::NoAuditsProvided("You must provide a block with audits") unless group_block
# 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 `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 control(*args, &control_block)
- if @example_groups.nil?
- raise "Cannot define a `control` unless inside a `controls` block"
- end
-
- example_name = args[0]
- puts "entered control block named #{example_name}"
- # TODO is this the correct way to define one?
- # https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L197
- # https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/example_group.rb#L323
- @example_groups << ::RSpec::Core::ExampleGroup.describe(args, &control_block)
+ # run_context.audit_runner.register
+ ::Chef::Audit::ChefExampleGroup.describe(group_name, &group_block)
end
end
diff --git a/lib/chef/event_dispatch/base.rb b/lib/chef/event_dispatch/base.rb
index bfd4503097..57152e9b8c 100644
--- a/lib/chef/event_dispatch/base.rb
+++ b/lib/chef/event_dispatch/base.rb
@@ -225,6 +225,22 @@ class Chef
def converge_complete
end
+ # Called if the converge phase fails
+ def converge_failed(exception)
+ end
+
+ # Called before audit phase starts
+ def audit_start(run_context)
+ end
+
+ # Called when the audit phase is finished.
+ def audit_complete
+ end
+
+ # Called if the audit phase fails
+ def audit_failed(exception)
+ end
+
# TODO: need events for notification resolve?
# def notifications_resolved
# end
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index 25f08455fc..2103812dca 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -366,5 +366,7 @@ class Chef
super "Found more than one provider for #{resource.resource_name} resource: #{classes}"
end
end
+
+ class NoAuditsProvided < RuntimeError; end
end
end