SlideShare a Scribd company logo
Test-Driven
Infrastructure with Chef
Principles & Tools
@kaktusmimi
Today
① Testing Principles
② Chef Testing Tools
③ Continuous Integration
Write 

Test
Run Test

(it fails)
Write 

Code
Test 

Passes
Refactor
TDD
BDD
TDDverification of a unit of code
specification of how code should
„behave“
T
DD
pitfall
Acceptance

Tests
Integration

Tests
Unit

Tests
To
d
ay
Today
① Testing Principles
② Chef Testing Tools
③ Continuous Integration
✔
ChefSpec
• Unit Testing Framework for Chef Cookbooks
• Runs locally without converging a (virtual) machine
$ gem install chefspec
!"" site-cookbooks

   !"" pt_jenkins

#"" recipes

$ !"" default.rb

   !"" spec

!"" default_spec.rb

execute "Install Jenkins from Ports" do

command "cd #{node['pt_jenkins']['jenkins_port_dir']} && make install clean BATCH="YES""

not_if {File.exist?(node['pt_jenkins']['jenkins_war_file'])}

end



directory node['pt_jenkins']['jenkins_home'] do

owner node['pt_jenkins']['jenkins_user']

group node['pt_jenkins']['jenkins_group']

mode '0766'

action :create

end



node.default['user']['git_ssh_wrapper'] = "/tmp/git_ssh_wrapper"

file node['user']['git_ssh_wrapper'] do

owner node['pt_jenkins']['jenkins_user']

group node['pt_jenkins']['jenkins_group']

mode 0777

content "/usr/bin/env ssh -A -o 'StrictHostKeyChecking=no' $1 $2"

action :create

end



cookbook_file 'Copy private vagrant key' do

path "/home/vagrant/.ssh/id_rsa"

source 'vagrant_private_key'

owner 'vagrant'

group 'vagrant'

backup false

mode 0600

action :create

end



cookbook_file 'Copy SSH config' do

path "/home/vagrant/.ssh/config"

source 'ssh_config'

owner 'vagrant'

group 'vagrant'

backup false

mode 0600

action :create

end



service node['pt_jenkins']['jenkins_service'] do

supports :status => true, :restart => true, :reload => true

action [ :enable, :start ]

end
require 'chefspec'



RSpec.configure do |config|

config.cookbook_path = [‚cookbooks','site-cookbooks']

config.role_path = 'roles'

end



describe 'pt_jenkins::default' do



let(:chef_run) do

ChefSpec::SoloRunner.new do |node|

node.set['jenkins']['plugins'] = {}

node.set['user']['git_ssh_wrapper'] = '/tmp/git_ssh_wrapper'

end.converge(described_recipe)

end



it 'installs Jenkins from Ports' do

expect(chef_run).to run_execute('Install Jenkins from Ports')

end



it 'creates a ssh wrapper file' do

expect(chef_run).to create_file('/tmp/git_ssh_wrapper')

end



it 'copies the ssh config' do

expect(chef_run).to create_cookbook_file('Copy SSH config')

end



it 'copies the private Vagrant key' do

expect(chef_run).to create_cookbook_file('Copy private vagrant key')

end



it 'starts the Jenkins service' do

expect(chef_run).to start_service('jenkins')

end

end
$ time bundle exec rspec site-cookbooks/pt_jenkins
pt_jenkins::default
installs Jenkins from Ports
creates a ssh wrapper file
copies the ssh config
copies the private Vagrant key
starts the Jenkins service
Finished in 0.34419 seconds
4 examples, 0 failures
1.43s user 0.24s system 76% cpu 2.186 total
Pros & Cons ChefSpec
Pro Contra
Fast White Box Testing
Can be run without
convergence
Harder to implement
Easy Setup Some tricky configuration
Don’t know what system
looks like at the end
ServerSpec
• Describe desired state
• Check whether convergence result matches
expectations
• Platform independent
• Run it „from inside“ or „from outside“
$ gem install serverspec
$ serverspec-init
Select OS type:
1) UN*X
2) Windows
Select number: 1
Select a backend type:
1) SSH
2) Exec (local)
Select number: 1
Vagrant instance y/n: y
Auto-configure Vagrant from Vagrantfile? y/n: y
$ rake -T
rake spec:pttech # Run serverspec tests to pttech
#"" site-cookbooks

$  !"" spec

$ #"" pt

$ $ !"" jenkins_spec.rb

$    !"" spec_helper.rb

!"" Rakefile
require 'serverspec'

require 'net/ssh'

require 'tempfile'



set :backend, :ssh



if ENV['ASK_SUDO_PASSWORD']

begin

require 'highline/import'

rescue LoadError

fail "highline is not available. Try installing it."

end

set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }

else

set :sudo_password, ENV['SUDO_PASSWORD']

end



host = ENV['TARGET_HOST']



`vagrant up #{host}`



config = Tempfile.new('', Dir.tmpdir)

`vagrant ssh-config #{host} > #{config.path}`



options = Net::SSH::Config.for(host, [config.path])



options[:user] ||= Etc.getlogin



set :host, options[:host_name] || host

set :ssh_options, options
require 'rake'

require 'rspec/core/rake_task'



task :spec => 'spec:all'

task :default => :spec



namespace :spec do

targets = []

Dir.glob('./spec/*').each do |dir|

next unless File.directory?(dir)

targets << File.basename(dir)

end



task :all => targets

task :default => :all



targets.each do |target|

desc "Run serverspec tests on #{target}"

RSpec::Core::RakeTask.new(target.to_sym) do |t|

ENV['TARGET_HOST'] = target

t.pattern = "spec/#{target}/*_spec.rb"

end

end

end
$ rake
Server contains
- jenkins.war at the expected location
- a folder /usr/local/jenkins, owned by user jenkins
- a folder /usr/local/jenkins, with mode 766
jenkins_config = JSON.parse(File.open("#{File.dirname(__FILE__)}/../../roles/jenkins.json").read)

jenkins_config["override_attributes"]["jenkins"]["plugins"].each do | plugin_name, plugin_config |

describe "Plugin: #{plugin_name}" do



if plugin_config['enabled']

it "has expected .hpi file in plugins directory" do

expect(file("/usr/local/jenkins/plugins/#{plugin_name}.hpi")).to be_file

end

end



if plugin_config['pinned']

it "is marked as pinned and has the .hpi.pinned file in the plugins directory" do

expect(file("/usr/local/jenkins/plugins/#{plugin_name}.hpi.pinned")).to be_file

end

it "is listed as pinned in the Jenkins plugin status" do

expect(check_jenkins_plugin_pinned(plugin_name)).to be_truthy

end

else

it "isn't marked as pinned and has no .hpi.pinned file in the plugins directory" do

expect(file("/usr/local/jenkins/plugins/#{plugin_name}.hpi.pinned")).not_to be_file

end

end



if plugin_config['version']

it "is version #{plugin_config['version']}" do

expect(check_jenkins_plugin_version(plugin_name, plugin_config['version'])).to be_truthy

end

end



end

end
Pros & Cons ServerSpec
Pro Contra
Easy Setup
& well documented
No specific Feedback
Black Box Tests Requires running Machine
Small but mighty
command set
Slow
(Easily?) extendable
Can only be run once per
convergence Run
Test Kitchen
• Test and Infrastructure Management
• Interesting for Cookbook Development
• Quite some (configuration) overhead
• Good Tutorial
• Bad Documentation
---

driver:

name: vagrant

network:

- ["private_network", {ip: "10.20.30.41", netmask: "255.255.255.0"}]



provisioner:

name: chef_zero

require_chef_omnibus: false

client_rb:

cookbook_path: ["/var/chef/cookbooks", "/var/chef/site-cookbooks"]



platforms:

- name: FreeBSD-10-PtTech-TestKitchen

driver:

name: vagrant

box: nanobsd-hosting-amd64.box-20140917

box_url: http:/vagrantbox.es/some-box-to-use

synced_folders:

- [".", "/var/chef", "create: true, type: :nfs"]



suites:

- name: server

run_list:

- recipe[testkitchen]

- role[jenkins]

attributes:
Pros & Cons TestKitchen
Pro Contra
Handles complex Setups
„Touches“ your Machine
after Convergence
Multi-platform Testing for
Cookbook Development
No ServerSpec via ssh
implemented
„Standard“ in many
Tutorials
Requires machine startup
from Scratch
Many Plugins
Foodcritic
• Linting for Chef Cookbooks
• Complex Rule Set far beyond Code Indentation
• Goals (from Foodcritic Website)
• To make it easier to flag problems in your Cookbooks
• Faster feedback.
• Automate checks for common problems
$ gem install foodcritic
$ foodcritic site-cookbooks/pt_jenkins -t FC001
# FC001 Use strings in preference to symbols to access node attributes
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/attributes/default.rb:55
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/attributes/default.rb:56
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/recipes/default.rb:21
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/recipes/default.rb:97
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/recipes/default.rb:98
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/recipes/default.rb:109
FC001: Use strings in preference to symbols to access node attributes:

site-cookbooks/pt_jenkins/recipes/default.rb:141
Further Testing Tools
• BATS: Bash Automated Testing System
• Shell Scripts 😟
• Cucumber Chef
• Great idea, but prototype state only
• Last commit > 1 year ago 😟
Today
① Testing Principles
② Chef Testing Tools
③ Continuous Integration
✔
✔
Deployment Stage
Knife Upload to

Chef Server
Acceptance Stage
Serverspec Test
Commit Stage
Build Project
ChefSpec Tests
Foodcritic
triggers uploads
Today
① Testing Principles
② Chef Testing Tools
③ Continuous Integration
✔
✔
✔
http://serverspec.org/
https://github.com/sethvargo/chefspec
http://kitchen.ci/
http://acrmp.github.io/foodcritic/
https://sethvargo.com/unit-testing-chef-cookbooks/
http://leopard.in.ua/2013/12/01/chef-and-tdd/

More Related Content

What's hot

Testing for infra code using test-kitchen,docker,chef
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,chef
kamalikamj
 
Learning chef
Learning chefLearning chef
Learning chef
Jonathan Carrillo
 
Test Driven Development with Chef
Test Driven Development with ChefTest Driven Development with Chef
Test Driven Development with Chef
Simone Soldateschi
 
CLUG 2014-10 - Cookbook CI with Jenkins
CLUG 2014-10 - Cookbook CI with JenkinsCLUG 2014-10 - Cookbook CI with Jenkins
CLUG 2014-10 - Cookbook CI with Jenkins
Zachary Stevens
 
Baking docker using chef
Baking docker using chefBaking docker using chef
Baking docker using chef
Mukta Aphale
 
Vagrant introduction for Developers
Vagrant introduction for DevelopersVagrant introduction for Developers
Vagrant introduction for Developers
Antons Kranga
 
Infrastructure Automation with Chef
Infrastructure Automation with ChefInfrastructure Automation with Chef
Infrastructure Automation with Chef
Jonathan Weiss
 
Compliance as Code
Compliance as CodeCompliance as Code
Compliance as Code
Matt Ray
 
Tips and Tricks for Automating Windows with Chef
Tips and Tricks for Automating Windows with ChefTips and Tricks for Automating Windows with Chef
Tips and Tricks for Automating Windows with Chef
Chef Software, Inc.
 
Server Installation and Configuration with Chef
Server Installation and Configuration with ChefServer Installation and Configuration with Chef
Server Installation and Configuration with Chef
Raimonds Simanovskis
 
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
Chef
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to Chef
Pubudu Suharshan Perera
 
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
Introduction to Chef: Automate Your Infrastructure by Modeling It In CodeIntroduction to Chef: Automate Your Infrastructure by Modeling It In Code
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
Josh Padnick
 
Chef, Devops, and You
Chef, Devops, and YouChef, Devops, and You
Chef, Devops, and You
Bryan Berry
 
Chef introduction
Chef introductionChef introduction
Chef introduction
FENG Zhichao
 
Opscode Webinar: Cooking with Chef on Microsoft Windows
Opscode Webinar: Cooking with Chef on Microsoft WindowsOpscode Webinar: Cooking with Chef on Microsoft Windows
Opscode Webinar: Cooking with Chef on Microsoft Windows
Chef Software, Inc.
 
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
Chef Software, Inc.
 
Ansible Introduction
Ansible Introduction Ansible Introduction
Ansible Introduction
Robert Reiz
 
Chef Cookbook Workflow
Chef Cookbook WorkflowChef Cookbook Workflow
Chef Cookbook Workflow
Amazon Web Services
 
Vagrant to-aws-flow
Vagrant to-aws-flowVagrant to-aws-flow
Vagrant to-aws-flow
Kimberly Macias
 

What's hot (20)

Testing for infra code using test-kitchen,docker,chef
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,chef
 
Learning chef
Learning chefLearning chef
Learning chef
 
Test Driven Development with Chef
Test Driven Development with ChefTest Driven Development with Chef
Test Driven Development with Chef
 
CLUG 2014-10 - Cookbook CI with Jenkins
CLUG 2014-10 - Cookbook CI with JenkinsCLUG 2014-10 - Cookbook CI with Jenkins
CLUG 2014-10 - Cookbook CI with Jenkins
 
Baking docker using chef
Baking docker using chefBaking docker using chef
Baking docker using chef
 
Vagrant introduction for Developers
Vagrant introduction for DevelopersVagrant introduction for Developers
Vagrant introduction for Developers
 
Infrastructure Automation with Chef
Infrastructure Automation with ChefInfrastructure Automation with Chef
Infrastructure Automation with Chef
 
Compliance as Code
Compliance as CodeCompliance as Code
Compliance as Code
 
Tips and Tricks for Automating Windows with Chef
Tips and Tricks for Automating Windows with ChefTips and Tricks for Automating Windows with Chef
Tips and Tricks for Automating Windows 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
 
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
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to Chef
 
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
Introduction to Chef: Automate Your Infrastructure by Modeling It In CodeIntroduction to Chef: Automate Your Infrastructure by Modeling It In Code
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
 
Chef, Devops, and You
Chef, Devops, and YouChef, Devops, and You
Chef, Devops, and You
 
Chef introduction
Chef introductionChef introduction
Chef introduction
 
Opscode Webinar: Cooking with Chef on Microsoft Windows
Opscode Webinar: Cooking with Chef on Microsoft WindowsOpscode Webinar: Cooking with Chef on Microsoft Windows
Opscode Webinar: Cooking with Chef on Microsoft Windows
 
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
 
Ansible Introduction
Ansible Introduction Ansible Introduction
Ansible Introduction
 
Chef Cookbook Workflow
Chef Cookbook WorkflowChef Cookbook Workflow
Chef Cookbook Workflow
 
Vagrant to-aws-flow
Vagrant to-aws-flowVagrant to-aws-flow
Vagrant to-aws-flow
 

Similar to Test-Driven Infrastructure with Chef

Using Test Kitchen for testing Chef cookbooks
Using Test Kitchen for testing Chef cookbooksUsing Test Kitchen for testing Chef cookbooks
Using Test Kitchen for testing Chef cookbooks
Timur Batyrshin
 
Cooking with Chef on Windows: 2015 Edition
Cooking with Chef on Windows: 2015 EditionCooking with Chef on Windows: 2015 Edition
Cooking with Chef on Windows: 2015 Edition
Julian Dunn
 
Dev ops meetup
Dev ops meetupDev ops meetup
Dev ops meetup
Bigdata Meetup Kochi
 
EC2 AMI Factory with Chef, Berkshelf, and Packer
EC2 AMI Factory with Chef, Berkshelf, and PackerEC2 AMI Factory with Chef, Berkshelf, and Packer
EC2 AMI Factory with Chef, Berkshelf, and Packer
George Miranda
 
Modern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSDModern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSD
Sean Chittenden
 
Using Vagrant
Using VagrantUsing Vagrant
Using Vagrant
andygale
 
Continuous Integration/Deployment with Docker and Jenkins
Continuous Integration/Deployment with Docker and JenkinsContinuous Integration/Deployment with Docker and Jenkins
Continuous Integration/Deployment with Docker and Jenkins
Francesco Bruni
 
Ansible: How to Get More Sleep and Require Less Coffee
Ansible: How to Get More Sleep and Require Less CoffeeAnsible: How to Get More Sleep and Require Less Coffee
Ansible: How to Get More Sleep and Require Less Coffee
Sarah Z
 
Getting started with puppet and vagrant (1)
Getting started with puppet and vagrant (1)Getting started with puppet and vagrant (1)
Getting started with puppet and vagrant (1)
Puppet
 
Testing Your Automation Code (Vagrant Version)
Testing Your Automation Code (Vagrant Version)Testing Your Automation Code (Vagrant Version)
Testing Your Automation Code (Vagrant Version)
Mischa Taylor
 
Cookbook testing with KitcenCI and Serverrspec
Cookbook testing with KitcenCI and ServerrspecCookbook testing with KitcenCI and Serverrspec
Cookbook testing with KitcenCI and Serverrspec
Daniel Paulus
 
Bangpypers april-meetup-2012
Bangpypers april-meetup-2012Bangpypers april-meetup-2012
Bangpypers april-meetup-2012
Deepak Garg
 
Test Kitchen and Infrastructure as Code
Test Kitchen and Infrastructure as CodeTest Kitchen and Infrastructure as Code
Test Kitchen and Infrastructure as Code
Cybera Inc.
 
[MDBCI] Mariadb continuous integration tool
[MDBCI] Mariadb continuous integration tool[MDBCI] Mariadb continuous integration tool
[MDBCI] Mariadb continuous integration tool
OSLL
 
Testing Terraform
Testing TerraformTesting Terraform
Testing Terraform
Nathen Harvey
 
Antons Kranga Building Agile Infrastructures
Antons Kranga   Building Agile InfrastructuresAntons Kranga   Building Agile Infrastructures
Antons Kranga Building Agile Infrastructures
Antons Kranga
 
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
Sylvain Tissot
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
Suresh Kumar
 
Moving from Jenkins 1 to 2 declarative pipeline adventures
Moving from Jenkins 1 to 2 declarative pipeline adventuresMoving from Jenkins 1 to 2 declarative pipeline adventures
Moving from Jenkins 1 to 2 declarative pipeline adventures
Frits Van Der Holst
 
Mastering selenium for automated acceptance tests
Mastering selenium for automated acceptance testsMastering selenium for automated acceptance tests
Mastering selenium for automated acceptance tests
Nick Belhomme
 

Similar to Test-Driven Infrastructure with Chef (20)

Using Test Kitchen for testing Chef cookbooks
Using Test Kitchen for testing Chef cookbooksUsing Test Kitchen for testing Chef cookbooks
Using Test Kitchen for testing Chef cookbooks
 
Cooking with Chef on Windows: 2015 Edition
Cooking with Chef on Windows: 2015 EditionCooking with Chef on Windows: 2015 Edition
Cooking with Chef on Windows: 2015 Edition
 
Dev ops meetup
Dev ops meetupDev ops meetup
Dev ops meetup
 
EC2 AMI Factory with Chef, Berkshelf, and Packer
EC2 AMI Factory with Chef, Berkshelf, and PackerEC2 AMI Factory with Chef, Berkshelf, and Packer
EC2 AMI Factory with Chef, Berkshelf, and Packer
 
Modern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSDModern tooling to assist with developing applications on FreeBSD
Modern tooling to assist with developing applications on FreeBSD
 
Using Vagrant
Using VagrantUsing Vagrant
Using Vagrant
 
Continuous Integration/Deployment with Docker and Jenkins
Continuous Integration/Deployment with Docker and JenkinsContinuous Integration/Deployment with Docker and Jenkins
Continuous Integration/Deployment with Docker and Jenkins
 
Ansible: How to Get More Sleep and Require Less Coffee
Ansible: How to Get More Sleep and Require Less CoffeeAnsible: How to Get More Sleep and Require Less Coffee
Ansible: How to Get More Sleep and Require Less Coffee
 
Getting started with puppet and vagrant (1)
Getting started with puppet and vagrant (1)Getting started with puppet and vagrant (1)
Getting started with puppet and vagrant (1)
 
Testing Your Automation Code (Vagrant Version)
Testing Your Automation Code (Vagrant Version)Testing Your Automation Code (Vagrant Version)
Testing Your Automation Code (Vagrant Version)
 
Cookbook testing with KitcenCI and Serverrspec
Cookbook testing with KitcenCI and ServerrspecCookbook testing with KitcenCI and Serverrspec
Cookbook testing with KitcenCI and Serverrspec
 
Bangpypers april-meetup-2012
Bangpypers april-meetup-2012Bangpypers april-meetup-2012
Bangpypers april-meetup-2012
 
Test Kitchen and Infrastructure as Code
Test Kitchen and Infrastructure as CodeTest Kitchen and Infrastructure as Code
Test Kitchen and Infrastructure as Code
 
[MDBCI] Mariadb continuous integration tool
[MDBCI] Mariadb continuous integration tool[MDBCI] Mariadb continuous integration tool
[MDBCI] Mariadb continuous integration tool
 
Testing Terraform
Testing TerraformTesting Terraform
Testing Terraform
 
Antons Kranga Building Agile Infrastructures
Antons Kranga   Building Agile InfrastructuresAntons Kranga   Building Agile Infrastructures
Antons Kranga Building Agile Infrastructures
 
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
 
Ansible presentation
Ansible presentationAnsible presentation
Ansible presentation
 
Moving from Jenkins 1 to 2 declarative pipeline adventures
Moving from Jenkins 1 to 2 declarative pipeline adventuresMoving from Jenkins 1 to 2 declarative pipeline adventures
Moving from Jenkins 1 to 2 declarative pipeline adventures
 
Mastering selenium for automated acceptance tests
Mastering selenium for automated acceptance testsMastering selenium for automated acceptance tests
Mastering selenium for automated acceptance tests
 

Recently uploaded

Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Envertis Software Solutions
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
Drona Infotech
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
Ayan Halder
 

Recently uploaded (20)

Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Requirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional SafetyRequirement Traceability in Xen Functional Safety
Requirement Traceability in Xen Functional Safety
 

Test-Driven Infrastructure with Chef

  • 3. Today ① Testing Principles ② Chef Testing Tools ③ Continuous Integration
  • 4. Write 
 Test Run Test
 (it fails) Write 
 Code Test 
 Passes Refactor TDD
  • 5. BDD TDDverification of a unit of code specification of how code should „behave“
  • 6.
  • 10. Today ① Testing Principles ② Chef Testing Tools ③ Continuous Integration ✔
  • 11. ChefSpec • Unit Testing Framework for Chef Cookbooks • Runs locally without converging a (virtual) machine
  • 12. $ gem install chefspec
  • 13. !"" site-cookbooks
    !"" pt_jenkins
 #"" recipes
 $ !"" default.rb
    !"" spec
 !"" default_spec.rb

  • 14. execute "Install Jenkins from Ports" do
 command "cd #{node['pt_jenkins']['jenkins_port_dir']} && make install clean BATCH="YES""
 not_if {File.exist?(node['pt_jenkins']['jenkins_war_file'])}
 end
 
 directory node['pt_jenkins']['jenkins_home'] do
 owner node['pt_jenkins']['jenkins_user']
 group node['pt_jenkins']['jenkins_group']
 mode '0766'
 action :create
 end
 
 node.default['user']['git_ssh_wrapper'] = "/tmp/git_ssh_wrapper"
 file node['user']['git_ssh_wrapper'] do
 owner node['pt_jenkins']['jenkins_user']
 group node['pt_jenkins']['jenkins_group']
 mode 0777
 content "/usr/bin/env ssh -A -o 'StrictHostKeyChecking=no' $1 $2"
 action :create
 end
 
 cookbook_file 'Copy private vagrant key' do
 path "/home/vagrant/.ssh/id_rsa"
 source 'vagrant_private_key'
 owner 'vagrant'
 group 'vagrant'
 backup false
 mode 0600
 action :create
 end
 
 cookbook_file 'Copy SSH config' do
 path "/home/vagrant/.ssh/config"
 source 'ssh_config'
 owner 'vagrant'
 group 'vagrant'
 backup false
 mode 0600
 action :create
 end
 
 service node['pt_jenkins']['jenkins_service'] do
 supports :status => true, :restart => true, :reload => true
 action [ :enable, :start ]
 end
  • 15. require 'chefspec'
 
 RSpec.configure do |config|
 config.cookbook_path = [‚cookbooks','site-cookbooks']
 config.role_path = 'roles'
 end
 
 describe 'pt_jenkins::default' do
 
 let(:chef_run) do
 ChefSpec::SoloRunner.new do |node|
 node.set['jenkins']['plugins'] = {}
 node.set['user']['git_ssh_wrapper'] = '/tmp/git_ssh_wrapper'
 end.converge(described_recipe)
 end
 
 it 'installs Jenkins from Ports' do
 expect(chef_run).to run_execute('Install Jenkins from Ports')
 end
 
 it 'creates a ssh wrapper file' do
 expect(chef_run).to create_file('/tmp/git_ssh_wrapper')
 end
 
 it 'copies the ssh config' do
 expect(chef_run).to create_cookbook_file('Copy SSH config')
 end
 
 it 'copies the private Vagrant key' do
 expect(chef_run).to create_cookbook_file('Copy private vagrant key')
 end
 
 it 'starts the Jenkins service' do
 expect(chef_run).to start_service('jenkins')
 end
 end
  • 16. $ time bundle exec rspec site-cookbooks/pt_jenkins pt_jenkins::default installs Jenkins from Ports creates a ssh wrapper file copies the ssh config copies the private Vagrant key starts the Jenkins service Finished in 0.34419 seconds 4 examples, 0 failures 1.43s user 0.24s system 76% cpu 2.186 total
  • 17. Pros & Cons ChefSpec Pro Contra Fast White Box Testing Can be run without convergence Harder to implement Easy Setup Some tricky configuration Don’t know what system looks like at the end
  • 18. ServerSpec • Describe desired state • Check whether convergence result matches expectations • Platform independent • Run it „from inside“ or „from outside“
  • 19. $ gem install serverspec $ serverspec-init Select OS type: 1) UN*X 2) Windows Select number: 1 Select a backend type: 1) SSH 2) Exec (local) Select number: 1 Vagrant instance y/n: y Auto-configure Vagrant from Vagrantfile? y/n: y
  • 20. $ rake -T rake spec:pttech # Run serverspec tests to pttech
  • 21. #"" site-cookbooks
 $  !"" spec
 $ #"" pt
 $ $ !"" jenkins_spec.rb
 $    !"" spec_helper.rb
 !"" Rakefile
  • 22. require 'serverspec'
 require 'net/ssh'
 require 'tempfile'
 
 set :backend, :ssh
 
 if ENV['ASK_SUDO_PASSWORD']
 begin
 require 'highline/import'
 rescue LoadError
 fail "highline is not available. Try installing it."
 end
 set :sudo_password, ask("Enter sudo password: ") { |q| q.echo = false }
 else
 set :sudo_password, ENV['SUDO_PASSWORD']
 end
 
 host = ENV['TARGET_HOST']
 
 `vagrant up #{host}`
 
 config = Tempfile.new('', Dir.tmpdir)
 `vagrant ssh-config #{host} > #{config.path}`
 
 options = Net::SSH::Config.for(host, [config.path])
 
 options[:user] ||= Etc.getlogin
 
 set :host, options[:host_name] || host
 set :ssh_options, options
  • 23. require 'rake'
 require 'rspec/core/rake_task'
 
 task :spec => 'spec:all'
 task :default => :spec
 
 namespace :spec do
 targets = []
 Dir.glob('./spec/*').each do |dir|
 next unless File.directory?(dir)
 targets << File.basename(dir)
 end
 
 task :all => targets
 task :default => :all
 
 targets.each do |target|
 desc "Run serverspec tests on #{target}"
 RSpec::Core::RakeTask.new(target.to_sym) do |t|
 ENV['TARGET_HOST'] = target
 t.pattern = "spec/#{target}/*_spec.rb"
 end
 end
 end
  • 24. $ rake Server contains - jenkins.war at the expected location - a folder /usr/local/jenkins, owned by user jenkins - a folder /usr/local/jenkins, with mode 766
  • 25. jenkins_config = JSON.parse(File.open("#{File.dirname(__FILE__)}/../../roles/jenkins.json").read)
 jenkins_config["override_attributes"]["jenkins"]["plugins"].each do | plugin_name, plugin_config |
 describe "Plugin: #{plugin_name}" do
 
 if plugin_config['enabled']
 it "has expected .hpi file in plugins directory" do
 expect(file("/usr/local/jenkins/plugins/#{plugin_name}.hpi")).to be_file
 end
 end
 
 if plugin_config['pinned']
 it "is marked as pinned and has the .hpi.pinned file in the plugins directory" do
 expect(file("/usr/local/jenkins/plugins/#{plugin_name}.hpi.pinned")).to be_file
 end
 it "is listed as pinned in the Jenkins plugin status" do
 expect(check_jenkins_plugin_pinned(plugin_name)).to be_truthy
 end
 else
 it "isn't marked as pinned and has no .hpi.pinned file in the plugins directory" do
 expect(file("/usr/local/jenkins/plugins/#{plugin_name}.hpi.pinned")).not_to be_file
 end
 end
 
 if plugin_config['version']
 it "is version #{plugin_config['version']}" do
 expect(check_jenkins_plugin_version(plugin_name, plugin_config['version'])).to be_truthy
 end
 end
 
 end
 end
  • 26. Pros & Cons ServerSpec Pro Contra Easy Setup & well documented No specific Feedback Black Box Tests Requires running Machine Small but mighty command set Slow (Easily?) extendable Can only be run once per convergence Run
  • 27. Test Kitchen • Test and Infrastructure Management • Interesting for Cookbook Development • Quite some (configuration) overhead • Good Tutorial • Bad Documentation
  • 28. ---
 driver:
 name: vagrant
 network:
 - ["private_network", {ip: "10.20.30.41", netmask: "255.255.255.0"}]
 
 provisioner:
 name: chef_zero
 require_chef_omnibus: false
 client_rb:
 cookbook_path: ["/var/chef/cookbooks", "/var/chef/site-cookbooks"]
 
 platforms:
 - name: FreeBSD-10-PtTech-TestKitchen
 driver:
 name: vagrant
 box: nanobsd-hosting-amd64.box-20140917
 box_url: http:/vagrantbox.es/some-box-to-use
 synced_folders:
 - [".", "/var/chef", "create: true, type: :nfs"]
 
 suites:
 - name: server
 run_list:
 - recipe[testkitchen]
 - role[jenkins]
 attributes:
  • 29. Pros & Cons TestKitchen Pro Contra Handles complex Setups „Touches“ your Machine after Convergence Multi-platform Testing for Cookbook Development No ServerSpec via ssh implemented „Standard“ in many Tutorials Requires machine startup from Scratch Many Plugins
  • 30. Foodcritic • Linting for Chef Cookbooks • Complex Rule Set far beyond Code Indentation • Goals (from Foodcritic Website) • To make it easier to flag problems in your Cookbooks • Faster feedback. • Automate checks for common problems
  • 31.
  • 32. $ gem install foodcritic $ foodcritic site-cookbooks/pt_jenkins -t FC001 # FC001 Use strings in preference to symbols to access node attributes FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/attributes/default.rb:55 FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/attributes/default.rb:56 FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/recipes/default.rb:21 FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/recipes/default.rb:97 FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/recipes/default.rb:98 FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/recipes/default.rb:109 FC001: Use strings in preference to symbols to access node attributes:
 site-cookbooks/pt_jenkins/recipes/default.rb:141
  • 33. Further Testing Tools • BATS: Bash Automated Testing System • Shell Scripts 😟 • Cucumber Chef • Great idea, but prototype state only • Last commit > 1 year ago 😟
  • 34. Today ① Testing Principles ② Chef Testing Tools ③ Continuous Integration ✔ ✔
  • 35. Deployment Stage Knife Upload to
 Chef Server Acceptance Stage Serverspec Test Commit Stage Build Project ChefSpec Tests Foodcritic triggers uploads
  • 36. Today ① Testing Principles ② Chef Testing Tools ③ Continuous Integration ✔ ✔ ✔
  • 37.