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_list` on the node, without actually converging the node.
-
#dry_run? ⇒ true, false
Boolean method to determine if this Runner is in ‘dry_run` mode.
-
#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 = {}) {|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 |
# 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[: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.
264 265 266 267 268 269 270 |
# File 'lib/chefspec/solo_runner.rb', line 264 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)
208 209 210 |
# File 'lib/chefspec/solo_runner.rb', line 208 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).
106 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 |
# File 'lib/chefspec/solo_runner.rb', line 106 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.
236 237 238 |
# File 'lib/chefspec/solo_runner.rb', line 236 def dry_run? !![:dry_run] end |
#find_resource(type, name) ⇒ Chef::Resource?
Find the resource with the declared type and resource name.
173 174 175 176 177 178 179 180 181 |
# File 'lib/chefspec/solo_runner.rb', line 173 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.
196 197 198 199 200 |
# File 'lib/chefspec/solo_runner.rb', line 196 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 258 259 |
# File 'lib/chefspec/solo_runner.rb', line 255 def inspect "#<#{self.class.name}" \ " options: #{options.inspect}," \ " run_list: [#{node.run_list}]>" end |
#node ⇒ Chef::Node
The Chef::Node corresponding to this Runner.
139 140 141 142 143 144 145 146 |
# File 'lib/chefspec/solo_runner.rb', line 139 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.
153 154 155 |
# File 'lib/chefspec/solo_runner.rb', line 153 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.
276 277 278 |
# File 'lib/chefspec/solo_runner.rb', line 276 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)
226 227 228 229 |
# File 'lib/chefspec/solo_runner.rb', line 226 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.
246 247 248 |
# File 'lib/chefspec/solo_runner.rb', line 246 def to_s "#<#{self.class.name} run_list: [#{node.run_list}]>" end |