Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate drivers with test-kitchen

Many cookbooks, such as the mysql cookbook have multiple .kitchen.yml files. For example, mysql has a .kitchen.yml and a .kitchen-cloud.yml. Looking at documentation and code for test-kitchen, I can't see any way to use config files other than .kitchen.yml, .kitchen.local.yml, and ~/.kitchen/config.yml. If I wanted to use the cloud driver from the mysql cookbook, would I:

  • cp .kitchen-cloud.yml .kitchen.yml
  • cp .kitchen-cloud.yml .kitchen.local.yml
  • something else??

It just seems like there should be a cleaner approach to using the alternative config file that a brute force replacement of the default ones.

Thanks

like image 406
Tejay Cardon Avatar asked Oct 22 '14 20:10

Tejay Cardon


4 Answers

Kitchen provides three environment variables to control where it looks for each of the possible configuration files. To make the default behaviour explicit, you could set them as follows:

KITCHEN_YAML="./.kitchen.yml"
KITCHEN_LOCAL_YAML="./.kitchen.local.yml"
KITCHEN_GLOBAL_YAML="$HOME/.kitchen/config.yml"

You don't need to override all of them, so you could run test-kitchen with your .kitchen-cloud.yml like so:

$ KITCHEN_YAML=".kitchen-cloud.yml" kitchen test
like image 84
zts Avatar answered Nov 05 '22 05:11

zts


... to add to coderanger, if you want to select drivers or options based on whether your CI tool sets environment variables, you could also do something like this:


---
<%
  #--------------------------------------------------------------------------
  # the driver_plugin can be overridden with an environment variable:
  #   $ KITCHEN_DRIVER=docker kitchen test
  # if not specified, defaults are used...
  #   - kitchen_driver_ci if environment variable CI=true or TRAVIS=true are present
  #   - kitchen_driver_local is used otherwise (which defaults to vagrant)
  #--------------------------------------------------------------------------

  kitchen_driver_ci = 'ec2'
  kitchen_driver_local = 'vagrant'
  kitchen_driver_default = kitchen_driver_local

  if ENV['KITCHEN_DRIVER']
    kitchen_driver = ENV['KITCHEN_DRIVER']
  elsif ENV['TRAVIS']=="true"
    kitchen_driver = kitchen_driver_ci
  elsif ENV['CI']=="true"
    kitchen_driver = kitchen_driver_ci
  else
    kitchen_driver = kitchen_driver_default
  end
  puts "-----> driver_plugin: #{kitchen_driver.to_s}"    
%>

driver_plugin: <%= kitchen_driver %>
driver_config:
  require_chef_omnibus: 11.10.4
  <% if kitchen_driver == 'ec2' %>
  aws_access_key_id:      <%= ENV['AWS_ACCESS_KEY_ID'] %>
  aws_secret_access_key:  <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  aws_ssh_key_id:         <%= ENV['AWS_SSH_KEY_ID']         || "test-kitchen" %>
  ssh_key:                <%= ENV['AWS_SSH_KEY_FILE']       || "./test-kitchen.pem" %>
  region:                 <%= ENV['AWS_REGION']             || "us-east-1" %>
  availability_zone:      <%= ENV['AWS_AVAILABILITY_ZONE']  || "us-east-1c" %>
  flavor_id: "t2.small"
  groups: ["test-kitchen"]
  <% end %>
  <% if kitchen_driver == 'vagrant' %>
  customize:
    memory: 2048
  <% end %>

platforms:
- name: ubuntu-14.04
  <% if kitchen_driver == 'ec2' %>
  driver_config:
    image_id: ami-6ab2a702
    username: ubuntu
    tags: { "Name": "Test Kitchen" }
  <% end %>

busser:
  sudo: true

suites:
- name: default
  run_list: [
  ]
  attributes: {
  }

This way you maintain a single file and avoid divergent platform tests (making a change in one file and forgetting to in another). There are also cases where options provided in .kitchen.local.yml might conflict with those in .kitchen.yml.

like image 38
ives Avatar answered Nov 05 '22 05:11

ives


In addition to what zts said, remember that you can use ERb in kitchen files, so your driver config can look like this:

driver:
  name: <%= ENV['KITCHEN_DRIVER'] || 'vagrant' %>
like image 9
coderanger Avatar answered Nov 05 '22 06:11

coderanger


I found this Question while looking for a solution to supporting multiple drivers with one kitchen file and Ives's answer was very helpful. I adapted it to do the following.

  • Default to the vagrant driver
  • Allow the user to override the driver setting with a KITCHEN_DRIVER environment variable
  • Select the docker_ssh driver if it is installed.
---
<%
require 'rubygems'
kitchen_driver = 'vagrant'

if ENV['KITCHEN_DRIVER']
    kitchen_driver = ENV['KITCHEN_DRIVER']
elsif Gem::Specification::find_all_by_name('kitchen-docker_ssh').any?
    kitchen_driver = 'docker_ssh'
end
%>

driver:
  name: <%= kitchen_driver %>
like image 4
Joseph Mulloy Avatar answered Nov 05 '22 04:11

Joseph Mulloy