Module: Halite::SpecHelper
- Extended by:
- RSpec::SharedContext
- Defined in:
- lib/halite/spec_helper.rb,
lib/halite/spec_helper/runner.rb,
lib/halite/spec_helper/patcher.rb
Overview
A helper module for RSpec tests of resource-based cookbooks.
Defined Under Namespace
Classes: Runner
Instance Attribute Summary collapse
-
#chef_run ⇒ ChefSpec::SoloRunner
readonly
Trigger a Chef converge.
-
#chef_runner ⇒ ChefSpec::SoloRunner
readonly
ChefSpec runner for this example.
-
#chefspec_options ⇒ Hash<Symbol, Object>
readonly
Options hash for the ChefSpec runner instance.
-
#default_attributes ⇒ Hash
readonly
Hash to use as default-level node attributes for this example.
-
#normal_attributes ⇒ Hash
readonly
Hash to use as normal-level node attributes for this example.
-
#override_attributes ⇒ Hash
readonly
Hash to use as override-level node attributes for this example.
Class Method Summary collapse
-
.provider(name, auto: true, rspec: true, parent: Chef::Provider, defined_at: , &block) ⇒ Object
Define a provider class for use in an example group.
-
.recipe(*recipe_names, subject: true, &block) ⇒ Object
Define a recipe to be run via ChefSpec and used as the subject of this example group.
-
.resource(name, auto: true, parent: Chef::Resource, step_into: true, unwrap_notifying_block: true, defined_at: , &block) ⇒ Object
Define a resource class for use in an example group.
-
.step_into(name, resource_name = nil, unwrap_notifying_block: true) ⇒ Object
Configure ChefSpec to step in to a resource/provider.
Instance Method Summary collapse
-
#run_chef ⇒ Object
An alias for slightly more semantic meaning, just forces the lazy #subject to run.
Instance Attribute Details
#chef_run ⇒ ChefSpec::SoloRunner (readonly)
Trigger a Chef converge. By default no resources are converged. This is normally overwritten by the #recipe helper.
111 |
# File 'lib/halite/spec_helper.rb', line 111 let(:chef_run) { chef_runner.converge() } |
#chef_runner ⇒ ChefSpec::SoloRunner (readonly)
ChefSpec runner for this example.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/halite/spec_helper.rb', line 95 let(:chef_runner) do Halite::SpecHelper::Runner.new( { step_into: step_into, default_attributes: default_attributes, normal_attributes: normal_attributes, override_attributes: override_attributes, halite_gemspec: halite_gemspec, }.merge() ) end |
#chefspec_options ⇒ Hash<Symbol, Object> (readonly)
Options hash for the ChefSpec runner instance.
91 |
# File 'lib/halite/spec_helper.rb', line 91 let(:chefspec_options) { Hash.new } |
#default_attributes ⇒ Hash (readonly)
Hash to use as default-level node attributes for this example.
73 |
# File 'lib/halite/spec_helper.rb', line 73 let(:default_attributes) { Hash.new } |
#normal_attributes ⇒ Hash (readonly)
Hash to use as normal-level node attributes for this example.
78 |
# File 'lib/halite/spec_helper.rb', line 78 let(:normal_attributes) { Hash.new } |
#override_attributes ⇒ Hash (readonly)
Hash to use as override-level node attributes for this example.
83 |
# File 'lib/halite/spec_helper.rb', line 83 let(:override_attributes) { Hash.new } |
Class Method Details
.provider(name, auto: true, rspec: true, parent: Chef::Provider, defined_at: , &block) ⇒ Object
Define a provider class for use in an example group. By default a :run action will be created, load_current_resource will be defined as a no-op, and the RSpec matchers will be available inside the provider.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/halite/spec_helper.rb', line 367 def provider(name, auto: true, rspec: true, parent: Chef::Provider, defined_at: caller[0], &block) parent = providers[parent] if parent.is_a?(Symbol) raise Halite::Error.new("Parent class for #{name} is not a class: #{[:parent].inspect}") unless parent.is_a?(Class) # Pull out the example group for use in the class. example_group = self # Create the provider class. provider_class = Class.new(parent) do # Pull in RSpec expectations. if rspec include RSpec::Matchers include RSpec::Mocks::ExampleMethods end if auto # Default blank impl to avoid error. def load_current_resource end # Blank action because I do that so much. def action_run end end # Make the anonymous class pretend to have a name. define_singleton_method(:name) do 'Chef::Provider::' + Chef::Mixin::ConvertToClassName.convert_to_class_name(name.to_s) end # Helper for debugging, shows where the class was defined. define_singleton_method(:halite_defined_at) do defined_at end # Create magic delegators for various metadata. { example_group: example_group, described_class: example_group.[:described_class], }.each do |key, value| define_method(key) { value } define_singleton_method(key) { value } end # Evaluate the class body. class_exec(&block) if block end # Clean up any global registration that happens on class compile. Patcher.post_create_cleanup(name, provider_class) # Store for use up with the parent system halite_helpers[:providers][name.to_sym] = provider_class around do |ex| if provider(name) == provider_class # We haven't been overridden from a nested scope. Patcher.patch(name, provider_class, Chef::Provider) { ex.run } else ex.run end end end |
.recipe(*recipe_names, subject: true, &block) ⇒ Object
Define a recipe to be run via ChefSpec and used as the subject of this example group. You can specify either a single recipe block or one-or-more recipe names.
168 169 170 171 172 |
# File 'lib/halite/spec_helper.rb', line 168 def recipe(*recipe_names, subject: true, &block) # Keep the actual logic in a let in case I want to define the subject as something else let(:chef_run) { chef_runner.converge(*recipe_names, &block) } subject { chef_run } if subject end |
.resource(name, auto: true, parent: Chef::Resource, step_into: true, unwrap_notifying_block: true, defined_at: , &block) ⇒ Object
Define a resource class for use in an example group. By default the :run action will be set as the default.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/halite/spec_helper.rb', line 264 def resource(name, auto: true, parent: Chef::Resource, step_into: true, unwrap_notifying_block: true, defined_at: caller[0], &block) parent = resources[parent] if parent.is_a?(Symbol) raise Halite::Error.new("Parent class for #{name} is not a class: #{parent.inspect}") unless parent.is_a?(Class) # Pull out the example group for use in the class. example_group = self # Create the resource class. resource_class = Class.new(parent) do # Make the anonymous class pretend to have a name. define_singleton_method(:name) do 'Chef::Resource::' + Chef::Mixin::ConvertToClassName.convert_to_class_name(name.to_s) end # Helper for debugging, shows where the class was defined. define_singleton_method(:halite_defined_at) do defined_at end # Create magic delegators for various metadata. { example_group: example_group, described_class: example_group.[:described_class], }.each do |key, value| define_method(key) { value } define_singleton_method(key) { value } end # Evaluate the class body. class_exec(&block) if block # Optional initialization steps. Disable for special unicorn tests. if auto # Fill in a :run action by default. old_init = instance_method(:initialize) define_method(:initialize) do |*args| old_init.bind(self).call(*args) # Fill in the resource name because I know it, but don't # overwrite because a parent might have done this already. @resource_name = name.to_sym # ChefSpec doesn't seem to work well with action :nothing if Array(@action) == [:nothing] @action = :run @allowed_actions |= [:run] end if defined?(self.class.default_action) && Array(self.class.default_action) == [:nothing] self.class.default_action(:run) end end end end # Try to set the resource name for 12.4+. if defined?(resource_class.resource_name) resource_class.resource_name(name) end # Clean up any global registration that happens on class compile. Patcher.post_create_cleanup(name, resource_class) # Store for use up with the parent system halite_helpers[:resources][name.to_sym] = resource_class # Automatically step in to our new resource step_into(resource_class, name, unwrap_notifying_block: ) if step_into around do |ex| if resource(name) == resource_class # We haven't been overridden from a nested scope. Patcher.patch(name, resource_class, Chef::Resource) { ex.run } else ex.run end end end |
.step_into(name) ⇒ Object .step_into(resource, resource_name) ⇒ Object
Configure ChefSpec to step in to a resource/provider. This will also automatically create ChefSpec matchers for the resource.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/halite/spec_helper.rb', line 190 def step_into(name, resource_name=nil, unwrap_notifying_block: true) resource_class = if name.is_a?(Class) name elsif resources[name.to_sym] # Handle cases where the resource has defined via a helper with # step_into:false but a nested example wants to step in. resources[name.to_sym] else # Won't see platform/os specific resources but not sure how to fix # that. I need the class here for the matcher creation below. Chef::Resource.resource_for_node(name.to_sym, Chef::Node.new) end resource_name ||= if resource_class.respond_to?(:resource_name) resource_class.resource_name else Chef::Mixin::ConvertToClassName.convert_to_snake_case(resource_class.name.split('::').last) end # Add a resource-level matcher to ChefSpec. ChefSpec.define_matcher(resource_name) # Figure out the available actions and create ChefSpec matchers. resource_class.new(nil, nil).allowed_actions.each do |action| define_method("#{action}_#{resource_name}") do |instance_name| ChefSpec::Matchers::ResourceMatcher.new(resource_name, action, instance_name) end end # Patch notifying_block from Poise::Provider to just run directly. # This is not a great solution but it is better than nothing for right # now. In the future this should maybe do an internal converge but using # ChefSpec somehow? if old_provider_for_action = resource_class.instance_method(:provider_for_action) resource_class.send(:define_method, :provider_for_action) do |*args| old_provider_for_action.bind(self).call(*args).tap do |provider| if provider.respond_to?(:notifying_block, true) provider.define_singleton_method(:notifying_block) do |&block| block.call end end end end end # Add to the let variable passed in to ChefSpec. before { step_into << resource_name } end |
Instance Method Details
#run_chef ⇒ Object
An alias for slightly more semantic meaning, just forces the lazy #subject to run.
122 123 124 |
# File 'lib/halite/spec_helper.rb', line 122 def run_chef chef_run end |