test driven infrastructure
TRANSCRIPT
![Page 2: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/2.jpg)
Filippo Liverani@filippo
![Page 3: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/3.jpg)
agile methods + IT operations?
photo credit: https://www.flickr.com/photos/kalexanderson/6354182139/
![Page 4: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/4.jpg)
![Page 5: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/5.jpg)
![Page 6: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/6.jpg)
1. Test Driven Development2. Infrastructure as code3. From theory to practice
![Page 7: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/7.jpg)
Test Driven Development
photo credit: https://www.flickr.com/photos/pagedooley/4308431673/
![Page 8: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/8.jpg)
write a failing unit
test
make it pass
refactor
![Page 9: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/9.jpg)
the 3 laws of TDD
photo credit: https://www.flickr.com/photos/oldpatterns/5837733407/
![Page 10: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/10.jpg)
1
You are not allowed to write any production code unless it is to make a failing unit test pass
![Page 11: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/11.jpg)
2You are not allowed to write any
more of a unit test than is sufficient to fail
![Page 12: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/12.jpg)
3You are not allowed to write any
more production code than is sufficient to pass the one failing
unit test
![Page 13: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/13.jpg)
drives design
small incremental changes
continuous validation
rapid feedback
![Page 14: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/14.jpg)
why it works?
reduces complexitybreaking down a difficult problem into smaller
pieces
![Page 15: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/15.jpg)
listen to the tests if it’s hard to writethe design is probably wrong
![Page 16: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/16.jpg)
![Page 17: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/17.jpg)
-> Acceptance Test Driven Development
acceptance criteriaexamplesdefinition of done
![Page 18: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/18.jpg)
write a failing acceptance test
![Page 19: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/19.jpg)
write a failing acceptance test
![Page 20: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/20.jpg)
write a failing unit
test
make it pass
refactor
write a failing acceptance test
![Page 21: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/21.jpg)
write a failing unit
test
make it pass
refactor
write a failing acceptance test
![Page 22: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/22.jpg)
benefits of TDDhigher code quality
regression test suite
feedback and confidence
![Page 23: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/23.jpg)
more benefits of TDDimplicit acceptance criteria
executable documentation
prevent gold plating
![Page 24: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/24.jpg)
challengestakes time to learn
requires discipline
changing habits is hard
management does not perceive internal quality
![Page 25: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/25.jpg)
infrastructure as code
photo credit: https://www.flickr.com/photos/mwichary/2348383457/
![Page 26: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/26.jpg)
DevOps+
virtualization and cloud
->
infrastructure is code too
![Page 27: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/27.jpg)
programmatically configure and provision
everything versioned
business = code repository
+ data backup
+ compute resources
![Page 28: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/28.jpg)
collaboration + automation
knowledge sharing
tooling
power of text
![Page 29: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/29.jpg)
benefitsconsistency and repeatability
scalability
testability
maintainability
![Page 30: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/30.jpg)
challengesspaghetti code
duplication
fear of change
low quality
side effects and chain reactions
![Page 31: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/31.jpg)
TDD can help!
![Page 32: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/32.jpg)
testing what?
Acceptance testsPrototypes
Exploratory testingUsability testing
Unit testsIntegration tests
Performance testingSecurity testing
Business facing
Technology facing
Supportingthe
team
CritiqueProduct
credit: Brian Marik
![Page 33: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/33.jpg)
testing what?
Acceptance testsPrototypes
Exploratory testingUsability testing
Unit testsIntegration tests
Performance testingSecurity testing
Business facing
Technology facing
Supportingthe
team
CritiqueProduct
credit: Brian Marik
![Page 34: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/34.jpg)
from theory to practice
photo credit: https://www.flickr.com/photos/jurvetson/489257240/
![Page 35: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/35.jpg)
install and start Apache httpd server
exercise
![Page 36: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/36.jpg)
tools neededgit
chefdk
vagrant
virtualbox
(packer)
![Page 37: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/37.jpg)
![Page 38: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/38.jpg)
$ git init httpd-cookbook$ cd httpd-cookbook
![Page 39: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/39.jpg)
![Page 40: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/40.jpg)
$ chef generate cookbook httpd
![Page 41: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/41.jpg)
Test Kitchen
![Page 42: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/42.jpg)
$ kitchen init --driver=kitchen-vagrant
![Page 43: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/43.jpg)
---driver: name: vagrant
provisioner: name: chef_zero
platforms: - name: ubuntu-14.04
suites: - name: default run_list: - recipe[httpd::default]
httpd-cookbook/.kitchen.yml
![Page 44: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/44.jpg)
write an acceptance test
![Page 45: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/45.jpg)
![Page 46: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/46.jpg)
require 'spec_helper'
describe service('apache2') do it { should be_running }
describe port(80) do it { should be_listening } endend
httpd-cookbook/test/integration/server/serverspec/default_spec.rb
![Page 47: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/47.jpg)
watch it fail
![Page 48: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/48.jpg)
$ kitchen test
![Page 49: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/49.jpg)
Service "apache2" should be running (FAILED - 1) Port "80" should be listening (FAILED - 2)
![Page 50: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/50.jpg)
write a unit test
![Page 51: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/51.jpg)
require 'spec_helper'
describe 'httpd::default' do let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
it 'installs apache2 package' do expect(chef_run).to install_package('apache2') endend
httpd-cookbook/spec/unit/recipes/default_spec.rb
![Page 52: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/52.jpg)
watch it fail
![Page 53: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/53.jpg)
$ chef exec rspec
![Page 54: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/54.jpg)
FFailures: 1) httpd::default installs apache2 packageFinished in 0.00957 seconds1 example, 1 failure
![Page 55: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/55.jpg)
make it pass
![Page 56: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/56.jpg)
package 'apache2' do action :installend
httpd-cookbook/recipes/default.rb
![Page 57: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/57.jpg)
$ chef exec rspec
![Page 58: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/58.jpg)
.Finished in 0.00946 seconds1 example, 0 failures
![Page 59: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/59.jpg)
refactor
![Page 60: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/60.jpg)
write a unit test
![Page 61: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/61.jpg)
require 'spec_helper'
describe 'httpd::default' do let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }
it 'installs apache2 package' do expect(chef_run).to install_package('apache2') end it 'starts apache2 service' do expect(chef_run).to start_service('apache2') end
end
httpd-cookbook/spec/unit/recipes/default_spec.rb
![Page 62: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/62.jpg)
watch it fail
![Page 63: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/63.jpg)
$ chef exec rspec
![Page 64: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/64.jpg)
.FFailures: 1) httpd::default starts apache2 serviceFinished in 0.02133 seconds2 example, 1 failure
![Page 65: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/65.jpg)
make it pass
![Page 66: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/66.jpg)
package 'apache2' do action :installend
service 'apache2' do action :startend
httpd-cookbook/recipes/default.rb
![Page 67: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/67.jpg)
$ chef exec rspec
![Page 68: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/68.jpg)
..Finished in 0.02041 seconds2 example, 0 failures
![Page 69: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/69.jpg)
refactor
![Page 70: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/70.jpg)
make acceptance test pass
![Page 71: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/71.jpg)
$ kitchen test
![Page 72: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/72.jpg)
Service "apache2" should be running Port "80" should be listening Finished in 0.09441 seconds2 example, 0 failuresFinished verifying <default-ubuntu-1404> (0m6.52s).-----> Kitchen is finished. (0m31.77s)
![Page 73: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/73.jpg)
![Page 74: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/74.jpg)
{ "variables": { "aws_access_key": null, "aws_secret_key": null },
...
packer.json - 1
![Page 75: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/75.jpg)
...
"builders": [{ "type": "amazon-ebs", "access_key": "{{user `aws_access_key`}}", "secret_key": "{{user `aws_secret_key`}}", "region": "eu-west-1", "ami_virtualization_type": "hvm", "source_ami": "ami-28ff505f", "instance_type": "t2.micro", "ssh_username": "ubuntu", "ami_name": "httpd" }]
...
packer.json - 2
![Page 76: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/76.jpg)
... "provisioners": [ { "type": "chef-solo", "cookbook_paths": ["berks-cookbooks"], "run_list": ["httpd::default"] } ]}
packer.json - 3
![Page 77: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/77.jpg)
source 'https://api.berkshelf.com' cookbook 'httpd', path: './httpd-cookbook'
Berksfile
![Page 78: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/78.jpg)
$ berks vendor $ packer build \ -var 'aws_access_key=YOUR ACCESS KEY' \ -var 'aws_secret_key=YOUR SECRET KEY' \ packer.json
![Page 79: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/79.jpg)
==> Builds finished. The artifacts of successful builds are: --> amazon-ebs: AMIs were created:
eu-west-1: ami-ac3199db
![Page 80: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/80.jpg)
next stepsmanage whole stack:
Terraform
Cloudformation
Heat
continuous delivery
![Page 81: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/81.jpg)
TDD is a powerful technique
Treat your infrastructure as code
You can start now
![Page 82: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/82.jpg)
risorse
![Page 83: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/83.jpg)
![Page 84: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/84.jpg)
![Page 85: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/85.jpg)
![Page 86: Test driven infrastructure](https://reader031.vdocuments.mx/reader031/viewer/2022021918/58a606ed1a28abf6198b6ab3/html5/thumbnails/86.jpg)
thank you