Get started with Chef Workstation
This getting started guide shows you how to create a cookbook, define cookbook tests, lint the cookbook, and deploy a local VM to run and test the cookbook.
Prerequisites
Develop a cookbook with Chef Workstation
Chef Infra defines a common workflow for cookbook development. To create and test your first cookbook, follow these steps:
Generate a new cookbook directory:
chef generate cookbook new_cookbook cd new_cookbookThis creates a cookbook with a default recipe, a
kitchen.ymlconfiguration for Test Kitchen, and a starter InSpec profile undertest/integration/default/.Open the default recipe (
recipes/default.rb) and paste in the following:# recipes/default.rb file '/tmp/hello.txt' do content 'Hello from Chef!' action :create endThis uses the Chef Infra
fileresource to create the/tmp/hello.txtfile and add text to it.Create a Chef InSpec profile that verifies the results of a Chef Infra Client run.
In the
test/integration/default/default_test.rbfile, replace the default InSpec tests with the following:describe file('/tmp/hello.txt') do it { should exist } it { should be_file } its('content') { should eq "Hello from Chef!\n" } endThis uses the InSpec
fileresource to verify that the/tmp/hello.txtfile exists and verify the file’s contents.Lint the cookbook with Cookstyle to check for style issues and autocorrect them:
cookstyle --auto-correct .Run Test Kitchen Enterprise to converge your cookbook in a local VM or container and verify it works:
kitchen convergeThis command does the following:
- Creates a local VM, container, or cloud instance. The default is a Vagrant VM.
- Installs the provisioner (Chef Infra Client) on the instance.
- Loads the cookbook and Test Kitchen config on the instance.
- Chef Infra Client runs the cookbook to bring the instance into the state defined in the cookbook.
Run your InSpec profile against the converged instance:
kitchen verifyTest Kitchen reports a pass or fail for each control defined in the InSpec profile.
You can also log into the instance and verify the file’s text:
kitchen login cat /tmp/hello.txt exitDestroy the test instance when you’re done:
kitchen destroyUpdate the cookbook and InSpec profile, and rerun
kitchen convergeandkitchen verifyuntil all tests pass.