How and why you should test

33
Sunday, April 7, 13

description

"How and why you should test" by Daniele Sluijters at Puppet Camp Amsterdam 2013.

Transcript of How and why you should test

Page 1: How and why you should test

Sunday, April 7, 13

Page 2: How and why you should test

www.nedap.com

testing!

Sunday, April 7, 13

Page 3: How and why you should test

www.nedap.com

testing!before production not in production

Sunday, April 7, 13

Page 4: How and why you should test

www.nedap.com

whoami

• Daniele Sluijters

• @daenney: irc, twitter, github, ...

• mail: [email protected]

• XMPP / Jabber: ☝

Sunday, April 7, 13

Page 6: How and why you should test

www.nedap.com

consequences

exec  {  ‘first-­‐run-­‐reboot’:

   command  =>  ‘touch  /tmp/first-­‐run  \                          &&  reboot’,

   creates  =>  ‘/tmp/first-­‐run’,

}

Sunday, April 7, 13

Page 7: How and why you should test

www.nedap.com

avoid latestclass  mysql  {

   package  {  ‘mysql-­‐server’:

       ensure  =>  latest,

       notify  =>  Service[‘mysql’],

   }

}

Sunday, April 7, 13

Page 8: How and why you should test

www.nedap.com

structure

Sunday, April 7, 13

Page 9: How and why you should test

www.nedap.com

paramsclass  acme::params  {

   case  $::osfamily  {

       ‘Debian’:  {  $founder  =  ‘Marvin’  }

       default:  {  fail(“No  can’t  do.”)  }

   }

}

Sunday, April 7, 13

Page 10: How and why you should test

www.nedap.com

paramsclass  acme::package  {  

   package  {  ‘marvin’:

       ensure  =>  $::acme::version,

   }

   ...

}

Sunday, April 7, 13

Page 11: How and why you should test

www.nedap.com

configurableclass  acme(

   $version  =  ‘1.6’,

   $enable  =  true,

)  inherits  acme::params  {  

   ...

}

Sunday, April 7, 13

Page 12: How and why you should test

www.nedap.com

users are crazyclass  acme(

   $version  =  ‘1.6’,

   $enable  =  true,

){  

   validate_string($version)

   validate_bool($enable)

}

Sunday, April 7, 13

Page 13: How and why you should test

www.nedap.com

data sources

• Don’t couple modules to data

• Pass in data through params

• Use a profile to configure the module

Sunday, April 7, 13

Page 14: How and why you should test

www.nedap.com

profileclass  profile::acme  {

   class  {  ‘acme’:

       version  =>  hiera(‘version’)

       ...

   }

}

Sunday, April 7, 13

Page 15: How and why you should test

www.nedap.com

misc

• Write docs

• Include a README + license

Sunday, April 7, 13

Page 18: How and why you should test

www.nedap.com

rspec-puppet

Sunday, April 7, 13

Page 20: How and why you should test

www.nedap.com

setup

source  :rubygems

gem  'puppet'                                  ,  '~>  3.1'

gem  'facter'                                  ,  '~>  1.6.18'

gem  'puppet-­‐lint'                        ,  '~>  0.3.2'

gem  'puppetlabs_spec_helper'  ,  '~>  0.4.1'

Sunday, April 7, 13

Page 21: How and why you should test

www.nedap.com

data

• let  :params  do  {}  end

• let  :facts  do  {}  end

• let  :pre_condition  do  {}  end

Sunday, April 7, 13

Page 22: How and why you should test

www.nedap.com

matchers

• should ⬌  should_not  

• include_type

• with_attr(‘value’)

• with({:attr1  =>  ‘value1’,  :attr2  =>  ‘value2’})

• with_content(regex)

Sunday, April 7, 13

Page 23: How and why you should test

www.nedap.com

example

describe  'acme'  do

   it  'should  declare  itself'  do

       should  contain_class('acme')

   end

end

Sunday, April 7, 13

Page 24: How and why you should test

www.nedap.com

context  ‘on  Debian’  do

   let  :facts  do  {  :osfamily  =>  ‘Debian’  }  end

   let  :params  do  {  :ver  =>  ‘1.2’  }  end

   it  ‘should  install  mysql’  do

       should  contain_package(‘mysql’).with_ensure(params[:ver]})

   end

   it  ‘should  not  install  redis’  do

       should_not  contain_package(‘redis’).with_ensure(‘present’)

   end

end

example

Sunday, April 7, 13

Page 25: How and why you should test

www.nedap.com

success

module  git:master  ›❯  rake  spec.............

Finished  in  2.43  seconds

13  examples,  0  failures

Sunday, April 7, 13

Page 27: How and why you should test

www.nedap.com

automate

• Generate module structure

• init, install, config, services, params

• Generate tests

• Generate README

• CI

Sunday, April 7, 13

Page 28: How and why you should test

www.nedap.com

behaviour

Sunday, April 7, 13

Page 29: How and why you should test

www.nedap.com

options

• Deploy to a testing environment

• Monitor your testing environment

• Test with rspec-...?

Sunday, April 7, 13

Page 30: How and why you should test

www.nedap.com

environment

• Jenkins + vagrant

• Can’t run on slaves

• Can’t run concurrent builds

Sunday, April 7, 13

Page 31: How and why you should test

www.nedap.com

monitoring

• Any tool you want to:

• nagios / icinga

• zabbix / zenoss / sensu

• ...

Sunday, April 7, 13

Page 32: How and why you should test

www.nedap.com© Kristina Alexanderson | https://secure.flickr.com/photos/kalexanderson/5421517469/Sunday, April 7, 13

Page 33: How and why you should test

www.nedap.com

q?

Sunday, April 7, 13