Still mud-wrestling with spec testing

29 views
Skip to first unread message

Peter Berghold

unread,
Nov 27, 2015, 3:23:49 PM11/27/15
to puppet-users
So, 

Now that my schedule has some slack in it I've turned my attention back to doing spec testing of my Puppet modules.  Taking a really really simple module that I wrote as an example I started in again.

Here is the one and only file making up the class.  

---------- init.pp ------------------------------
class ntp {

  package { 'ntp':
    ensure => latest
  }
  service { 'ntp':
    ensure  => running,
    enable  => true,
    require => Package[ntp]
  }
    

}
-----------------------------------------------------

That should be really easy to run tests against in my opinion.  I wrote a Gemfle for the occasion.

---------Gemfile---------------------------

puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 3.3']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 0.3.2'
gem 'facter', '>= 1.7.0'
gem 'rake','>= 0.0.0'
gem 'spec','>= 0.0.0'
--------------------------------------------------------------------
and my spec file looks like this: 

------- spec/classes/init_spec.rb ---------------
require 'spec_helper'
describe 'ntp', :type => 'class' do

  context 'On Debian' do
    let :facts do {
         :osfamily => 'Debian'
}
    end
      
      it {
        should contain_package('ntp').with({ 'name' =>  'ntp' })
        should contain_service('ntp').with({ 'name' => 'ntp' })
      }
    end
    
end
-------------------------------------------------------------------

and when I run "rake spec" I get this (severely trimmed) set of errors:
-------------errors--------------------------------------------------
 1) ntp On Debian should contain Package[ntp] with name => "ntp"
     Failure/Error: should contain_package('ntp').with({ 'name' =>  'ntp' })
     
     ArgumentError:
       wrong number of arguments (2 for 1)
------------------------------------------------------------------------

Looks to me after reading "The Next Generation of Puppet Module Testing"  at this page: https://puppetlabs.com/blog/the-next-generation-of-puppet-module-testing
there should only be one argument to  "with" so.. if that's not an authoritative page for that information, which one is? 

Can somebody clarify for me what;s going on here?


Martin Alfke

unread,
Nov 28, 2015, 7:02:55 AM11/28/15
to puppet...@googlegroups.com
I assume you have Rakefile, spec/spec_helper.rb and .fixtures.yml in place as well?
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CAArvnv0%3DEobRO49%3DhYNuf6D2e2feN4md2rFqmx%2BGySCNByhtCw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Gareth Rushgrove

unread,
Nov 28, 2015, 1:29:29 PM11/28/15
to puppet...@googlegroups.com
Hi Peter
That blog post is a good 3 years old. Probably the best place to look
for up-to-date information is the rspec-puppet site itself.

http://rspec-puppet.com/

You'll also find lots of examples in the the supported and approved
modules on the Forge, and the module skeleton I maintain generates
working tests out-of-the-box from which you could copy the requisite
files. https://github.com/garethr/puppet-module-skeleton

My guess here is this is something to do with versions or some other
file you're using to setup the tests. If you checkout the skeleton
above you should be able to do:

puppet module generate yourname/ntp
cd ntp
bundle install
bundle exec rake spec

You don't need to then use the resulting module if you don't want, but
you should have a working setup from which you can port to your
existing modules.

Gareth


> Can somebody clarify for me what;s going on here?
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CAArvnv0%3DEobRO49%3DhYNuf6D2e2feN4md2rFqmx%2BGySCNByhtCw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gareth Rushgrove
@garethr

devopsweekly.com
morethanseven.net
garethrushgrove.com

Corey Osman

unread,
Nov 28, 2015, 9:23:59 PM11/28/15
to Puppet Users
You should use my tool that retrofits all the files you need in your module automatically for spec testing.  


gem install puppet-retrospec
cd your_favorite_module
retrospec puppet
bundle install
bundle exec rake spec

If you question a previous file you created is not right, just delete and re-run retrospec to have it automatically re-created according to the retrospec templates.

This tool isn't meant to automate the learning process, it just helps immensely with repetitive tasks.   So you will still need to learn quite a bit to understand unit testing.


Corey

Rich Burroughs

unread,
Nov 29, 2015, 12:13:36 AM11/29/15
to puppet...@googlegroups.com
+1 for Gareth's skeleton, it's a great way to get started with testing. It comes with rspec-puppet and Beaker set up.

I ended up updating some modules recently to use the latest version of it and I was very happy with it.



Rich

Reply all
Reply to author
Forward
0 new messages