替代司机与测试厨房

时间:2014-10-22 20:19:15

标签: chef test-kitchen

许多食谱,例如mysql cookbook都有多个.kitchen.yml文件。例如,mysql有一个.kitchen.yml和一个.kitchen-cloud.yml。查看测试厨房的文档和代码,我看不到使用.kitchen.yml.kitchen.local.yml~/.kitchen/config.yml以外的配置文件的任何方法。如果我想使用mysql食谱中的云驱动程序,我会:

  • cp .kitchen-cloud.yml .kitchen.yml
  • cp .kitchen-cloud.yml .kitchen.local.yml
  • 别的什么?

似乎应该有一个更清晰的方法来使用替代配置文件,强制替换默认配置文件。

由于

4 个答案:

答案 0 :(得分:26)

Kitchen提供了三个环境变量来控制每个可能的配置文件的查找位置。要使默认行为显式,可以按如下方式设置它们:

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

您无需覆盖所有这些内容,因此您可以使用.kitchen-cloud.yml运行test-kitchen,如下所示:

$ KITCHEN_YAML=".kitchen-cloud.yml" kitchen test

答案 1 :(得分:12)

...要添加到coderanger,如果要根据CI工具是否设置环境变量来选择驱动程序或选项,还可以执行以下操作:

---
<%
  #--------------------------------------------------------------------------
  # 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: {
  }

这样,您可以维护单个文件并避免不同的平台测试(在一个文件中进行更改而在另一个文件中进行更改)。在某些情况下,.kitchen.local.yml中提供的选项可能会与.kitchen.yml中的选项冲突。

答案 2 :(得分:7)

除了zts所说的,记住你可以在厨房文件中使用ERb,所以你的驱动程序配置可能如下所示:

driver:
  name: <%= ENV['KITCHEN_DRIVER'] || 'vagrant' %>

答案 3 :(得分:3)

我找到了这个问题,同时寻找一个解决方案,用一个厨房文件支持多个驱动程序,而艾夫斯的回答非常有帮助。我改编它来做以下事情。

  • 默认为流浪汉驱动程序
  • 允许用户使用KITCHEN_DRIVER环境变量覆盖驱动程序设置
  • 选择docker_ssh驱动程序(如果已安装)。
---
<%
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 %>