Update: Test-Kitchen beta is here! See this post for an example using the latest prerelease gems.
I've posted about using Test-Kitchen on Windows a couple times. See this post and this one too. Both of these posts include rather fragile instructions on how to prepare your environment in order to make this possible. Writing them feels like handing out scissors and then encouraging people to run on newly oiled floors generously sprinkled with legos while transporting said scissors. Then, if they are lucky, their windows nodes will converge and kick off tests ready for their review once they reach "the other side." Its dangerous. Its exciting. Pain may be an active ingredient.
Well development has been ramping up in this effort. Some of the outside forks have now been merged into a dedicated branch of the official Test-Kitchen repo - windows-guest-support and its been rebased with the latest master branch of Test-Kitchen. A group of folks from within and outside of chef including test-kitchen creator Fletcher Nichol as well as Salim Afiune who got the ball rolling on windows compatibility meet regularly to discuss progress and bugs. I'm honored to be involved and contributed the winrm based file copying logic (future blog post pending - my wounds have not yet fully healed).
I can't wait until the day that no special instructions are required and we think that day is not far off but here is an update on how to get up and running with the latest bits. Lots have changed since my last post but I think its much simpler now.
What to install and where to get it
First clone the windows-guest-support branch of the Test-Kitchen repo:
git clone -b windows-guest-support https://github.com/test-kitchen/test-kitchen
Build and install the gem. If you are running the chefdk on either windows or linux, you can use the rake task dk_install which will do the build and install and additionally overlay the bits on top of the omnibussed Test-Kitchen.
rake dk_install
This may not be compatible with all drivers. I use it regularly with:
- The windows-guest-support branch of Kitchen-vagrant
- kitchen-docker - Linux only but shows that linux still works
- A closed source vSphere driver I support CenturyLink Cloud where I work (hoping it will one day be open sourced)
Lets run through the vagrant setup.
Clone the windows-guest-support branch of kitchen-vagrant:
git clone -b windows-guest-support https://github.com/test-kitchen/kitchen-vagrant
Build and install the gem:
rake install
You should now be good to go:
C:\> kitchen -v Test Kitchen version 1.3.2.dev
Configuration
There is just one thing that needs changing in your .kitchen.yml configuration file. As an example, here is my .kitchen.yml for a recent PR of mine adding windows support to the chef-minecraft cookbook:
driver_plugin: vagrant provisioner: name: chef_zero platforms: - name: windows-2012R2 driver_config: box_url: https://wrock.blob.core.windows.net/vhds/vbox2012r2.box communicator: winrm vm_hostname: false transport: name: winrm - name: ubuntu-12.04 run_list: - recipe[ubuntu] driver_config: box: hashicorp/precise64 suites: - name: default run_list: - recipe[minitest-handler] - recipe[minecraft] attributes: minecraft: accept_eula: true
The windows box hosted in my Azure storage is an evaluation copy due to expire in a couple months. I try to rebuild it before it expires. Note the transport setting here:
transport: name: winrm
This tells test-kitchen to use the winrm transport instead of the default ssh transport. Furthermore, you will notice that a
kitchen list
produces slightly modified output:
Instance Driver Provisioner Transport Last Action default-windows-2012R2 Vagrant ChefZero Winrm <Not Created> default-ubuntu-1204 Vagrant ChefZero Ssh <Not Created>
Note the new transport column.
A note for Hyper-V users
I tend to use Hyper-V on my personal windows laptop and VirtualBox on my work Ubuntu laptop. I have only one issue on Hyper-V now. It hangs when vagrant tries to change the hostname of the box. I believe this is a bug in Vagrant. If you interrupt the box provisioning and boot into the box, it then blue screens - at least this has been my experience. To work around this for now I comment out line 24 of templates/Vagrantfile.erb in the kitchen-vagrant driver:
<% if config[:vm_hostname] %> # c.vm.hostname = "<%= config[:vm_hostname] %>" <% end %>
Then I reinstall the gem.
Tip: The url to my Hyper-V vagrant box with an evaluation copy of windows 2012R2 is:
https://wrock.blob.core.windows.net/vhds/hyperv2012r2.box
Lets all join hands and bow our heads in convergence
You'll appreciate the spiritual tone when your screen reveals a converged vm with passing tests. Either that or you will rage quit windows when this all goes to bloody hell, but I'm gonna try and keep a "glass half full" attitude here. You are welcome to follow along with me and the mine craft server cookbook. Clone my repo:
git clone -b windows https://github.com/mwrock/chef-minecraft
Now set the .kitchen-vagrant.yml file to be the "active" kitchen config file instead of .kitchen.yml which is configured to use DigitalOcean:
Powershell
$env:KITCHEN_YAML=".kitchen-vagrant.yml"
Bash
export KITCHEN_YAML=.kitchen-vagrant.yml
And all together now on 1, 2, 3...Converge!!
kitchen converge default-windows-2012R2
While you wait...:
Just 3 minutes later, its a success!!!
15 tests, 5 assertions, 0 failures, 0 errors, 0 skips - MiniTest::Chef::Handler Running handlers complete Chef Client finished, 34/57 resources updated in 180.231501 seconds Finished converging <default-windows-2012R2> (5m4.94s).
Side note on automating local policy
This might be somewhat unrelated but I just cannot let it go. The minecraft server cookbook creates a windows scheduled task (kinda like a linux cron job) that runs the java process that hosts the minecraft server and it creates a user under which the job runs. In order to run a scheduled task, a windows user must have the "log on as batch job" right configured in their local policy.
Turns out this is a bit tricky to automate. I'll spare the audience from the horror which makes this possible but if you must look, see https://github.com/mwrock/chef-minecraft/blob/windows/templates/default/LsaWrapper.ps1.erb. Basically this can only be done by calling into the windows API as is done here. Big thanks to Fabien Dibot for this information!
Hurry up and wait! Test and provide feedback
There is alot of work going into making sure this is stable and provides a good experience for those wanting to test windows infrastructure with test-kitchen. However there are so many edge cases that are easy to miss. I very much encourage anyone wanting to try this out to do so and reach out via github issues to report problems.