Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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
Tweet

More Decks by Seth Vargo

Other Decks in Technology

Transcript

  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