SlideShare une entreprise Scribd logo
1  sur  46
Télécharger pour lire hors ligne
1
v1.1.0 
Building Testable 
Infrastructure with Chef, Test 
Kitchen, and Docker 
mandi walls 
mandi@getchef.com 
CodeMesh.io 5 November 2014 
2
whoami 
• Mandi Walls 
• Professional Services at Chef 
• @lnxchk 
3
v1.1.0 
What is Chef 
4
Automation Platform 
• Creates a dependable view of your entire network’s 
state. 
• Can handle complex dependencies among the 
nodes of your network. 
• Is fault tolerant. 
• Is secure. 
• Can handle multiple platforms 
• Can manage cloud resources 
• Provides a foundation for innovation 
5
6
Infrastructure As Code 
• Programmatically 
provision and configure 
components 
• Treat like any other 
code base 
• Reconstruct business 
from code repository, 
data backup, and 
compute resources 
7
Policy-Based 
•You capture the policy for your infrastructure 
in code 
•Chef ensures each node in your 
infrastructure complies with the policy 
8
v1.1.0 
Our Goals 
9
Reliable, Repeatable Infrastructure 
• Everyone on the team gets the same config 
• Minimize surprises at deploy time 
• Test app code against real config 
• Test config with the app code 
10
Testing Chef 
• Chef is built on ruby 
• Test chef code with ruby tools like rspec 
• Integrate with Test Kitchen, ServerSpec, foodcritic 
11
v1.1.0 
My Tools 
12
Components of Workflow 
• My workstation - 3 year old mba 
• git, github repo 
• chefdk from downloads.getchef.com 
• Cloud box - CentOS 6.something 
• docker-io installed from peel 
• chefdk from downloads.getchef.com 
• kitchen-docker gem 
• git 
13
How I’m Working 
• Edit recipe code on my machine 
• my nice editor settings, not much RAM 
• Run local tests - syntax, style, lint, chef spec 
• Check into git, upload to repo 
• git pull on test box 
• kitchen converge, kitchen verify, kitchen test 
14
Simple Chef Recipe 
15 
package "httpd" 
! 
service "httpd" do 
action :start 
end 
! 
file "/var/www/html/index.html" do 
content "<h1>hello world</h1>n" 
end
v1.1.0 
What is Test Kitchen 
16
Test Kitchen 
• If Chef is code, we should be able to test it 
17
Test Kitchen 
•Test harness to execute code on one or more 
platforms 
•Driver plugins to allow your code to run on 
various cloud and virtualization providers 
•Includes support for many testing 
frameworks 
•Included with ChefDK 
18
.kitchen.yml 
•The configuration file for your Test Kitchen 
• driver – virtualization or cloud provider 
•provisioner – application to configure the node 
•platforms – target operating systems 
•suites – target configurations 
19
.kitchen.yml 
20 
--- 
driver: 
name: docker 
! 
provisioner: 
name: chef_zero 
! 
platforms: 
- name: centos-6.4 
driver_config: 
forward: 
- 80:80 
! 
suites: 
- name: default 
run_list: 
- recipe[apache::default] 
attributes:
.kitchen.yml 
• Create a docker container 
• With centos-6.4 
• I’m testing a webapp, so forward port 80 
• Provisions our environment with an apache 
webserver as configured by our team in a Chef 
recipe 
21
kitchen create 
-----> Creating <default-centos-64>...! 
build context to Docker daemon 2.048 kB! 
Sending build context to Docker daemon! 
Step 0 : FROM centos:centos6! 
Pulling repository centos! 
---> 70441cac1ed5! 
Step 1 : RUN yum clean all! 
---> Running in b4ed54c86150! 
Loaded plugins: fastestmirror! 
Cleaning repos: base extras updates! 
Cleaning up Everything! 
---> 1c333241ae96! 
Removing intermediate container b4ed54c86150! 
Step 2 : RUN yum install -y sudo openssh-server openssh-clients which curl 
22
docker ps 
[chef@CentOS63 codemesh]$ sudo docker ps! 
CONTAINER ID IMAGE COMMAND CREATED 
STATUS PORTS NAMES! 
9ddd3300de51 87f714782104 "/usr/sbin/sshd -D - 2 minutes ago 
Up 2 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:49157->22/tcp stoic_euclid 
23
kitchen converge 
- install version 2.2.15-39.el6.centos of package httpd! 
! 
* service[httpd] action start [2014-11-05T00:18:29+00:00] INFO: Processing 
service[httpd] action start (codemesh::default line 16)! 
[2014-11-05T00:18:29+00:00] INFO: service[httpd] started! 
! 
- start service service[httpd]! 
! 
* service[httpd] action enable [2014-11-05T00:18:29+00:00] INFO: Processing service[httpd] 
action enable (codemesh::default line 16)! 
[2014-11-05T00:18:29+00:00] INFO: service[httpd] enabled! 
! 
- enable service service[httpd]! 
! 
* file[/var/www/html/index.html] action create [2014-11-05T00:18:29+00:00] INFO: 
Processing file[/var/www/html/index.html] action create (codemesh::default line 20)! 
[2014-11-05T00:18:29+00:00] INFO: file[/var/www/html/index.html] created file /var/www/ 
html/index.html! 
24
kitchen list 
[chef@CentOS63 codemesh]$ kitchen list! 
Instance Driver Provisioner Last Action! 
default-centos-64 Docker ChefZero Converged! 
25
show me 
[chef@CentOS63 codemesh]$ curl localhost! 
<h1>hello world</h1>! 
[chef@CentOS63 codemesh]$ 
26
Other Plugins 
• kitchen-vagrant (included!)! 
• kitchen-docker! 
• kitchen-ec2! 
• kitchen-rackspace! 
• kitchen-gce! 
• kitchen-digitalocean! 
• kitchen-openstack! 
• kitchen-bluebox! 
• kitchen-joyent! 
• kitchen-lxc 
27
Reliable Configuration 
• Provision onto platforms that match production 
• Write Chef recipes once for everyone 
• Local development work 
• Testing systems 
• Production 
• Customize environments as necessary 
• ports, user accounts, backend services 
28
v1.1.0 
TDI: Test-Driven 
Infrastructure 
29
Testing Bits for Chef 
• Pre-testing (no running host) 
• rubocop: ruby style and syntax 
• ChefSpec: chef-specific unit testing 
• foodcritic: chef-specific logic and style rules 
• Post-testing (on a running host) 
• ServerSpec: integration testing on services 
• bats: unix system testing 
30
rubocop 
$ rubocop recipes/default.rb ! 
Inspecting 1 file! 
C! 
Offenses:! 
recipes/default.rb:17:11: C: Space inside square brackets detected.! 
action [ :start, :enable ]! 
^! 
recipes/default.rb:17:27: C: Space inside square brackets detected.! 
action [ :start, :enable ]! 
^! 
recipes/default.rb:20:6: C: Prefer single-quoted strings when you don't need 
string interpolation or special symbols.! 
file "/var/www/html/index.html" do! 
^^^^^^^^^^^^^^^^^^^^^^^^^^! 
1 file inspected, 3 offenses detected 
31
fix 
$ rubocop recipes/default.rb ! 
Inspecting 1 file! 
.! 
! 
1 file inspected, no offenses detected 
32
ChefSpec 
• Test the intention of your code 
• Built on rspec 
• Specific to Chef 
33
Methods 
• You can TDD infrastructure code 
• Keep on top of regressions 
• Give safe configurations to the whole team 
34
chefspec file 
require 'chefspec'! 
! 
describe 'codemesh::default' do! 
let(:chef_run) do! 
ChefSpec::Runner.new.converge(described_recipe)! 
end ! 
! 
it 'installs apache' do! 
expect(chef_run).to install_package('httpd')! 
end ! 
end! 
35
Run chefspec 
$ rspec spec/unit/default.rb ! 
.! 
! 
Finished in 0.00735 seconds (files 
took 1.85 seconds to load)! 
1 example, 0 failures! 
36
ServerSpec 
• Tests running systems 
• Run from Test Kitchen at the end of kitchen 
converge 
• Not dependent on Chef! 
• Use from other config tools 
• Use standalone! 
• serverspec.org 
37
Serverspec File 
require 'serverspec'! 
! 
set :backend, :exec! 
! 
describe 'apache' do! 
it "is installed" do! 
expect(package 'httpd').to 
be_installed! 
end ! 
it "is running" do! 
expect(service 'httpd').to be_running! 
end ! 
it "is listening on port 80" do! 
expect(port 80).to be_listening! 
end ! 
38 
it "displays a custom home page" 
do! 
expect(command("curl 
localhost").stdout).to match / 
hello/! 
end ! 
end!
kitchen verify 
apache! 
is installed! 
is running! 
is listening on port 80! 
displays a custom home page! 
! 
Finished in 1.45 seconds (files took 0.6278 seconds to 
load)! 
4 examples, 0 failures! 
Finished verifying <default-centos-64> (0m18.23s).! 
-----> Kitchen is finished. (0m39.50s)! 
39
test vs verify 
• kitchen verify will run tests on an instance that is 
already running 
• kitchen test will start a new instance, run all the 
chef recipes, run all the tests, and then destroy the 
instance 
• nice for different workflow - user watching a test vs a 
build server watching for a good return code 
• fast with Docker! 
40
v1.1.0 
Further Resources 
41
Container Ecosystem 
• Chef container resources help you manage 
container workflow 
• Create and deploy images 
• https://docs.getchef.com/containers.html 
42
Testing Tools 
• ChefSpec: https://docs.getchef.com/chefspec.html 
• Serverspec: http://serverspec.org/ 
• bats: https://github.com/sstephenson/bats 
• Foodcritic: 
• More on ChefDK: 
• http://foodfightshow.org/2014/08/chefdk.html 
• More on this workflow: 
• http://www.slideshare.net/misheska/testing-yourautomationcode- 
docker-version-v02 
43
Some URLs 
• http://getchef.com 
• http://docs.getchef.com 
• http://supermarket.getchef.com 
• http://youtube.com/getchef 
• http://lists.opscode.com 
• irc.freenode.net: #chef, #chef-hacking 
• Twitter: @chef #getchef, @learnchef #learnchef 
•Meetups in your city?!? 
44
v1.1.0 
Questions? 
45
46

Contenu connexe

Tendances

Automate your Development Environment with Vagrant & Chef
Automate your Development Environment with Vagrant & ChefAutomate your Development Environment with Vagrant & Chef
Automate your Development Environment with Vagrant & Chef
Michael Lihs
 

Tendances (20)

London Hashicorp Meetup #8 - Testing Programmable Infrastructure By Matt Long
London Hashicorp Meetup #8 -  Testing Programmable Infrastructure By Matt LongLondon Hashicorp Meetup #8 -  Testing Programmable Infrastructure By Matt Long
London Hashicorp Meetup #8 - Testing Programmable Infrastructure By Matt Long
 
Docker
DockerDocker
Docker
 
2019 Chef InSpec Jumpstart Part 2 of 2
2019 Chef InSpec Jumpstart Part 2 of 22019 Chef InSpec Jumpstart Part 2 of 2
2019 Chef InSpec Jumpstart Part 2 of 2
 
Baking docker using chef
Baking docker using chefBaking docker using chef
Baking docker using chef
 
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6
 
Vagrant and Chef on FOSSASIA 2014
Vagrant and Chef on FOSSASIA 2014Vagrant and Chef on FOSSASIA 2014
Vagrant and Chef on FOSSASIA 2014
 
Automated Infrastructure Testing
Automated Infrastructure TestingAutomated Infrastructure Testing
Automated Infrastructure Testing
 
Michelin Starred Cooking with Chef
Michelin Starred Cooking with ChefMichelin Starred Cooking with Chef
Michelin Starred Cooking with Chef
 
Server Installation and Configuration with Chef
Server Installation and Configuration with ChefServer Installation and Configuration with Chef
Server Installation and Configuration with Chef
 
2019 Chef InSpec Jumpstart Part 1 of 2
2019 Chef InSpec Jumpstart Part 1 of 22019 Chef InSpec Jumpstart Part 1 of 2
2019 Chef InSpec Jumpstart Part 1 of 2
 
Automate your Development Environment with Vagrant & Chef
Automate your Development Environment with Vagrant & ChefAutomate your Development Environment with Vagrant & Chef
Automate your Development Environment with Vagrant & Chef
 
Compliance as Code
Compliance as CodeCompliance as Code
Compliance as Code
 
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
 
Chef Fundamentals Training Series Module 2: Workstation Setup
Chef Fundamentals Training Series Module 2: Workstation SetupChef Fundamentals Training Series Module 2: Workstation Setup
Chef Fundamentals Training Series Module 2: Workstation Setup
 
Test Driven Infrastructure with Docker, Test Kitchen and Serverspec
Test Driven Infrastructure with Docker, Test Kitchen and ServerspecTest Driven Infrastructure with Docker, Test Kitchen and Serverspec
Test Driven Infrastructure with Docker, Test Kitchen and Serverspec
 
Compliance Automation with Inspec Part 4
Compliance Automation with Inspec Part 4Compliance Automation with Inspec Part 4
Compliance Automation with Inspec Part 4
 
Introduction to Chef - April 22 2015
Introduction to Chef - April 22 2015Introduction to Chef - April 22 2015
Introduction to Chef - April 22 2015
 
Using Nagios with Chef
Using Nagios with ChefUsing Nagios with Chef
Using Nagios with Chef
 
Introduction to Chef - Techsuperwomen Summit
Introduction to Chef - Techsuperwomen SummitIntroduction to Chef - Techsuperwomen Summit
Introduction to Chef - Techsuperwomen Summit
 
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
Node setup, resource, and recipes - Fundamentals Webinar Series Part 2
 

En vedette

En vedette (20)

Role of Pipelines in Continuous Delivery
Role of Pipelines in Continuous DeliveryRole of Pipelines in Continuous Delivery
Role of Pipelines in Continuous Delivery
 
Chef - Infrastructure Automation for the Masses
Chef - Infrastructure Automation for the Masses�Chef - Infrastructure Automation for the Masses�
Chef - Infrastructure Automation for the Masses
 
Testing Your Automation Code (Docker Version)
Testing Your Automation Code (Docker Version)Testing Your Automation Code (Docker Version)
Testing Your Automation Code (Docker Version)
 
Chef For OpenStack Overview
Chef For OpenStack OverviewChef For OpenStack Overview
Chef For OpenStack Overview
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to Chef
 
Velocity2011 chef-workshop
Velocity2011 chef-workshopVelocity2011 chef-workshop
Velocity2011 chef-workshop
 
IT Automation with Chef
IT Automation with ChefIT Automation with Chef
IT Automation with Chef
 
Infrastructure as Code with Chef / Puppet
Infrastructure as Code with Chef / PuppetInfrastructure as Code with Chef / Puppet
Infrastructure as Code with Chef / Puppet
 
Cook Infrastructure with chef -- Justeat.IN
Cook Infrastructure with chef  -- Justeat.INCook Infrastructure with chef  -- Justeat.IN
Cook Infrastructure with chef -- Justeat.IN
 
Chef
ChefChef
Chef
 
Devops madrid: successful case in AWS
Devops madrid: successful case in AWSDevops madrid: successful case in AWS
Devops madrid: successful case in AWS
 
Infrastructure as Code with Chef
Infrastructure as Code with ChefInfrastructure as Code with Chef
Infrastructure as Code with Chef
 
Infrastructure Automation with Chef & Ansible
Infrastructure Automation with Chef & AnsibleInfrastructure Automation with Chef & Ansible
Infrastructure Automation with Chef & Ansible
 
Chef introduction
Chef introductionChef introduction
Chef introduction
 
Introducing Chef | An IT automation for speed and awesomeness
Introducing Chef | An IT automation for speed and awesomenessIntroducing Chef | An IT automation for speed and awesomeness
Introducing Chef | An IT automation for speed and awesomeness
 
DevOps and Chef improve your life
DevOps and Chef improve your life DevOps and Chef improve your life
DevOps and Chef improve your life
 
Infrastructure Automation with Chef
Infrastructure Automation with ChefInfrastructure Automation with Chef
Infrastructure Automation with Chef
 
Overview of chef ( Infrastructure as a Code )
Overview of chef ( Infrastructure as a Code )Overview of chef ( Infrastructure as a Code )
Overview of chef ( Infrastructure as a Code )
 
Testing as a container
Testing as a containerTesting as a container
Testing as a container
 
Chef Cookbook Testing and Continuous Integration
Chef Cookbook Testing and Continuous IntegrationChef Cookbook Testing and Continuous Integration
Chef Cookbook Testing and Continuous Integration
 

Similaire à Testable Infrastructure with Chef, Test Kitchen, and Docker

Introduction To Continuous Compliance & Remediation
Introduction To Continuous Compliance & RemediationIntroduction To Continuous Compliance & Remediation
Introduction To Continuous Compliance & Remediation
Nicole Johnson
 
Practical introduction to dev ops with chef
Practical introduction to dev ops with chefPractical introduction to dev ops with chef
Practical introduction to dev ops with chef
LeanDog
 

Similaire à Testable Infrastructure with Chef, Test Kitchen, and Docker (20)

Testing your-automation-code (vagrant version) v0.2
Testing your-automation-code (vagrant version) v0.2Testing your-automation-code (vagrant version) v0.2
Testing your-automation-code (vagrant version) v0.2
 
Chef Cookbook Workflow
Chef Cookbook WorkflowChef Cookbook Workflow
Chef Cookbook Workflow
 
Testing Your Automation Code (Vagrant Version)
Testing Your Automation Code (Vagrant Version)Testing Your Automation Code (Vagrant Version)
Testing Your Automation Code (Vagrant Version)
 
Node object and roles - Fundamentals Webinar Series Part 3
Node object and roles - Fundamentals Webinar Series Part 3Node object and roles - Fundamentals Webinar Series Part 3
Node object and roles - Fundamentals Webinar Series Part 3
 
SCALE12X: Chef for OpenStack
SCALE12X: Chef for OpenStackSCALE12X: Chef for OpenStack
SCALE12X: Chef for OpenStack
 
The Environment Restaurant
The Environment RestaurantThe Environment Restaurant
The Environment Restaurant
 
Introduction To Continuous Compliance & Remediation
Introduction To Continuous Compliance & RemediationIntroduction To Continuous Compliance & Remediation
Introduction To Continuous Compliance & Remediation
 
Azure handsonlab
Azure handsonlabAzure handsonlab
Azure handsonlab
 
Introduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to ChefIntroduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to Chef
 
Chef: Smart infrastructure automation
Chef: Smart infrastructure automationChef: Smart infrastructure automation
Chef: Smart infrastructure automation
 
Compliance Automation with InSpec - Chef NYC Meetup - April 2017
Compliance Automation with InSpec - Chef NYC Meetup - April 2017Compliance Automation with InSpec - Chef NYC Meetup - April 2017
Compliance Automation with InSpec - Chef NYC Meetup - April 2017
 
Automated Deployment and Configuration Engines. Ansible
Automated Deployment and Configuration Engines. AnsibleAutomated Deployment and Configuration Engines. Ansible
Automated Deployment and Configuration Engines. Ansible
 
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
 
OSDC 2017 - Mandi Walls - Building security into your workflow with inspec
OSDC 2017 - Mandi Walls - Building security into your workflow with inspecOSDC 2017 - Mandi Walls - Building security into your workflow with inspec
OSDC 2017 - Mandi Walls - Building security into your workflow with inspec
 
Adding Security to Your Workflow with InSpec (MAY 2017)
Adding Security to Your Workflow with InSpec (MAY 2017)Adding Security to Your Workflow with InSpec (MAY 2017)
Adding Security to Your Workflow with InSpec (MAY 2017)
 
Practical introduction to dev ops with chef
Practical introduction to dev ops with chefPractical introduction to dev ops with chef
Practical introduction to dev ops with chef
 
OSDC 2017 | Building Security Into Your Workflow with InSpec by Mandi Walls
OSDC 2017 | Building Security Into Your Workflow with InSpec by Mandi WallsOSDC 2017 | Building Security Into Your Workflow with InSpec by Mandi Walls
OSDC 2017 | Building Security Into Your Workflow with InSpec by Mandi Walls
 
Introduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to ChefIntroduction to Infrastructure as Code & Automation / Introduction to Chef
Introduction to Infrastructure as Code & Automation / Introduction to Chef
 
Prescriptive System Security with InSpec
Prescriptive System Security with InSpecPrescriptive System Security with InSpec
Prescriptive System Security with InSpec
 
Prescriptive Security with InSpec - All Things Open 2019
Prescriptive Security with InSpec - All Things Open 2019Prescriptive Security with InSpec - All Things Open 2019
Prescriptive Security with InSpec - All Things Open 2019
 

Plus de Mandi Walls

Addo reducing trauma in organizations with SLOs and chaos engineering
Addo  reducing trauma in organizations with SLOs and chaos engineeringAddo  reducing trauma in organizations with SLOs and chaos engineering
Addo reducing trauma in organizations with SLOs and chaos engineering
Mandi Walls
 

Plus de Mandi Walls (20)

DOD Raleigh Gamedays with Chaos Engineering.pdf
DOD Raleigh Gamedays with Chaos Engineering.pdfDOD Raleigh Gamedays with Chaos Engineering.pdf
DOD Raleigh Gamedays with Chaos Engineering.pdf
 
Addo reducing trauma in organizations with SLOs and chaos engineering
Addo  reducing trauma in organizations with SLOs and chaos engineeringAddo  reducing trauma in organizations with SLOs and chaos engineering
Addo reducing trauma in organizations with SLOs and chaos engineering
 
Full Service Ownership
Full Service OwnershipFull Service Ownership
Full Service Ownership
 
PagerDuty: Best Practices for On Call Teams
PagerDuty: Best Practices for On Call TeamsPagerDuty: Best Practices for On Call Teams
PagerDuty: Best Practices for On Call Teams
 
InSpec at DevOps ATL Meetup January 22, 2020
InSpec at DevOps ATL Meetup January 22, 2020InSpec at DevOps ATL Meetup January 22, 2020
InSpec at DevOps ATL Meetup January 22, 2020
 
Using Chef InSpec for Infrastructure Security
Using Chef InSpec for Infrastructure SecurityUsing Chef InSpec for Infrastructure Security
Using Chef InSpec for Infrastructure Security
 
Adding Security to Your Workflow With InSpec - SCaLE17x
Adding Security to Your Workflow With InSpec - SCaLE17xAdding Security to Your Workflow With InSpec - SCaLE17x
Adding Security to Your Workflow With InSpec - SCaLE17x
 
Habitat talk at CodeMonsters Sofia, Bulgaria Nov 27 2018
Habitat talk at CodeMonsters Sofia, Bulgaria Nov 27 2018Habitat talk at CodeMonsters Sofia, Bulgaria Nov 27 2018
Habitat talk at CodeMonsters Sofia, Bulgaria Nov 27 2018
 
BuildStuff.LT 2018 InSpec Workshop
BuildStuff.LT 2018 InSpec WorkshopBuildStuff.LT 2018 InSpec Workshop
BuildStuff.LT 2018 InSpec Workshop
 
InSpec Workshop at Velocity London 2018
InSpec Workshop at Velocity London 2018InSpec Workshop at Velocity London 2018
InSpec Workshop at Velocity London 2018
 
DevOpsDays InSpec Workshop
DevOpsDays InSpec WorkshopDevOpsDays InSpec Workshop
DevOpsDays InSpec Workshop
 
Adding Security and Compliance to Your Workflow with InSpec
Adding Security and Compliance to Your Workflow with InSpecAdding Security and Compliance to Your Workflow with InSpec
Adding Security and Compliance to Your Workflow with InSpec
 
InSpec - June 2018 at Open28.be
InSpec - June 2018 at Open28.beInSpec - June 2018 at Open28.be
InSpec - June 2018 at Open28.be
 
habitat at docker bud
habitat at docker budhabitat at docker bud
habitat at docker bud
 
Ingite Slides for InSpec
Ingite Slides for InSpecIngite Slides for InSpec
Ingite Slides for InSpec
 
Habitat at LinuxLab IT
Habitat at LinuxLab ITHabitat at LinuxLab IT
Habitat at LinuxLab IT
 
InSpec Workshop DevSecCon 2017
InSpec Workshop DevSecCon 2017InSpec Workshop DevSecCon 2017
InSpec Workshop DevSecCon 2017
 
Habitat Workshop at Velocity London 2017
Habitat Workshop at Velocity London 2017Habitat Workshop at Velocity London 2017
Habitat Workshop at Velocity London 2017
 
InSpec Workflow for DevOpsDays Riga 2017
InSpec Workflow for DevOpsDays Riga 2017InSpec Workflow for DevOpsDays Riga 2017
InSpec Workflow for DevOpsDays Riga 2017
 
Habitat at SRECon
Habitat at SREConHabitat at SRECon
Habitat at SRECon
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Dernier (20)

What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Testable Infrastructure with Chef, Test Kitchen, and Docker

  • 1. 1
  • 2. v1.1.0 Building Testable Infrastructure with Chef, Test Kitchen, and Docker mandi walls mandi@getchef.com CodeMesh.io 5 November 2014 2
  • 3. whoami • Mandi Walls • Professional Services at Chef • @lnxchk 3
  • 4. v1.1.0 What is Chef 4
  • 5. Automation Platform • Creates a dependable view of your entire network’s state. • Can handle complex dependencies among the nodes of your network. • Is fault tolerant. • Is secure. • Can handle multiple platforms • Can manage cloud resources • Provides a foundation for innovation 5
  • 6. 6
  • 7. Infrastructure As Code • Programmatically provision and configure components • Treat like any other code base • Reconstruct business from code repository, data backup, and compute resources 7
  • 8. Policy-Based •You capture the policy for your infrastructure in code •Chef ensures each node in your infrastructure complies with the policy 8
  • 10. Reliable, Repeatable Infrastructure • Everyone on the team gets the same config • Minimize surprises at deploy time • Test app code against real config • Test config with the app code 10
  • 11. Testing Chef • Chef is built on ruby • Test chef code with ruby tools like rspec • Integrate with Test Kitchen, ServerSpec, foodcritic 11
  • 13. Components of Workflow • My workstation - 3 year old mba • git, github repo • chefdk from downloads.getchef.com • Cloud box - CentOS 6.something • docker-io installed from peel • chefdk from downloads.getchef.com • kitchen-docker gem • git 13
  • 14. How I’m Working • Edit recipe code on my machine • my nice editor settings, not much RAM • Run local tests - syntax, style, lint, chef spec • Check into git, upload to repo • git pull on test box • kitchen converge, kitchen verify, kitchen test 14
  • 15. Simple Chef Recipe 15 package "httpd" ! service "httpd" do action :start end ! file "/var/www/html/index.html" do content "<h1>hello world</h1>n" end
  • 16. v1.1.0 What is Test Kitchen 16
  • 17. Test Kitchen • If Chef is code, we should be able to test it 17
  • 18. Test Kitchen •Test harness to execute code on one or more platforms •Driver plugins to allow your code to run on various cloud and virtualization providers •Includes support for many testing frameworks •Included with ChefDK 18
  • 19. .kitchen.yml •The configuration file for your Test Kitchen • driver – virtualization or cloud provider •provisioner – application to configure the node •platforms – target operating systems •suites – target configurations 19
  • 20. .kitchen.yml 20 --- driver: name: docker ! provisioner: name: chef_zero ! platforms: - name: centos-6.4 driver_config: forward: - 80:80 ! suites: - name: default run_list: - recipe[apache::default] attributes:
  • 21. .kitchen.yml • Create a docker container • With centos-6.4 • I’m testing a webapp, so forward port 80 • Provisions our environment with an apache webserver as configured by our team in a Chef recipe 21
  • 22. kitchen create -----> Creating <default-centos-64>...! build context to Docker daemon 2.048 kB! Sending build context to Docker daemon! Step 0 : FROM centos:centos6! Pulling repository centos! ---> 70441cac1ed5! Step 1 : RUN yum clean all! ---> Running in b4ed54c86150! Loaded plugins: fastestmirror! Cleaning repos: base extras updates! Cleaning up Everything! ---> 1c333241ae96! Removing intermediate container b4ed54c86150! Step 2 : RUN yum install -y sudo openssh-server openssh-clients which curl 22
  • 23. docker ps [chef@CentOS63 codemesh]$ sudo docker ps! CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES! 9ddd3300de51 87f714782104 "/usr/sbin/sshd -D - 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:49157->22/tcp stoic_euclid 23
  • 24. kitchen converge - install version 2.2.15-39.el6.centos of package httpd! ! * service[httpd] action start [2014-11-05T00:18:29+00:00] INFO: Processing service[httpd] action start (codemesh::default line 16)! [2014-11-05T00:18:29+00:00] INFO: service[httpd] started! ! - start service service[httpd]! ! * service[httpd] action enable [2014-11-05T00:18:29+00:00] INFO: Processing service[httpd] action enable (codemesh::default line 16)! [2014-11-05T00:18:29+00:00] INFO: service[httpd] enabled! ! - enable service service[httpd]! ! * file[/var/www/html/index.html] action create [2014-11-05T00:18:29+00:00] INFO: Processing file[/var/www/html/index.html] action create (codemesh::default line 20)! [2014-11-05T00:18:29+00:00] INFO: file[/var/www/html/index.html] created file /var/www/ html/index.html! 24
  • 25. kitchen list [chef@CentOS63 codemesh]$ kitchen list! Instance Driver Provisioner Last Action! default-centos-64 Docker ChefZero Converged! 25
  • 26. show me [chef@CentOS63 codemesh]$ curl localhost! <h1>hello world</h1>! [chef@CentOS63 codemesh]$ 26
  • 27. Other Plugins • kitchen-vagrant (included!)! • kitchen-docker! • kitchen-ec2! • kitchen-rackspace! • kitchen-gce! • kitchen-digitalocean! • kitchen-openstack! • kitchen-bluebox! • kitchen-joyent! • kitchen-lxc 27
  • 28. Reliable Configuration • Provision onto platforms that match production • Write Chef recipes once for everyone • Local development work • Testing systems • Production • Customize environments as necessary • ports, user accounts, backend services 28
  • 29. v1.1.0 TDI: Test-Driven Infrastructure 29
  • 30. Testing Bits for Chef • Pre-testing (no running host) • rubocop: ruby style and syntax • ChefSpec: chef-specific unit testing • foodcritic: chef-specific logic and style rules • Post-testing (on a running host) • ServerSpec: integration testing on services • bats: unix system testing 30
  • 31. rubocop $ rubocop recipes/default.rb ! Inspecting 1 file! C! Offenses:! recipes/default.rb:17:11: C: Space inside square brackets detected.! action [ :start, :enable ]! ^! recipes/default.rb:17:27: C: Space inside square brackets detected.! action [ :start, :enable ]! ^! recipes/default.rb:20:6: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.! file "/var/www/html/index.html" do! ^^^^^^^^^^^^^^^^^^^^^^^^^^! 1 file inspected, 3 offenses detected 31
  • 32. fix $ rubocop recipes/default.rb ! Inspecting 1 file! .! ! 1 file inspected, no offenses detected 32
  • 33. ChefSpec • Test the intention of your code • Built on rspec • Specific to Chef 33
  • 34. Methods • You can TDD infrastructure code • Keep on top of regressions • Give safe configurations to the whole team 34
  • 35. chefspec file require 'chefspec'! ! describe 'codemesh::default' do! let(:chef_run) do! ChefSpec::Runner.new.converge(described_recipe)! end ! ! it 'installs apache' do! expect(chef_run).to install_package('httpd')! end ! end! 35
  • 36. Run chefspec $ rspec spec/unit/default.rb ! .! ! Finished in 0.00735 seconds (files took 1.85 seconds to load)! 1 example, 0 failures! 36
  • 37. ServerSpec • Tests running systems • Run from Test Kitchen at the end of kitchen converge • Not dependent on Chef! • Use from other config tools • Use standalone! • serverspec.org 37
  • 38. Serverspec File require 'serverspec'! ! set :backend, :exec! ! describe 'apache' do! it "is installed" do! expect(package 'httpd').to be_installed! end ! it "is running" do! expect(service 'httpd').to be_running! end ! it "is listening on port 80" do! expect(port 80).to be_listening! end ! 38 it "displays a custom home page" do! expect(command("curl localhost").stdout).to match / hello/! end ! end!
  • 39. kitchen verify apache! is installed! is running! is listening on port 80! displays a custom home page! ! Finished in 1.45 seconds (files took 0.6278 seconds to load)! 4 examples, 0 failures! Finished verifying <default-centos-64> (0m18.23s).! -----> Kitchen is finished. (0m39.50s)! 39
  • 40. test vs verify • kitchen verify will run tests on an instance that is already running • kitchen test will start a new instance, run all the chef recipes, run all the tests, and then destroy the instance • nice for different workflow - user watching a test vs a build server watching for a good return code • fast with Docker! 40
  • 42. Container Ecosystem • Chef container resources help you manage container workflow • Create and deploy images • https://docs.getchef.com/containers.html 42
  • 43. Testing Tools • ChefSpec: https://docs.getchef.com/chefspec.html • Serverspec: http://serverspec.org/ • bats: https://github.com/sstephenson/bats • Foodcritic: • More on ChefDK: • http://foodfightshow.org/2014/08/chefdk.html • More on this workflow: • http://www.slideshare.net/misheska/testing-yourautomationcode- docker-version-v02 43
  • 44. Some URLs • http://getchef.com • http://docs.getchef.com • http://supermarket.getchef.com • http://youtube.com/getchef • http://lists.opscode.com • irc.freenode.net: #chef, #chef-hacking • Twitter: @chef #getchef, @learnchef #learnchef •Meetups in your city?!? 44
  • 46. 46