user9753902
user9753902

Reputation:

Run nested cookbooks? | Chefspec

I am trying to run chefspec tests for cookbook Nginx and it is in the same directory as a bunch of others like bar. For my InSpec tests run kitchen test in ../../Nginx which has my .kitchen.yml file.

Reason I mention the bar cookbook is that it is the first one in the run list in my .kitchen.yml. I run bar and then it calls on a few others too as well as Nginx is further along in the cookbook.

Hope that made some sense.

Here is my error message in the terminal

[2018-05-15T12:45:57-04:00] WARN: Port 8889 not available

nginx::default
  Nginx
    Converges successfully (FAILED - 1)

Failures:

  1) nginx::default Nginx Converges successfully
     Failure/Error: expect { chef_run }.to_not raise_error

       expected no Exception, got #<NoMethodError: undefined method `[]' for nil:NilClass> with backtrace:
         # /var/folders/pj/c4gpnwws6n7gb7x_plpv9hzc0000gp/T/chefspec20180515-32598-1um8wa6file_cache_path/cookbooks/nginx/recipes/_configure.rb:23:in `from_file'
         # /var/folders/pj/c4gpnwws6n7gb7x_plpv9hzc0000gp/T/chefspec20180515-32598-1um8wa6file_cache_path/cookbooks/nginx/recipes/default.rb:324:in `from_file'
         # ./spec/unit/recipes/default_spec.rb:10:in `block (3 levels) in <top (required)>'
         # ./spec/unit/recipes/default_spec.rb:13:in `block (4 levels) in <top (required)>'
         # ./spec/unit/recipes/default_spec.rb:13:in `block (3 levels) in <top (required)>'
     # ./spec/unit/recipes/default_spec.rb:13:in `block (3 levels) in <top (required)>'

Finished in 4.08 seconds (files took 1.98 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/unit/recipes/default_spec.rb:12 # nginx::default Nginx Converges successfully

My command I am using

Josh:nginx josh$ chef exec rspec spec/unit/recipes/default_spec.rb --format d

My test file

Super basic just till it runs then I can use my full test list.

require 'chefspec'

describe 'nginx::default' do
  context 'Nginx' do
    let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04').converge(described_recipe) }

    it 'Converges successfully' do
      expect { chef_run }.to_not raise_error
    end


  end #end context Nginx
end # end describe nginx::default

Upvotes: 0

Views: 440

Answers (1)

EugeneRomero
EugeneRomero

Reputation: 596

I suggest you rewrite your post to actually add a question... I was unable to find any, so I'm not sure what you're asking about :) So I will answer the two most probable things you might be asking:

1) How to run all tests inside a particular cookbook?

This is quite simple, you just need to modify your rspec command a bit, like so:

rspec ./spec/

you can of course specify a different folder. I believe that by default, rspec will run any files it finds in that folder which end in _spec.rb (subfolders too).

2) What is causing that <NoMethodError: undefined method '[]' for nil:NilClass>?

This is a Ruby error which you will get if you try to use a variable somewhere, but that variable has not been set. Check your recipe/test and make sure you don't have a chef attribute being used somewhere without being set. The error trace might help figure out where this is.

I hope this helps!

Upvotes: 0

Related Questions