3. WHY TEST ?
* Raise confidence in your work
* Raise confidence for others to contribute
* upgrading to Puppet 4
* No IDE to catch your mistakes
* No sleep
* Your cat jumps on your keyboard before you git commit
http://www.wikihow.com/Get-Your-Cat-
to-Stop-Jumping-on-Your-Keyboard
5. THE PROBLEM
• Puppet unit testing is constantly evolving.
• Best practices have not fully been established.
• Information is scattered across blogs, talks and github repos.
• Module testing requires same steps to setup with every module.
• No tool exists for post module creation.
8. RETROSPEC FEATURES
• Sets up module testing scaffolding for you.
• Parses your puppet code and creates some simple tests
• Auto inserts params that your class/define uses
• Auto resolves some variables for you.
• Uses common rspec test patterns
• Allows for easy template overriding
• Easy CLI workflow
10. USER TEMPLATES
.puppet_retrospec_templates/
├── acceptance_spec_test.erb
├── module_files
│ ├── Gemfile
│ ├── Rakefile
│ └── spec
│ ├── acceptance
│ │ └── nodesets
│ │ ├── centos-59-x64.yml
│ │ ├── ubuntu-server-12042-x64.yml
│ │ ├── ubuntu-server-1310-x64.yml
│ │ └── ubuntu-server-1404-x64.yml
│ ├── shared_contexts.rb
│ ├── spec_helper.rb
│ └── spec_helper_acceptance.rb
└── resource_spec_file.erb
Retrospect will recursively
create all files in the
modules_file path and
render them as erb
templates.
11. HOW RETROSPEC WORKS
1. Determines if directory is a module
2. Reads each manifest one at a time
3. Uses the AST puppet parser to parse all types in the module
4. Records parameters for given type and stores variables used
inside each manifest.
4. Parses conditional blocks that contain resources and variables*
5. Uses this information as a context to pass to templates for file
creation.
6. Determines if file already exists before creation.
Does not compile a catalog
13. FUNCTION MOCKING
describe 'foo::bar' do
let!(:add_stuff) { MockFunction.new('add_stuff') { |f|
f.stub.with([1, 2]).returns(3)
}
}
https://github.com/Accuity/rspec-puppet-utils
class test_add{
add_stuff(1,2)
}
14. FACT MOCKING
* https://github.com/mcanevet/rspec-puppet-facts
* https://github.com/apenney/puppet_facts
# below is the facts hash that gives you the ability to mock
# facts on a per describe/context block. If you use a fact in your
# manifest you should mock the facts below.
let(:facts) do
{:osfamily => ‘RedHat’}
end
15. HIERA/PARAMS
MOCKING
shared_context :hiera do
# example only,
let(:windows_hiera_data) do
{ 'tomcat::xmx_mem':
'1024M' }
end
end
describe 'Windows' do
include_context :windows_hiera_data
let(:facts) do
{ osfamily: 'Windows', path: 'c:programdata', operatingsystem: 'windows'}
end
let(:params) do
{
build: '50', version: '1.0', java_home: 'C:/Program Files/Java/jre7'
}
end
it { should contain_package('Go Agent') }
end