test-driven infrastructure with puppet, test kitchen, serverspec and rspec

57
1 #Dynatrace with Test Kitchen, Serverspec and RSpec Test-Driven Infrastructure Puppet Meetup

Upload: martin-etmajer

Post on 21-Feb-2017

3.486 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

1 #Dynatrace

with Test Kitchen, Serverspec and RSpecTest-Driven Infrastructure

PuppetMeetup

Page 2: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

2 #Dynatrace

Insertimage here Martin Etmajer

Senior Technology Strategist at Dynatrace

[email protected]

@metmajer

Page 3: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

3 #Dynatrace

Why Continuous Delivery?

Page 4: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

4 #Dynatrace

Why Continuous Delivery?

Page 5: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

5 #Dynatrace

Utmost Goal: Minimize Cycle Time

feature cycle time time

Customer Users

Page 6: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

6 #Dynatrace

Utmost Goal: Minimize Cycle Time

feature cycle time time

Customer minimize Users

Page 7: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

7 #Dynatrace

Utmost Goal: Minimize Cycle Time

feature cycle time time

Customer

This is when youcreate value!

minimize

Page 8: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

8 #Dynatrace

Utmost Goal: Minimize Cycle Time

feature cycle time time

Customer

You

This is when youcreate value!

minimize

Page 9: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

9 #Dynatrace

Utmost Goal: Minimize Cycle Time

feature cycle time time

Customer

You

minimizeIt’s about getting your features into your user’s hands

as quickly and confidently as possible!

Page 10: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

10 #Dynatrace

Align Development and IT Operations

IT OPERATIONS

DEVELOPMENT

time

Page 11: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

11 #Dynatrace

Align Development and IT Operations

IT OPERATIONS

DEVELOPMENT

current iteration(e.g. 2 weeks)

time

Page 12: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

12 #Dynatrace

Align Development and IT Operations

IT OPERATIONS

DEVELOPMENT

current iteration(e.g. 2 weeks)

time

Planning

Page 13: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

13 #Dynatrace

Align Development and IT Operations

IT OPERATIONS

DEVELOPMENT

current iteration(e.g. 2 weeks)

time

PlanningImplementing

and testing

Page 14: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

14 #Dynatrace

Align Development and IT Operations

IT OPERATIONS

DEVELOPMENT

current iteration(e.g. 2 weeks)

time

PlanningImplementing

and testingWorking and

deployable code

Page 15: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

15 #Dynatrace

Why Test-Driven Infrastructure?

Page 16: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

16 #Dynatrace

You write code!

Page 17: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

17 #Dynatrace

“Make it work. Make it right. Make it fast.”Kent Beck, Creator of Extreme Programming and Test-Driven Development

Get the code tooperate correctly

Make the code clear,enforce good design Optimize

Page 18: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

18 #Dynatrace

The Red, Green, Refactor Cycle of TDD

Write a Failing Test

Make the Test PassClean Up your Code

Small increments

Able to return to known working code

Designed andtested code

Protects against regressions

Page 19: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

19 #Dynatrace

Test KitchenKey Concepts

Pluggable Architecture

Page 20: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

20 #Dynatrace

» Drivers» Platforms» Provisioners» Test Suites

Key Concepts

Page 21: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

21 #Dynatrace

» Drivers» Platforms» Provisioners» Test Suites

Key Concepts

Page 22: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

22 #Dynatrace

Drivers let you run your code on various...

Cloud Providers» Azure, Cloud Stack, EC2, Digital Ocean, GCE,

Rackspace,...

Virtualization Technologies» Vagrant, Docker, LXC

Test Kitchen: Drivers

Page 23: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

23 #Dynatrace

» Drivers» Platforms» Provisioners» Test Suites

Key Concepts

Page 24: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

24 #Dynatrace

Platforms are the Operating Systems you want to run on.

Platforms» Linux- or Windows-based (since Test Kitchen 1.4.0)

How to manage dependencies?» Automatically resolved when using Docker or Vagrant» Build your own and link them in the configuration file

Test Kitchen: Platforms

Page 25: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

25 #Dynatrace

» Drivers» Platforms» Provisioners» Test Suites

Key Concepts

Page 26: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

26 #Dynatrace

Provisioners are the tools used to converge the environment.

Provisioners» Ansible, Chef, CFEngine, Puppet, SaltStack

Why cool?» Useful if you need to support multiple of these tools

Test Kitchen: Provisioners

Page 27: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

27 #Dynatrace

» Drivers» Platforms» Provisioners» Test Suites

Key Concepts

Page 28: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

28 #Dynatrace

Test Suites define the tests to run against each platform.

Test Frameworks» Bash, Bats, Cucumber, RSpec, Serverspec

Test Kitchen: Test Suites

Page 29: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

29 #Dynatrace

Test KitchenInstallation

Page 30: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

30 #Dynatrace

Installation

Ready?

$ gem install test-kitchen kitchen-docker kitchen-puppet

Test Kitchen: Installation

$ kitchen versionTest Kitchen version 1.4.2

Page 31: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

31 #Dynatrace

Test KitchenConfiguration

Page 32: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

32 #Dynatrace

Create a Puppet Module

Initialize Test Kitchen

$ mkdir –p my-puppet-module$ cd my-puppet-module

Test Kitchen: Testing a Puppet Module

$ kitchen init --driver=docker --provisioner=puppet_applycreate .kitchen.ymlcreate test/integration/default

Configuration goes here!Tests go here!

Page 33: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

33 #Dynatrace

.kitchen.yml (as provided via `kitchen init`)---driver: name: dockerprovisioner: name: puppet_applyplatforms: - name: ubuntu-14.04 - name: centos-7.1suites: - name: default run_list: attributes:

Test Kitchen: Testing a Puppet Module

Names resolve toDocker Images onthe Docker Hub

Page 34: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

34 #Dynatrace

.kitchen.yml (slightly adjusted)---driver: name: dockerprovisioner: name: puppet_apply puppet_version: latest files_path: files manifests_path: test/integration manifest: default/init.pp puppet_verbose: true puppet_debug: false

Test Kitchen: Testing a Puppet Module

platforms: - name: ubuntu-14.04 - name: centos-7.1suites: - name: default

Page 35: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

35 #Dynatrace

$ kitchen list

Instance Driver Provisioner Transport Last Actiondefault-ubuntu-1404 Docker PuppetApply Ssh <Not Created>default-centos-71 Docker PuppetApply Ssh <Not Created>

`kitchen list`: List Test Kitchen Instances

Test Kitchen: Installation

This will change...Test Suite Platform

Page 36: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

36 #Dynatrace

Test KitchenWrite an Integration Test

Page 37: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

37 #Dynatrace

Create a Puppet Manifest for Test Suite ‘default’

Test Kitchen: Testing a Puppet Module

$ kitchen init --driver=docker --provisioner=puppet_applycreate .kitchen.ymlcreate test/integration/default

Configuration goes here!Tests go here!

Page 38: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

38 #Dynatrace

test/integration/default/init.ppclass { ‘foo’: }

class { ‘bar‘: bar_param => ..., require => Class[‘foo’]}

class { ‘baz’: baz_param => .., require => Class[‘bar’]}...

Test Kitchen: Testing a Puppet Module

Create yourenvironment

Puppet Manifest

Test Suite

Page 39: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

39 #Dynatrace

Serverspec and RSpecA Short Primer

Page 40: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

40 #Dynatrace

RSpec is a TDD tool for Ruby programmers.

RSpec

require ‘foo’

describe Foo do before do @foo = Foo.new end

it ‘method #bar does something useful’ do @foo.bar.should eq ‘something useful’ endend

Page 41: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

41 #Dynatrace

Serverspec is RSpec for your infrastructure.

Serverspec

require ‘serverspec’

describe user(‘foo’) do it { should exist } it { should belong_to_group ‘foo’ }end

describe file(‘/opt/bar’) do it { should be_directory } it { should be_mode 777 } it { should be_owned_by ‘foo’ } it { should be_grouped_into ‘foo’ }end

describe service(‘bar’) do it { should be_enabled } it { should be_running }end

describe port(8080) do it { should be_listening }end

describe command(‘apachectl –M’) do its(:stdout) { should contain(‘proxy_module’) }end

Resource

Matcher

Page 42: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

42 #Dynatrace

test/integration/default/serverspec/default_spec.rbrequire ‘serverspec’

describe user(‘foo’) do it { should exist } it { should belong_to_group ‘foo’ }end

Test Kitchen: Testing a Puppet Module

Test

Test Suite Do Serverspec!

Page 43: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

43 #Dynatrace

`kitchen test`: Run Test Kitchen Test

Test Kitchen: Testing a Puppet Module

$ kitchen test default-ubuntu-1404$ kitchen test ubuntu$ kitchen test regex!

$ kitchen list

Instance Driver Provisioner Transport Last Actiondefault-ubuntu-1404 Docker PuppetApply Ssh <Not Created>default-centos-71 Docker PuppetApply Ssh <Not Created>

Page 44: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

44 #Dynatrace

Test Kitchen: Actions

Test = Converge Setup Verify

Instance createdand provisioned

Instance ready for verification(dependencies installed)

Page 45: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

45 #Dynatrace

`kitchen test`: Run Test Kitchen Test$ kitchen test ubuntu

...User "foo" should exist should belong to group "foo"

Finished in 0.14825 seconds (files took 0.6271 seconds to load)2 examples, 0 failures

Finished verifying <default-ubuntu-1404> (0m37.21s).

Test Kitchen: Testing a Puppet Module

Page 46: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

46 #Dynatrace

`kitchen list`: List Test Kitchen Instances

Test Kitchen: Testing a Puppet Module

$ kitchen list

Instance Driver Provisioner Transport Last Actiondefault-ubuntu-1404 Docker PuppetApply Ssh Verifieddefault-centos-71 Docker PuppetApply Ssh <Not Created>

Page 47: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

47 #Dynatrace

Test Kitchen with PuppetAdvanced Tips

Page 48: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

48 #Dynatrace

Testing Puppet Modulesin Amazon EC2

Page 49: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

49 #Dynatrace

.kitchen.yml---driver: name: ec2 aws_access_key_id: "<%= ENV['AWS_ACCESS_KEY_ID']%>" aws_secret_access_key: "<%= ENV['AWS_SECRET_ACCESS_KEY']%>" aws_ssh_key_id: "<%= ENV['AWS_SSH_KEY_ID']%>" region: eu-west-1 availability_zone: eu-west-1btransport: ssh_key: "<%= ENV['AWS_SSH_KEY_PATH']%>" username: admin...

Test Kitchen: Testing Puppet Modules in EC2

Environment Variables

Page 50: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

50 #Dynatrace

Testing REST APIswith RSpec Not supported by Serverspec

Page 51: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

51 #Dynatrace

Testing REST APIswith RSpec Infraspec not yet integrated

Page 52: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

52 #Dynatrace

test/integration/default/rspec/default_spec.rbrequire ‘json’require ‘net/http’

...describe ‘Server REST API’ do it ‘/rest/foo responds correctly’ do uri = URI(‘http://localhost:8080/rest/foo’)

request = Net::HTTP::Get.new(uri, { ‘Accept’ => ‘application/json’ }) request.basic_auth(‘foo’, ‘foo’) response = Net::HTTP.new(uri.host, uri.port).request(request)

expect(response.code).to eq 200 expect(JSON.parse(response.body)).to eq { ‘bar’ => ‘baz’ } endend

Test Kitchen: Testing REST APIs

Do RSpec!

Page 53: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

53 #Dynatrace

test/integration/default/rspec/default_spec.rbrequire ‘json’require ‘net/http’

...describe ‘Server REST API’ do it ‘/rest/foo responds correctly’ do uri = URI(‘http://localhost:8080/rest/foo’)

request = Net::HTTP::Get.new(uri, { ‘Accept’ => ‘application/json’ }) request.basic_auth(‘foo’, ‘foo’) response = Net::HTTP.new(uri.host, uri.port).request(request)

expect(response.code).to eq 200 expect(JSON.parse(response.body)).to eq { ‘bar’ => ‘baz’ } endend

Test Kitchen: Testing REST APIs

Could use serverspec!

Page 54: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

54 #Dynatrace

Dynatrace-Puppet ModuleFurther Examples: https://github.com/dynaTrace/Dynatrace-Puppet

Page 55: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

55 #Dynatrace

Page 56: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

56 #Dynatrace

Questions?

Page 57: Test-Driven Infrastructure with Puppet, Test Kitchen, Serverspec and RSpec

57 #Dynatrace