Top Banner
Testing Cookbooks Validating Our Recipes in Virtual Environments Slide 1 of 88
88

Chef for beginners module 5

Apr 15, 2017

Download

Software

Chef
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chef for beginners   module 5

Testing CookbooksValidating Our Recipes in Virtual Environments

Slide 1 of 88

Page 2: Chef for beginners   module 5

After completing this module, you should be able to:

Use Test Kitchen to verify your recipes converge on avirtual instanceRead the ServerSpec documentationWrite and execute tests

Objectives

Slide 2 of 88

Page 3: Chef for beginners   module 5

As we start to define our infrastructure as code we alsoneed to start thinking about testing it.

Can We Test Cookbooks?

Slide 3 of 88

Page 4: Chef for beginners   module 5

As we start to define our infrastructure as code we alsoneed to start thinking about testing it.

Will the recipes that we created work onanother system similar to this one? Will theywork in production?

Can We Test Cookbooks?

Slide 4 of 88

Page 5: Chef for beginners   module 5

As we start to define our infrastructure as code we alsoneed to start thinking about testing it.

Will the recipes that we created work onanother system similar to this one? Will theywork in production?

"Works on my machine".

Can We Test Cookbooks?

Slide 5 of 88

Page 6: Chef for beginners   module 5

As we start to define our infrastructure as code we alsoneed to start thinking about testing it.

Will the recipes that we created work onanother system similar to this one? Will theywork in production?

"Works on my machine".

So how could we solve a problem like this?

Can We Test Cookbooks?

Slide 6 of 88

Page 7: Chef for beginners   module 5

What steps would it take to test one of the cookbooks thatwe have created?

Mandating Testing

Slide 7 of 88

Page 8: Chef for beginners   module 5

Slide 8 of 88

Page 9: Chef for beginners   module 5

We can start by first mandating that all cookbooks aretested.

Testing Cookbooks

Slide 9 of 88

Page 10: Chef for beginners   module 5

We can start by first mandating that all cookbooks aretested.

Considerations--

Testing Cookbooks

Slide 10 of 88

Page 11: Chef for beginners   module 5

We can start by first mandating that all cookbooks aretested.

Considerations--

How often should you test your cookbook?

Testing Cookbooks

Slide 11 of 88

Page 12: Chef for beginners   module 5

We can start by first mandating that all cookbooks aretested.

Considerations--

How often should you test your cookbook?

How often do you think changes will occur?

Testing Cookbooks

Slide 12 of 88

Page 13: Chef for beginners   module 5

We can start by first mandating that all cookbooks aretested.

Considerations--

How often should you test your cookbook?

How often do you think changes will occur?

What happens when the rate of cookbookchanges exceed the time interval it takes toverify the cookbook?

Testing Cookbooks

Slide 13 of 88

Page 14: Chef for beginners   module 5

An automated way to ensure code accomplishes theintended goal and help the team understand its intent.

Code Testing

Slide 14 of 88

Page 15: Chef for beginners   module 5

"What are we running in production? Maybe I could testthe cookbook against a virtual machine."

Test Configuration

Slide 15 of 88

Page 16: Chef for beginners   module 5

"What are we running in production? Maybe I could testthe cookbook against a virtual machine."

Objective:Configure the workstation cookbook to test against thecentos-6.7 platformTest the workstation cookbook on a virtual machine

Test Configuration

Slide 16 of 88

Page 17: Chef for beginners   module 5

Slide 17 of 88

Page 18: Chef for beginners   module 5

What Can kitchen Do?$ kitchen --help

Commands: kitchen console # Kitchen Console! kitchen converge [INSTANCE|REGEXP|all] # Converge one or more instances kitchen create [INSTANCE|REGEXP|all] # Create one or more instances kitchen destroy [INSTANCE|REGEXP|all] # Destroy one or more instances ... kitchen help [COMMAND] # Describe available commands or one specif... kitchen init # Adds some configuration to your cookbook... kitchen list [INSTANCE|REGEXP|all] # Lists one or more instances kitchen setup [INSTANCE|REGEXP|all] # Setup one or more instances kitchen test [INSTANCE|REGEXP|all] # Test one or more instances kitchen verify [INSTANCE|REGEXP|all] # Verify one or more instances kitchen version # Print Kitchen's version information

Slide 18 of 88

Page 19: Chef for beginners   module 5

What Can kitchen init Do?$ kitchen help init

Usage: kitchen init -D, [--driver=one two three] # One or more Kitchen Driver gems ... # Default: kitchen-vagrant -P, [--provisioner=PROVISIONER] # The default Kitchen Provisioner to use # Default: chef_solo [--create-gemfile], [--no-create-gemfile] # Whether or not to create a Gemfi ...

Description: Init will add Test Kitchen support to an existing project for convergence integration testing. A default .kitchen.yml file (which is intended to be customized) is created in the project's root directory and one or more gems will be added to the project's Gemfile.

Slide 19 of 88

Page 20: Chef for beginners   module 5

Do We Have a .kitchen.yml?$ tree cookbooks/workstation -a -I .git

workstation├── Berksfile├── chefignore├── .gitignore├── .kitchen.yml├── metadata.rb├── README.md├── recipes│ ├── default.rb│ └── setup.rb├── spec│ ├── spec_helper.rb│ └── unit...

Slide 20 of 88

Page 21: Chef for beginners   module 5

What is Inside .kitchen.yml?$ cat cookbooks/workstation/.kitchen.yml

---driver: name: vagrant

provisioner: name: chef_zero

platforms: - name: ubuntu-14.04 - name: centos-7.1

suites: - name: default

Slide 21 of 88

Page 22: Chef for beginners   module 5

When chef generates a cookbook, a default.kitchen.yml is created.It contains kitchen configuration for the driver,provisioner, platform, and suites.

.kitchen.yml

http://kitchen.ci/docs/getting-started/creating-cookbook

Slide 22 of 88

Page 23: Chef for beginners   module 5

Demo: The kitchen Driver~/cookbooks/workstation/.kitchen.yml

---driver: name: vagrant

provisioner: name: chef_zero

platforms: - name: ubuntu-14.04 - name: centos-7.1

...

The driver is responsible for creating a machine that we'll use to test ourcookbook.

Example Drivers:

dockervagrant

Slide 23 of 88

Page 24: Chef for beginners   module 5

Demo: The kitchen Provisioner~/cookbooks/workstation/.kitchen.yml

---driver: name: vagrant

provisioner: name: chef_zero

platforms: - name: ubuntu-14.04 - name: centos-7.1

...

This tells Test Kitchen how to run Chef to apply the code in our cookbook tothe machine under test.

The default and simplest approach is to use chef_zero.

Slide 24 of 88

Page 25: Chef for beginners   module 5

Demo: The kitchen Platforms~/cookbooks/workstation/.kitchen.yml

---driver: name: vagrant

provisioner: name: chef_zero

platforms: - name: ubuntu-14.04 - name: centos-7.1

...

This is a list of operation systems on which we want to run our code.

Slide 25 of 88

Page 26: Chef for beginners   module 5

Demo: The kitchen Suites~/cookbooks/workstation/.kitchen.yml

...

suites: - name: default run_list: - recipe[workstation::default] attributes:

This section defines what we want to test. It includes the Chef run-list ofrecipes that we want to test.

We define a single suite named default.

Slide 26 of 88

Page 27: Chef for beginners   module 5

Demo: The kitchen Suites~/cookbooks/workstation/.kitchen.yml

...

suites: - name: default run_list: - recipe[workstation::default] attributes:

The suite named default defines a run_list.

Run the workstation cookbook's default recipe file.

Slide 27 of 88

Page 28: Chef for beginners   module 5

Kitchen defines a list of instances, or test matrix,based on the platforms multiplied by the suites.

PLATFORMS x SUITES

Running kitchen list will show that matrix.

Kitchen Test Matrix

Slide 28 of 88

Page 29: Chef for beginners   module 5

suites: - name: default run_list: - recipe[workstation::default] attributes:

platforms: - name: ubuntu-12.04 - name: centos-6.5

Example: Kitchen Test Matrix$ kitchen list

Instance Driver Provisioner Verifier Transport Last Actiondefault-ubuntu-1204 Vagrant ChefZero Busser Ssh <Not Created>default-centos-65 Vagrant ChefZero Busser Ssh <Not Created>

Slide 29 of 88

Page 30: Chef for beginners   module 5

suites: - name: default run_list: - recipe[workstation::default] attributes:

platforms: - name: ubuntu-12.04 - name: centos-6.5

Example: Kitchen Test Matrix$ kitchen list

Instance Driver Provisioner Verifier Transport Last Actiondefault-ubuntu-1204 Vagrant ChefZero Busser Ssh <Not Created>default-centos-65 Vagrant ChefZero Busser Ssh <Not Created>

Slide 30 of 88

Page 31: Chef for beginners   module 5

"What are we running in production? Maybe I could testthe cookbook against a virtual machine."

Group Exercise: Test Configuration

Slide 31 of 88

Page 32: Chef for beginners   module 5

"What are we running in production? Maybe I could testthe cookbook against a virtual machine."

Objective:Configure the workstation cookbook's .kitchen.yml touse the Docker driver and centos 6.7 platform.

Use kitchen converge to apply the recipe on a virtualmachine.

Group Exercise: Test Configuration

Slide 32 of 88

Page 33: Chef for beginners   module 5

Group Exercise: Move into the Cookbook's Directory$ cd ~/cookbooks/workstation

Slide 33 of 88

Page 34: Chef for beginners   module 5

Group Exercise: Edit the Kitchen Configuration File~/cookbooks/workstation/.kitchen.yml

---driver: name: docker

provisioner: name: chef_zero

platforms: - name: centos-6.7

suites:# ... REMAINDER OF FILE ...

https://github.com/portertech/kitchen-docker

Slide 34 of 88

Page 35: Chef for beginners   module 5

Group Exercise: Edit the Kitchen Configuration File~/cookbooks/workstation/.kitchen.yml

---driver: name: docker

provisioner: name: chef_zero

platforms: - name: centos-6.7

suites:# ... REMAINDER OF FILE ...

https://www.centos.org

Slide 35 of 88

Page 36: Chef for beginners   module 5

Group Exercise: Look at the Test Matrix$ kitchen list

Instance Driver Provisioner Verifier Transport Last Actiondefault-centos-67 Docker ChefZero Busser Ssh <Not Created>

Slide 36 of 88

Page 37: Chef for beginners   module 5

"Before I add features it really would be nice to test thesecookbooks against the environments that resembleproduction."

Converging a Cookbook

Slide 37 of 88

Page 38: Chef for beginners   module 5

"Before I add features it really would be nice to test thesecookbooks against the environments that resembleproduction."

Objective:Configure the workstation cookbook's .kitchen.yml touse the Docker driver and centos-6.7 platformUse kitchen converge to apply the recipe on a virtualmachine

Converging a Cookbook

Slide 38 of 88

Page 39: Chef for beginners   module 5

Kitchen Create

$ kitchen create [INSTANCE|REGEXP|all]

Create one or more instances.

Slide 39 of 88

Page 40: Chef for beginners   module 5

Group Exercise: Kitchen Converge

$ kitchen converge [INSTANCE|REGEXP|all]

Create the instance (if necessary) and then apply the run list to one or moreinstances.

Slide 40 of 88

Page 41: Chef for beginners   module 5

Group Exercise: Converge the Cookbook$ cd ~/cookbooks/workstation$ kitchen converge

-----> Starting Kitchen (v1.4.0)-----> Creating <default-centos-67>... Sending build context to Docker daemon 2.56 kB(skipping)-----> Finished creating <default-centos-67> (1m18.32s).-----> Converging <default-centos-67>...$$$$$$ Running legacy converge for 'Docker' Driver(skipping)Synchronizing Cookbooks: - workstation Compiling Cookbooks... Converging 0 resources Running handlers:

Slide 41 of 88

Page 42: Chef for beginners   module 5

We want to validate that our run-list installs correctly.

Within the apache cookbook, use kitchen converge forthe default suite on the centos 6.7 platform.

Lab: Converge the Recipe for apache

Slide 42 of 88

Page 43: Chef for beginners   module 5

Lab: Configuring Test Kitchen for apache~/cookbooks/apache/.kitchen.yml

---driver: name: docker

provisioner: name: chef_zero

platforms: - name: centos-6.7

suites: - name: default run_list: - recipe[apache::default] attributes:

Slide 43 of 88

Page 44: Chef for beginners   module 5

Lab: Converge the apache Cookbook$ cd ~/cookbooks/apache$ kitchen converge

-----> Starting Kitchen (v1.4.0)-----> Creating <default-centos-67>... Sending build context to Docker daemon 2.56 kB Sending build context to Docker daemon(skipping) Installing Chef installing with rpm... warning: /tmp/install.sh.23/chef-12.4.1-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY(skipping) Synchronizing Cookbooks: - apache Compiling Cookbooks...

Slide 44 of 88

Page 45: Chef for beginners   module 5

What is being tested when kitchen converges a recipewithout error?

Test Kitchen

Slide 45 of 88

Page 46: Chef for beginners   module 5

What is being tested when kitchen converges a recipewithout error?

What is NOT being tested when kitchen converges therecipe without error?

Test Kitchen

Slide 46 of 88

Page 47: Chef for beginners   module 5

What is being tested when kitchen converges a recipewithout error?

What is NOT being tested when kitchen converges therecipe without error?

What is left to validate to ensure that the cookbooksuccessfully applied the policy defined in the recipe?

Test Kitchen

Slide 47 of 88

Page 48: Chef for beginners   module 5

"Converging seems to validate that the recipe runssuccessfully. But does it assert what actually is installed?"

The First Test

Slide 48 of 88

Page 49: Chef for beginners   module 5

"Converging seems to validate that the recipe runssuccessfully. But does it assert what actually is installed?"

Objective:In a few minutes we'll write and execute a test thatasserts that the tree package is installed when theworkstation cookbook's default recipe is applied.

The First Test

Slide 49 of 88

Page 50: Chef for beginners   module 5

Kitchen Verify

$ kitchen verify [INSTANCE|REGEXP|all]

Create, converge, and verify one or more instances.

Slide 50 of 88

Page 51: Chef for beginners   module 5

Kitchen Destroy

$ kitchen destroy [INSTANCE|REGEXP|all]

Destroys one or more instances.

Slide 51 of 88

Page 52: Chef for beginners   module 5

Kitchen Test

$ kitchen test [INSTANCE|REGEXP|all]

Destroys (for clean-up), creates, converges, verifies and then destroys one ormore instances.

Slide 52 of 88

Page 53: Chef for beginners   module 5

Serverspec tests your servers' actual state byexecuting command locally, via SSH, via WinRM, viaDocker API and so on.

So you don't need to install any agent software onyour servers and can use any configurationmanagement tools, Puppet, Chef, CFEngine, Itamaeand so on.

Serverspec

http://serverspec.org

Slide 53 of 88

Page 54: Chef for beginners   module 5

ExampleIs the tree package installed?

describe package('tree') do it { should be_installed }end

I expect the package tree should be installed.

http://serverspec.org/resource_types.html#package

Slide 54 of 88

Page 55: Chef for beginners   module 5

Group Exercise: Requiring a Test Helper~/cookbooks/workstation/test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe 'workstation::default' do

describe package('tree') do it { should be_installed } end

end

Loads a helper file with that name in the same directory.

http://kitchen.ci/docs/getting-started/writing-test

Slide 55 of 88

Page 56: Chef for beginners   module 5

Group Exercise: Describing the Test Context~/cookbooks/workstation/test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe 'workstation::default' do

describe package('tree') do it { should be_installed } end

end

Describes a body of tests for the workstation cookbook's default recipe.

https://relishapp.com/rspec/rspec-core/v/3-3/docs

Slide 56 of 88

Page 57: Chef for beginners   module 5

Group Exercise: Our Assertion in a spec File~/cookbooks/workstation/test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe 'workstation::default' do

describe package('tree') do it { should be_installed } end

end

When we converge the workstation cookbook's default recipe we expect thetree package to be installed.

http://serverspec.org/resource_types.html#package

Slide 57 of 88

Page 58: Chef for beginners   module 5

workstation/test/ /default/serverspec/default_spec.rb

Test Kitchen will look for tests to run under this directory.It allows you to put unit or other tests in test/unit, spec,acceptance, or wherever without mixing them up. This isconfigurable, if desired.

Where do Tests Live?integration

http://kitchen.ci/docs/getting-started/writing-test

Slide 58 of 88

Page 59: Chef for beginners   module 5

workstation/test/integration/ /serverspec/default_spec.rb

This corresponds to the name of the test suite that isdefined in the .kitchen.yml file.In our case the name of the suite is default so whenTest Kitchen performs a kitchen verify for the defaultsuite it will look within the default folder for thespecifications to run.

Where do Tests Live?default

http://kitchen.ci/docs/getting-started/writing-test

Slide 59 of 88

Page 60: Chef for beginners   module 5

workstation/test/integration/default/ /default_spec.rb

This tells Test Kitchen that we wish to use Serverspecframework for testing.

Where do Tests Live?serverspec

http://kitchen.ci/docs/getting-started/writing-test

Slide 60 of 88

Page 61: Chef for beginners   module 5

workstation/test/integration/default/serverspec/

All test files (or specs) are named after the recipe theytest and end with the suffix _spec.rb.A spec missing that will not be found when executingkitchen verify.

Where do Tests Live?default_spec.rb

http://kitchen.ci/docs/getting-started/writing-test

Slide 61 of 88

Page 62: Chef for beginners   module 5

Group Exercise: Move into the Cookbook$ cd ~/cookbooks/workstation

Slide 62 of 88

Page 63: Chef for beginners   module 5

Group Exercise: Running the Specification$ kitchen verify

-----> Starting Kitchen (v1.4.0)-----> Converging <default-centos-67>...$$$$$$ Running legacy converge for 'Docker' Driver(skipping)-----> Chef Omnibus installation detected (install only if missing) Transferring files to <default-centos-67> Starting Chef Client, version 12.4.1(skipping) Running handlers: Running handlers complete Chef Client finished, 6/6 resources updated in 64.426896317 seconds Finished converging <default-centos-67> (1m9.02s).-----> Kitchen is finished. (1m9.69s)

Slide 63 of 88

Page 64: Chef for beginners   module 5

Group Exercise: Commit Your Work$ cd ~/cookbooks/workstation$ git add .$ git status$ git commit -m "Added first test for the default recipe"

Slide 64 of 88

Page 65: Chef for beginners   module 5

More TestsWhat are other resources within the recipe that we could test?

Slide 65 of 88

Page 66: Chef for beginners   module 5

Serverspec can help us assert different characteristicsabout files on the file system. Like if it is a file,directory, socket or symlink.

The file's mode owner or group. If the file is readable,writeable, or executable. It is even able to verify thedata contained within the file.

Testing a File

http://serverspec.org/resource_types.html#file

Slide 66 of 88

Page 67: Chef for beginners   module 5

Example: The File Contains Datadescribe file('/etc/passwd') do it { should be_file }end

I expect the file named /etc/passwd to be a file (as opposed to a directory,socket or symlink).

http://serverspec.org/resource_types.html#file

Slide 67 of 88

Page 68: Chef for beginners   module 5

Example: The File Contains Specific Contentdescribe file('/etc/httpd/conf/httpd.conf') do its(:content) { should match /ServerName www.example.jp/ }end

I expect the file named /etc/httpd/conf/httpd.conf to have content thatmatches ServerName www.example.jp

http://serverspec.org/resource_types.html#file

Slide 68 of 88

Page 69: Chef for beginners   module 5

Example: The File is Owned by a Particular Userdescribe file('/etc/sudoers') do it { should be_owned_by 'root' }end

I expect the file named /etc/sudoers to be owned by the root user.

Slide 69 of 88

Page 70: Chef for beginners   module 5

Add tests that validate that the remaining packageresources have been installed(http://serverspec.org/resource_types.html#package)

Add tests that validate the file resource(http://serverspec.org/resource_types.html#file)

Run kitchen verify to validate the test meets theexpectations that you defined

Commit your changes

Lab: More Tests

Slide 70 of 88

Page 71: Chef for beginners   module 5

Lab: Our Assertion in a spec File~/cookbooks/workstation/test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe 'workstation::default' do # ... other tests for packages ...

describe package('tree') do it { should be_installed } end

describe package('git') do it { should be_installed } end

end

The package named git is installed.

http://serverspec.org/resource_types.html#package

Slide 71 of 88

Page 72: Chef for beginners   module 5

Lab: Our Assertion in a spec File~/cookbooks/workstation/test/integration/default/serverspec/default_spec.rb

...

describe package('git') do it { should be_installed } end

describe file('/etc/motd') do it { should be_owned_by 'root' } end

end

The file named /etc/motd should be owned by root.

http://serverspec.org/resource_types.html#file

Slide 72 of 88

Page 73: Chef for beginners   module 5

Group Exercise: Return to the Cookbook Directory$ cd ~/cookbooks/workstation

Slide 73 of 88

Page 74: Chef for beginners   module 5

Lab: Running the Specification$ kitchen verify

-----> Starting Kitchen (v1.4.0)-----> Converging <default-centos-67>...$$$$$$ Running legacy converge for 'Docker' Driver(skipping)-----> Chef Omnibus installation detected (install only if missing) Transferring files to <default-centos-67> Starting Chef Client, version 12.4.1(skipping) Running handlers: Running handlers complete Chef Client finished, 6/6 resources updated in 64.426896317 seconds Finished converging <default-centos-67> (1m9.02s).-----> Kitchen is finished. (1m9.69s)

Slide 74 of 88

Page 75: Chef for beginners   module 5

Lab: Commit Your Work$ cd ~/cookbooks/workstation$ git add .$ git status$ git commit -m "Added additional tests for default recipe"

Slide 75 of 88

Page 76: Chef for beginners   module 5

"I would love to know that the webserver is installed andrunning correctly."

Testing Our Webserver

Slide 76 of 88

Page 77: Chef for beginners   module 5

"I would love to know that the webserver is installed andrunning correctly."

Objective:Discuss and decide what should be tested with theapache cookbook

Testing Our Webserver

Slide 77 of 88

Page 78: Chef for beginners   module 5

What are some things we could test to validate ourweb server has deployed correctly?

What manual tests do we use now to validate aworking web server?

Testing

Slide 78 of 88

Page 79: Chef for beginners   module 5

Create a test file for the apache cookbook's defaultrecipeAdd tests that validate a working web server

http://serverspec.org/resource_types.html#porthttp://serverspec.org/resource_types.html#command

Run kitchen verifyCommit your changes

Lab: Testing Apache

Slide 79 of 88

Page 80: Chef for beginners   module 5

Lab: Switch to the apache cookbook$ cd ~/cookbooks/apache

Slide 80 of 88

Page 81: Chef for beginners   module 5

Lab: What Does the Webserver Say?~/cookbooks/apache/test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe 'apache::default' do describe port(80) do it { should be_listening } end

describe command('curl http://localhost') do its(:stdout) { should match /Hello, world!/ } endend

Port 80 should be listening.

The standard out from the command curl http://localhost should match'Hello, world!'

Slide 81 of 88

Page 82: Chef for beginners   module 5

Lab: Commit Your Work$ cd ~/cookbooks/apache$ git add .$ git status$ git commit -m "Added tests for the default recipe"

Slide 82 of 88

Page 83: Chef for beginners   module 5

Why do you have to run kitchen within the directoryof the cookbook?

Discussion

Slide 83 of 88

Page 84: Chef for beginners   module 5

Why do you have to run kitchen within the directoryof the cookbook?

Where would you define additional platforms?

Discussion

Slide 84 of 88

Page 85: Chef for beginners   module 5

Why do you have to run kitchen within the directoryof the cookbook?

Where would you define additional platforms?

Why would you define a new test suite?

Discussion

Slide 85 of 88

Page 86: Chef for beginners   module 5

Why do you have to run kitchen within the directoryof the cookbook?

Where would you define additional platforms?

Why would you define a new test suite?

What are the limitations of using Test Kitchen tovalidate recipes?

Discussion

Slide 86 of 88

Page 87: Chef for beginners   module 5

Q&AWhat questions can we help you answer?

Test Kitchenkitchen commandskitchen configurationServerspec

Slide 87 of 88

Page 88: Chef for beginners   module 5

Slide 88 of 88