befor you run a spec test, you first have to run:
Announcement
You can find all my latest posts on medium.bundle install
This will download all the dependencies (i.e ruby gems).
Then you do:
bundle exec rake {task}
This in parts means:
bundle exec – “execute the following script using the gems specified in the gemfile”
This in turn executes:
rake {task}
Here since we are calling rake it means that rake calls the rakefile, as a script, and it passes the “{task}” as an input parameter. This input parameter can take any of the following values:
- lint – check for good formatting, e.g. code is properly indented
- validate – checks for syntax error
- spec – runs the rspec tests (unit tests)
- beaker – runs the beaker tests (acceptance tests, also written in rspec).
If you want to see all the available input parameters for rake, do (run this while in the directory containing the gemfile):
[root@puppetmaster dummyModule]# bundle exec rake rake beaker # Run beaker acceptance tests rake beaker_nodes # List available beaker nodesets rake build # Build puppet module package rake clean # Clean a built module package rake coverage # Generate code coverage information rake help # Display the list of available rake tasks rake lint # Check puppet manifests with puppet-lint rake spec # Run spec tests in a clean fixtures directory rake spec_clean # Clean up the fixtures directory rake spec_prep # Create the fixtures directory rake spec_standalone # Run spec tests on an existing fixtures directory rake syntax # Syntax check Puppet manifests and templates rake syntax:hiera # Syntax check Hiera config files rake syntax:manifests # Syntax check Puppet manifests rake syntax:templates # Syntax check Puppet templates rake validate # Check syntax of Ruby files and call :syntax [root@puppetmaster dummyModule]#
What this does is that
Each puppet module has to contain the following files:
- rakefile – this basically runs a sequence of tasks
- gemfile – this contains a set of gemfiles required to run the rspec tests.
- fixtures.yml – this contains the puppet module dependencies, i.e. one module may require another module in order for it to run. Note, this often a hidden file, located at teh top level of the module folder
- spec_helper.rb – this contains the actual rspec code.
- spec_acceptance.rb – this is to do with beaker
On each puppet machine, the following exists:
- /etc/puppet/puppet.conf
- /etc/hiera.yaml
- /etc/r10k.yaml
You should only have one of these files per puppet master/agent.
If you want to test against a particular version of puppet, then you can do something like:
PUPPET_GEM_VERSION="~> 4.0" bundle exec rake spec
In this example it will test against the latest 4+ puppet version, e.g. 4.3.2.
This will work if you gem file has a builtin if statement, e.g.:
$ cat ../Gemfile source 'https://rubygems.org' group :development, :test do gem 'puppetlabs_spec_helper', '1.0.1', :require => false gem 'rspec-puppet', '2.3.2', :require => false gem 'rspec-core', '3.1.7', :require => false gem 'puppet-lint-strict_indent-check', :require => false gem 'metadata-json-lint', :require => false gem 'rspec-puppet-facts', :require => false end if puppetversion = ENV['PUPPET_GEM_VERSION'] gem 'puppet', puppetversion, :require => false else gem 'puppet', '~> 3.4', :require => false end # vim:ft=ruby
Resources