Test Kitchen (kitchen-vagrant) can provision disposable Microsoft Windows Server virtual machines on a Linux or macOS workstation using VirtualBox, Vagrant, and WinRM. This allows you to develop and test Windows Chef cookbooks locally before uploading them to a Chef Server.
This guide walks through:
- Installing the
vagrant-winrmplugin. - Installing HashiCorp Packer and building a Windows Server 2012 R2 evaluation Vagrant
.boximage from theboxcutter/windowstemplates. - Importing the Windows box into Vagrant and configuring
.kitchen.ymlto runkitchen test.
Step 1: Install the vagrant-winrm Plugin
For Vagrant and Test Kitchen to communicate with a Windows guest over WinRM, install the vagrant-winrm plugin:
vagrant plugin install vagrant-winrm
If vagrant-winrm is missing when .kitchen.yml targets a Windows platform (windows-*), kitchen list will raise:
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::UserError
>>>>>> Message: WinRM Transport requires the vagrant-winrm Vagrant plugin to properly communicate with this Vagrant VM. Please install this plugin with: `vagrant plugin install vagrant-winrm' and try again.
(Note: Vagrant 1.6+ includes native WinRM communicator support, though older releases of kitchen-vagrant explicitly check for the vagrant-winrm plugin.)
Step 2: Install HashiCorp Packer
To build a clean Windows Server Vagrant base box from an official Microsoft Evaluation ISO, we use HashiCorp Packer and the boxcutter/windows Packer templates.
- Download the Packer binary archive from releases.hashicorp.com/packer:
cd ~/work wget https://releases.hashicorp.com/packer/0.10.1/packer_0.10.1_linux_amd64.zip - Extract the
packerbinary and install it into/usr/local/sbin/(or/usr/local/bin/):unzip packer_0.10.1_linux_amd64.zip sudo cp packer /usr/local/sbin/ packer --version # 0.10.1
Step 3: Build a Windows Server 2012 R2 Vagrant Box with boxcutter/windows
- Clone the
boxcutter/windowsrepository:cd ~/work git clone https://github.com/boxcutter/windows.git cd windowsThe repository includes Packer JSON templates for Windows Server 2008 R2, Windows Server 2012 R2 (
eval-win2012r2-standard.json), Windows 7, Windows 8.1, and Windows 10 evaluation images. - Build the VirtualBox image for
eval-win2012r2-standardusingmake:make virtualbox/eval-win2012r2-standard
What the Packer Build Does
Packer automates the entire Windows Server installation and image preparation:
- Downloads the Windows Server 2012 R2 Evaluation ISO and VirtualBox Guest Additions ISO.
- Creates a virtual floppy image containing
Autounattend.xmland post-install setup scripts (install-winrm.cmd,fixnetwork.ps1,disablewinupdate.bat,powerconfig.bat). - Boots the VirtualBox VM, performs an unattended Windows installation, configures the
vagrantuser account, enables WinRM, installs VirtualBox Guest Additions, defragments and zeros free disk space (sdelete), and exports the compressed.boxartifact tobox/virtualbox/eval-win2012r2-standard-nocm-1.0.4.box.
==> virtualbox-iso: Creating floppy disk...
virtualbox-iso: Copying: floppy/eval-win2012r2-standard/Autounattend.xml
virtualbox-iso: Copying: floppy/install-winrm.cmd
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Starting the virtual machine...
==> virtualbox-iso: Waiting for WinRM to become available...
==> virtualbox-iso: Connected to WinRM!
==> virtualbox-iso: Provisioning with shell script: script/vagrant.bat
==> virtualbox-iso: Provisioning with shell script: script/vmtool.bat
==> virtualbox-iso: Provisioning with shell script: script/clean.bat
==> virtualbox-iso: Provisioning with shell script: script/ultradefrag.bat
==> virtualbox-iso: Provisioning with shell script: script/sdelete.bat
(Tip: If you prefer a pre-built Windows evaluation box instead of building from ISO locally, you can also pull boxes directly from Vagrant Cloud such as gusztavvargadr/windows-server.)
Step 4: Register the Windows Box in Vagrant
Once the Packer build completes, add the generated .box file to your local Vagrant box inventory under the name windows-2012r2:
cd ~/work/windows/box/virtualbox
vagrant box add windows-2012r2 eval-win2012r2-standard-nocm-1.0.4.box
vagrant box list
Step 5: Configure .kitchen.yml and Run kitchen test
In your Chef cookbook, update .kitchen.yml to reference the windows-2012r2 platform. Because the platform name begins with windows-, Test Kitchen automatically selects the winrm transport:
---
driver:
name: vagrant
provisioner:
name: chef_zero
platforms:
- name: windows-2012r2
suites:
- name: default
run_list:
- recipe[starter-windows-cookbook::default]
Verify that Test Kitchen recognizes the Windows instance and selects the Winrm transport:
kitchen list
Instance Driver Provisioner Verifier Transport Last Action
windows-2012r2 Vagrant ChefZero Busser Winrm <Not Created>
Run kitchen converge or kitchen test to boot the Windows Server VM, install Chef Client inside the guest, and converge your Windows recipe:
kitchen test