Testing your infallibleness

16
TESTING YOUR INFALLIBLENESS How to use puppet-retrospec to kickstart your module testing.

Transcript of Testing your infallibleness

TESTING YOUR INFALLIBLENESS

How to use puppet-retrospec to kickstart your module testing.

ABOUT METwitter: @cosman2001 Github: logicminds IRC: cosman2001 Email: [email protected]

• Devops toolsmith • Puppet Consultant • Ruby Developer • Foreman Contributor (API, BMC,

Rubyipmi)

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

MODULE TESTING

TRANSFORMS FEAR INTO BOREDOM.

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.

SIMILAR TOOLS

• https://github.com/garethr/puppet-module-skeleton • $ puppet module generate lmc-test_module • $ rspec-puppet-init (outdated) • Amazon mechanical turk

WHY RETROSPEC?

Your modules need to be tested and you just found out about rspec-puppet. Now what?

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

MODULE TESTING COMPONENTS

* Gemfile * .travis.yml * Rakefile * spec directory * spec helper * spec files * .fixtures.yml * Beaker files * misc…

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.

HOW RETROSPEC WORKS1. 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

DEMO

$ mkdir ~/modules $ puppet module install -i ~/modules/ puppetlabs/apache $ cd ~/modules/apache $ rm -rf spec $ gem install puppet-retrospec $ retrospec -h $ retrospec

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) }

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

HIERA/PARAMS MOCKING

shared_context :hiera do # example only, let(:windows_hiera_data) do { 'tomcat::xmx_mem': '1024M' } endend

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

QUESTIONS?

Twitter: @cosman2001 Github: logicminds IRC: cosman2001 Email: [email protected]

https://github.com/logicminds/puppet-retrospec