Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Testing with Test Kitchen

InSpec and Cookstyle: Verify and Lint

Learn InSpec controls and resources to verify servers, how Test Kitchen runs them, and how Cookstyle lints cookbooks, with a runnable Ruby stand-in of an InSpec-style DSL.

Two small tools finish the Chef testing toolbox. InSpec checks the state of a real machine: is the package installed, is the port listening, is the file mode 0640? Cookstyle reads your cookbook source code and warns about style problems and deprecated Chef code before you run anything.

You will learn the InSpec language (profiles, controls, resources, matchers), how it plugs into Test Kitchen, what Cookstyle checks and how to fix its findings automatically. The runnable example is a small InSpec-style DSL in plain Ruby, because the real InSpec cannot connect to a machine from this page.

InSpec and Cookstyle are not installed here. Their snippets are real and read-only. The Ruby DSL stand-in checks a fake in-memory system and is labelled as such.

InSpec basics

An InSpec test file is Ruby with a special vocabulary. You describe a resource (a thing on the machine) and expect things about it:

Ruby
# test/integration/default/default_test.rb
describe package('nginx') do
  it { should be_installed }
end

describe service('nginx') do
  it { should be_enabled }
  it { should be_running }
end

describe port(8080) do
  it { should be_listening }
  its('protocols') { should include 'tcp' }
end

describe file('/etc/myapp/app.conf') do
  it { should exist }
  its('mode') { should cmp '0644' }
  its('owner') { should eq 'root' }
  its('content') { should match(/^port=8080$/) }
end

describe command('nginx -t') do
  its('exit_status') { should eq 0 }
end

Useful resources include package, service, file, directory, user, group, port, command, sshd_config, os, http and json. The matcher cmp compares loosely ('0644' equals 0644); eq is strict.

Controls and profiles

For compliance work, wrap tests in a control, which carries an id, a title, a description and an impact (0.0 to 1.0). Reports then say which requirement failed, not just which line:

Ruby
control 'myapp-config-1' do
  impact 0.7
  title 'App config is not world readable'
  desc 'The config contains database settings.'

  describe file('/etc/myapp/app.conf') do
    it { should_not be_readable.by('others') }
  end
end

A profile is a directory with inspec.yml and a controls/ folder. You can run profiles by themselves against any host, which makes InSpec useful far beyond Chef:

Terminal
inspec exec myprofile -t ssh://deploy@web01
inspec exec myprofile -t docker://container_id
inspec exec https://github.com/dev-sec/linux-baseline   # a public baseline profile

InSpec inside Test Kitchen

In kitchen.yml set verifier: name: inspec and point each suite to its tests (test/integration/<suite>). kitchen verify runs the tests on the converged instance, so you check what Chef really produced, not what the recipe says.

flowchart LR
  K["kitchen verify"] --> I["InSpec connects to the instance"]
  I --> R1["package, service, port"]
  I --> R2["file, user, command"]
  R1 --> OUT["pass / fail report"]
  R2 --> OUT

InSpec is agentless: it connects over SSH, WinRM or Docker and runs checks remotely.

A mini InSpec in Ruby

The stand-in below has a fake system (a hash) and a DSL with describe, it, its and a few matchers. It is not InSpec, but it shows how such a language is built: blocks that evaluate lazily against an object.

Ruby Stand-in: an InSpec-style DSL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The file mode check fails because the fake machine has 0640 and the test wants 0644. That is the useful result of InSpec: it points at a concrete difference between what you wanted and what exists.

Cookstyle

Cookstyle is RuboCop with a set of Chef-specific cops. It finds style issues, probable bugs, and deprecated Chef code, and many problems can be fixed automatically:

Terminal
cookstyle .                # report problems
cookstyle -a .             # auto-correct safe problems
cookstyle -A .             # auto-correct everything it can, including unsafe fixes

Examples of what it finds:

Ruby
# Before
execute 'install nginx' do
  command 'apt-get install -y nginx'      # ChefStyle: use the package resource
end

service 'nginx' do
  supports restart: true                 # deprecated, not needed any more
  action :nothing
end

# After
package 'nginx'

service 'nginx' do
  action :nothing
end

Cookstyle also catches missing chef_version in metadata, unused variables, resources with the wrong property names, and code that will break on newer Chef versions. Because it is fast, it runs first in CI (and often as a pre-commit hook), before ChefSpec and Test Kitchen.

A complete pipeline

  1. cookstyle . (seconds): style and deprecation problems.
  2. chef exec rspec (seconds): ChefSpec unit tests.
  3. kitchen test (minutes): converge, idempotence, InSpec verification on real platforms.
  4. Upload or push the policy only if all steps are green.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does InSpec check?

    InSpec connects to a machine, container or cloud API and verifies its state against expectations.
  2. 2. What is a control in InSpec?

    Controls group tests under an id and impact so reports can show which requirement failed.
  3. 3. Which command asks Cookstyle to fix problems automatically?

    The -a flag safely auto-corrects; -A also applies unsafe corrections.
  4. 4. Where does kitchen verify run the InSpec tests?

    The verifier checks the converged instance, which shows the real result of the Chef run.

Summary

  • InSpec describes expected machine state with resources and matchers, grouped into controls and profiles; it is agentless.
  • kitchen verify runs InSpec against the converged instance.
  • Cookstyle lints cookbooks, flags deprecated code and can auto-correct.
  • A solid pipeline runs Cookstyle, ChefSpec and Test Kitchen in that order.

Next: Chef Infra Server, Knife and Search