Class: ChefSpec::Runner
- Inherits:
-
Object
- Object
- ChefSpec::Runner
- Includes:
- Normalize
- Defined in:
- lib/chefspec/runner.rb,
lib/chefspec/deprecations.rb,
lib/chefspec/server.rb
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
- #run_context ⇒ Chef::RunContext readonly
Class Method Summary collapse
-
.define_runner_method(resource_name) ⇒ self
Defines a new runner method on the
ChefSpec::Runner
.
Instance Method Summary collapse
-
#compiling? ⇒ Boolean
Boolean method to determine the current phase of the Chef run (compiling or converging).
-
#converge(*recipe_names) ⇒ ChefSpec::Runner
Execute the given ‘run_list` on the node, without actually converging the node.
-
#dry_run? ⇒ Boolean
Boolean method to determine if this Runner is in ‘dry_run` mode.
-
#existing_initialize {|node| ... } ⇒ Runner
Instantiate a new Runner to run examples with.
-
#find_resource(type, name) ⇒ Chef::Resource?
Find the resource with the declared type and resource name.
-
#find_resources(type) ⇒ Array<Chef::Resource>
Find the resource with the declared type.
-
#initialize(options = {}, &block) ⇒ Runner
constructor
Override the existing initialize method, setting the appropriate configuration to use a real Chef Server instead.
-
#inspect ⇒ String
The runner as a String with helpful output.
-
#node ⇒ Chef::Node
The
Chef::Node
corresponding to this Runner. -
#old_initialize {|node| ... } ⇒ Runner
Instantiate a new Runner to run examples with.
-
#resource_collection ⇒ Hash<String, Chef::Resource>
The full collection of resources for this Runner.
-
#step_into?(resource) ⇒ Boolean
Determines if the runner should step into the given resource.
-
#to_s ⇒ String
This runner as a string.
Methods included from Normalize
Constructor Details
#initialize(options = {}, &block) ⇒ Runner
Override the existing initialize method, setting the appropriate configuration to use a real Chef Server instead.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/chefspec/runner.rb', line 65 def initialize( = {}, &block) @options = = { cookbook_path: RSpec.configuration.cookbook_path || calling_cookbook_path(caller), role_path: RSpec.configuration.role_path || default_role_path, log_level: RSpec.configuration.log_level, path: RSpec.configuration.path, platform: RSpec.configuration.platform, version: RSpec.configuration.version, }.merge() Chef::Log.level = [:log_level] Chef::Config.reset! Chef::Config.formatters.clear Chef::Config.add_formatter('chefspec') Chef::Config[:cache_type] = 'Memory' Chef::Config[:client_key] = nil Chef::Config[:cookbook_path] = Array([:cookbook_path]) Chef::Config[:role_path] = Array([:role_path]) Chef::Config[:force_logger] = true Chef::Config[:solo] = true yield node if block_given? end |
Instance Attribute Details
#options ⇒ Hash (readonly)
28 29 30 |
# File 'lib/chefspec/runner.rb', line 28 def @options end |
#run_context ⇒ Chef::RunContext (readonly)
31 32 33 |
# File 'lib/chefspec/runner.rb', line 31 def run_context @run_context end |
Class Method Details
.define_runner_method(resource_name) ⇒ self
Defines a new runner method on the ChefSpec::Runner
.
19 20 21 22 23 24 25 |
# File 'lib/chefspec/runner.rb', line 19 def self.define_runner_method(resource_name) define_method(resource_name) do |identity| find_resource(resource_name, identity) end self end |
Instance Method Details
#compiling? ⇒ Boolean
Boolean method to determine the current phase of the Chef run (compiling or converging)
207 208 209 |
# File 'lib/chefspec/runner.rb', line 207 def compiling? !@converging end |
#converge(*recipe_names) ⇒ ChefSpec::Runner
Execute the given ‘run_list` on the node, without actually converging the node.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/chefspec/runner.rb', line 107 def converge(*recipe_names) node.run_list.reset! recipe_names.each { |recipe_name| node.run_list.add(recipe_name) } return self if dry_run? # Expand the run_list # Save the node back to the server for searching purposes unless Chef::Config[:solo] client.register node.save end # Setup the run_context @run_context = client.setup_run_context # Allow stubbing/mocking after the cookbook has been compiled but before the converge yield if block_given? @converging = true @client.converge(@run_context) self end |
#dry_run? ⇒ Boolean
Boolean method to determine if this Runner is in ‘dry_run` mode.
235 236 237 |
# File 'lib/chefspec/runner.rb', line 235 def dry_run? !![:dry_run] end |
#existing_initialize {|node| ... } ⇒ Runner
Instantiate a new Runner to run examples with.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/chefspec/deprecations.rb', line 32 def initialize( = {}, &block) @options = = { cookbook_path: RSpec.configuration.cookbook_path || calling_cookbook_path(caller), role_path: RSpec.configuration.role_path || default_role_path, log_level: RSpec.configuration.log_level, path: RSpec.configuration.path, platform: RSpec.configuration.platform, version: RSpec.configuration.version, }.merge() Chef::Log.level = [:log_level] Chef::Config.reset! Chef::Config.formatters.clear Chef::Config.add_formatter('chefspec') Chef::Config[:cache_type] = 'Memory' Chef::Config[:client_key] = nil Chef::Config[:cookbook_path] = Array([:cookbook_path]) Chef::Config[:role_path] = Array([:role_path]) Chef::Config[:force_logger] = true Chef::Config[:solo] = true yield node if block_given? end |
#find_resource(type, name) ⇒ Chef::Resource?
Find the resource with the declared type and resource name.
172 173 174 175 176 177 178 179 180 |
# File 'lib/chefspec/runner.rb', line 172 def find_resource(type, name) begin return resource_collection.lookup("#{type}[#{name}]") rescue Chef::Exceptions::ResourceNotFound; end resource_collection.all_resources.find do |resource| resource_name(resource) == type && (name === resource.identity || name === resource.name) end end |
#find_resources(type) ⇒ Array<Chef::Resource>
Find the resource with the declared type.
195 196 197 198 199 |
# File 'lib/chefspec/runner.rb', line 195 def find_resources(type) resource_collection.all_resources.select do |resource| resource_name(resource) == type.to_sym end end |
#inspect ⇒ String
The runner as a String with helpful output.
255 256 257 |
# File 'lib/chefspec/runner.rb', line 255 def inspect "#<#{self.class} options: #{.inspect}, run_list: '#{node.run_list.to_s}'>" end |
#node ⇒ Chef::Node
The Chef::Node
corresponding to this Runner.
138 139 140 141 142 143 144 145 |
# File 'lib/chefspec/runner.rb', line 138 def node return @node if @node @node = client.build_node @node.instance_variable_set(:@runner, self) @node.class.send(:attr_reader, :runner) @node end |
#old_initialize {|node| ... } ⇒ Runner
Instantiate a new Runner to run examples with.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/chefspec/server.rb', line 21 def initialize( = {}, &block) @options = = { cookbook_path: RSpec.configuration.cookbook_path || calling_cookbook_path(caller), role_path: RSpec.configuration.role_path || default_role_path, log_level: RSpec.configuration.log_level, path: RSpec.configuration.path, platform: RSpec.configuration.platform, version: RSpec.configuration.version, }.merge() Chef::Log.level = [:log_level] Chef::Config.reset! Chef::Config.formatters.clear Chef::Config.add_formatter('chefspec') Chef::Config[:cache_type] = 'Memory' Chef::Config[:client_key] = nil Chef::Config[:cookbook_path] = Array([:cookbook_path]) Chef::Config[:role_path] = Array([:role_path]) Chef::Config[:force_logger] = true Chef::Config[:solo] = true yield node if block_given? end |
#resource_collection ⇒ Hash<String, Chef::Resource>
The full collection of resources for this Runner.
152 153 154 |
# File 'lib/chefspec/runner.rb', line 152 def resource_collection @resource_collection ||= @run_context.resource_collection end |
#step_into?(resource) ⇒ Boolean
Determines if the runner should step into the given resource. The step_into
option takes a string, but this method coerces everything to symbols for safety.
This method also substitutes any dashes (-
) with underscores (_
), because that’s what Chef does under the hood. (See GitHub issue #254 for more background)
225 226 227 228 |
# File 'lib/chefspec/runner.rb', line 225 def step_into?(resource) key = resource_name(resource) Array([:step_into]).map(&method(:resource_name)).include?(key) end |
#to_s ⇒ String
This runner as a string.
may change between versions of this gem.
245 246 247 248 |
# File 'lib/chefspec/runner.rb', line 245 def to_s return "chef_run: #{node.run_list.to_s}" unless node.run_list.empty? 'chef_run' end |