Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Puppet 5

You're reading from   Mastering Puppet 5 Optimize enterprise-grade environment performance with Puppet

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781788831864
Length 292 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jason Southgate Jason Southgate
Author Profile Icon Jason Southgate
Jason Southgate
Ryan Russell-Yates Ryan Russell-Yates
Author Profile Icon Ryan Russell-Yates
Ryan Russell-Yates
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Authoring Modules FREE CHAPTER 2. Roles and Profiles 3. Extending Puppet 4. Hiera 5 5. Managing Code 6. Workflow 7. Continuous Integration 8. Extending Puppet with Tasks and Discovery 9. Exported Resources 10. Application Orchestration 11. Scaling Puppet 12. Troubleshooting and Profiling 13. Other Books You May Enjoy

Unit testing your module

The number one most important thing you can do to bring quality to your modules is to test them! Testing really is one of the most important aspects of software quality assurance in any field of software development. In the agile development community, we've been banging on the table about automated testing for more than 10 years!

Puppet RSpec (http://rspec-puppet.com/tutorial) has been allowing the Puppet community to unit test their modules for quite some time, but it's even easier now with the new PDK 1.0, as everything is set up ready, and you can just add your testing code and run the tests.

From a Puppet perspective, unit testing means checking the output from the compiler. Are the resources contained in the compiled relationship resource catalog, and is their order as expected, given the parameters passed and/or facts present?

When you begin to write tests in Puppet-RSpec, it seems at first like all you are doing is rewriting the Puppet manifests in another Ruby-like language. There is, however, really more to it than that. If there is some reasonable complexity to the module's functionality, for example, testing the dynamic content produced by Puppet templates, support for multiple operating systems, and different actions according to the passed parameters, then these tests actually form a safety net when editing or adding new functionality to your modules, protecting against regressions when refactoring, or upgrading to a new Puppet release.

Let's carry on from the previous two sections and use the development kit to unit test our module. Whenever you generate a class using the pdk new class command, PDK creates a corresponding unit test file. This file, located in your module's /spec/classes folder, already includes a template for writing your unit tests (see http://rspec-puppet.com/tutorial). You can then run the tests using the following command:

$ pdk test unit
You have been reading a chapter from
Mastering Puppet 5
Published in: Sep 2018
Publisher: Packt
ISBN-13: 9781788831864
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime