Skip to content

Instantly share code, notes, and snippets.

@s2t2
Created February 18, 2015 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2t2/977a8a88c36789751a85 to your computer and use it in GitHub Desktop.
Save s2t2/977a8a88c36789751a85 to your computer and use it in GitHub Desktop.
Notes on using Vagrant and Test Kitchen with Chef. From an "Ask a Chef Architect" session at the AWS Popup Loft.

Overview

Test Kitchen provides command line tools for interacting with one or more vagrant virtual machines, and can generate multiple vagrant configurations based on information provided in a .kitchen.ymlfile.

Vagrant configures a development environment based on information proved in the Vagrantfile.

Virtual Box is the default virtual machine provider used with the "vagrant" driver (see .kitchen.yml).

BATS is a simple BASH-based unit testing framework for testing the actions that have actually been performed on a virtual machine.

Prerequisites

Install chef via ruby gem, online download, or chef dk. This guide assumes ruby gem installation.

Steps

Download vagrant from http://www.vagrantup.com/downloads. Reference also http://docs.vagrantup.com/v2/getting-started/index.html.

cd /path/to/dir
vagrant init

Install test kitchen. Reference http://kitchen.ci/docs/getting-started/installing and https://github.com/test-kitchen/test-kitchen.

gem install test-kitchen

If provisioning a local development environment, download Virtual Box from https://www.virtualbox.org/wiki/Download. You may have to open the application to accept terms of agreement.

If provisioning an Amazon EC2 instance, install the "ec2" driver. Reference https://github.com/test-kitchen/kitchen-ec2.

gem install kitchen-ec2

Use test kitchen commands.

kitchen help
kitchen init

PRO TIP: gitignore .kitchen

Configure .kitchen.yml:

---
driver:
  #name: vagrant # OR ...
  name: ec2
  aws_access_key_id: MY_IAM_SECRET_ID # create via IAM
  aws_secret_access_key: MY_IAM_SECRET_KEY # create via IAM
  aws_ssh_key_id: aws-popup-chef # see keys menu, use "name"
  ssh_key: /path/to/MY_KEY.pem
  security_group_ids: ["MY_SECURITY_GROUP_ID"] # see security groups menu?
  region: us-west-2 # see aws console region
  availability_zone: us-west-2b # see aws console region
  #require_chef_omnibus: true
  #subnet_id: subnet-6d6...
  #iam_profile_name: chef-client
  #ssh_timeout: 10
  #ssh_retries: 5
  #ebs_volume_size: 6,
  #ebs_delete_on_termination: true
  #ebs_device_name: '/dev/sda1'
  #flavor_id: t2.micro

provisioner:
  name: chef_solo

platforms:
  #- name: ubuntu-12.04
  #  driver:
  #    image_id: ami-XYZZABC
  #    username: ubuntu
  #- name: centos-6.4
  - name: centos-6
    driver:
      image_id: ami-ABCYGF
      username: root # or ec2-user for amazon linux

suites:
  - name: default
    run_list:
     - recipe[mybook::default] # where mybook is the cookbook name
    attributes:
kitchen test

Kitchen test will run all the following:

kitchen create # creates a new virtual machine node
kitchen converge # bootstraps chef on the node, preparing and cooking
kitchen verify # runs integration tests
kitchen destroy

Example Vagrantfile generated from kitchen.yml upon kitchen create:

Vagrant.configure("2") do |c|
  c.vm.box = "opscode-ubuntu-12.04"
  c.vm.box_url = "https://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_ubuntu-12.04_chef-provisionerless.box"
  c.vm.hostname = "default-ubuntu-1204.vagrantup.com"
  c.vm.synced_folder ".", "/vagrant", disabled: true
  c.vm.provider :virtualbox do |p|
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment