cfgmgmt2015 - Testing with Beaker

31
Testing with Beaker

Transcript of cfgmgmt2015 - Testing with Beaker

Page 1: cfgmgmt2015 - Testing with Beaker

Testing with Beaker

Page 2: cfgmgmt2015 - Testing with Beaker

Who am I?

• Senior Infrastructure Engineer at OpenTable• 4 year Puppeteer• Frequent contributor to the Beaker project

@liamjbennett

Page 3: cfgmgmt2015 - Testing with Beaker

Who am I?

Page 4: cfgmgmt2015 - Testing with Beaker

Who am I?

Page 5: cfgmgmt2015 - Testing with Beaker
Page 6: cfgmgmt2015 - Testing with Beaker

What this talk is about?

What is beaker?

What does acceptance testing your puppet code look like?

Demos and future things.

Page 7: cfgmgmt2015 - Testing with Beaker

What is beaker?

• Testing framework built on top of a testing framework

• DSL for acceptance testing puppet code against a ‘real’ environment

• Spin up a box• Apply your puppet code• Test your expectations

• Uses rspec and serverspec under the hood to spin up nodes in vagrant/aws/vsphere/docker

Page 8: cfgmgmt2015 - Testing with Beaker

Why should you care?

• Modules that support multiple distros• Modules that support multiple versions of distros• Not every “base” image is the same• You care about idempotency• You don’t trust modules written by others• You’re baking your images

Page 9: cfgmgmt2015 - Testing with Beaker

The building blocks of Beaker

• Rspec• ServerSpec• Hypervisors (and the nodeset file)

Page 10: cfgmgmt2015 - Testing with Beaker

Rspec

describe 'modulename::classname' do

context 'create users' do

it 'should work with no errors' do

end

end

end

Page 11: cfgmgmt2015 - Testing with Beaker

ServerSpec

Page 12: cfgmgmt2015 - Testing with Beaker

ServerSpec

describe file('/etc/passwd') do

it { should be_file }

end

describe service('ntpd') do

it { should be_enabled }

end

describe docker_container(‘mysql’) do

it { should be_running }

end

describe windows_feature('Web-Webserver') do

it { should be_installed.by("powershell") }

end

Page 13: cfgmgmt2015 - Testing with Beaker

Hypervisors

Page 14: cfgmgmt2015 - Testing with Beaker

NodeSetHOSTS:

ubuntu-server-12042-x64:

roles:

- master

platform: ubuntu-12.04-amd64

box: ubuntu-server-12042-x64-vbox4210-nocm

box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box

hypervisor: vagrant

CONFIG:

type: foss

HOSTS:

centos-65-x64:

roles:

- master

platform: el-6-x86_64

box: puppetlabs/centos-6.5-64-puppet

hypervisor : vagrant

CONFIG:

type: foss

Page 15: cfgmgmt2015 - Testing with Beaker

NodeSetHOSTS:

ubuntu-server-12042-x64:

roles:

- master

platform: ubuntu-12.04-amd64

box: ubuntu-server-12042-x64-vbox4210-nocm

box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-

nocm.box

hypervisor: vagrant

CONFIG:

type: foss

HOSTS:

centos-65-x64:

roles:

- master

platform: el-6-x86_64

box: puppetlabs/centos-6.5-64-puppet

hypervisor : vagrant

CONFIG:

type: foss

Page 16: cfgmgmt2015 - Testing with Beaker
Page 17: cfgmgmt2015 - Testing with Beaker
Page 18: cfgmgmt2015 - Testing with Beaker

NodeSetHOSTS:

ubuntu-server-12042-x64:

roles:

- master

platform: ubuntu-12.04-amd64

box: ubuntu-server-12042-x64-vbox4210-nocm

box_url: http://puppet-vagrant-boxes.puppetlabs.com/ubuntu-server-12042-x64-vbox4210-nocm.box

hypervisor: vagrant

CONFIG:

type: foss

HOSTS:

centos-65-x64:

roles:

- master

platform: el-6-x86_64

box: puppetlabs/centos-6.5-64-puppet

hypervisor : vagrant

CONFIG:

type: foss

Page 19: cfgmgmt2015 - Testing with Beaker

NodeSetHOSTS:

ubuntu-server-12042-x64-master:

roles:

- master

platform: ubuntu-12.04-amd64

box: puppetlabs/ubuntu-12.04-64-nocm

hypervisor: vagrant

ubuntu-server-12042-x64-agent:

roles:

- agent

- default

platform: ubuntu-12.04-amd64

box: puppetlabs/ubuntu-12.04-64-nocm

hypervisor: vagrant

CONFIG:

log_level: verbose

type: git

Page 20: cfgmgmt2015 - Testing with Beaker

Writing the first test

-- DEMO --

https://github.com/opentable/puppet-homes/

Page 21: cfgmgmt2015 - Testing with Beaker

The DSL Sugar

on <role>

pp = <<-EOS

node fubar.local {

package { “openssl”:

ensure => installed

}

}

EOS

on master, “echo ‘#{pp}’ >> /etc/puppet/manifest/site.pp”

Page 22: cfgmgmt2015 - Testing with Beaker

The DSL Sugar

install_puppet

hosts.each do |host|

install_puppet(:version => ‘3.7.1’)

end

Page 23: cfgmgmt2015 - Testing with Beaker

The DSL Sugar

apply_manifest_on

# Run it twice and test for idempotency

apply_manifest(pp, :catch_failures => true)

run = apply_manifest(pp, :catch_failures => true)

expect(run.exit_code).to be_zero

Page 24: cfgmgmt2015 - Testing with Beaker

The DSL Sugar

run_agent_on

run = run_agent_on(agent, :catch_failures => true)

expect(run.exit_code).to be_zero

Page 25: cfgmgmt2015 - Testing with Beaker

The DSL Sugar

• scp_from + scp_to• sign_certificate_for• fact_on• facter• cfacter• hiera

• puppet command wrappers

Page 26: cfgmgmt2015 - Testing with Beaker

Testing modules in the wild

https://github.com/opentable/puppet-homes

https://github.com/puppetlabs/puppet/

https://github.com/opentable/puppet-windowsfeature

https://github.com/solarkennedy/puppet-consul/

Page 27: cfgmgmt2015 - Testing with Beaker

Testing Roles and Profiles

• Multi-node nodeset (master+agent)• Setting up the master

• puppet.conf• environment• signing• installing forge modules• copying private modules

http://tech.opentable.co.uk/blog/2014/09/01/testing-puppet-with-beaker-pt-dot-3-testing-roles/

https://gist.github.com/liamjbennett/ad6e2a2689aa8c4e2460

Page 28: cfgmgmt2015 - Testing with Beaker

Integration into CI

• Different NodeSet file• Junit report

• How do you manage yours??

Compile

rake syntax

Style

rake lint

Unit Test

rake spec

Acceptance Test

rake acceptance

Release

rake module:release

Page 29: cfgmgmt2015 - Testing with Beaker

Other things you can do with Beaker

BEAKER_destroy=no

Provisioning ???

Testing real servers ???

Baking into base images

BEAKER_provision=no

Page 30: cfgmgmt2015 - Testing with Beaker

The Future

More Hypervisors - Azure- Digital Ocean

More OS support- FreeBSD

WinRM for Windows

Plugin Support

Page 31: cfgmgmt2015 - Testing with Beaker

liamjbennett

@liamjbennett

[email protected]

liamjbennett.me

opentable

@OpenTableTechUK

tech.opentable.co.uk