Thursday, April 7, 2016

Testing Custom puppet facts

Hey everyone!

I've been a bit distracted for a while, but just wanted to post this tidbit here..

I recently stumbled across this post on testing Custom Facter facts  over at the blog unethicalblogger.com, written by the creator of the Jenkins module for puppetR. Tyler Croy.  

It highlights a solution to one of the problems that I faced when I started learning how to test my puppet code.  For unit tests of functions and and manifests/catalogs, rspec-puppet and the puppetlabs_spec_helper gem are great places to start testing your code (along with some great examples from rnelson0.com). 

There are still some holes in your unit testing strategy (ignoring beaker for integration testing for now).  Custom Types/Providers and custom Facter facts!  The link above addresses the custom factor facts.  

Only having dabbled in ruby (mostly to aid my understanding of puppet), I haven't been fully versed on the full power of rspec. So it's good to seen an example where they take a more rubyish approach to the layout of their code to aid in testability.  It may not seem that important for your own small module that only you use, but if you start to have facts which are full of moderately complicated logic, you have multiple people on your team editing your modules, and you want to prevent regressions, it seems like the way to go.  

I won't rewrite the article here, but basically they take the 'script' feeling of a traditional Facter fact, which is hard to test, and they refactor it into a proper collection of ruby modules/classes, add in tests for all of the parts of the module, break up behavior into separate easily testable methods.  To plug the 'ruby' code into facter, they have an '#install' method which just calls the module's main entry point, and returns it's values to Facter.add().

Happy trails everyone, and I hope that some of this information can help guide you to having some solid tests on your puppet module's facter facts! 

No comments:

Post a Comment