Ask Your Question
0

rspec-puppet tests for roles/profiles

asked 2016-06-02 17:58:05 -0500

DarylW gravatar image

updated 2016-06-06 01:16:03 -0500

I want to make some tests that will help prevent issues with deploying our roles and profiles..

Does anyone have a good example of writing rspec-puppet tests for roles and profiles to ensure that you don't have silly 'catalog compilation' failures (ignoring things you can't test for, odd facter states on the nodes, etc..). We have a few 'main paths' in our hiera data (DEV/TEST/PROD environments, and we have all of our roles..

Anyone have a good example of how to get a list of all the roles you want to test (either by looking in roles/manifests/(.*).pp and turning that into a list, or having a list to test), and looping over that for all three environments, and comp, and using something like

it { is_expected.to compile.with_all_deps }

to make sure there aren't any egregious omissions?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-06 01:29:26 -0500

I have described how I do this in some detail in this blog post here.

Briefly, the way I do it is compile catalogs for all nodes like:

require 'spec_helper'  # stub all your other facts in the spec helper

[
 'webapp01.prod.example.com',
 'webapp02.prod.example.com',
 'webapp01.test.example.com',
 'webapp02.test.example.com',
].each do |fqdn|

  node_tier = ...  # extract node tier from hostname using a regexp

  describe fqdn do
    let(:facts) {{
      :fqdn      => fqdn,
      :node_tier => node_tier,
      ...
    }}
    it { is_expected.to compile.with_all_deps }
  end
end

Secondly, I ensure I have data validation in my profiles for parameters that the profile classes accept.

Thirdly, I have a rule never to provide defaults in profile classes.

Now, concerning the second part of your question, I'm not following the second part of your question as you seem to have provided the correct answer yourself!

edit flag offensive delete link more

Comments

Ah, I noticed I neglected to comment about the second part. Basically, all of my nodes use the 'default node' and a fact determines their role. I would have to loop across possible values in facter to set the role.

DarylW gravatar imageDarylW ( 2016-09-12 13:31:27 -0500 )edit

I'm curious why people do that. What's wrong with simply doing puppet apply -e 'include role::foo' ? Anyway, can't you just have rspec-puppet tests for your role classes?

Alex Harvey gravatar imageAlex Harvey ( 2016-09-13 23:11:14 -0500 )edit

That is an option, but how do you scale it for use in your CI system? You would have to use it as a --noop run, and you would have to set up the path to the hiera data, and mock out the appropriate factor facts if your CI and prod are different

DarylW gravatar imageDarylW ( 2016-09-14 00:09:14 -0500 )edit

I think I like the idea of a separate file for each role in spec/classes/rolename_spec.rb, that would allow you to create as many sets of facter facts as you require (most of our roles run on one system, some need to work on two - redhat 6/7) and having a specific rspec test makes a single place

DarylW gravatar imageDarylW ( 2016-09-14 00:11:36 -0500 )edit

I had wanted something 'simple', something ['role1', ... ,'roleN'].each do |role| .... which I CAN do. It would just get complicated if you needed different facts for different roles, but it would co-locate them in one place. My initial idea would be to find all of my role files programatically

DarylW gravatar imageDarylW ( 2016-09-14 00:13:28 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2016-06-02 17:58:05 -0500

Seen: 206 times

Last updated: Jun 06 '16