86
@filippo Test Driven Infrastructures

Test driven infrastructure

Embed Size (px)

Citation preview

Page 1: Test driven infrastructure

@filippo

Test Driven Infrastructures

Page 3: Test driven infrastructure

agile methods + IT operations?

photo credit: https://www.flickr.com/photos/kalexanderson/6354182139/

Page 4: Test driven infrastructure
Page 5: Test driven infrastructure
Page 6: Test driven infrastructure

1. Test Driven Development2. Infrastructure as code3. From theory to practice

Page 7: Test driven infrastructure

Test Driven Development

photo credit: https://www.flickr.com/photos/pagedooley/4308431673/

Page 8: Test driven infrastructure

write a failing unit

test

make it pass

refactor

Page 9: Test driven infrastructure

the 3 laws of TDD

photo credit: https://www.flickr.com/photos/oldpatterns/5837733407/

Page 10: Test driven infrastructure

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

2You are not allowed to write any

more of a unit test than is sufficient to fail

Page 12: Test driven infrastructure

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

drives design

small incremental changes

continuous validation

rapid feedback

Page 14: Test driven infrastructure

why it works?

reduces complexitybreaking down a difficult problem into smaller

pieces

Page 15: Test driven infrastructure

listen to the tests if it’s hard to writethe design is probably wrong

Page 16: Test driven infrastructure
Page 17: Test driven infrastructure

-> Acceptance Test Driven Development

acceptance criteriaexamplesdefinition of done

Page 18: Test driven infrastructure

write a failing acceptance test

Page 19: Test driven infrastructure

write a failing acceptance test

Page 20: Test driven infrastructure

write a failing unit

test

make it pass

refactor

write a failing acceptance test

Page 21: Test driven infrastructure

write a failing unit

test

make it pass

refactor

write a failing acceptance test

Page 22: Test driven infrastructure

benefits of TDDhigher code quality

regression test suite

feedback and confidence

Page 23: Test driven infrastructure

more benefits of TDDimplicit acceptance criteria

executable documentation

prevent gold plating

Page 24: Test driven infrastructure

challengestakes time to learn

requires discipline

changing habits is hard

management does not perceive internal quality

Page 25: Test driven infrastructure

infrastructure as code

photo credit: https://www.flickr.com/photos/mwichary/2348383457/

Page 26: Test driven infrastructure

DevOps+

virtualization and cloud

->

infrastructure is code too

Page 27: Test driven infrastructure

programmatically configure and provision

everything versioned

business = code repository

+ data backup

+ compute resources

Page 28: Test driven infrastructure

collaboration + automation

knowledge sharing

tooling

power of text

Page 29: Test driven infrastructure

benefitsconsistency and repeatability

scalability

testability

maintainability

Page 30: Test driven infrastructure

challengesspaghetti code

duplication

fear of change

low quality

side effects and chain reactions

Page 31: Test driven infrastructure

TDD can help!

Page 32: Test driven infrastructure

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

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

from theory to practice

photo credit: https://www.flickr.com/photos/jurvetson/489257240/

Page 35: Test driven infrastructure

install and start Apache httpd server

exercise

Page 36: Test driven infrastructure

tools neededgit

chefdk

vagrant

virtualbox

(packer)

Page 37: Test driven infrastructure
Page 38: Test driven infrastructure

$ git init httpd-cookbook$ cd httpd-cookbook

Page 39: Test driven infrastructure
Page 40: Test driven infrastructure

$ chef generate cookbook httpd

Page 41: Test driven infrastructure

Test Kitchen

Page 42: Test driven infrastructure

$ kitchen init --driver=kitchen-vagrant

Page 43: Test driven infrastructure

---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

write an acceptance test

Page 45: Test driven infrastructure
Page 46: Test driven infrastructure

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

watch it fail

Page 48: Test driven infrastructure

$ kitchen test

Page 49: Test driven infrastructure

Service "apache2" should be running (FAILED - 1) Port "80" should be listening (FAILED - 2)

Page 50: Test driven infrastructure

write a unit test

Page 51: Test driven infrastructure

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

watch it fail

Page 53: Test driven infrastructure

$ chef exec rspec

Page 54: Test driven infrastructure

FFailures: 1) httpd::default installs apache2 packageFinished in 0.00957 seconds1 example, 1 failure

Page 55: Test driven infrastructure

make it pass

Page 56: Test driven infrastructure

package 'apache2' do action :installend

httpd-cookbook/recipes/default.rb

Page 57: Test driven infrastructure

$ chef exec rspec

Page 58: Test driven infrastructure

.Finished in 0.00946 seconds1 example, 0 failures

Page 59: Test driven infrastructure

refactor

Page 60: Test driven infrastructure

write a unit test

Page 61: Test driven infrastructure

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

watch it fail

Page 63: Test driven infrastructure

$ chef exec rspec

Page 64: Test driven infrastructure

.FFailures: 1) httpd::default starts apache2 serviceFinished in 0.02133 seconds2 example, 1 failure

Page 65: Test driven infrastructure

make it pass

Page 66: Test driven infrastructure

package 'apache2' do action :installend

service 'apache2' do action :startend

httpd-cookbook/recipes/default.rb

Page 67: Test driven infrastructure

$ chef exec rspec

Page 68: Test driven infrastructure

..Finished in 0.02041 seconds2 example, 0 failures

Page 69: Test driven infrastructure

refactor

Page 70: Test driven infrastructure

make acceptance test pass

Page 71: Test driven infrastructure

$ kitchen test

Page 72: Test driven infrastructure

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
Page 74: Test driven infrastructure

{ "variables": { "aws_access_key": null, "aws_secret_key": null },

...

packer.json - 1

Page 75: Test driven infrastructure

...

"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

... "provisioners": [ { "type": "chef-solo", "cookbook_paths": ["berks-cookbooks"], "run_list": ["httpd::default"] } ]}

packer.json - 3

Page 77: Test driven infrastructure

source 'https://api.berkshelf.com' cookbook 'httpd', path: './httpd-cookbook'

Berksfile

Page 78: Test driven infrastructure

$ 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

==> Builds finished. The artifacts of successful builds are: --> amazon-ebs: AMIs were created:

eu-west-1: ami-ac3199db

Page 80: Test driven infrastructure

next stepsmanage whole stack:

Terraform

Cloudformation

Heat

continuous delivery

Page 81: Test driven infrastructure

TDD is a powerful technique

Treat your infrastructure as code

You can start now

Page 82: Test driven infrastructure

risorse

Page 83: Test driven infrastructure
Page 84: Test driven infrastructure
Page 85: Test driven infrastructure
Page 86: Test driven infrastructure

thank you