Image for post
Image for post

Testing on the Chef Platform: Overview

Introduction to Test Automation with Chef

You awake from you deep slumber, trying to grasp fading memories of an amazing dream. You hear the sound of your phone alerts, pager duty, EDB (Enterprise Database) server not responding. Ouch.

Overview

When developing infrastructure automation, especially in the area of change configuration, such as Chef cookbooks, one opportunity that is often neglected is writing some actual tests, despite the high risk that mistakes in infrastructure could cause.

Types of Testing

On the Chef platform, there are at three types of checks and testing supported: static analysis, unit testing and integration testing. This is an overview of the two types of testing, what they are, and why you might want to use them, or not use them.

Static Analysis

This is not really testing as far as executing code, but rather analyzing the code itself without running it. The process of performing static analysis will analyze the code and report back any common problems and style guide issues. This is usually a first step before doing any testing.

Unit Test

In unit testing, we look at what is the minimal component that we can test, called the unit. In Chef, this would be a Chef recipe, or a custom resource. We look at this component, as an solitary or isolated unit, consider what is the the input and the output.

Integration Testing

  • Multiple Cookbook Integration: testing several cookbooks together, such as a LAMP stack, which would use an Apache cookbook and MySQL cookbook on separate systems, and test the full integration of all the components working together. The integration test would exist outside the cookbook directory.

Systems Testing

The Testing Tools

CookStyle

Category: Static Analysis

FoodCritic

Category: Static Analysis

ChefSpec

Category: Unit Tests

TestKitchen

Category: Test Harness that enables Integration Testing

InSpec

Category: Verifier used for integration testing, systems testing, and compliance

Practices and Trends

Currently, after ChefConf 18, there’s a recent emphasis on jumping straight into integration testing using Test Kitchen, and skipping unit testing altogether.

Practices for Unit Tests

The documentation, training, and tutorials will encourage doing unit tests in the worst way possible, writing tautological tests to redundantly test the Chef itself, rather than test your code logic (if there is code logic).

package 'apache2' { action: install }
it { expect(chef_run).to install_package 'apache2' } # <= Bad! 

Practices for Integration Tests

In integration tests, the highest value you could have for tests the promise of what the cookbook should do, like install a web service with some content.

describe port(80) do
it { should be_listening }
end
describe http('localhost') do
its('status') { should eq 200 }
its('headers.Content-Type') { should include 'text/html' }
its('body') { should include 'Hello World!' }
end

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store