Test Kitchen

From Bonus Bits
Jump to: navigation, search

Description

This article gives information on using Test Kitchen for testing Chef automation.


Commands

Commands:
  kitchen console                                 # Kitchen Console!
  kitchen converge [INSTANCE|REGEXP|all]          # Change instance state to converge. Use a provisioner to configure one or more instances
  kitchen create [INSTANCE|REGEXP|all]            # Change instance state to create. Start one or more instances
  kitchen destroy [INSTANCE|REGEXP|all]           # Change instance state to destroy. Delete all information for one or more instances
  kitchen diagnose [INSTANCE|REGEXP|all]          # Show computed diagnostic configuration
  kitchen driver                                  # Driver subcommands
  kitchen driver create [NAME]                    # Create a new Kitchen Driver gem project
  kitchen driver discover                         # Discover Test Kitchen drivers published on RubyGems
  kitchen driver help [COMMAND]                   # Describe subcommands or one specific subcommand
  kitchen exec INSTANCE|REGEXP -c REMOTE_COMMAND  # Execute command on one or more instance
  kitchen help [COMMAND]                          # Describe available commands or one specific command
  kitchen init                                    # Adds some configuration to your cookbook so Kitchen can rock
  kitchen list [INSTANCE|REGEXP|all]              # Lists one or more instances
  kitchen login INSTANCE|REGEXP                   # Log in to one instance
  kitchen setup [INSTANCE|REGEXP|all]             # Change instance state to setup. Prepare to run automated tests. Install busser and related gems on one or more instances
  kitchen test [INSTANCE|REGEXP|all]              # Test (destroy, create, converge, setup, verify and destroy) one or more instances
  kitchen verify [INSTANCE|REGEXP|all]            # Change instance state to verify. Run automated tests on one or more instances
  kitchen version                                 # Print Kitchen's version information

Icon-Tip-Square-Green.png Do all of the kitchen commands from the root of your cookbook

cd /path/cookbooks/mycookbook

Initialize

Create default files in Chef cookbook directory.

kitchen init


List Instances

Default Config

``.kitchen.yml``

kitchen list
Instance                     Driver   Provisioner  Verifier  Transport  Last Action
base-centos-511              Vagrant  ChefZero     Busser    Ssh        <Not Created>
base-centos-67               Vagrant  ChefZero     Busser    Ssh        <Not Created>
base-centos-72               Vagrant  ChefZero     Busser    Ssh        Created
base-no-firewall-centos-511  Vagrant  ChefZero     Busser    Ssh        <Not Created>
base-no-firewall-centos-67   Vagrant  ChefZero     Busser    Ssh        <Not Created>
base-no-firewall-centos-72   Vagrant  ChefZero     Busser    Ssh        <Not Created>

Alt Config

``.kitchen.ec2.yml``

KITCHEN_YAML=.kitchen.ec2.yml kitchen list
Instance        Driver  Provisioner  Verifier  Transport  Last Action
base-rhel6      Ec2     ChefZero     Busser    Ssh        <Not Created>
base-rhel7      Ec2     ChefZero     Busser    Ssh        <Not Created>
base-win2008r2  Ec2     ChefZero     Busser    Winrm      <Not Created>
base-win2012r2  Ec2     ChefZero     Busser    Winrm      <Not Created>


Create

Create Spins up the VM, but does not install Chef Solo or run your Chef code.

All Instances

kitchen create

Individual Instance

kitchen create base-centos-67


Converge

Install Chef Solo and run Chef Code. Similar to vagrant provision; can be used to re-run Chef code after making changes locally.

All Instances

kitchen converge

Individual Instance

kitchen converge base-centos-67


Setup

Prepare to run automated tests. Install busser and related gems on one or more instances.

All Instances

kitchen setup

Individual Instance

kitchen setup base-centos-67


Verify

Run automated tests on one or more instances

All Instances

kitchen verify

Individual Instance

kitchen verify base-centos-67


Test

Runs the following in order destroy, create, converge, setup, verify and destroy

All Instances

kitchen test

Individual Instance

kitchen test base-centos-67


Run Command

All Instances

kitchen exec -c '/bin/nodeinfo'

Individual Instance

kitchen exec base-centos-67 -c '/bin/nodeinfo'


Login

Login to the instance like vagrant ssh

kitchen login base-centos-67


Delete VM

All Instances

kitchen destroy

Individual Instance

kitchen destroy base-centos-67


Local Override Config File

You can create a test kitchen yaml file that you do not commit to the repo that will override the default section by section. I say section by section, because you can't just override one item of say the driver section. If you did; it would simply wipe out all the other driver settings in the default and only have your one entry. So you have to grab the who section and make your customizations. The easiest option is to simply make a copy of the default kitchen yaml and add in your personal settings. As you will see in my example below. This is good for adding your AWS settings, custom proxy settings, changing CPU/Mem on a local etc.

  1. Copy the default kitchen configuration file.
    cd /path/to/cookbooks/mycookbook
    
    cp .kitchen.yml .kitchen.local.yml
    
  2. Edit the local override configuration.
    vim .kitchen.local.yml
    
  3. Now kitchen commands will default to first pulling setting from the .kitchen.local.yml and then if there are additional sections not in that local from the .kitchen.yml


Icon-Tip-Square-Green.png Another option is to make a copy of any of the kitchen configurations and name it whatever you want. Add your customizations and then set the KITCHEN_YAML environment variable method to call it instead of the default kitchen configuration.
KITCHEN_YAML=.kitchen.mycoolconfig.yml kitchen <command>


YAML Configuration Examples

Reference Test Kitchen Config Examples


Sources


Related Articles