Skip to content

voxpupuli/voxpupuli-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

208827f · May 24, 2022
May 24, 2022
Nov 11, 2021
Jul 20, 2021
Feb 9, 2020
May 24, 2022
Jul 20, 2021
Feb 9, 2020
Aug 26, 2021
Apr 11, 2020
May 17, 2022
May 24, 2022

Repository files navigation

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 currently serves only to encapsulate common rake tasks related to releasing modules. 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

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.