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
1
vote
1 answer

Does it make senses to test a call to a chef definition with chefspec?

I have a recipe that depends on the apache2 cookbook to, among other things, enable a module using the definition: apache_module 'passenger' Is there a way to test that call with chefspec ? According to "Chef libraries or definitions?", a…
patatepartie
  • 178
  • 1
  • 6
1
vote
1 answer

Speeding Up Chefspec Run

I have a lot of cookbooks and they are heavily tested with ChefSpec. I have more than 800 specs and it has become a problem to run them every time before committing code, because on my machine it takes about 20 minutes to run them. That makes…
Draco Ater
  • 20,820
  • 8
  • 62
  • 86
0
votes
0 answers

chefspec failed with "ffi-libarchive could not be loaded"

I'm new to chef. Recently, I'm studying how to use chefspec to test recipe. But I always failed with "ffi-libarchive could not be loaded" error. Here's my steps. I use gem to install chefspec -- gem install 'chefspec' It's successfully…
EisenWang
  • 189
  • 1
  • 10
0
votes
1 answer

How to write chef spec for following cookbook?

This is my recipe.rb: require 'mixlib/shellout' require "pry" find = Mixlib::ShellOut.new("echo hostname -I") find.run_command.stdout What should I write it in my spec?
user7208575
0
votes
1 answer

ChefSpec Ruby: How to skip all includes (allow_any_instance_of) but one?

I would like to test whether a certain include which is subject to conditions is being executed. All other includes should not be taken into account in the test and therefore should not be included. My file that I want to test looks like…
user5580578
  • 1,134
  • 1
  • 12
  • 28
0
votes
0 answers

Ruby: Return hash value within stub

How do I correctly return the hash object from the stub so that I don't always get the error message "NoMethoderror: undefined method 'each' for nil: NilClass"? # ruby code to test env_json =…
user5580578
  • 1,134
  • 1
  • 12
  • 28
0
votes
1 answer

In chefspec is it possible to write a method once and call that method in other spec files?

I am trying to write a method test_run(first_param,second_param) in one spec file program_spec.rb and I want to call this method test_run in another spec file second_program_spec.rb. How do I call the method which is declared in program_spec.rb to…
blueowl
  • 35
  • 6
0
votes
1 answer

How can I test "super" in a Chef resource?

Is there an approach similar to step_into to encourage Chefspec to follow a resource's super call into its parent class? Or can you suggest an approach to test that the super call is followed (or not followed) as expected, to perform the appropriate…
Sarai
  • 58
  • 1
  • 5
0
votes
0 answers

ChefSpec - Can i mock UserID (owner)? i get Chef::Exceptions::UserIDNotFound

i'm testing a recipe that create few file with owner "dev" and getting this exception: Chef::Exceptions::UserIDNotFound -------------------------------- cannot determine user id for 'dev', does the user exist on this system? now, in another recipe…
danfromisrael
  • 2,982
  • 3
  • 30
  • 40
0
votes
1 answer

Testing resource cookbook with Policyfiles & ChefSpec

This problem arises when you: Write a library cookbook (i.e. it has no recipes apart from a no-op default.rb) Create a test cookbook under test/fixtures/cookbooks/ to exercise the library. That is…
Hedgehog
  • 5,487
  • 4
  • 36
  • 43
0
votes
0 answers

How do I write chefspec code for seven_zip_archive resource?

I have used a resource seven_zip_archive to unzip files. I need to write chefspec code for this resource. I have not found any solution yet. I tried something like:- it ('updates 7zip') do expect(chef_run).to…
0
votes
1 answer

How to add a partiular version to the roles file in chef

I have the created a role file with the below contents. name "Windows_role_150719" description "Windows_role_150719" run_list "recipe[deep-sec1]", "recipe[deep-sec1::install]", "recipe[deep-sec1::activate]" override_attributes({ }) The…
anish anil
  • 2,299
  • 7
  • 21
  • 41
0
votes
1 answer

Chefspec how to unit test for update_apt_update without update

I have the following chef code: apt_update 'Ubuntu apt repo update' do subscribes :nothing, 'apt_repository[some-repo]', :immediately end I'm trying to test with this code: it 'updates apt repo' do expect(chef_run).to update_apt_update('Ubuntu…
ClickThisNick
  • 5,110
  • 9
  • 44
  • 69
0
votes
1 answer

ChefSpec testing directory exists

I am trying to write a ChefSpec test to check that the recipe creates a directory only if it doesn't exist. I get my first test of "creating directory" pass but the second test fails. The recipe is down below. Can someone please help in getting the…
janedoegcp
  • 27
  • 5
0
votes
1 answer

Chef colored output in Jenkins

I'm running unit tests in Jenkins with chef. chef exec rake unit Everything is working correctly except there is not any colored output. I have enabled AnsiColor Plugin Jenkins plugin and am getting colored output for non chef tests just fine. Are…
1 2 3
8 9