how and why you should test
DESCRIPTION
"How and why you should test" by Daniele Sluijters at Puppet Camp Amsterdam 2013.TRANSCRIPT
Sunday, April 7, 13
www.nedap.com
testing!before production not in production
Sunday, April 7, 13
www.nedap.com
whoami
• Daniele Sluijters
• @daenney: irc, twitter, github, ...
• mail: [email protected]
• XMPP / Jabber: ☝
Sunday, April 7, 13
www.nedap.com© Ines Hegedus-Garcia | https://secure.flickr.com/photos/miamism/8446238376/Sunday, April 7, 13
www.nedap.com
consequences
exec { ‘first-‐run-‐reboot’:
command => ‘touch /tmp/first-‐run \ && reboot’,
creates => ‘/tmp/first-‐run’,
}
Sunday, April 7, 13
www.nedap.com
avoid latestclass mysql {
package { ‘mysql-‐server’:
ensure => latest,
notify => Service[‘mysql’],
}
}
Sunday, April 7, 13
www.nedap.com
paramsclass acme::params {
case $::osfamily {
‘Debian’: { $founder = ‘Marvin’ }
default: { fail(“No can’t do.”) }
}
}
Sunday, April 7, 13
www.nedap.com
paramsclass acme::package {
package { ‘marvin’:
ensure => $::acme::version,
}
...
}
Sunday, April 7, 13
www.nedap.com
configurableclass acme(
$version = ‘1.6’,
$enable = true,
) inherits acme::params {
...
}
Sunday, April 7, 13
www.nedap.com
users are crazyclass acme(
$version = ‘1.6’,
$enable = true,
){
validate_string($version)
validate_bool($enable)
}
Sunday, April 7, 13
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
www.nedap.com
profileclass profile::acme {
class { ‘acme’:
version => hiera(‘version’)
...
}
}
Sunday, April 7, 13
www.nedap.com
misc
• Write docs
• Include a README + license
Sunday, April 7, 13
www.nedap.com© elycefeliz | https://secure.flickr.com/photos/elycefeliz/4649829000/Sunday, April 7, 13
www.nedap.com© a200/a77Wells | https://secure.flickr.com/photos/aiwells/4672742619/Sunday, April 7, 13
www.nedap.com
setup• Setup:
• use puppetlabs_spec_helper
• https://puppetlabs.com/blog/the-next-generation-of-puppet-module-testing/
• Read the tutorial:
• http://rspec-puppet.com/tutorial/
Sunday, April 7, 13
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
www.nedap.com
data
• let :params do {} end
• let :facts do {} end
• let :pre_condition do {} end
Sunday, April 7, 13
www.nedap.com
matchers
• should ⬌ should_not
• include_type
• with_attr(‘value’)
• with({:attr1 => ‘value1’, :attr2 => ‘value2’})
• with_content(regex)
Sunday, April 7, 13
www.nedap.com
example
describe 'acme' do
it 'should declare itself' do
should contain_class('acme')
end
end
Sunday, April 7, 13
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
www.nedap.com
success
module git:master ›❯ rake spec.............
Finished in 2.43 seconds
13 examples, 0 failures
Sunday, April 7, 13
www.nedap.com© Colin Fahrion | https://secure.flickr.com/photos/headlouse/1484615917/Sunday, April 7, 13
www.nedap.com
automate
• Generate module structure
• init, install, config, services, params
• Generate tests
• Generate README
• CI
Sunday, April 7, 13
www.nedap.com
options
• Deploy to a testing environment
• Monitor your testing environment
• Test with rspec-...?
Sunday, April 7, 13
www.nedap.com
environment
• Jenkins + vagrant
• Can’t run on slaves
• Can’t run concurrent builds
Sunday, April 7, 13
www.nedap.com
monitoring
• Any tool you want to:
• nagios / icinga
• zabbix / zenoss / sensu
• ...
Sunday, April 7, 13
www.nedap.com© Kristina Alexanderson | https://secure.flickr.com/photos/kalexanderson/5421517469/Sunday, April 7, 13