22

许多食谱,例如mysql 食谱有多个 .kitchen.yml 文件。例如,mysql 有一个 .kitchen.yml 和一个 .kitchen-cloud.yml。查看 test-kitchen 的文档和代码,除了.kitchen.yml.kitchen.local.yml~/.kitchen/config.yml. 如果我想使用说明书中的云驱动程序mysql,我会:

  • cp .kitchen-cloud.yml .kitchen.yml
  • cp .kitchen-cloud.yml .kitchen.local.yml
  • 别的东西??

似乎应该有一种更简洁的方法来使用替代配置文件,即强力替换默认配置文件。

谢谢

4

4 回答 4

32

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
于 2014-10-22T21:06:53.913 回答
13

... 添加到 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 中的选项冲突。

于 2015-05-21T22:12:45.750 回答
8

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

driver:
  name: <%= ENV['KITCHEN_DRIVER'] || 'vagrant' %>
于 2014-10-23T00:22:10.247 回答
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 %>
于 2015-07-16T03:28:53.750 回答