Voxpupuli Test Gem
This is a helper Gem to test the various Vox Pupuli Puppet modules. This Gem provides common functionality for rspec-puppet based testing and static code analysis. The aim is to reduce the boiler plate and need for modulesync.
Usage
Add the voxpupuli-test Gem to your Gemfile:
gem 'voxpupuli-test'
Then, at the top of your Rakefile, add:
require 'voxpupuli/test/rake'
In your spec/spec_helper.rb
require 'voxpupuli/test/spec_helper'
In your .rubocop.yml (see Rubocop's documentation).
inherit_gem:
voxpupuli-test: rubocop.yml
To run the linter, the syntax checker and the unit tests:
bundle exec rake test
To run your all the unit tests:
bundle exec rake spec
To run a specific spec test set the SPEC variable:
SPEC=spec/classes/foo_spec.rb bundle exec rake spec
To run all the static code analysis and linting:
bundle exec rake validate lint check rubocop
To autocorrect Puppet files:
bundle exec rake lint_fix
To autocorrect Ruby files:
bundle exec rake rubocop:autocorrect
Rake tasks
check:trailing_whitespace
The rake task check:trailing_whitespace checks for trailing whitespace in all markdown files in the repository.
It has an exclude pattern for: %r{^((modules|acceptance|\.?vendor|spec/fixtures|pkg)/|REFERENCE.md)}
We recommend using the GitHub style guide for markdown files, which includes no trailing whitespace. See GitHub Markdown Style Guide
check:misplaced_files
The rake task check:misplaced_files checks for misplaced files in the repository.
Files are considered misplaced if they don't belong in the directory tree they are under.
I.e. functions/, manifests/, and types/ may only contain .pp files, templates/ may only contain .erb and .epp, data/ may only contain .yml and .yaml, and lib/ may only contain .rb.
check:utf8
The rake task check:utf8 checks that all files that will be parsed by Puppet are encoded in valid UTF-8 without a BOM.
The task validates all files under data/, functions/, lib/, manifests/, templates/, and types/.
Environment variables
CODECLIMATE_REPORT_FILE
Setting CODECLIMATE_REPORT_FILE to a file path will configure tasks that support it to output CodeClimate-style JSON reports into the given file.
The tasks that currently support this feature include lint and rubocop.
Note that if multiple tasks all attempt to output CodeClimate reports in a single rake call, then only the final task will be able store its report.
Fact handling
The recommended method is using rspec-puppet-facts and is set up by default. This means the tests are writting as follows:
require 'spec_helper'
describe 'myclass' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
it { is_expected.to compile.with_all_deps }
end
end
end
Now a common case is to override facts in tests. Let's take the example of SELinux with legacy facts.
require 'spec_helper'
describe 'mytool' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
it { is_expected.to compile.with_all_deps }
describe 'with SELinux enabled' do
let(:facts) { super().merge(selinux: true) }
it { is_expected.to contain_package('mytool-selinux') }
end
describe 'with SELinux disabled' do
let(:facts) { super().merge(selinux: false) }
it { is_expected.not_to contain_package('mytool-selinux') }
end
end
end
end
This is all fairly straight forward, but it gets more complex when using modern facts. Modern facts are nested which means you need to do deep merging. There is deep_merge but its results are not at all useful for spec testing. That's why voxpupuli-test has an override_facts helper.
require 'spec_helper'
describe 'mytool' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }
it { is_expected.to compile.with_all_deps }
describe 'with SELinux enabled' do
let(:facts) { override_facts(super(), os: {selinux: {enabled: true}}) }
it { is_expected.to contain_package('mytool-selinux') }
end
describe 'with SELinux disabled' do
let(:facts) { override_facts(super(), os: {selinux: {enabled: false}}) }
it { is_expected.not_to contain_package('mytool-selinux') }
end
end
end
end
Note that this helper deals with symbols/strings for you as well.