Featured resource
Forrester Wave Report 2025
Pluralsight named a Leader in the Forrester Wave™

Our tech skill development platform earned the highest scores possible across 11 criteria.

Learn more
  • Labs icon Lab
  • Cloud
Google Cloud Platform icon
Labs

Unit Testing with Puppet and RSpec

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Jun 07, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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 }
          end
        end
      end
      
  2. Challenge

    Test That the `nginx` Package Is Installed

    Add the package-based unit test:

    it { is_expected.to contain_package('nginx') }
    
  3. Challenge

    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) }
      
  4. Challenge

    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'
      end
      
    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') }
      end
      

      Save and exit.

    3. Run the unit test:

      sudo pdk test unit
      

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.