Menu

PUPPET TESTING WITH JENKINS 2

I recently wrote a book entitled Puppet Continuous Delivery in which I outlined how to configure Jenkins to do your puppet module testing as part of a continuous delivery pipeline. Since then, Jenkins 2.0 has been released and I decided to take a look on how that would work for puppet testing with Jenkins 2.0. This post will cover how to configure Jenkins 2.0 to do your puppet module testing.

If you don’t have a Tomcat server you can play around with I have a post on how to install and configure one here:

Install Apache Tomcat 9 on Ubuntu Server 17.10

Installing Jenkins 2.0

Previously, this post directed to another site that walked you through installing Jenkins.  I have recently added an article on how to install Jenkins which will walk you through the process:

Installing Jenkins on Ubuntu Server 17.10

 

Configure Jenkins for Puppet Testing

Now that we have Jenkins ready we can start configuring it for puppet testing. This will be very similar to configuring the older versions of Jenkins. The first thing you need to do is install RVM as the tomcat user. Since the tomcat user doesn’t have a password set you will need to su to the root account then su to the tomcat user:

ubuntu@jenkins2:~$ sudo su -
root@jenkins2:~# su - tomcat7
tomcat7@jenkins2:~$

Now install RVM like you normally would:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable --ruby=2.1.2

Notice that we specified a version of Ruby. We will need this version for our rspec testing. Verify that you have the correct version of ruby installed as the tomcat7 user:

tomcat7@jenkins2:~$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
tomcat7@jenkins2:~$

Now install the bundler gem. We will need this as part of our workflow to install the gems required by our puppet module.


$ gem install bundler

That gets Ruby ready. Now for the fun part…

Jenkins 2.0 Puppet Testing Using a Jenkinsfile

Jenkins 2.0 comes with an awesome new feature called Pipeline. This is cool for us puppet guys because we can just throw in a textfile called ‘Jenkinsfile’ into our module code. When we setup a webhook from github that calls our Jenkins job, Jenkins will know what to do from this file. Check out my example puppet module on GitHub

https://github.com/admintome/billcloud-app

Here is the Jenkinsfile I included in that module:

node {
  deleteDir()
  checkout scm
  echo 'beginnning workflow...'

  stage 'prepare gems'
  sh '''#!/bin/bash
  source ~/.rvm/scripts/rvm
  bundle install --path=.bundle/gems/
  '''

  stage 'syntax testing'
  sh '''#!/bin/bash
  source ~/.rvm/scripts/rvm
  bundle exec puppet parser validate manifests/
  '''

  stage 'lint testing'
  sh '''#!/bin/bash
  source ~/.rvm/scripts/rvm
  bundle exec bundle exec puppet-lint --no-autoloader_layout-check manifests/*.pp
  '''

  stage 'rspec testing'
  sh '''#!/bin/bash
  source ~/.rvm/scripts/rvm
  bundle exec rake spec
  '''

  stage 'smoke testing'
  sh '''#!/bin/bash
  source ~/.rvm/scripts/rvm
  bundle exec rake spec_prep
  puppet apply tests/init.pp --noop --modulepath=spec/fixtures/modules
  '''

  stage 'deploy'
  echo 'deploy to puppet masters'
}

This is written in Groovy and tells Jenkins what to do. We define stages that make up our pipeline. For puppet testing I have a stage to prepare our gems. This uses bundler to install the gems required for this module (obtained from the Gemfile in the module) to a subdirectory of our module .bundle/gems/. This is a good idea because we don’t want to install the gems globally rather we want them installed in the workspace directory for this Jenkins job so that Jenkins’ garbage collection can remove them.

The next stage is syntax checking where we do a puppet parser validate command. The following commands perform the rest of our stages: lint testing, rspec testing, and smoke testing. The last stage is the deploy stage which will ultimately deploy the module to our puppet masters using R10K.

Create a Pipleline job in Jenkins with the same name as your module. Configure it with the following settings, substituting your git ssh url.

 

One thing I should note is to make sure you setup an SSH key from the tomcat7 users account and post the public key to GitHub and add the private key as a credential in Jenkins. This will allow jenkins to talk to github and poll for changes.

Save your job. Configure a webhook to your jenkins server (make sure your jenkins server is accessible from outside if you are using github.com instead of something like gitlab). Push a change to your puppet code and you should see Jenkins fire off and test your module:

Jenkins 2.0 gives us some very powerful features and I can’t wait to start using them for more automated puppet testing.

Tags: ,
%d bloggers like this: