Unit Testing with Puppet and RSpec

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:15:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Unit Testing with Puppet and RSpec

Introduction

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 actually 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.

Connecting to the Lab

  1. Open your terminal application, and run the following command (remember to replace <PUBLIC_IP> with the public IP you were provided on the lab instructions page):
    ssh cloud_user@<PUBLIC_IP>
  2. Type yes at the prompt.
  3. Enter your cloud_user password at the prompt.

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. Locate the line it { is_expected.to compile }, and add the following on a new line above it:

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

    Test That the nginx Package Is Installed

  4. Space down to a new line, and add the following:

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

Test That the nginx service Is Started, Enabled, and Able to Be Restarted

  1. Space down to the next line in the file, and add the following:
    it { is_expected.to contain_service('nginx').with(ensure: 'running',enable: true, hasrestart: true) }

Test the Contents of the Static Configuration File

  1. Add the following on a new line after the line it { is_expected.to compile }:

    case os_facts[:osfamily]
    when 'Debian'
    when 'RedHat'
    end
  2. Add the following under the line when 'Debian':

    it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_source('puppet:///modules/nginx/Debian.conf') }
  3. Add the following under the line when 'RedHat':

    it { is_expected.to contain_file('/etc/nginx/nginx.conf').with_source('puppet:///modules/nginx/RedHat.conf
  4. Save and exit the file.

  5. Run the unit test.

    sudo pdk test unit

Conclusion

Congratulations, you've successfully completed this hands-on lab!