Skip to content

Test-Kitchen & Serverspec with Windows guests

Earlier this year, I attended ChefConf 2015 in Santa Clara where I attended the Hack Day at Microsoft on Friday. While I spent most of that day working on various projects like kitchen-rackspace with Jonathan Hartman, I also tested the upcoming test-kitchen 1.4 release upon Fletcher‘s request. Among the release notes was something I would encounter a few months later in November:

Note that this release has the much-fabled “Windows guest support.”

A few months later, in November, I received some bug reports in the Firewall cookbook related to Windows support — specifically, bad Powershell commands that weren’t applying the specified rules to the Windows Firewall. At that time, I decided to go back and really ensure we were testing the Firewall cookbook thoroughly on Windows. After a bit of Google-fu, I discovered the blog post Fletcher had written regarding Windows support released in Test-Kitchen back in April.

As it turns out, support for Windows in Test-Kitchen & Vagrant were actually quite simple to use. Just running `vagrant plugin install vagrant-winrm` on my workstation was enough for me to get started by adding a new platform to .kitchen.yml: windows-2012r2. The hardest part was actually getting a Windows Vagrant box, and Fletcher had even already documented that quite well himself. Once I had a Windows box defined, test-kitchen converges went swimmingly on Windows.

Next, I needed to actually test these firewall commands. Back to Google to find the Serverspec documentation on Windows Support, where I found this gem:

Serverspec is now providing a limited support for Microsoft Windows.

As it turns out, enough functionality was there to do the limited testing I actually wanted to do. I had to adapt the provided spec_helper.rb example to work on both Windows and Linux guests, like so:

if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM).nil?
  set :backend, :exec
  set :path, '/sbin:/usr/local/sbin:/bin:/usr/bin:$PATH'
  set :backend, :cmd
  set :os, family: 'windows'

# helper method to conditionally guard each test by platform
def linux?
  %w(debian redhat ubuntu).include?(os[:family])

def windows?

After these were all in place, and removing some platform-specific environment variables like $HOME, writing tests against Windows guests was easy. And by combining those helper methods with each test, I could ensure the above test would only run when windows? returns true.

describe file("#{ENV['HOME']}/windows-chef.rules"), if: windows? do
  its(:content) { should match(%r{firewall add rule name="prepend" description="prepend" dir=in service=any protocol=tcp localip=any localport=7788 interfacetype=any remoteip=any remoteport=any action=allow}) }

With all of this done, I could actually test pull requests and contributions, and be sure I didn’t break things myself, on Windows. And it has already prevented merges of broken code. Using Test-Kitchen & Serverspec with Windows guests, software testing saves the day again!

Published inTechnology