Writing a Test

Being able to manually verify the Chef Infra Client run is great but it would be even better if we had an executable test that would fail if our assumptions were ever proven to be false. Remember how we manually verified that Git was installed in the previous section? Seems like a pretty decent first test to me!

Test Kitchen presumes you want to test things and supports a variety of different frameworks for doing so. For the purpose of our guide we’re going to use a framework called Chef InSpec which Test Kitchen consumes via the kitchen-inspec plugin. For more information on available testing frameworks check out Verifiers

The cookbook skeleton already has conveniently created a test for you at test/integration/default/default_test.rb. Open this file in your editor and edit to match the following content:

describe package('git') do
  it { should be_installed }
end

Why test/integration/default/default_test.rb? Let’s take a look again at our kitchen.yml:

# relevant sections of config
---
verifier:
  name: inspec

suites:
  - name: default
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:

Here we are telling the verifier with the inspec_tests key to look in this directory. In our example we are pointing to a local directory but kitchen-inspec supports remote locations such as git repositories or even a Chef Automate server.

Can you guess what this does even if you haven’t seen a Chef InSpec test before? If not, then take a look at Chef InSpec’s package resource documentation. Otherwise your friends in the Chef Community Slack should be able to help.