Including additional resources in your rspec-puppet tests

I’m a strong advocate of creating unit tests for your puppet code with rspec-puppet. I’ve written a number of articles on tests before, so here’s one more.

When you’re testing a class, sometimes there’s an expectation that it’s used alongside another class or resource. But your test is only against your class, so how do you add the other resource to the catalog? As an example, let’s look at the module jlambert121/puppet and how some of the subclasses in the module are tested, specifically puppet::server::install. The top level describe statement ensures that the class puppet::server::install is present. It does not ensure that some of the other classes it relies upon are present.

This is done through the : :pre_condition statements at the beginning of each 2nd level describe, starting on line 3:

  let(:pre_condition) { 'class {"::puppet": server => true}' }

Other describe blocks instantiate the class with different parameters, as needed for the individual test.

In the : :pre_condition, you can add any Puppet DSL you want. I would not suggest going overboard, but including or instantiating another class or a small number of resources can be very helpful. This can be especially helpful with profile classes, where you may want to test behavior when another profile is present, e.g. testing an application profile with multiple webserver profiles.

I did not find the : :pre_condition statement documented anywhere in the rspec-puppet docs, so I hope this helps others discover it.

Non-conforming in IT

Many people in IT are non-conformists, compared to our non-IT brethren. Common themes include being good at math, enjoying games – video, but other tabletop games like D&D as well –  beginning our day with coffee or other forms of caffeine, can’t stand that sportsball, introverted, spend all day in front of a computer, and a few others. This certainly isn’t an exhaustive list, and not everyone hits these, but I imagine most reading this found a few things that describe them. Others, like coffee and caffeine, aren’t limited to IT people, but add to the collective list of commonalities. For a bunch of people going against the grain, we seem to be choosing a rather similar direction!

You don’t have to be a non-conformist in IT, though. I’m not! I do enjoy my video games and I’m on the introverted side of the spectrum. However, I don’t enjoy math beyond calculus and even prefer english and related studies, I don’t drink caffeine on a regular basis, I love team sports especially football, and my main hobby is woodworking in my garage. I’m the guy whining about the lack of just plain water at a conference or causing your eyes to glaze over when using football analogies in meetings.

If there’s any point to this short article, it’s a reminder that we all do really have different interests. It can be a little weird to be made the outsider of the outsider group, so be sure to celebrate those differences instead of using them against each other.