Unit Testing with Puppet and RSpec

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

The Puppet Development Kit doesn’t just set us up to create well-written modules—it also lets us write unit tests for our modules and ensure the changes we expect are being made when we run our Puppet code. This is done through the use of RSpec, a Ruby-based testing framework, and the `rspec-puppet` and `rspec-puppet-facts` plugins. RSpec uses behavior-driven development concepts to check the end state of our code. These tests are written in a human-readable language based on Ruby.

By writing unit tests for our modules, we can ensure we’re putting forth the highest-quality modules and prevent any unexpected production issues. Unit tests essentially work as another layer of protection against poor code, typos, and all the other gremlins that can end up in our modules.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Test That the Required Classes Are Contained
  1. Change to the spec directory of the nginx module:

    cd /etc/puppetlabs/code/environments/production/modules/nginx/spec/

  2. Open the classes/nginx_spec.rb file:

    sudo vim classes/nginx_spec.rb

  3. Review the existing test. Currently, it is set up to check if the module will compile.

  4. Add unit tests to ensure the desired classes are contained:

    describe ‘nginx’ do
    on_supported_os.each do |os, os_facts|
    context "on #{os}" do
    let(:facts) { os_facts }

         it { is_expected.to contain_class('nginx::install') }
         it { is_expected.to contain_class('nginx::config') }
         it { is_expected.to contain_class('nginx::service') }
         it { is_expected.to compile }


Test That the `nginx` Package Is Installed

Add the package-based unit test:

it { is_expected.to contain_package('nginx') }
Test That the `nginx` service Is Started, Enabled, and Able to Be Restarted
  1. Add the basic service test:

    it { is_expected.to contain_service(‘nginx’) }

  2. Refine the test by adding the required parameter information:

    it { is_expected.to contain_service(‘nginx’).with(ensure: ‘running’, enable: true, hasrestart: true) }

Test the Contents of the Static Configuration File
  1. Create a case statement for both distros within the context block:

    case os_facts[:osfamily]
    when ‘Debian’
    when ‘RedHat’

  2. Check that the file exists:

    case os_facts[:osfamily]
    when ‘Debian’
    it { is_expected.to contain_file(‘/etc/nginx/nginx.conf’).with_source(‘puppet:///modules/nginx/Debian.conf’) }
    when ‘RedHat’
    it { is_expected.to contain_file(‘/etc/nginx/nginx.conf’).with_source(‘puppet:///modules/nginx/RedHat.conf’) }

    Save and exit.

  3. Run the unit test:

    sudo pdk test unit

Additional Resources

You are a DevOps Engineer whose team uses Puppet extensively to manage your infrastructure. As such, large swathes of your infrastructure depend on you and your team writing clean, well-written Puppet code that works across all required operating systems and node types. One way your team ensures your code satisfies these requirements is by writing unit tests for each module, and since a new Nginx module has recently been created for use, you need to generate the RSpec tests for this module.

The module has already been written and provided for you, and can be found at /etc/puppetlabs/code/production/modules/nginx. Add the unit tests to the spec directory, specifically the class/nginx_spec.rb file. Ensure your tests check that:

  • The following classes are contained:
    • nginx::install
    • nginx::service
    • nginx::config
  • The nginx package is installed
  • The nginx service has been started, enabled, and is able to be restarted appropriately
  • Any static files exist with the appropriate source

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?