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
0
votes
1 answer

How to run Chefspec on a cookbook that depends on another cookbook?

I'm re-learning Chef (I did it for 2+ years in a previous life), and to help I am just installing a LAMP application on a CentOS 7 Vagrant box. To keep it simple, all my cookbooks are in the same /path_to/cookbooks directory, that…
Chris F
  • 14,337
  • 30
  • 94
  • 192
0
votes
1 answer

ChefSpec test if python modules were installed

I found a way in InSpec to test whether or not python modules have been installed with chef, but I'm using ChefSpec and I can't find an alternative. Does anybody know if there's a way to check if a certain pip module has been installed with…
mjkaufer
  • 4,047
  • 5
  • 27
  • 55
0
votes
1 answer

How to load attributes from a file to chefspec node

I have the following layout: attributes/default.rb recipes/my_recipe.rb spec/unit/recipes/my_recipe_spec.rb In the attributes files I have a lot of common settings likes default['framework']['folder']['lib'] = '/usr/lib/fwrk' I would like to use…
mishka
  • 2,027
  • 2
  • 20
  • 30
0
votes
1 answer

chefspec: render_file fails to check content of template that is removed in another context

Consider the following recipe: recipe_name = 'mongodb' service_name = if node['szdigi'][recipe_name]['version'].to_f < 2.6 'mongodb' else 'mongod' end conffile =…
trish
  • 1
  • 3
0
votes
1 answer

chefspec: What is the difference between predicate matchers and attributes?

For example, to check that a directory has been created with the user "nobody" I can use either of two methods: "Assert that a directory was createed with predicate matchers" expect(chef_run).to create_directory('/tmp').with_user('nobody') "Assert…
user3481957
  • 151
  • 1
  • 1
  • 10
0
votes
1 answer

Running chefspec on recipes that have dependencies on other cookbooks / libraries erroring

I'm working through a few chefspec tutorials and have come up against an issue. My codebase has a two cookbooks in, with cookbookB being a collection of common methods/libraries that are used by other cookbooks. CookbookA depends on CookbookB and…
null
  • 3,469
  • 7
  • 41
  • 90
0
votes
1 answer

Unable to run chefspec tests having databags

I am trying to run unit tests using chefspec. I added databags to my recipe. Without databags, chefspec test running fine. After adding databags chefspec showing below error: 1) database::prerequisites installs a package Failure/Error:…
Naveen kumar
  • 1,585
  • 8
  • 8
0
votes
2 answers

How to stub two level of json data bag in chef spec

I'm trying to write the unit test for chef and stubbing the encrypted data bag as below. Recipe part variables(car_model: Chef::EncryptedDataBagItem.load('databagname', node.environment, key_name)['cardetails']['car_model']) Doing stub in below…
Mahattam
  • 5,405
  • 4
  • 23
  • 33
0
votes
2 answers

How to write ChefSpec Unit Test for ruby_block resource?

How to write the ChefSpec Unit tests for ruby_block? What if the local variables are declared in the recipe? How will it be handled? Here is the code of a recipe: package 'autofs' do action :install end src = '/etc/ssh/sshd_config' unless…
Jayu
  • 189
  • 1
  • 1
  • 8
0
votes
2 answers

unable to solve rspec issue for a remote file resource

I tried to write rspec for one of my remote_file resource. But I am unsuccessful to make it. My concept is using remote_file it should download a remote file which is a zip file. What happen is rspec is expecting more after It downloaded remotely.…
user7486728
0
votes
1 answer

How to write spec for a Directories created recursively, in chef

eg. Creating Directories using directory resource in chef. directory '/app/my_app/log' do owner 'myuser' group 'myuser' recursively true end Now writing spec for this resource. it 'creates directory /app' do expect(chef_run).to…
srajappa
  • 484
  • 8
  • 19
0
votes
2 answers

How can I check chefspec/unit test for following code of my recipe

I am trying to write the chefspec/unit test for my recipe. I am facing problem. I need to write the unit test case for below code. If I comment the last statement of my code, the test execute successfully but I have to catch that statement too as…
0
votes
1 answer

Chefspec mocking issue

I have two methods without class, they both have same code but return object is different. def get_load_balancer(network_client, resource_group_name, load_balancer_name) promise = network_client.load_balancers.get(resource_group_name,…
0
votes
1 answer

How do you unit test a custom chef resource in isolation with ChefSpec?

So I'm trying to create a library of reusable custom resources. This video explains how to do that but it doesn't address testing the resource in isolation. I know you can test a resource through a recipe and a converge, but I don't want to write a…
0
votes
1 answer

Chefspec not working when trying to install multiple window feature

I have a recipe to install components for IIS. Since there's a lot, it's an array that feeds into a windows_feature install block. Like so: strings.each do |st| windows_feature st do guard_interpreter :powershell_script …
user3066571
  • 1,381
  • 4
  • 14
  • 37
1 2 3
8 9