Use Onceover to start testing your Puppet control repository
I am a Puppet beginner and I’m happy to make the code manager integration happen. I like to play around with my code and test my code changes on my agent nodes. However, the way I’m testing my code is a bit tedious: Save my code change, push into remote repo, run code deploy, and run Puppet on agents.
Is there a simple way to quickly test my code? Yes! The answer is using Onceover.
First, I need to prepare my lab environment
I am using a fresh installation of RHEL 7.
Install Ruby by RVM
As to the Ruby version we want to install, we can follow this link to find Ruby versions for PE releases. In our example here, we are going to install Ruby v2.5.7. We can use RVM to install wanted Ruby versions easily.
Now we are ready to install Onceover and start our testing!
Install Onceover and test my Puppet Code
Clone down my control-repo
You can use this example repo for learning purposes, but you should ultimately try Onceover at your own control repo.
Okay, now I need to install the onceover gem
Now it’s time to configure and test
Now Onceover has been installed. The README says I need to configure it, and I can do so by running
bundle exec onceover init.
As the command runs, I can see that it created a number of files for me.
I’m curious what’s going to happen if I just try to run the spec tests. Back to the README, where I discover I can run
bundle exec onceover run spec.
Starting at the beginning of the output, I see that it’s deploying my modules from the Puppetfile.
After that, I can see the tests begin!
Now I’m seeing a couple of errors. For example:
After reading Puppet documentation, I realized that
augeas has been removed from Puppet 6, and I have to install a module called augeas_core to use
augeas resource type. Thus, I add the following line into my Puppetfile:
If I run
bundle exec onceover run spec again, I can see that my above error message is gone.
Then, I notice there are still two errors for
First, a few messages saying
The XXX system is not supported with the haproxy module. Now my question is: How can I tell Onceover to test modules with only wanted OSes? After checking with Onceover Configure, I understand I am able to configure class and node groups for tests. The Onceover configuration file is located at
onceover-lab-repo/spec/onceover.yaml. I have put an example onceover-example.yaml in my control repo so we can just copy it over.
Now I’m going to explain the three main changes made from the
First, I created a new class group called
general_class and included all classes but
Secondly, I created a new node group called
haproxy_nodes and excluded nodes that I didn’t want to test against
Lastly, I needed to update my
test_matrix to match the test classes and the node groups:
If I ran
bundle exec onceover run spec once more, I would see that Onceover stops testing some of the OSes that I filtered out and the above error is gone:
My only test error left is:
Onceover clearly tells me which file and which line is reporting this error. After checking that
loadbalancer.pp manifest file, I realized there’s a bug in my code, which I never realized in my real Puppet production setup! To fix this bug, I need to add a conditional check to the manifest file
site-modules/role/manifests/loadbalancer.pp. I have included a loadbalancer-improved.pp file in my repo. The main enhancement here is to add a
if statement before managing
Without this, there will be a chance when
$rule2 is not needed and its value will take the default value
undef. Consequently, the title for this rule will be
undef, which is not accepted. I’d never realized this issue in my production environment as I had always configured the second rule.
Now we can copy over the file:
Now, if I run
bundle exec onceover run spec again…
Hooray!!! All of my module tests passed and I am glad to push the current code status to my remote repository!
What I’ve done so far with Onceover
- Onceover provides me the flexibility to test my modules with wanted OSes
- Onceover helps me identify my code bugs which I can’t easily find from my Puppet environment
- Onceover helps me to test Puppet code with the latest version
This is a good start; I’ve built more confidence in my Puppet code before I can push them into my production control repo. Onceover provides an easy way to test my codes, and the tests are reusable with just a simple
onceover run command! Meanwhile, I’m joined by my Puppet colleagues, because Puppet has other cool tools like Puppet Development Kit (PDK) and Continuous Delivery for Puppet Enterprise. PDK provides integrated testing tools and a command line interface to help you develop, validate, and test modules. Continuous Delivery for Puppet Enterprise offers a prescriptive workflow to test and deploy Puppet code across environments! Feel excited? Feel free to check out Learn more below!
Henry is a customer support team lead for Asia-Pacific & Japan at Puppet.
- Read about roles and profiles.
- How to set up a control repo in case you haven't done it.
- Read more about Puppet Development Kit
- Check out the documentation and user guide for Continuous Delivery for Puppet Enterprise
- Haven't tried Puppet Enterprise yet? You can do it now by downloading our Learning VM