42
Puppet Testing For The Win Phil Zimmerman [email protected] https://twitter.com/phil_zimmerman

Puppet meetup testing

Embed Size (px)

DESCRIPTION

Puppet testing presentation given at Denver Puppet Meetup on 11/12/2013

Citation preview

Page 1: Puppet meetup testing

Puppet Testing For The Win

Phil Zimmerman [email protected]://twitter.com/phil_zimmerman

Page 2: Puppet meetup testing

Puppet

Software defined infrastructure – perfect for VCS

Configuration Management for servers

Declarative language written in Ruby DSL

Uses manifests to define server configurations

Brings servers into a desired state and keeps them there

Eliminates “snowflake” environments

Page 3: Puppet meetup testing

Puppet Review - Modules Puppet Modules

Self-contained bundles of code Develop your own Download from the Puppet Forge (

https://forge.puppetlabs.com/) Contain manifests, files, templates and,

ahem… tests

Page 4: Puppet meetup testing

Puppet Review – Manifests Puppet Manifests

End in the .pp file extension Each manifest in a puppet module should

contain one class or defined type Define the set of resources (packages,

files, services) that the module represents Can contain logic (conditionals,

collections, functions, etc) Are the source for the compiled catalog

Page 5: Puppet meetup testing

Puppet Review - Catalog

The catalog Represents the DAG (directed acyclic graph)

of resources and the desired system state for a given node

Is compiled from the set of modules’ manifests defined for a given node

In master/agent puppet, compiled by the master and applied on the agent node

Masterless puppet, compiled locally on node Represented on disk as a YAML document

Page 6: Puppet meetup testing

How Puppet Compiles and Applies a Catalog

Page 7: Puppet meetup testing

Hypothetical Workflow

Need to upgrade Java version on tomcat6 vms

Get latest puppet code from vcs Make the version change in my

manifest Simple change, it looks good to me Commit my changes

Page 8: Puppet meetup testing

Please work, please work…

Page 9: Puppet meetup testing

Face Palm

Oh no – Java was updated on my tomcat7 vms too…. Wait, wat?!

Face Palm

FAIL!!

Page 10: Puppet meetup testing

Daily Life Without Tests

Page 11: Puppet meetup testing

Why Test Puppet Code?

Puppet manifests are code Improve consistency and predictability

of server provisioning Well-defined tools (rspec-puppet,

puppet parser, puppet-lint, serverspec, vagrant, etc.)

Automatable Complex, data-driven server

configuration Think of others and future you!

Page 12: Puppet meetup testing

Testing Tools

Syntax Checking Static Analysis Unit Tests (rspec-puppet) Configure Jenkins to Run These Vagrant Server-spec Packer

Page 13: Puppet meetup testing

Syntax Checking

puppet parser validate-make sure the manifests will

generate a catalog

Page 14: Puppet meetup testing

Static Analysis

puppet-lint-make sure we adhere to the

puppet style guide

Page 15: Puppet meetup testing

“Unit” Tests

rspec-puppet (http://rspec-puppet.com/) Written by Tim Sharpe (https://github.com/rodjek) rspec, extended to work with puppet “unit tests” for puppet code Designed to test the catalog▪ Tests at the module level, not system level▪ Verify resources are present and dependencies are met▪ Verify resources are configured as expected▪ Verify file content (even when using templates and hiera

– yes!) puppetlabs-spec_helper (Rakefile, .fixtures.yml)

Page 16: Puppet meetup testing

rspec-puppet

rspec-puppet ruby gem rspec-puppet-init▪ Rakefile▪ spec/spec_helper.rb▪ spec/{classes,defines,functions,hosts,fixtures}

puppetlabs_spec_helper ruby gem .fixtures.yml Ideal for testing manifests referencing forge

modules Both gems work together to ease the burden

of boilerplate setup and configuration

Page 17: Puppet meetup testing

An Example – sshd

Page 18: Puppet meetup testing

sshd intall_spec.rb file

Test that the sshd package is installed

Page 19: Puppet meetup testing

sshd intall_spec.rb file

Make sure sshd_config file is present with desired attributes:

Page 20: Puppet meetup testing

sshd intall_spec.rb file

Ensure sshd_config has certain entries:

Page 21: Puppet meetup testing

sshd intall_spec.rb file

Verify sshd service is enabled and running with proper resource dependencies in place:

Page 22: Puppet meetup testing

Testing Behavior

Parameterized class let(:params) { {:foo => ‘abc’, :bar =>

‘xyz’} }

Specify values for facter facts let(:facts) { {:operatingsystem =>

‘CentOS’, :ipaddress => ‘192.168.33.10’} }

Specify fqdn for a node let(:node) { ‘puppet-test-

01.lab.webapps.rr.com’ }

Page 23: Puppet meetup testing

Mocking (Sort of , But Not Really)

Page 24: Puppet meetup testing

Testing With Hiera Data

hiera-puppet-helper gem

Page 25: Puppet meetup testing

Run the Tests

Page 26: Puppet meetup testing

What This Looks Like in Jenkins

Page 27: Puppet meetup testing

Puppet Testing In Jenkins

Page 28: Puppet meetup testing

Lint Warnings

Page 29: Puppet meetup testing

rspec-puppet Test Results

Page 30: Puppet meetup testing

Automated Tests and Trending in Jenkins

Page 31: Puppet meetup testing

We Can Test Modules – WIN!

This is awesome, but we’re not done Next level of testing is to perform a

puppet run on a test vm and verify all is good

We are ready for a server test – enter serverspec

Page 32: Puppet meetup testing

How Do I Know My Server Is Configured Correctly?

Server Spec (http://serverspec.org/)

Designed to validate that a server is configured appropriately after it’s been provisioned

Independent of Puppet, Chef, CFEngine, SaltStack, etc.

Tests your servers’ actual state directly via ssh▪ No server-side software or agents required!

Page 33: Puppet meetup testing

Serverspec Should Feel Familiar

serverspec ruby gem similar dsl as rspec, rspec-puppet serverspec-init

spec dir sample spec file spec_helper.rb Rakefile

Page 34: Puppet meetup testing

spec_helper.rb

Page 35: Puppet meetup testing

serverspec - sshd_spec.rb

Page 36: Puppet meetup testing

serverspec resource types

Page 37: Puppet meetup testing

Some Examples

describe iptables do it { should have_rule(‘-P INPUT ACCEPT’).with_table(‘mangle’).with_chain(‘INPUT’) }end

describe port(2003) do it { should be_listening.with(‘udp’) }end

describe package(‘httpd’) do it { should be_installed }end

describe service(‘sshd’) do it { should be_monitored_by(‘monit’) }end

Page 38: Puppet meetup testing

Group Spec Files According to Host Name

Page 39: Puppet meetup testing

Automated Post-Provisioning Validation

Page 40: Puppet meetup testing

Vagrant Disposable Testing VMs We use Puppet Enterprise at TWC Vagrantfile that auto installs and configures

Puppet Enterprise master and agent(s) https://github.com/adrienthebo/vagrant-pe_build

Personal replica of production Puppet Enterprise setup

Can apply any role to the agent and test the server config

Destroy the agent vm when done

Page 41: Puppet meetup testing

Packer.io

“Create identical machine images for multiple platforms from a single source configuration”

Supports all the main provisioners including Puppet

Can optionally create a vagrant box from the same source configuration

Automatable and Testable Extendable plugin architecture Powerful option for any vm architecture,

especially cloud-based (internal and external) Full of awesome

Page 42: Puppet meetup testing

Helpful Links

Miscellaneous Links http://www.slideshare.net/PuppetLabs/stephen-connolly http://www.slideshare.net/PuppetLabs/automated-puppet-testing-puppe

tcamp-chicago-12-scott-nottingham

https://github.com/adrienthebo/vagrant-pe_build https://github.com/puppetlabs/rspec-system

Vim Tools Syntastic (https://github.com/scrooloose/syntastic)

▪ Checks syntax and displays errors to the user

Vim-puppet (https://github.com/rodjek/vim-puppet)▪ Syntax highlighting▪ Style checking

Cool Tool Links Vagrant - http://www.vagrantup.com Packer - http://www.packer.io Stackhammer - http://www.cloudsmith.com