One of the lovely parts of Chef is using Test-Kitchen to build out your cookbooks using TDD, or just easy testing as build out your cookbooks. I’ve just gone through the local-development-and-testing module on LearnChef (A great resource for Chef users!) and while i love having tests in InSpec, I already have a lot in Pester.. While reading through the kitchen.yml documentation i saw that there is a pester driver. allowing you to run pester tests from within Test-Kitchen! This saves me re-writting 100s of tests, so lets give it a try.. I’m expecting that you already have the ChefDK installed and you have configured test-kitchen to run with your driver

Install the Kitchen-Pester Gem

To get started we need the kitchen-pester gem. This can be installed via:

chef gem install kitchen-pester

This should get an output like below:

kitchen-pester01

Update kitchen.yml to use kitchen-pester

Now open a cookbook that you wish to add pester tests (Ill be using a cookbook created with learnchef that installs IIS on a windows 2012 server)

Open the kitchen.yml file:

Before making any changes mine looks like below and is running some InSpec tests.

kitchen-pester02

Now update the Verifier to be pester (it should look below)

kitchen-pester03

Create a Pester Test

Now we need to create a test. We didn’t specify a folder for tests so use the default which is

<COOKBOOK>\Tests\Integration\default

The test folder is at the same level as Recipe.

Create a new file called “iis.tests.ps1” and put in the below text, save the file.

Describe 'IIS Tests' {
    Context 'IIS' {
        It "Should have port 80 respond" {
            (Test-NetConnection -port 80).TcpTestSucceeded |
            Should be $true
        }
    }
}

Run Test-Kitchen

Now run your tests. (either step by step or just use kitchen test to destroy, build, converge, test, destroy)

Below is an example of running the kitchen verify results (aka running your tests

kitchen verify

The output should look like:

kitchen-pester04

And there we go, we are using Pester for our test!

Advertisements