Test Driven Development with Chef

Simone Soldateschi
Simone SoldateschiStaff Engineer at Slack um Slack
TDD WITH CHEF
FUN WITH FLAGS TESTING
Created by /Simone Soldateschi @soldasimo
( )
WHO AM I?
Simone Soldateschi @soldasimo
- Snr DevOps Engineer based in Sydney
- Leading DevOps Engineering Team at
- 15 years of experience as:
    - SysAdmin
    - SoftwareEng
    - SysEng
- Been DevOps'in for the last 5-ish years
Rackspace AU
WHAT IS TDD?
Test Driven Development
Meaning... write tests
see them fail
write code to pass tests
  ...still waiting...
SOUNDS WEIRD? ;)
WHY?
no PowerPoint-like presentation
using the mouse is boring
    and I don't have one
coding is fun
 
IT'S LIVE-DEMO, WITH TDD!
HOW?
Use TDD
Write Chef Cookbook
Deploy node.js web-app
Leverage Vagrant, Docker, or Cloud Server
Run this very presentation
    
Kinda recursive, isn't it? ;)
WHAT DO YOU USE?
 
"THE USUAL" DEPLOY WORKFLOW
start coding
compile it
deploy artifacts
looks good?
  wait   ...still waiting... any issues?
REACTIVE APPROACH
CHEF OVERVIEW
 
Test Driven Development with Chef
Test Driven Development with Chef
Test Driven Development with Chef
IN SHORT
code
test it
OK? push it
KO? fix it!
?
Test Driven Development with Chef
and many others
Berkshelf
Test Kitchen
Foodcritic
Default Ruby interpreter
$ which ruby
/usr/bin/ruby
Save you from headaches ( )Chef Docs
$ chef exec which ruby
/opt/chefdk/embedded/bin/ruby
Permanent settings
$ chef shell-init bash
export PATH="/opt/chefdk/bin:/Users/siso/.chefdk/gem/ruby/2.1.0/bin:/opt/chef
export GEM_ROOT="/opt/chefdk/embedded/lib/ruby/gems/2.1.0"
export GEM_HOME="/Users/siso/.chefdk/gem/ruby/2.1.0"
export GEM_PATH="/Users/siso/.chefdk/gem/ruby/2.1.0:/opt/chefdk/embedded/lib/
PROJECT SPECS
GOAL
Deploy this Node.js presentation
TASKS
Node.js should be installed
Checkout Git repository
Let the Internet access the web-server
Web-server should not run as root
Test Driven Development with Chef
Test Driven Development with Chef
Test Driven Development with Chef
Test Driven Development with Chef
LET'S COOK
 
Manually
$ cd cookbooks
$ mkdir -p foo/attributes foo/recipes
$ cat > foo/metadata.rb << EOF
> name 'foo'
> maintainer 'YOUR_COMPANY_NAME'
> maintainer_email 'YOUR_EMAIL'
> license 'All rights reserved'
> description 'Installs/Configures foo'
> long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
> version '0.1.0'
> EOF
...are you kidding me?!
knife
$ knife cookbook create foo
** Creating cookbook foo in /Users/siso/.chef/cookbooks
** Creating README for cookbook: foo
** Creating CHANGELOG for cookbook: foo
** Creating metadata for cookbook: foo
$ ls /Users/siso/.chef/cookbooks/foo/
CHANGELOG.md README.md attributes definitions files
libraries metadata.rb providers recipes resources
templates
Berkshelf
$ berks cookbook cheftdd
Get off on the right foot!
Berkshelf
$ berks cookbook cheftdd
...
create cheftdd/attributes
...
create cheftdd/recipes
...
create cheftdd/metadata.rb
...
create cheftdd/Berksfile
...
create .kitchen.yml
...
create test/integration/default
...
You must run `bundle install' to fetch any new gems.
create cheftdd/Vagrantfile
Berkshelf (full output)
$ berks cookbook cheftdd
create cheftdd/files/default
create cheftdd/templates/default
create cheftdd/attributes
create cheftdd/libraries
create cheftdd/providers
create cheftdd/recipes
create cheftdd/resources
create cheftdd/recipes/default.rb
create cheftdd/metadata.rb
create cheftdd/LICENSE
create cheftdd/README.md
create cheftdd/CHANGELOG.md
create cheftdd/Berksfile
create cheftdd/Thorfile
create cheftdd/chefignore
create cheftdd/.gitignore
GRAB RUBY GEMS
 
Bundler
$ bundle
Fetching gem metadata from https://rubygems.org/.......
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Installing CFPropertyList 2.3.1
...
Using vagrant-wrapper 2.0.2
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
RUBOCOP
A Ruby static code analyzer, based on the community Ruby
style guide.
$ rubocop
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.4.
Inspecting 2 files
..
2 files inspected, no offenses detected
All good!
is a lint tool for your Opscode Chef cookbooks.
FOODCRITIC
Foodcritic
is a simple ruby build program with capabilities similar
to make.
RAKE
Rake
CHECK STYLE
Use Rake
to run andRubocop Foodcritic
in the right environment
with Bundler
$ bundle exec rake style
RACK005: Cookbook metadata must have maintainer='Rackspace' maintainer_email=
RACK005: Cookbook metadata must have maintainer='Rackspace' maintainer_email=
RACK008: Files expected by Support must exist: Berksfile.lock:0
RACK009: Berksfile.lock must not be in .gitignore: .gitignore:11
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.4.
Running RuboCop...
Inspecting 2 files
..
2 files inspected, no offenses detected
Need to fix something
Change Maintainer and email, and try again:
$ bundle exec rake style
RACK008: Files expected by Support must exist: Berksfile.lock:0
RACK009: Berksfile.lock must not be in .gitignore: .gitignore:11
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.4.
Running RuboCop...
Inspecting 2 files
..
2 files inspected, no offenses detected
It's better
Generate Berkshelf.lock:
$ berks install
Resolving cookbook dependencies...
Fetching 'cheftdd' from source at .
Fetching cookbook index from https://supermarket.chef.io...
Using cheftdd (0.1.0) from source at .
and let Git handle it
Try again:
$ bundle exec rake style
warning: parser/current is loading parser/ruby21, which recognizes
warning: 2.1.5-compliant syntax, but you are running 2.1.4.
Running RuboCop...
Inspecting 2 files
..
2 files inspected, no offenses detected
OK!
Test Driven Development with Chef
write RSpec tests
check servers are configured correctly
RSpec
Behaviour Driven Development for Ruby
Making TDD Productive and Fun
Default recipe should
update the package-manager database
upgrade packages
Which is
# apt-get update
# apt-get upgrade
Then write a test
$ cat test/unit/spec/default_spec.rb
...
it 'executes apt-get update' do
expect(chef_run).to run_execute('apt-get update')
end
...
it fails
$ bundle exec rake spec
/opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec
F
Failures:
1) cheftdd::default executes apt-get update
Failure/Error: expect(chef_run).to run_execute('apt-get update')
expected "execute[apt-get update]" with action :run to be in Chef run.
# ./test/unit/spec/default_spec.rb:23:in `block (2 levels) in <top (requ
Finished in 0.15203 seconds (files took 2.39 seconds to load)
1 example, 1 failure
Write code to pass the test
#
# Cookbook Name:: cheftdd
# Recipe:: default
#
# Copyright (C) 2015 YOUR_NAME
#
# All rights reserved - Do Not Redistribute
#
execute "apt-get update/upgrade" do
command "apt-get update"
action :run
end
Run the test again
$ bundle exec rake spec
/opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec
.
Finished in 0.16453 seconds (files took 2.52 seconds to load)
1 example, 0 failures
ChefSpec Coverage report generated...
Total Resources: 1
Touched Resources: 1
Touch Coverage: 100.0%
You are awesome and so is your test coverage! Have a fantastic day!
it's OK!
'NODEJS' RECIPE
Install Node.js
+
deploy web-app
Test Driven Development with Chef
Deploy :reveal.js
install and
clone reveal.js
resolve dependencies
serve the slide-deck
Node.js Grunt
$ git clone https://github.com/hakimel/reveal.js.git
$ npm install
$ grunt serve --port 80
FILE-NAMING SCHEMA
recipes/nodejs.rb
⥥
test/unit/spec/nodejs_spec.rb
test/unit/spec/nodejs_spec.rb
it 'include nodejs recipe' do
expect(chef_run).to include_recipe('nodejs')
end
# Follow instructions to install reveal.js on https://github.com/hakimel/reve
it 'install grunt-cli' do
expect(chef_run).to run_execute('npm install -g grunt-cli')
end
it 'install git' do
expect(chef_run).to install_apt_package('git')
end
it 'clone reveal.js GitHub repository' do
expect(chef_run).to run_execute('git clone https://github.com/hakimel/revea
end
recipes/nodejs.rbtests fail:
$ bundle exec rake spec
/opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec
.FFFFF
Failures:
1) cheftdd::nodejs include nodejs recipe
Failure/Error: expect(chef_run).to include_recipe('nodejs')
expected ["cheftdd::nodejs"] to include "nodejs::default"
# ./test/unit/spec/nodejs_spec.rb:23:in `block (2 levels) in <top (requi
2) cheftdd::nodejs install grunt-cli
Failure/Error: expect(chef_run).to run_execute('npm install -g grunt-cli
expected "execute[npm install -g grunt-cli]" with action :run to be in
Install Node.js
#
# Cookbook Name:: cheftdd-demo
# Recipe:: nodejs
#
# Copyright (C) 2015 YOUR_NAME
#
# All rights reserved - Do Not Redistribute
#
#
# Install reveal.js - https://github.com/hakimel/reveal.js/
#
# install nodejs, from package by default
include_recipe 'nodejs'
execute 'install grunt-cli' do
Install reveal.js
package 'git' do
action :install
end
execute 'clone reveal.js GitHub repository' do
command 'git clone https://github.com/hakimel/reveal.js.git'
action :run
cwd '/opt'
not_if { File.exist?('/opt/reveal.js') }
end
Resolve dependencies
execute 'install npm packages' do
command 'npm install'
action :run
cwd '/opt/reveal.js'
end
execute 'start serving node.js app' do
command 'grunt serve &'
action :run
cwd '/opt/reveal.js'
end
Test nodejsrecipe:
$ bundle exec rake spec
/opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec
......
Finished in 1.06 seconds (files took 2.51 seconds to load)
6 examples, 0 failures
Great, it's OK!
INTEGRATION TESTING
TEST KITCHEN
Test Driven Development with Chef
- run Integration Tests
- awesome documentation
have a read!
.KITCHEN.YML
---
driver:
name: vagrant
customize:
memory: 1024
provisioner:
name: chef_zero
nodes_path: "test/integration/nodes"
require_chef_omnibus: 11.16.4
client_rb:
environment: _default
attributes:
testkitchen: true
openssh:
server:
password_authentication: 'yes'
permit_root_login: 'yes'
authorization:
sudo:
users: ['vagrant']
passwordless: true
platforms:
- name: ubuntu-14.04
suites:
- name: default
run_list:
attributes:
- name: nodejs
run_list:
- recipe[cheftdd::default]
- recipe[cheftdd::nodejs]
NODE LIFECYCLE
Current status of nodes:
$ kitchen list
Instance Driver Provisioner Last Action
nodejs-ubuntu-1404 Rackspace ChefZero Not Created
Create node
# kitchen create
Node status
$ kitchen list
Instance Driver Provisioner Last Action
nodejs-ubuntu-1404 Rackspace ChefZero Created
Converge node
# kitchen converge
CONVERGE NODE
run chef-client on node
apply recipes
Node status
$ kitchen list
Instance Driver Provisioner Last Action
nodejs-ubuntu-1404 Rackspace ChefZero Converged
INTEGRATION TESTS
FILE-NAMING SCHEMA
test/integration/SUITE/rspec/WHATEVER_spec.rb
⥥
KitchenCI SUITE: nodejs
⥥
test/integration/nodejs/rspec/nodejs_spec.rb
RECIPE
# install nodejs, from package by default
include_recipe 'nodejs'
TEST
describe command('nodejs --version') do
its(:stdout) { should match(/v0.10.37/) }
end
RECIPE
package 'git' do
action :install
end
TEST
describe package('git') do
it { should be_installed }
end
RECIPE
execute 'clone reveal.js GitHub repository' do
command 'git clone https://github.com/hakimel/reveal.js.git'
action :run
cwd '/opt'
not_if { File.exist?('/opt/reveal.js') }
end
TEST
describe file('/opt/reveal.js') do
it { should be_directory }
end
RECIPE
execute 'start serving node.js app' do
command 'grunt serve --port 80 &'
action :run
cwd '/opt/reveal.js'
end
TEST
describe port('80') do
it { should be_listening }
end
RECIPE
add_iptables_rule('INPUT', '-p tcp -m state --state NEW --dport 80 -
s 0.0.0.0/0 -j ACCEPT', 50, 'Allow the Internet access the web-serve
r')
TEST
describe iptables do
it { should have_rule('-A INPUT -p tcp -m state --state NEW -m tcp
--dport 80 -m comment --comment "Allow the Internet access the web-
server" -j ACCEPT').with_chain('INPUT') }
end
KITCHENCI - INTEGRATION TESTS
Eventually, run integration tests
# kitchen verify
$ kitchen verify
-----> Starting Kitchen (v1.3.1)
-----> Setting up <nodejs-ubuntu-1404>...
-----> Setting up Busser
...
-----> Verifying <nodejs-ubuntu-1404>...
...
-----> Running rspec test suite
...
Finished in 0.09244 seconds
4 examples, 0 failures
Finished verifying <nodejs-ubuntu-1404> (0m2.94s).
-----> Kitchen is finished. (0m11.06s)
Test Driven Development with Chef
Full output:
$ kitchen verify
-----> Starting Kitchen (v1.3.1)
-----> Setting up <nodejs-ubuntu-1404>...
-----> Setting up Busser
Creating BUSSER_ROOT in /tmp/busser
Creating busser binstub
Plugin rspec already installed
Finished setting up <nodejs-ubuntu-1404> (0m3.09s).
-----> Verifying <nodejs-ubuntu-1404>...
Removing /tmp/busser/suites/rspec
Uploading /tmp/busser/suites/rspec/Gemfile (mode=0644)
Uploading /tmp/busser/suites/rspec/nodejs_spec.rb (mode=0644)
Uploading /tmp/busser/suites/rspec/spec_helper.rb (mode=0644)
-----> Running rspec test suite
run PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/
Don't run Bundler as root. Bundler can ask for sudo if it is needed, a
installing your bundle as root will break this application for all non
ALL IN ONE GO
$ kitchen test
«ha ah»
$ kitchen test
⥥
$ kitchen destroy
⥥
$ kitchen create
⥥
$ kitchen converge
⥥
$ kitchen verify
⥥
$ kitchen destroy
RECAP
write unit-tests
⥥
implement features
run unit-tests
⥥
$ bundle exec rake style
⥥
$ bundle exec rake spec
KO? fix it!
OK? move on
write integration-tests
⥥
run integration-tests
$ kitchen create
⥥
$ kitchen converge
⥥
$ kitchen verify
⥥
KO? fix it!
⥥
OK? done
LINKS
- click links on this slide-deck
-
-
-
-
-
-
-
An Overview of Chef
Install Chef DK
TDD with Chef, by Nathen Harvey
reveal.js
TDD with Chef, by Nathen Harvey
DevOps Reactions on Tumblr
The Phoenix Project
Test Driven Development with Chef
Source Quantity
The Big Bang Theory 1
Breaking Bad 1
How I Met Your Mother 2
Matrix 1
1 von 95

Recomendados

Testing for infra code using test-kitchen,docker,chef von
Testing for infra code using  test-kitchen,docker,chefTesting for infra code using  test-kitchen,docker,chef
Testing for infra code using test-kitchen,docker,chefkamalikamj
2.8K views14 Folien
CLUG 2014-10 - Cookbook CI with Jenkins von
CLUG 2014-10 - Cookbook CI with JenkinsCLUG 2014-10 - Cookbook CI with Jenkins
CLUG 2014-10 - Cookbook CI with JenkinsZachary Stevens
2.2K views40 Folien
Testable Infrastructure with Chef, Test Kitchen, and Docker von
Testable Infrastructure with Chef, Test Kitchen, and DockerTestable Infrastructure with Chef, Test Kitchen, and Docker
Testable Infrastructure with Chef, Test Kitchen, and DockerMandi Walls
5.5K views46 Folien
How to Write Chef Cookbook von
How to Write Chef CookbookHow to Write Chef Cookbook
How to Write Chef Cookbookdevopsjourney
691 views15 Folien
Jenkins and Chef: Infrastructure CI and Automated Deployment von
Jenkins and Chef: Infrastructure CI and Automated DeploymentJenkins and Chef: Infrastructure CI and Automated Deployment
Jenkins and Chef: Infrastructure CI and Automated DeploymentDan Stine
56.2K views54 Folien
Continous delivery with Jenkins and Chef von
Continous delivery with Jenkins and ChefContinous delivery with Jenkins and Chef
Continous delivery with Jenkins and Chefdefrag2
1.6K views46 Folien

Más contenido relacionado

Was ist angesagt?

Server Installation and Configuration with Chef von
Server Installation and Configuration with ChefServer Installation and Configuration with Chef
Server Installation and Configuration with ChefRaimonds Simanovskis
3.4K views27 Folien
Community Cookbooks & further resources - Fundamentals Webinar Series Part 6 von
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 6Chef
10.9K views87 Folien
Automating your infrastructure with Chef von
Automating your infrastructure with ChefAutomating your infrastructure with Chef
Automating your infrastructure with ChefJohn Ewart
4.2K views103 Folien
Chef Cookbook Workflow von
Chef Cookbook WorkflowChef Cookbook Workflow
Chef Cookbook WorkflowAmazon Web Services
7.5K views77 Folien
Infrastructure Automation with Chef von
Infrastructure Automation with ChefInfrastructure Automation with Chef
Infrastructure Automation with ChefJonathan Weiss
5.9K views51 Folien
Automating Infrastructure with Chef von
Automating Infrastructure with ChefAutomating Infrastructure with Chef
Automating Infrastructure with ChefJennifer Davis
554 views168 Folien

Was ist angesagt?(20)

Community Cookbooks & further resources - Fundamentals Webinar Series Part 6 von Chef
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
Chef10.9K views
Automating your infrastructure with Chef von John Ewart
Automating your infrastructure with ChefAutomating your infrastructure with Chef
Automating your infrastructure with Chef
John Ewart4.2K views
Infrastructure Automation with Chef von Jonathan Weiss
Infrastructure Automation with ChefInfrastructure Automation with Chef
Infrastructure Automation with Chef
Jonathan Weiss5.9K views
Automating Infrastructure with Chef von Jennifer Davis
Automating Infrastructure with ChefAutomating Infrastructure with Chef
Automating Infrastructure with Chef
Jennifer Davis554 views
Introduction To Continuous Compliance & Remediation von Nicole Johnson
Introduction To Continuous Compliance & RemediationIntroduction To Continuous Compliance & Remediation
Introduction To Continuous Compliance & Remediation
Nicole Johnson665 views
Drupal Continuous Integration with Jenkins - The Basics von John Smith
Drupal Continuous Integration with Jenkins - The BasicsDrupal Continuous Integration with Jenkins - The Basics
Drupal Continuous Integration with Jenkins - The Basics
John Smith41.6K views
Michelin Starred Cooking with Chef von Jon Cowie
Michelin Starred Cooking with ChefMichelin Starred Cooking with Chef
Michelin Starred Cooking with Chef
Jon Cowie5.4K views
Chef Fundamentals Training Series Module 2: Workstation Setup von Chef Software, Inc.
Chef Fundamentals Training Series Module 2: Workstation SetupChef Fundamentals Training Series Module 2: Workstation Setup
Chef Fundamentals Training Series Module 2: Workstation Setup
Chef Software, Inc.17.5K views
Compliance Automation with Inspec Part 4 von Chef
Compliance Automation with Inspec Part 4Compliance Automation with Inspec Part 4
Compliance Automation with Inspec Part 4
Chef787 views
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012 von Patrick McDonnell
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Lessons from Etsy: Avoiding Kitchen Nightmares - #ChefConf 2012
Patrick McDonnell20.2K views
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut... von Chef Software, Inc.
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 Software, Inc.19.1K views
Vagrant and Chef on FOSSASIA 2014 von Michael Lihs
Vagrant and Chef on FOSSASIA 2014Vagrant and Chef on FOSSASIA 2014
Vagrant and Chef on FOSSASIA 2014
Michael Lihs2.3K views
Compliance Automation Workshop von Chef
Compliance Automation WorkshopCompliance Automation Workshop
Compliance Automation Workshop
Chef1.8K views

Similar a Test Driven Development with Chef

Cooking Perl with Chef: Real World Tutorial with Jitterbug von
Cooking Perl with Chef: Real World Tutorial with JitterbugCooking Perl with Chef: Real World Tutorial with Jitterbug
Cooking Perl with Chef: Real World Tutorial with JitterbugDavid Golden
5.2K views15 Folien
Cloud Automation with Opscode Chef von
Cloud Automation with Opscode ChefCloud Automation with Opscode Chef
Cloud Automation with Opscode ChefSri Ram
596 views31 Folien
Making TDD [Somewhat] Bearable on OpenStack von
Making TDD [Somewhat] Bearable on OpenStackMaking TDD [Somewhat] Bearable on OpenStack
Making TDD [Somewhat] Bearable on OpenStackHart Hoover
1.2K views58 Folien
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011) von
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Fabrice Bernhard
3.8K views52 Folien
Chef infrastructure as code - paris.rb von
Chef infrastructure as code - paris.rbChef infrastructure as code - paris.rb
Chef infrastructure as code - paris.rbNicolas Ledez
457 views45 Folien
Cook Infrastructure with chef -- Justeat.IN von
Cook Infrastructure with chef  -- Justeat.INCook Infrastructure with chef  -- Justeat.IN
Cook Infrastructure with chef -- Justeat.INRajesh Hegde
1.1K views33 Folien

Similar a Test Driven Development with Chef(20)

Cooking Perl with Chef: Real World Tutorial with Jitterbug von David Golden
Cooking Perl with Chef: Real World Tutorial with JitterbugCooking Perl with Chef: Real World Tutorial with Jitterbug
Cooking Perl with Chef: Real World Tutorial with Jitterbug
David Golden5.2K views
Cloud Automation with Opscode Chef von Sri Ram
Cloud Automation with Opscode ChefCloud Automation with Opscode Chef
Cloud Automation with Opscode Chef
Sri Ram596 views
Making TDD [Somewhat] Bearable on OpenStack von Hart Hoover
Making TDD [Somewhat] Bearable on OpenStackMaking TDD [Somewhat] Bearable on OpenStack
Making TDD [Somewhat] Bearable on OpenStack
Hart Hoover1.2K views
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011) von Fabrice Bernhard
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Adopt DevOps philosophy on your Symfony projects (Symfony Live 2011)
Fabrice Bernhard3.8K views
Chef infrastructure as code - paris.rb von Nicolas Ledez
Chef infrastructure as code - paris.rbChef infrastructure as code - paris.rb
Chef infrastructure as code - paris.rb
Nicolas Ledez457 views
Cook Infrastructure with chef -- Justeat.IN von Rajesh Hegde
Cook Infrastructure with chef  -- Justeat.INCook Infrastructure with chef  -- Justeat.IN
Cook Infrastructure with chef -- Justeat.IN
Rajesh Hegde1.1K views
Using Test Kitchen for testing Chef cookbooks von Timur Batyrshin
Using Test Kitchen for testing Chef cookbooksUsing Test Kitchen for testing Chef cookbooks
Using Test Kitchen for testing Chef cookbooks
Timur Batyrshin321 views
Cookbook testing with KitcenCI and Serverrspec von Daniel Paulus
Cookbook testing with KitcenCI and ServerrspecCookbook testing with KitcenCI and Serverrspec
Cookbook testing with KitcenCI and Serverrspec
Daniel Paulus528 views
The Modern Developer Toolbox von Pablo Godel
The Modern Developer ToolboxThe Modern Developer Toolbox
The Modern Developer Toolbox
Pablo Godel2.6K views
Linecook - A Chef Alternative von thinkerbot
Linecook - A Chef AlternativeLinecook - A Chef Alternative
Linecook - A Chef Alternative
thinkerbot1.2K views
Rspec and Capybara Intro Tutorial at RailsConf 2013 von Brian Sam-Bodden
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
Brian Sam-Bodden29.7K views
Head in the Clouds: Testing Infra as Code - Config Management 2020 von Peter Souter
Head in the Clouds: Testing Infra as Code - Config Management 2020Head in the Clouds: Testing Infra as Code - Config Management 2020
Head in the Clouds: Testing Infra as Code - Config Management 2020
Peter Souter474 views
InSpec Workshop at Velocity London 2018 von Mandi Walls
InSpec Workshop at Velocity London 2018InSpec Workshop at Velocity London 2018
InSpec Workshop at Velocity London 2018
Mandi Walls350 views
Continous Delivering a PHP application von Javier López
Continous Delivering a PHP applicationContinous Delivering a PHP application
Continous Delivering a PHP application
Javier López2.6K views
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit... von MongoDB
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
MongoDB World 2018: Tutorial - Got Dibs? Building a Real-Time Bidding App wit...
MongoDB672 views
Introduction to Chef von Knoldus Inc.
Introduction to ChefIntroduction to Chef
Introduction to Chef
Knoldus Inc.10.8K views
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview von Leo Lorieri
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
[EXTENDED] Ceph, Docker, Heroku Slugs, CoreOS and Deis Overview
Leo Lorieri2K views

Más de Simone Soldateschi

Codemotion Rome 2018 Docker Swarm Mode von
Codemotion Rome 2018 Docker Swarm ModeCodemotion Rome 2018 Docker Swarm Mode
Codemotion Rome 2018 Docker Swarm ModeSimone Soldateschi
414 views51 Folien
Immutable Systems in the AWS Cloud von
Immutable Systems in the AWS CloudImmutable Systems in the AWS Cloud
Immutable Systems in the AWS CloudSimone Soldateschi
300 views39 Folien
Build Chef development box from scratch von
Build Chef development box from scratchBuild Chef development box from scratch
Build Chef development box from scratchSimone Soldateschi
312 views35 Folien
Ansible - Crash course von
Ansible - Crash courseAnsible - Crash course
Ansible - Crash courseSimone Soldateschi
3.8K views66 Folien
Git Crash Course von
Git Crash CourseGit Crash Course
Git Crash CourseSimone Soldateschi
682 views57 Folien
PyCon Russia 2014 - Auto Scale in the Cloud von
PyCon Russia 2014 - Auto Scale in the CloudPyCon Russia 2014 - Auto Scale in the Cloud
PyCon Russia 2014 - Auto Scale in the CloudSimone Soldateschi
1.7K views80 Folien

Más de Simone Soldateschi(6)

Último

Marketing and Community Building in Web3 von
Marketing and Community Building in Web3Marketing and Community Building in Web3
Marketing and Community Building in Web3Federico Ast
12 views64 Folien
IETF 118: Starlink Protocol Performance von
IETF 118: Starlink Protocol PerformanceIETF 118: Starlink Protocol Performance
IETF 118: Starlink Protocol PerformanceAPNIC
297 views22 Folien
Is Entireweb better than Google von
Is Entireweb better than GoogleIs Entireweb better than Google
Is Entireweb better than Googlesebastianthomasbejan
12 views1 Folie
Building trust in our information ecosystem: who do we trust in an emergency von
Building trust in our information ecosystem: who do we trust in an emergencyBuilding trust in our information ecosystem: who do we trust in an emergency
Building trust in our information ecosystem: who do we trust in an emergencyTina Purnat
100 views18 Folien
DU Series - Day 4.pptx von
DU Series - Day 4.pptxDU Series - Day 4.pptx
DU Series - Day 4.pptxUiPathCommunity
106 views28 Folien
PORTFOLIO 1 (Bret Michael Pepito).pdf von
PORTFOLIO 1 (Bret Michael Pepito).pdfPORTFOLIO 1 (Bret Michael Pepito).pdf
PORTFOLIO 1 (Bret Michael Pepito).pdfbrejess0410
8 views6 Folien

Último(10)

Marketing and Community Building in Web3 von Federico Ast
Marketing and Community Building in Web3Marketing and Community Building in Web3
Marketing and Community Building in Web3
Federico Ast12 views
IETF 118: Starlink Protocol Performance von APNIC
IETF 118: Starlink Protocol PerformanceIETF 118: Starlink Protocol Performance
IETF 118: Starlink Protocol Performance
APNIC297 views
Building trust in our information ecosystem: who do we trust in an emergency von Tina Purnat
Building trust in our information ecosystem: who do we trust in an emergencyBuilding trust in our information ecosystem: who do we trust in an emergency
Building trust in our information ecosystem: who do we trust in an emergency
Tina Purnat100 views
PORTFOLIO 1 (Bret Michael Pepito).pdf von brejess0410
PORTFOLIO 1 (Bret Michael Pepito).pdfPORTFOLIO 1 (Bret Michael Pepito).pdf
PORTFOLIO 1 (Bret Michael Pepito).pdf
brejess04108 views
UiPath Document Understanding_Day 3.pptx von UiPathCommunity
UiPath Document Understanding_Day 3.pptxUiPath Document Understanding_Day 3.pptx
UiPath Document Understanding_Day 3.pptx
UiPathCommunity105 views
How to think like a threat actor for Kubernetes.pptx von LibbySchulze1
How to think like a threat actor for Kubernetes.pptxHow to think like a threat actor for Kubernetes.pptx
How to think like a threat actor for Kubernetes.pptx
LibbySchulze15 views

Test Driven Development with Chef

  • 1. TDD WITH CHEF FUN WITH FLAGS TESTING Created by /Simone Soldateschi @soldasimo
  • 2. ( ) WHO AM I? Simone Soldateschi @soldasimo - Snr DevOps Engineer based in Sydney - Leading DevOps Engineering Team at - 15 years of experience as:     - SysAdmin     - SoftwareEng     - SysEng - Been DevOps'in for the last 5-ish years Rackspace AU
  • 3. WHAT IS TDD? Test Driven Development Meaning... write tests see them fail write code to pass tests   ...still waiting... SOUNDS WEIRD? ;)
  • 4. WHY? no PowerPoint-like presentation using the mouse is boring     and I don't have one coding is fun   IT'S LIVE-DEMO, WITH TDD!
  • 5. HOW? Use TDD Write Chef Cookbook Deploy node.js web-app Leverage Vagrant, Docker, or Cloud Server Run this very presentation      Kinda recursive, isn't it? ;)
  • 6. WHAT DO YOU USE?  
  • 7. "THE USUAL" DEPLOY WORKFLOW start coding compile it deploy artifacts looks good?   wait   ...still waiting... any issues? REACTIVE APPROACH
  • 12. IN SHORT code test it OK? push it KO? fix it!
  • 13. ?
  • 15. and many others Berkshelf Test Kitchen Foodcritic
  • 16. Default Ruby interpreter $ which ruby /usr/bin/ruby
  • 17. Save you from headaches ( )Chef Docs $ chef exec which ruby /opt/chefdk/embedded/bin/ruby
  • 18. Permanent settings $ chef shell-init bash export PATH="/opt/chefdk/bin:/Users/siso/.chefdk/gem/ruby/2.1.0/bin:/opt/chef export GEM_ROOT="/opt/chefdk/embedded/lib/ruby/gems/2.1.0" export GEM_HOME="/Users/siso/.chefdk/gem/ruby/2.1.0" export GEM_PATH="/Users/siso/.chefdk/gem/ruby/2.1.0:/opt/chefdk/embedded/lib/
  • 19. PROJECT SPECS GOAL Deploy this Node.js presentation TASKS Node.js should be installed Checkout Git repository Let the Internet access the web-server Web-server should not run as root
  • 25. Manually $ cd cookbooks $ mkdir -p foo/attributes foo/recipes $ cat > foo/metadata.rb << EOF > name 'foo' > maintainer 'YOUR_COMPANY_NAME' > maintainer_email 'YOUR_EMAIL' > license 'All rights reserved' > description 'Installs/Configures foo' > long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) > version '0.1.0' > EOF
  • 27. knife $ knife cookbook create foo ** Creating cookbook foo in /Users/siso/.chef/cookbooks ** Creating README for cookbook: foo ** Creating CHANGELOG for cookbook: foo ** Creating metadata for cookbook: foo $ ls /Users/siso/.chef/cookbooks/foo/ CHANGELOG.md README.md attributes definitions files libraries metadata.rb providers recipes resources templates
  • 29. Get off on the right foot!
  • 30. Berkshelf $ berks cookbook cheftdd ... create cheftdd/attributes ... create cheftdd/recipes ... create cheftdd/metadata.rb ... create cheftdd/Berksfile ... create .kitchen.yml ... create test/integration/default ... You must run `bundle install' to fetch any new gems. create cheftdd/Vagrantfile
  • 31. Berkshelf (full output) $ berks cookbook cheftdd create cheftdd/files/default create cheftdd/templates/default create cheftdd/attributes create cheftdd/libraries create cheftdd/providers create cheftdd/recipes create cheftdd/resources create cheftdd/recipes/default.rb create cheftdd/metadata.rb create cheftdd/LICENSE create cheftdd/README.md create cheftdd/CHANGELOG.md create cheftdd/Berksfile create cheftdd/Thorfile create cheftdd/chefignore create cheftdd/.gitignore
  • 33. Bundler $ bundle Fetching gem metadata from https://rubygems.org/....... Fetching additional metadata from https://rubygems.org/.. Resolving dependencies... Using rake 10.4.2 Installing CFPropertyList 2.3.1 ... Using vagrant-wrapper 2.0.2 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
  • 34. RUBOCOP A Ruby static code analyzer, based on the community Ruby style guide.
  • 35. $ rubocop warning: parser/current is loading parser/ruby21, which recognizes warning: 2.1.5-compliant syntax, but you are running 2.1.4. Inspecting 2 files .. 2 files inspected, no offenses detected All good!
  • 36. is a lint tool for your Opscode Chef cookbooks. FOODCRITIC Foodcritic
  • 37. is a simple ruby build program with capabilities similar to make. RAKE Rake
  • 38. CHECK STYLE Use Rake to run andRubocop Foodcritic in the right environment with Bundler
  • 39. $ bundle exec rake style RACK005: Cookbook metadata must have maintainer='Rackspace' maintainer_email= RACK005: Cookbook metadata must have maintainer='Rackspace' maintainer_email= RACK008: Files expected by Support must exist: Berksfile.lock:0 RACK009: Berksfile.lock must not be in .gitignore: .gitignore:11 warning: parser/current is loading parser/ruby21, which recognizes warning: 2.1.5-compliant syntax, but you are running 2.1.4. Running RuboCop... Inspecting 2 files .. 2 files inspected, no offenses detected Need to fix something
  • 40. Change Maintainer and email, and try again: $ bundle exec rake style RACK008: Files expected by Support must exist: Berksfile.lock:0 RACK009: Berksfile.lock must not be in .gitignore: .gitignore:11 warning: parser/current is loading parser/ruby21, which recognizes warning: 2.1.5-compliant syntax, but you are running 2.1.4. Running RuboCop... Inspecting 2 files .. 2 files inspected, no offenses detected It's better
  • 41. Generate Berkshelf.lock: $ berks install Resolving cookbook dependencies... Fetching 'cheftdd' from source at . Fetching cookbook index from https://supermarket.chef.io... Using cheftdd (0.1.0) from source at . and let Git handle it
  • 42. Try again: $ bundle exec rake style warning: parser/current is loading parser/ruby21, which recognizes warning: 2.1.5-compliant syntax, but you are running 2.1.4. Running RuboCop... Inspecting 2 files .. 2 files inspected, no offenses detected OK!
  • 44. write RSpec tests check servers are configured correctly
  • 45. RSpec Behaviour Driven Development for Ruby Making TDD Productive and Fun
  • 46. Default recipe should update the package-manager database upgrade packages
  • 47. Which is # apt-get update # apt-get upgrade
  • 48. Then write a test $ cat test/unit/spec/default_spec.rb ... it 'executes apt-get update' do expect(chef_run).to run_execute('apt-get update') end ...
  • 49. it fails $ bundle exec rake spec /opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec F Failures: 1) cheftdd::default executes apt-get update Failure/Error: expect(chef_run).to run_execute('apt-get update') expected "execute[apt-get update]" with action :run to be in Chef run. # ./test/unit/spec/default_spec.rb:23:in `block (2 levels) in <top (requ Finished in 0.15203 seconds (files took 2.39 seconds to load) 1 example, 1 failure
  • 50. Write code to pass the test # # Cookbook Name:: cheftdd # Recipe:: default # # Copyright (C) 2015 YOUR_NAME # # All rights reserved - Do Not Redistribute # execute "apt-get update/upgrade" do command "apt-get update" action :run end
  • 51. Run the test again $ bundle exec rake spec /opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec . Finished in 0.16453 seconds (files took 2.52 seconds to load) 1 example, 0 failures ChefSpec Coverage report generated... Total Resources: 1 Touched Resources: 1 Touch Coverage: 100.0% You are awesome and so is your test coverage! Have a fantastic day! it's OK!
  • 54. Deploy :reveal.js install and clone reveal.js resolve dependencies serve the slide-deck Node.js Grunt $ git clone https://github.com/hakimel/reveal.js.git $ npm install $ grunt serve --port 80
  • 56. test/unit/spec/nodejs_spec.rb it 'include nodejs recipe' do expect(chef_run).to include_recipe('nodejs') end # Follow instructions to install reveal.js on https://github.com/hakimel/reve it 'install grunt-cli' do expect(chef_run).to run_execute('npm install -g grunt-cli') end it 'install git' do expect(chef_run).to install_apt_package('git') end it 'clone reveal.js GitHub repository' do expect(chef_run).to run_execute('git clone https://github.com/hakimel/revea end
  • 57. recipes/nodejs.rbtests fail: $ bundle exec rake spec /opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec .FFFFF Failures: 1) cheftdd::nodejs include nodejs recipe Failure/Error: expect(chef_run).to include_recipe('nodejs') expected ["cheftdd::nodejs"] to include "nodejs::default" # ./test/unit/spec/nodejs_spec.rb:23:in `block (2 levels) in <top (requi 2) cheftdd::nodejs install grunt-cli Failure/Error: expect(chef_run).to run_execute('npm install -g grunt-cli expected "execute[npm install -g grunt-cli]" with action :run to be in
  • 58. Install Node.js # # Cookbook Name:: cheftdd-demo # Recipe:: nodejs # # Copyright (C) 2015 YOUR_NAME # # All rights reserved - Do Not Redistribute # # # Install reveal.js - https://github.com/hakimel/reveal.js/ # # install nodejs, from package by default include_recipe 'nodejs' execute 'install grunt-cli' do
  • 59. Install reveal.js package 'git' do action :install end execute 'clone reveal.js GitHub repository' do command 'git clone https://github.com/hakimel/reveal.js.git' action :run cwd '/opt' not_if { File.exist?('/opt/reveal.js') } end
  • 60. Resolve dependencies execute 'install npm packages' do command 'npm install' action :run cwd '/opt/reveal.js' end execute 'start serving node.js app' do command 'grunt serve &' action :run cwd '/opt/reveal.js' end
  • 61. Test nodejsrecipe: $ bundle exec rake spec /opt/chefdk/embedded/bin/ruby -I/Users/siso/.chefdk/gem/ruby/2.1.0/gems/rspec ...... Finished in 1.06 seconds (files took 2.51 seconds to load) 6 examples, 0 failures Great, it's OK!
  • 65. - run Integration Tests - awesome documentation have a read!
  • 67. provisioner: name: chef_zero nodes_path: "test/integration/nodes" require_chef_omnibus: 11.16.4 client_rb: environment: _default attributes: testkitchen: true openssh: server: password_authentication: 'yes' permit_root_login: 'yes' authorization: sudo: users: ['vagrant'] passwordless: true
  • 69. suites: - name: default run_list: attributes: - name: nodejs run_list: - recipe[cheftdd::default] - recipe[cheftdd::nodejs]
  • 70. NODE LIFECYCLE Current status of nodes: $ kitchen list Instance Driver Provisioner Last Action nodejs-ubuntu-1404 Rackspace ChefZero Not Created
  • 72. Node status $ kitchen list Instance Driver Provisioner Last Action nodejs-ubuntu-1404 Rackspace ChefZero Created
  • 74. CONVERGE NODE run chef-client on node apply recipes
  • 75. Node status $ kitchen list Instance Driver Provisioner Last Action nodejs-ubuntu-1404 Rackspace ChefZero Converged
  • 77. RECIPE # install nodejs, from package by default include_recipe 'nodejs' TEST describe command('nodejs --version') do its(:stdout) { should match(/v0.10.37/) } end
  • 78. RECIPE package 'git' do action :install end TEST describe package('git') do it { should be_installed } end
  • 79. RECIPE execute 'clone reveal.js GitHub repository' do command 'git clone https://github.com/hakimel/reveal.js.git' action :run cwd '/opt' not_if { File.exist?('/opt/reveal.js') } end TEST describe file('/opt/reveal.js') do it { should be_directory } end
  • 80. RECIPE execute 'start serving node.js app' do command 'grunt serve --port 80 &' action :run cwd '/opt/reveal.js' end TEST describe port('80') do it { should be_listening } end
  • 81. RECIPE add_iptables_rule('INPUT', '-p tcp -m state --state NEW --dport 80 - s 0.0.0.0/0 -j ACCEPT', 50, 'Allow the Internet access the web-serve r') TEST describe iptables do it { should have_rule('-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -m comment --comment "Allow the Internet access the web- server" -j ACCEPT').with_chain('INPUT') } end
  • 82. KITCHENCI - INTEGRATION TESTS Eventually, run integration tests # kitchen verify
  • 83. $ kitchen verify -----> Starting Kitchen (v1.3.1) -----> Setting up <nodejs-ubuntu-1404>... -----> Setting up Busser ... -----> Verifying <nodejs-ubuntu-1404>... ... -----> Running rspec test suite ... Finished in 0.09244 seconds 4 examples, 0 failures Finished verifying <nodejs-ubuntu-1404> (0m2.94s). -----> Kitchen is finished. (0m11.06s)
  • 85. Full output: $ kitchen verify -----> Starting Kitchen (v1.3.1) -----> Setting up <nodejs-ubuntu-1404>... -----> Setting up Busser Creating BUSSER_ROOT in /tmp/busser Creating busser binstub Plugin rspec already installed Finished setting up <nodejs-ubuntu-1404> (0m3.09s). -----> Verifying <nodejs-ubuntu-1404>... Removing /tmp/busser/suites/rspec Uploading /tmp/busser/suites/rspec/Gemfile (mode=0644) Uploading /tmp/busser/suites/rspec/nodejs_spec.rb (mode=0644) Uploading /tmp/busser/suites/rspec/spec_helper.rb (mode=0644) -----> Running rspec test suite run PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/ Don't run Bundler as root. Bundler can ask for sudo if it is needed, a installing your bundle as root will break this application for all non
  • 86. ALL IN ONE GO $ kitchen test «ha ah»
  • 88. RECAP
  • 93. LINKS - click links on this slide-deck - - - - - - - An Overview of Chef Install Chef DK TDD with Chef, by Nathen Harvey reveal.js TDD with Chef, by Nathen Harvey DevOps Reactions on Tumblr The Phoenix Project
  • 95. Source Quantity The Big Bang Theory 1 Breaking Bad 1 How I Met Your Mother 2 Matrix 1