Questions tagged [chefspec]

ChefSpec is a unit testing framework for testing Chef cookbooks. ChefSpec makes it easy to write examples and get fast feedback on cookbook changes without the need for virtual machines or cloud servers.

ChefSpec runs your cookbook locally using Chef Solo without actually converging a node. This has two primary benefits:

  • It's really fast!
  • Your tests can vary node attributes, operating systems, and search results to assert behavior under varying conditions.

You can install ChefSpec from Rubygems:

gem install chefspec

Or add it to your Gemfile

gem 'chefspec'

Additional Resources/Links

135 questions
3
votes
2 answers

How can I use ChefSpec during refactoring of environments/roles/nodes

I have a Chef repo that needs some cleanup. Configuration data is scattered around into files in nodes, environments and roles folders. My idea is to use ChefSpec to test against regressions during restructuring the data. As only roles appear to be…
2
votes
2 answers

Chefspec Stubbing out Win32::Service on Linux

Our cookbook is used on both Linux and Windows machines, and when it runs/converges, the code runs perfectly on both types of systems. However the rake unit test fails on Linux. In my recipe I have: service 'MyService' do action %i[enable start] …
2
votes
1 answer

How do you mock a function call in chefspec for an object instance extended with another class

I am new to unit testing and chefspec. I am trying to mock/intercept a function call in a recipe from a dependent library Library module Helper def do_something_useful return "http://example.com/file.txt" end end Recipe remote_file…
mcede
  • 117
  • 5
2
votes
1 answer

ChefSpec - Stub method inside resource

Is there a way to stub a method call with a test value? For example, I want get_folder_name to return 'test' during my ChefSpec test directory 'Log_Folder' do def get_folder_name 'c:\temp\folder' end action :create path…
chief7
  • 14,263
  • 14
  • 47
  • 80
2
votes
1 answer

Assert the return value of a method in Ruby (Chef Recipe) using Rspec

I am very new to Rspec and was trying to test a Ruby script (Its a Chef recipe) with the following skeleton. def foo1 # do stuff list_of_names # returns a list end def foo2(list_of_names) # do stuff counter = [...] . . …
ka2010
  • 98
  • 1
  • 9
2
votes
0 answers

How to coverage with chefspecs resources programatically added to run_context

I have following code in my recipe, which runs bash script that returns users (except root). 1 user per line. ruby_block "Delete users" do action :run block do users = Mixlib::ShellOut.new("whatever command with lines containing…
Lukino
  • 1,407
  • 13
  • 14
2
votes
2 answers

chefspec testing resource with loops

If I have a chef recipe with the following attribute node.default["cookbook"]["directory"] = %w(/mnt/directory1 /mnt/directory2) node["cookbook"]["directory"].each do |dir| directory dir do owner "user" mode 0644" …
2
votes
2 answers

Testing library Chef cookbooks

Summary: How can I best test a library cookbook meant for inclusion from another cookbook? Details: I am writing a cookbook that contains an LWRP and a minimal default recipe that sets some attributes based on where it is run. However the new…
Friedrich 'Fred' Clausen
  • 3,321
  • 8
  • 39
  • 70
2
votes
1 answer

Is it possible (or even recommended) to mock a Ruby require statement in ChefSpec?

I've been writing ChefSpec unit test suites for the past couple of weeks and have managed to do quite a bit with it, but I have found a scenario that got me stumped. I have a recipe that includes the 'aws' cookbook's default recipe, which in turn…
Rafael Fonseca
  • 161
  • 1
  • 13
2
votes
2 answers

List all the declared packages in chef

I'm working on a infrastructure where some servers don't have access to the internet, so I have to push the packages to the local repo before declaring them to be installed on Chef. However we've been on a situation where Chef failed to install a…
Bamdad Dashtban
  • 354
  • 3
  • 17
2
votes
2 answers

Stub the include_recipe call to take no action but still count the recipe as included

The default recipe for my cookbook simply includes several other recipes. I know that I can test that the appropriate recipes are included using: expect(chef_run).to include_recipe 'cookbook::recipe_name but this doesn't work when I've stubbed the…
Iain_b
  • 1,043
  • 5
  • 13
1
vote
1 answer

Accessing Chef::Config during chefspec run / variables outside action

Say I have this spec file: require 'chefspec' describe 'dummy::dummy' do step_into 'dummy_dummy' platform 'ubuntu' context 'creates directory' do recipe do dummy_dummy 'foo' end it { is_expected.to…
Sören
  • 1,803
  • 2
  • 16
  • 23
1
vote
1 answer

ChefSpec: How to mock node.chef_environment?

Does anyone know how to mock the node attribute "node.chef_environment" in chefspec? template 'my_script.sh' do source 'my_script.erb' variables( environment: node.chef_environment ) end Unfortunately, mocking via default_attributes does not…
user5580578
  • 1,134
  • 1
  • 12
  • 28
1
vote
1 answer

Chef workstation: Can not find omnibus installation directory for Chef

I just installed chef-workstation on mac, I also have rvm installed on my machine. The chef-workstation is successfully installed but when I try to run chef exec command it fails with below error - Traceback (most recent call last): 20: from…
Tushar H
  • 755
  • 11
  • 29
1
vote
1 answer

Stubbing chef_environment variable in spec tests

Having an issue with stabbing of the chef_env variable in my spec tests. Part of recipe: recipe.rb example_credentials = data_bag_item(:credentials, 'example') template '/etc/nginx/sites-available/example.conf' do source…
FidleG
  • 23
  • 6
1
2
3
8 9