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
3 answers

ChefSpec runs super slow

I'm following this basic tutorial and it seems these tests are supposed to be run in like 2 seconds max, but I'm getting 41 seconds for 5 tests. I ran ChefSpec with --profile and this is the result Top 8 slowest examples (41.17 seconds, 99.8% of…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
1
vote
1 answer

Mocking kernel release on ChefSpec

I am trying to test that linux-image-extra-#{node['kernel']['release']} will be installed by my cookbook and I'm trying to mock up #{node['kernel']['release'] using Fauxhai. So far I have; describe 'my-cookbook::recipe' do let(:chef_run) {…
Ste-3PO
  • 1,165
  • 1
  • 9
  • 18
1
vote
2 answers

Chefspec with Hashicorp Vault

I'm trying to use ChefSpec to test an implementation of Chef and Hashicorp Vault Recipe chef_gem 'vault' do compile_time true if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time) end require 'vault' Vault.address =…
jsmickey
  • 718
  • 10
  • 20
1
vote
1 answer

Chef NoMethodError when using library module method with registry_key in recipe

I receive the error NoMethodError ------------- undefined method `registry_key' for HashOperations:Module when converging a Chef cookbook. This is the short version of the code from libraries/hash_operations.rb: module HashOperations # Tried:…
tibileo
  • 73
  • 1
  • 9
1
vote
1 answer

Chef spec error: expected "execute[..]" with action :run to be in Chef run. Other execute resources:

I am getting chefspec error as shown in title. Below are recipe resource and spec for which I am getting error: Resource: execute 'generate ssl cert for simple https file server' do cwd '/root/git/chef-bluecloud/bin/' command <<-EOH openssl…
1
vote
1 answer

chefspec testing for stray ruby variable

Over here I am returning status of provider in variable, how can I mock that status in chefspec for testing ? ret = yum_package 'blah' do action :install end cookbook_file "/etc/init.d/blah" do source "blah" only_if {…
Valmik Roy
  • 57
  • 1
  • 7
1
vote
1 answer

chefspec test for non core resource

Could anyone advise how I can write a chefspec test for the following recipe code please? {'tomcat_exec_t' => '/apps/.*/bin(/.*)?', 'tomcat_unit_file_t' => '/usr/lib/systemd/system/tomcat@.*.service', 'tomcat_cache_t' =>…
Edward
  • 33
  • 8
1
vote
1 answer

stub node.attribute? in chefspec

I'm trying to create a spec test for the following recipe code: if node.attribute?(node['tested_cookbook']['some_attribute']) include_recipe('tested_cookbook::first') else include_recipe('tested_cookbook::second') I have the…
1
vote
3 answers

How to test idempotency with chefspec

I would like to use chefspec to test the idempotency of my recipe. Let's say I have a recipe that includes these two resource statements: file '/etc/app.config' do action :create notifies :restart, "service[app]" end service 'app' do action…
Kief
  • 4,383
  • 4
  • 26
  • 21
1
vote
1 answer

Chef cookbook_file and template resource: how to test that source exists?

If I use a template resource in a recipe template '/home/user1/foo' do source 'bar' end And I test using chefspec/rspec ... expect(chef_run).to render_file('/home/user1/foo') expect(chef_run).to…
Tom Wilson
  • 797
  • 9
  • 26
1
vote
1 answer

"Cookbook apache_wrapper not found" when running chefspec in apache_wrapper cookbook

I'm new in chef spec and try to figure out how to use unit testing in my cookbooks. I have installed chefdk(v2 and v3 on different ubuntu instances) and knife spec plugin. After "apache_wrapper" cookbook creation I have changed next…
user3484021
  • 27
  • 1
  • 10
1
vote
1 answer

ChefSpec test breaking with rvm include

So I'm trying to write a unit test (via ChefSpec) for one of my Chef recipes but I am getting some odd behaviour. My recipe in question is including the rvm::system_install recipe and apparently this is causing issues with ChefSpec: Failures: 1)…
georgthebub
  • 407
  • 6
  • 18
1
vote
1 answer

ChefSpec doesn't find my Chef resources

I have a cookbook with a bunch of recipes and LWRP's. I now want to start using ChefSpec for coverage, and wrote one example: require 'chefspec' require 'chefspec/berkshelf' ChefSpec::Coverage.start! RSpec.configure do |config| config.platform =…
Cas van Noort
  • 255
  • 3
  • 9
1
vote
1 answer

ChefSpec and Ark resources testing

I'm starting to test with chefspec, which i think is a really easy and reliable test framework. But i'm having some issues regarding to ark resources testing. I have this default_spec.rb require 'chefspec' require_relative 'spec_helper' describe…
JGutierrezC
  • 4,398
  • 5
  • 25
  • 42
1
vote
1 answer

Unable to stub partial_search for chefspec

I'm writing a cookbook that runs a partial search to find an attribute on other nodes. My chefspec test is failing with error ERROR: Connection refused connecting to localhost:443. The search is instantiated as below: describe 'my_recipe::default'…
raeshell
  • 13
  • 3
1 2 3
8 9