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

Chefspec: override attribute doesn't work

I'm trying to check my spec test with bundle exec rspec for the following test require 'spec_helper' describe 'my_recipe::default' do windows_platforms = { windows: %w(2008R2 2012 2012R2 2016 2019) } windows_platforms.each do |platform,…
pmamueng
  • 11
  • 1
1
vote
1 answer

Chef Library Testing with shell_out

I am writing a custom resource for Chef. The resource is used for setting sysctl values. I am basically using the Chef sysctl code and putting some limitations around it. We don't trust all the users at the company :P I am trying to put most of…
Matt
  • 77
  • 4
1
vote
3 answers

undefined local variable in chefspec on method included from gem

I'm including a custom gem in a chef cookbook. No matter what I've tried I'm get similar errors to undefined local variable or method `host' I've tried many different variations to this. allow(HostOpsCookbook).to…
debow
  • 73
  • 7
1
vote
1 answer

Chefspec: does chefspec load all the recipes, libraries, custom resources etc all initially

In my cookbook(its a legacy code), I have around 20+ recipes, around 15 modules in libraries directory, some custom resources. I am trying to add chefspecs for recipes, I have created package_spec.rb spec file for recipe package (I am using chef12,…
Tushar H
  • 755
  • 11
  • 29
1
vote
0 answers

Mocking library function is not working properly under context block?

I have a ChefSpec test below, I am not sure if it is a bug in ChefSpec or not. However, whenever I mocked a library function under context block, the mocking library did not mock the value for the function and caused the failure of the test. The…
SmNg1028
  • 43
  • 7
1
vote
1 answer

How to stub_command in ChefSpec?

I have this condition in my recipe: install_action = (::Win32::Service.exists?(windows_service['name']) ? :configure : :create) and a ChefSpec for that in spec file: #1: not working allow_any_instance_of(Win32::Service) .to…
SmNg1028
  • 43
  • 7
1
vote
1 answer

ChefSpec unable to find the git installed

When I am running the ChefSpec, getting the below error, though I have the git installed and the required cookbook is getting cloned before reaching this step. As I understand from the below error that, BerkShelf is unable to find the Git…
1
vote
1 answer

stub_command with node attribute variables

I've google around, it appears this can be done, but I'm just doing it wrong apparently. So I'm trying to run this snippet of powershell_script: powershell_script 'Unzip' do code <<-EOH Expand-Archive -Path 'E:\\apache-tomee-1.7.4-plus.zip'…
1
vote
1 answer

Chefspec - writing unit tests for execute resource that include cwd,command and environment

I'm trying to figure out how I can write my chefspec unit tests that will check every line on the below piece of code Execute 'download gz' do cwd 'my/working/dir' environment ('environ' => node[:environ]) command 'some commands…
Kgothatso Kurt
  • 951
  • 6
  • 5
1
vote
2 answers

How do I test whether an apt_update call is triggered or not?

While building out some recipes we discovered that triggering an apt_update as part of the flow can greatly slow things down if the system is already in a good state but the update runs anyways. To work around this we made an apt_update call with…
dragon788
  • 3,583
  • 1
  • 40
  • 49
1
vote
0 answers

ChefSpec passed local environment but failed on Jenkins

I'm running a cookbook and when I run it on my local machine, all of my ChefSpec tests passed. However, when Jenkins ran them, it failed some of my tests. I checked the version of Chef running on both of my local machine and Jenkins server and they…
nguyli03
  • 41
  • 1
  • 11
1
vote
1 answer

Testing Chef cookbook failure

I'm writing a cookbook with custom resource that (among other things) validates SSH keys through ssh-keygen. I need to test scenario in which user feeds in invalid input and resource should raise according exception, so i'm searching for a way to…
Etki
  • 2,042
  • 2
  • 17
  • 40
1
vote
1 answer

Chef | Writting a Spec for Directory Rights assignment

I'm new to Chef and ruby and working through a training project and I've gotten hung up on something which is probably very simple but I have exhausted my ability to google search and/or beat into submission. I have the following…
Jeff W
  • 13
  • 4
1
vote
1 answer

Test a block that has no action and receives a notification?

So, I am trying to add some unit test to my recipe. Testing something that has an action is easy. But when your action is :nothing because it gets triggered by a notification then it sucks. The following block gets called with: notifies :create,…
Adrien
  • 55
  • 1
  • 4
1
vote
1 answer

Mock Chef::ReservedNames::Win32::Version.new in Chef unit/rspec test? [continued]

Assuming, I have the following recipe: install_iis: require 'chef/win32/version' windows_version = Chef::ReservedNames::Win32::Version.new node.set['iis']['components'] = [ 'IIS-HttpErrors', 'IIS-HttpRedirect', 'IIS-HttpLogging', …
Matthew
  • 146
  • 2
  • 9
1 2
3
8 9