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
12
votes
2 answers

ChefSpec and Test Kitchen

I am investigating if ChefSpec report (with coverage) and Test Kitchen report can be generated as junit format so they can be parsed then by Jenkins to publish results. Or if there is some other friendly way those results could be published by…
user2620348
  • 309
  • 4
  • 15
7
votes
1 answer

NoMethodError: undefined method `run_list_for' when testing role with ChefSpec

I'm trying to test a role with ChefSpec. I would rather not use Chef Zero (by requiring 'chefspec/server') just because it runs a bit slower than ChefSpec does on it's own. Perhaps I'm reading the documentation wrong, but it doesn't look like Chef…
user949286
  • 1,259
  • 1
  • 13
  • 22
6
votes
1 answer

How can I test my LWRP with ChefSpec?

I created my custom LWRP, but when I run the ChefSpec units test. It doesn't know my LWRP actions. Here is my resource: actions :install, :uninstall default_action :install attribute :version, :kind_of => String attribute :options, :kind_of =>…
Robert
  • 10,403
  • 14
  • 67
  • 117
6
votes
1 answer

ChefSpec - Unable to set node attributes

I have a simple test for the nginx cookbook: require 'spec_helper' describe 'my_cookbook::nginx' do let(:chef_run) do ChefSpec::Runner.new do |node| node.set['nginx']['dir'] = '/etc/nginx' end.converge(described_recipe) end it…
ben lemasurier
  • 2,582
  • 4
  • 22
  • 37
6
votes
1 answer

Feedback on Ruby / ChefSpec coding style

I'm rather new to Ruby, but I have been doing a lot of research on Chef testing for the past two weeks. This test uses ChefSpec & Fauxhai, but it doesn't look very "ruby-ish" and I was hoping the community could give me some pointers on coding…
Rapsey
  • 599
  • 2
  • 7
  • 17
5
votes
1 answer

Chefspec loads libraries repeatedly and gives the warning "already initialized constant CONSTANT"

I have a chef cookbook with a library, e.g. library.rb. It contains a CONSTANT: CONSTANT = 'constant' When I write unit tests for this cookbook, it always gives me the warning: (Some prefix...)warning: already initialized constant CONSTANT (Some…
vmcloud
  • 650
  • 4
  • 13
5
votes
2 answers

Mocking Files during a ChefSpec run

I created a Chef resource which 'extends' the deploy resource of chef. The basic idea is to check for the existence of a file deploy/crontab similar to mechanisms deploy/after_restart.rb in the source to be deployed, and create cronjobs out of…
5
votes
1 answer

Unit testing a Chef provider

How can I write a unit test for a Chef provider? So far, our unit testing strategy uses ChefSpec for recipes, and we stuff most of the interesting logic for our providers in libraries to make the logic more testable. However, we still run into…
user949286
  • 1,259
  • 1
  • 13
  • 22
5
votes
3 answers

ChefSpec should not test included recipe

I have built a cookbook for installing Jenkins CI. It uses the key and repository resources from the yum cookbook, so I end up with the following recipe: yum_key "RPM-GPG-KEY-jenkins" do url "http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key" …
mtak
  • 365
  • 7
  • 23
4
votes
3 answers

How to create a dummy resource defined in another recipe without including the other recipe in test run?

I have the following Chef recipe: # recipes/default.rb include_recipe 'my-cookbook::another_recipe' execute 'Do some stuff' do command "echo some stuff" action :run end template "my_dir/my_file.txt" do source "my_file.txt.erb" owner…
k6ps
  • 359
  • 4
  • 11
4
votes
1 answer

Stub file open in chefspec

Hi am doing test with chefspec and I find the following error. in /cookbook/wordpress/recipes/default.rb wp_secrets = Chef::Config[:file_cache_path] + '/wp-secrets.php' if File.exist?(wp_secrets) salt_data = File.read(wp_secrets) else require…
Psyreactor
  • 343
  • 2
  • 7
4
votes
1 answer

Mocking an external provider in chefspec

I am trying to test a provider in Chef using ChefSpec. In this provider, I am calling another external provider, which I would like to mock/disable, so that only my provider is being executed, not the external provider as well. My provider is…
4
votes
2 answers

Test file initialization based off template using ChefSpec

I've got the following template file creation in my cookbook: template "my_file" do path "my_path" source "my_file.erb" owner "root" group "root" mode "0644" variables(@template_variables) notifies :restart, resources(service:…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
3
votes
1 answer

chefspec: setting global node attributes to be used in all tests in recipe

I'm currently writing a chefspec recipe which sets certain node attributes, which are necessary to complete my unit tests. I am currently setting these attributes in each test, which seems wasteful. I wish to carry this out in such a way that I am…
mickm
  • 281
  • 2
  • 6
  • 20
3
votes
1 answer

"Cookbook not found" error with ChefSpec test

I'm trying to running my ChefSpec tests. This is my ChefSpec test: require_relative '../spec_helper' describe 'my-demo::basesystem' do let(:chef_run) { ChefSpec::Runner.new.converge(described_recipe)} describe 'basesystem' do it "should…
Robert
  • 10,403
  • 14
  • 67
  • 117
1
2 3
8 9