Slide 1

Slide 1 text

CHEF TESTING COOKBOOK

Slide 2

Slide 2 text

WANTED @ SETHVARGO EATING BACON AND TESTING ALL THE THINGS!

Slide 3

Slide 3 text

CHEF TESTING COOKBOOK

Slide 4

Slide 4 text

CHEF TESTING

Slide 5

Slide 5 text

CHEF TESTING

Slide 6

Slide 6 text

CHEF TESTING COOKBOOK

Slide 7

Slide 7 text

TESTING COOKBOOK

Slide 8

Slide 8 text

TESTING COOKBOOK

Slide 9

Slide 9 text

CHEF TESTING COOKBOOK

Slide 10

Slide 10 text

TESTING

Slide 11

Slide 11 text

TESTING ?

Slide 12

Slide 12 text

TESTING LINT

Slide 13

Slide 13 text

TESTING LINT semantic and best-practice coding standards for Chef cookbooks

Slide 14

Slide 14 text

TESTING LINT semantic and best-practice coding standards for Chef cookbooks Foodcritic https://github.com/acrmp/foodcritic

Slide 15

Slide 15 text

TESTING UNIT

Slide 16

Slide 16 text

TESTING UNIT universe-controlled, self-contained, singular test surrounding a particular function

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

TESTING REGRESSION

Slide 20

Slide 20 text

TESTING REGRESSION a test, typically accompanying a bug fix, ensuring the fix is not reverted in the future

Slide 21

Slide 21 text

TESTING INTEGRATION

Slide 22

Slide 22 text

TESTING INTEGRATION high level, post-convergence test

Slide 23

Slide 23 text

TESTING INTEGRATION high level, post-convergence test Test Kitchen https://github.com/opscode/test-kitchen

Slide 24

Slide 24 text

TESTING ACCEPTANCE

Slide 25

Slide 25 text

TESTING ACCEPTANCE YOU

Slide 26

Slide 26 text

TESTING STRAIGHT UP

Slide 27

Slide 27 text

MOCKS it 'writes cheese' do expect(chef_run).to create_file 'cheese.txt' end object with a controlled response

Slide 28

Slide 28 text

MOCKS let(:aws) { double('aws') } before { AWS.stub(:initialize).and_return(aws) }

Slide 29

Slide 29 text

STUBS it 'writes cheese' do expect(chef_run).to create_file 'cheese.txt' end method with a canned response

Slide 30

Slide 30 text

STUBS before { AWS.stub(:connect).and_return(true) }

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

EXAMPLE 2.0 it 'warns the username' do Chef::Recipe.any_instance.stub(:data_bag_item) .with(:users) .and_return(JSON.parse(File.read('support/users.json'))) expect(chef_run).to warn('seth') end

Slide 35

Slide 35 text

KITCHEN it 'writes cheese' do expect(chef_run).to create_file 'cheese.txt' end a place for cooking

Slide 36

Slide 36 text

KITCHEN it 'writes cheese' do expect(chef_run).to create_file 'cheese.txt' end a place for testing

Slide 37

Slide 37 text

driver_plugin: vagrant platforms: - name: ubuntu-12.04 driver_config: box: opscode-ubuntu-12.04 box_url: ... suites: - name: default run_list: - recipe[myface] KITCHEN

Slide 38

Slide 38 text

LEFT FOR THE VIEWERS EXERCISES https://github.com/opscode-cookbooks/java HAS IT ALL https://github.com/sethvargo-cookbooks/chef-zero SIMPLEST https://github.com/customink-webops/hostsfile COMPLEX

Slide 39

Slide 39 text

DO IT LIVE FUCK IT