Chef Cookbook Testing

Chef Cookbook Testing

A brief overview of how to test, what to test, and who to test with Chef.

Seth Vargo

June 26, 2013

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

39 slides
  1. TESTING UNIT universe-controlled, self-contained, singular test surrounding a particular function

    ChefSpec RSpec ServerSpec https://github.com/acrmp/chefspec https://github.com/mizzy/serverspec https://github.com/rspec/rspec
  2. it 'writes cheese' do expect(chef_run).to create_file 'cheese.txt' end it 'installs

    apache2' do expect(chef_run).to install_package 'apache' end it 'starts bacon' do expect(chef_run).to start_service 'bacon' end
  3. EXAMPLE search(:node, 'role:web') do |web_node| Chef::Log.warn web_node['ipaddress'] end it 'warns

    the IP Addresses' do Chef::Recipe.any_instance.stub(:search) .with(:node, 'role:web') .and_yield(Hashie::Mash.new(ipaddress: '1.2.3.4')) expect(chef_run).to warn('1.2.3.4') end
  4. EXAMPLE it 'warns the IP Addresses' do Chef::Recipe.any_instance.stub(:search) .with(:node, 'role:web')

    .and_yield(JSON.parse(File.read('support/ips.json'))) expect(chef_run).to warn('1.2.3.4') end
  5. EXAMPLE 2.0 data_bag_item(:users) do |user| Chef::Log.warn user['username'] end it 'warns

    the username' do Chef::Recipe.any_instance.stub(:data_bag_item) .with(:users) .and_return(Hashie::Mash.new(username: 'seth')) expect(chef_run).to warn('seth') end

More Decks by Seth Vargo

Other Decks in Technology