Talk Recap: TDD with Chef, PowerShell DSC and Pester on Windows

Just finished a fun talk at the 2017 Powershell & Devops Summit about test-driven development with Chef, PowerShell DSC and Pester on WIndows.  We ended up with 60+ attendees in the talk, all of which were engaged and asked some really great questions.

Source

All the demo code is available on Github at the following location for you to try.

https://github.com/rschiefer/TDDwithChefDSCPester

Do note the MSI/APPX file are referencing a local cache in the .kitchen.yml file.  You can swap the commented lines to reference these packages on the web. 

Also compare the versions of my setup with your own to avoid issues. 

image

Demo Steps

The basic steps for the test-driven demo are as follows:

1 – Setup VM and Run existing tests

Install the ChefDK (see version info above) if needed and pull the source from Github.  Run kitchen verify nano to create/converge/verify the cookbook.  You should see each stage succeed and ultimately 2 passing Pester tests:

image

2 – Add an Integration (Pester) Test

Uncomment the registry key test at the bottom of the when_running_the_default_recipe.Tests.ps1 Pester file and run kitchen verify nano.  The new test will fail because we haven’t implemented the registry change in our cookbook or applied the cookbook to the VM.

    it 'should have registry set' {
        (Get-ItemProperty 'HKLM:\SOFTWARE\Test').foobar | should be 0
    }

3 – Add a Unit (ChefSpec/rspec) Test

Uncomment the registry key unit test at the bottom of the default_spec.rb file and run chef exec rspec.  The unit test should fail because we haven’t added the registry resource to the cookbook yet.

      it 'creates registry key' do
        expect(chef_run).to run_dsc_resource('addRegistryKey').with(
            resource: :registry,
            properties: {
              Key: 'HKEY_LOCAL_MACHINE\SOFTWARE\Test',
              Ensure: 'Present',
              ValueType: 'DWORD',
              ValueName: 'foobar',
              ValueData: ['0']
            }
          )
      end

4 – Add the Registry change to the Cookbook/Recipe

Uncomment the addRegistryKey DSC resource in the default.rb recipe file. 

dsc_resource 'addRegistryKey' do
    resource :registry
    property :Key, 'HKEY_LOCAL_MACHINE\SOFTWARE\Test'
    property :Ensure, 'Present'
    property :ValueType, 'DWORD'
    property :ValueName, 'foobar'
    property :ValueData, ['0' ]
end

5 – Passing Tests!

Rerun the chef exec rspec command to see the unit test passing. Next run kitchen converge nano to apply the new resource to the virtual machine.  Lastly, rerun kitchen verify nano to see the Pester test passing as well.

image

image

TDD is Good with CHEF!

As you can see it is not hard to use a TDD workflow with Chef.  I highly suggest using TDD to develop your Chef cookbooks.  I have found that it provides  a much faster feedback loop and results in a better design for cookbooks.

If you have further questions or have issues please leave a comment below to continue the conversation.

Happy configuring!

1 thought on “Talk Recap: TDD with Chef, PowerShell DSC and Pester on Windows”

  1. Pingback: ChefDK 1.4.3 Upgrade – dotnet catch

Leave a Reply