Skip to content

Latest commit

 

History

History
162 lines (112 loc) · 4.66 KB

README.md

File metadata and controls

162 lines (112 loc) · 4.66 KB

Voxpupuli Test Gem

License Test codecov Release RubyGem Version RubyGem Downloads

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

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.