Puppet Tech Debt: Moving Rspec Tests

Now that we have shaved the yak of generate-puppetfile, it’s time to move my rspec tests to the top of the controlrepo, as discussed on Thursday. To do so, we need to move not just the spec tests, but also the files Rakefile, .rspec, .rubocop.yml, and create a facsimile metadata.json, and of course, generate a new .fixtures.yml.

Moving the files is pretty simple. In my controlrepo, I only have a profile class with tests, so I’m going to take those as-is. We do want to make sure that we start with a clean environment, we don’t want to copy a bunch of fixtures or other temp files. We could use git clean -ffdxn; if you don’t want to redownload your gems you can manually clean up the relevant directories. Then we can do the file shuffle:

$ git clean -ffdxn
Would remove .bundle/
Would remove Gemfile.lock
Would remove coverage/
Would remove dist/profile/spec/fixtures/manifests/
Would remove dist/profile/spec/fixtures/modules/
Would remove vendor/
$ rm -fR dist/profile/spec/fixtures/manifests/ dist/profile/spec/fixtures/modules/
$ git mv dist/profile/spec ./
$ git mv dist/profile/{Rakefile,.rspec,.rubocop.yml} ./

Make sure your Rakefile works now. A simple way is to list your tasks. If you run into issues, you’ll need to fix them before you proceed. For instance, if you still use the older Rake::Task[:coverage].clear and coverage/ does not exist, you’ll to clean that up:

$ be rake -T
rake aborted!
Don't know how to build task 'coverage' (see --tasks)
/home/rnelson0/puppet/controlrepo/Rakefile:27:in `'
/home/rnelson0/puppet/controlrepo/vendor/ruby/gems/rake-11.3.0/exe/rake:27:in `'
...
$ git diff
diff --git a/Rakefile b/Rakefile
index 94f0bec..0d6f77f 100644
--- a/Rakefile
+++ b/Rakefile
@@ -22,10 +22,6 @@ exclude_paths = [
   "spec/**/*",
 ]

-# Coverage from puppetlabs-spec-helper requires rcov which
-# doesn't work in anything since 1.8.7
-Rake::Task[:coverage].clear
-
 Rake::Task[:lint].clear

 PuppetLint.configuration.relative = true
[rnelson0@build03 controlrepo:rspec_moves±]$ be rake -T
rake acceptance                              # Run acceptance tests
rake beaker                                  # Run beaker acceptance tests
rake beaker:sets                             # List available beaker nodesets
...

Now is a great time to get any other Rakefile adjustments done, since we’re paying down tech debt. I also converted my PuppetLint configuration to the newer send() format while I was at it:

-PuppetLint.configuration.relative = true
-PuppetLint.configuration.disable_80chars
-PuppetLint.configuration.disable_arrow_alignment
-PuppetLint.configuration.disable_class_inherits_from_params_class
-PuppetLint.configuration.disable_class_parameter_defaults
+PuppetLint.configuration.send('relative')
+PuppetLint.configuration.send('disable_140chars')
+PuppetLint.configuration.send('disable_arrow_alignment')
+PuppetLint.configuration.send('disable_class_inherits_from_params_class')
+PuppetLint.configuration.send('disable_class_parameter_defaults')

Finally, we need to create a metadata.json. We can steal the one from the profile module and tweak it a bit. We want to do this so rspec-puppet-facts and other tools and checks work properly. That will look something like this:

{
  "name": "rnelson0-controlrepo",
  "version": "0.1.0",
  "author": "rnelson0",
  "summary": "Controlrepo",
  "license": "Apache-2.0",
  "source": "https://github.com/rnelson0/controlrepo",
  "project_page": "https://github.com/rnelson0/controlrepo",
  "issues_url": "https://github.com/rnelson0/controlrepo/issues",
  "dependencies": [
    {
      "name": "puppetlabs-stdlib",
      "version_range": ">= 1.0.0"
    }
  ],
  "operatingsystem_support": [
    {
      "operatingsystem": "RedHat",
      "operatingsystemrelease": [
        "7"
      ]
    }
  ]
}

We want to update our Puppetfile with any new or replaced modules before the next – don’t worry about versions, though. I replaced nanliu/staging with puppet/staging, everything else was okay. Now we can run generate-puppetfile to update the versions and dependencies and generate .fixtures.yml:

$ be generate-puppetfile -p Puppetfile -c --create-fixtures

Installing modules. This may take a few minutes.


=======================================================================
...

...
=======================================================================

Generating .fixtures.yml using module name profile

Generating .fixtures.yml for a controlrepo.

We can now try running our tests and ensure that our conversion worked well:

$ be rake spec_prep
...

...
$ be rake spec_clean
...

...

Finished in 19.4 seconds (files took 5.53 seconds to load)
115 examples, 0 failures

Finally, if you have tests set up on your controlrepo, be sure to adjust those. You no longer need to cd into a modules directory to run the tests, you just run rake test at the top of the repo!

And now, it’s time to turn on the tv and watch the Steelers defeat the Ravens 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s