jenkins_spec.rb

After our fixtures are in place, let's inspect our jenkins_spec.rb, as provided by the PDK:

# Brings in our Global Configuration from spec/spec_helper.rb
require 'spec_helper'

# Tells RSpec with manifest to check, in this case: profile/manifests.jenkins.pp
describe 'profile::jenkins' do

# Runs the test once for each operating system listed in metadata.json, with a suite of default facts
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts }

# The manifest should compile into a catalog
it { is_expected.to compile }
end
end
end

The preceding simple test just ensures that the catalog compiles for each and every operating system listed in metadata.json. Normally, we'd run this test and we'd receive a passing status. In this particular case, rtyler/jenkins requires us to supply an additional fact of systemd that is not available in the base on_supported_os function.

Check popular modules on the forge for samples of code, especially in cases where you're testing profiles against existing modules. Often, the upstream module has a fix, like the one we're about to implement.

We'll edit our existing spec class to introduce a new fact to our system to support systemd:

require 'spec_helper'

describe 'profile::jenkins' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do

# Add a new ruby variable that returns true when the OS major release version is 6
systemd_fact = case os_facts[:operatingsystemmajrelease]
when '6'
{ systemd: false }
else
{ systemd: true }
end
# Change our facts to merge in our systemd_fact
let :facts { os_facts.merge(systemd_fact) }

it { is_expected.to compile }
end
end
end

Now, our test will be able to compile, as the upstream Jenkins module will have the systemd fact it needs to compile. Let's go ahead and compile our tests:

[root@pe-puppet-master profile]# pdk test unit
pdk (INFO): Using Ruby 2.4.4
pdk (INFO): Using Puppet 5.5.2
[] Preparing to run the unit tests.
[] Running unit tests.
Evaluated 4 tests in 3.562477833 seconds: 0 failures, 0 pending.

You may have noticed that we have four passing tests. Although we wrote just one test, our on_supported_os function looked in our metadata.json file and provided a test for each listed operating system, all within the Red Hat family.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.19.251