Class: ChefSpec::SoloRunner
- Inherits:
-
Object
- Object
- ChefSpec::SoloRunner
- Includes:
- Normalize
- Defined in:
- lib/chefspec/solo_runner.rb
Direct Known Subclasses
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
- #run_context ⇒ Chef::RunContext readonly
Class Method Summary collapse
-
.converge(*recipe_names) ⇒ Object
Handy class method for just converging a runner if you do not care about initializing the runner with custom options.
Instance Method Summary collapse
-
#compiling? ⇒ true, false
Boolean method to determine the current phase of the Chef run (compiling or converging).
-
#converge(*recipe_names) {|node| ... } ⇒ ChefSpec::SoloRunner
Execute the given
run_liston the node, without actually converging the node. -
#dry_run? ⇒ true, false
Boolean method to determine if this Runner is in
dry_runmode. -
#find_resource(type, name, action = nil) ⇒ Chef::Resource?
Find the resource with the declared type and resource name, and optionally match a performed action.
-
#find_resources(type) ⇒ Array<Chef::Resource>
Find the resource with the declared type.
-
#initialize(options = {}) {|node| ... } ⇒ SoloRunner
constructor
Instantiate a new SoloRunner to run examples with.
-
#inspect ⇒ String
The runner as a String with helpful output.
-
#method_missing(m, *args, &block) ⇒ Object
Respond to custom matchers defined by the user.
-
#node ⇒ Chef::Node
The
Chef::Nodecorresponding to this Runner. -
#resource_collection ⇒ Hash<String, Chef::Resource>
The full collection of resources for this Runner.
-
#respond_to_missing?(m, include_private = false) ⇒ Boolean
Inform Ruby that we respond to methods that are defined as custom matchers.
-
#step_into?(resource) ⇒ true, false
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 = {}) {|node| ... } ⇒ SoloRunner
Instantiate a new SoloRunner to run examples with.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/chefspec/solo_runner.rb', line 64 def initialize( = {}) = () 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[:client_name] = nil Chef::Config[:node_name] = nil Chef::Config[:file_cache_path] = [:file_cache_path] || file_cache_path Chef::Config[:cookbook_path] = Array([:cookbook_path]) Chef::Config[:no_lazy_load] = true Chef::Config[:role_path] = Array([:role_path]) Chef::Config[:force_logger] = true Chef::Config[:solo] = true Chef::Config[:solo_legacy_mode] = true Chef::Config[:use_policyfile] = false Chef::Config[:environment_path] = [:environment_path] yield node if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Respond to custom matchers defined by the user.
265 266 267 268 269 270 271 |
# File 'lib/chefspec/solo_runner.rb', line 265 def method_missing(m, *args, &block) if block = ChefSpec.matchers[resource_name(m.to_sym)] instance_exec(args.first, &block) else super end end |
Instance Attribute Details
#options ⇒ Hash (readonly)
25 26 27 |
# File 'lib/chefspec/solo_runner.rb', line 25 def end |
#run_context ⇒ Chef::RunContext (readonly)
28 29 30 |
# File 'lib/chefspec/solo_runner.rb', line 28 def run_context @run_context end |
Class Method Details
.converge(*recipe_names) ⇒ Object
Handy class method for just converging a runner if you do not care about initializing the runner with custom options.
16 17 18 19 20 |
# File 'lib/chefspec/solo_runner.rb', line 16 def self.converge(*recipe_names) new.tap do |instance| instance.converge(*recipe_names) end end |
Instance Method Details
#compiling? ⇒ true, false
Boolean method to determine the current phase of the Chef run (compiling or converging)
209 210 211 |
# File 'lib/chefspec/solo_runner.rb', line 209 def compiling? !@converging end |
#converge(*recipe_names) {|node| ... } ⇒ ChefSpec::SoloRunner
Execute the given run_list on the node, without actually converging the node. Each time #converge is called, the run_list is reset to the new value (it is not additive).
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 132 133 |
# File 'lib/chefspec/solo_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 # Setup the run_context, rescuing the exception that happens when a # resource is not defined on a particular platform begin @run_context = client.setup_run_context rescue Chef::Exceptions::NoSuchResourceType => e raise Error::MayNeedToSpecifyPlatform.new(original_error: e.) end # Allow stubbing/mocking after the cookbook has been compiled but before the converge yield node if block_given? @converging = true converge_val = @client.converge(@run_context) if converge_val.is_a?(Exception) raise converge_val end self end |
#dry_run? ⇒ true, false
Boolean method to determine if this Runner is in dry_run mode.
237 238 239 |
# File 'lib/chefspec/solo_runner.rb', line 237 def dry_run? !![:dry_run] end |
#find_resource(type, name, action = nil) ⇒ Chef::Resource?
Find the resource with the declared type and resource name, and optionally match a performed action.
If multiples match it returns the last (which more or less matches the chef last-inserter-wins semantics)
178 179 180 181 182 |
# File 'lib/chefspec/solo_runner.rb', line 178 def find_resource(type, name, action = nil) resource_collection.all_resources.reverse_each.find do |resource| resource.declared_type == type.to_sym && (name === resource.identity || name === resource.name) && (action.nil? || resource.performed_action?(action)) end end |
#find_resources(type) ⇒ Array<Chef::Resource>
Find the resource with the declared type.
197 198 199 200 201 |
# File 'lib/chefspec/solo_runner.rb', line 197 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.
256 257 258 259 260 |
# File 'lib/chefspec/solo_runner.rb', line 256 def inspect "#<#{self.class.name}" \ " options: #{options.inspect}," \ " run_list: [#{node.run_list}]>" end |
#node ⇒ Chef::Node
The Chef::Node corresponding to this Runner.
140 141 142 143 144 145 146 147 |
# File 'lib/chefspec/solo_runner.rb', line 140 def node return @node if @node @node = client.build_node @node.instance_variable_set(:@runner, self) @node.class.send(:attr_reader, :runner) @node end |
#resource_collection ⇒ Hash<String, Chef::Resource>
The full collection of resources for this Runner.
154 155 156 |
# File 'lib/chefspec/solo_runner.rb', line 154 def resource_collection @resource_collection ||= @run_context.resource_collection end |
#respond_to_missing?(m, include_private = false) ⇒ Boolean
Inform Ruby that we respond to methods that are defined as custom matchers.
277 278 279 |
# File 'lib/chefspec/solo_runner.rb', line 277 def respond_to_missing?(m, include_private = false) ChefSpec.matchers.key?(m.to_sym) || super end |
#step_into?(resource) ⇒ true, false
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)
227 228 229 230 |
# File 'lib/chefspec/solo_runner.rb', line 227 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.
247 248 249 |
# File 'lib/chefspec/solo_runner.rb', line 247 def to_s "#<#{self.class.name} run_list: [#{node.run_list}]>" end |