Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • 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
Lab platform
Lab Info
Level
Intermediate
Last updated
Apr 27, 2025
Duration
30m

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
      
About the author

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.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight