Unit testing with Puppet RSpec

Unit testing is testing focused around the smallest unit of code. In the case of Puppet, the smallest functional unit of code is the manifest. RSpec provides us with a unit testing framework for Puppet code, which is fast and effective at checking that our Puppet code is producing the Puppet catalogs we expect. Whatever tests we write in RSpec, we're essentially asking: would what I want be in the Puppet catalog when I execute this code?

RSpec as a system is run on the command line, and does not involve a new virtual machine or container. It is now included in the Puppet PDK under the command pdk test unit. We're going to look at the files involved in running unit tests, and writing simple unit tests from the templates provided by the PDK.

We're beginning a new feature set, so we'll want to start from master, pull down the remote commits, and start on a new branch:

[rary@workstation profile]# git checkout master
Switched to branch 'master'

[rary@workstation profile]# git pull origin master
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
From github.com:RARYates/cicd-walkthrough-profile
* branch master -> FETCH_HEAD
Updating 1b91eec..639f8f6
Fast-forward
...

[rary@workstation profile]# git checkout -b rspec
Switched to a new branch 'rspec'

Before we begin with RSpec, we'll want a sample set of files we can work with. At the time of writing this book, there is no command in the PDK to create a unit test without creating a new manifest. To overcome this limitation, we'll simply rename our jenkins.pp file, create a new class with the PDK, and place our existing file back in place over it:

[rary@workstation profile]# mv manifests/jenkins.pp manifests/jenkins.pp.bak;pdk new class jenkins;mv manifests/jenkins.pp.bak manifests/jenkins.pp
pdk (INFO): Creating '/root/profile/manifests/jenkins.pp' from template.
pdk (INFO): Creating '/root/profile/spec/classes/jenkins_spec.rb' from template.
mv: overwrite ‘manifests/jenkins.pp’? y

We'll now have our jenkins_spec.rb built from template, and will be ready to begin writing unit tests in RSpec.

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

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