Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChefSpec mysteriously fails on custom resource when configuring the platform using RSpec.configure #953

Open
simon-baatz opened this issue Feb 21, 2019 · 3 comments

Comments

@simon-baatz
Copy link

For a new custom resource, we tried to use the new syntax as given in the README.md. The cookbook has many "old" style ChefSpec tests and configuration.

As the platform and version is configured globally in the cookbook using RSpec.configure, we did not include the platform method in our spec file. ChefSpec does not complain in this case, but can't find the custom resource (apparently it can find builtin resources, though).

The example below uses the custom resource from the example in README.md.

  • Command: rspec spec/greet_spec_old_platform.rb

Content of greet_spec_old_platform.rb is:

require 'chefspec'

# Old style generic platform configuration (usually resides in
# spec_helper)
RSpec.configure do |config|
  config.platform = 'ubuntu'
end

describe 'mycookbook_greet' do

  step_into :mycookbook_greet

  context 'with the default greeting' do
    recipe do
      mycookbook_greet 'test'
    end

    it { is_expected.to write_log('Hello world') }
  end
end
  • OS: CentOS 7
  • ChefDK: 3.7.24 (also tried with chefspec gem built from master)
  • Output:
F

Failures:

  1) mycookbook_greet with the default greeting 
     Failure/Error: mycookbook_greet 'test'
     
     NoMethodError:
       undefined method `mycookbook_greet' for cookbook: mycookbook, recipe: _test :Chef::Recipe
     # ./spec/greet_spec_old_platform.rb:15:in `block (3 levels) in <top (required)>'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/solo_runner.rb:140:in `instance_exec'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/solo_runner.rb:140:in `block in converge_block'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/solo_runner.rb:119:in `converge'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/solo_runner.rb:138:in `converge_block'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/api/core.rb:135:in `block in recipe'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/api/core.rb:208:in `block in included'
     # ./spec/greet_spec_old_platform.rb:18:in `block (3 levels) in <top (required)>'
     # /home/baatz/.chefdk/gem/ruby/2.5.0/gems/chefspec-7.3.4/lib/chefspec/api/core.rb:18:in `block in <module:Core>'

Finished in 0.068 seconds (files took 1.32 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/greet_spec_old_platform.rb:18 # mycookbook_greet with the default greeting 

Adding platform 'ubuntu' to the describe block fixes the problem.

  • Expected behavior:

ChefSpec should work in this case or complain that it needs platform.

@ccrebolder
Copy link

Thanks, this took me forever to figure out. The error message sent me a long way down the wrong path.

@bastianschwarz
Copy link

Not sure if I should jump around in joy or break random things in my living room since I just spent 2 hours debugging my tests ... should have checked here sooner. @simon-baatz thanks, you just saved what's left of my sanity ;)

Also:

The issue only occured after I deleted the recipes and the tests for them so I had ONLY the custom resource left. Everything worked (that is the tests for the resource too) when the recipes were tested before without defining the platform in the describes block

@dwmarshall
Copy link
Contributor

Interesting wrinkle. Suppose you have this in spec_helper.rb:

RSpec.configure do |config|
  config.platform = 'redhat'
  config.version = '7.8'
end

and then you use platform: 'ubuntu' as in the greet_spec.rb example:

You'll get:

    Fauxhai::Exception::InvalidPlatform:
       Could not find platform 'ubuntu/7.8' on the local disk and an HTTP error was encountered when fetching from Github. A list of available platforms is available at https://github.com/chefspec/fauxhai/blob/master/PLATFORMS.md```

So it would seem that the run from the bare recipe in the spec file gets some information from the RSpec configuration but not enough to avoid using `platform` in the test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants