我用test-kitchen + serverspec进行了尝试
经过
上次尝试了第一次使用Vagrant + Chef Zero + Berkshelf,
通过SSH连接到客户操作系统,并通过目视检查确认应用的烹饪书是否生效。
然而,对于今后自己编写烹饪书时,我希望能够轻松地确认配方是否反映在其中,
于是遇到了tet-kitchen + serverspec。
立刻试用一下
创建 test-kitchen 的配置文件
我们将创建一个.kitchen.yml文件。默认的驱动程序似乎是Vagrant。
$ cd test_centos64/chef-repo
$ kitchen init
---
driver:
name: vagrant
provisioner:
name: chef_solo
platforms:
- name: ubuntu-12.04
- name: centos-6.4
suites:
- name: default
run_list:
attributes:
-
- driver
-
- テストする仮想環境を指定します。
-
- provisioner
-
- 利用する provisioner を指定します。
-
- platforms
-
- テストする環境を指定します。複数可。
-
- suites
- 実行するテストスイート。
请将 .kitchen.yml 文件如下所示进行更改。
我们将创建一个测试套件,以验证上次使用的 recipe 的时区-ii 是否正确反映。
---
driver:
name: vagrant
provisioner:
name: chef_zero
require_chef_omnibus: latest
platforms:
- name: ubuntu-12.04
- name: centos-6.4
driver:
box: centos-6.4
box_url: http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20131103.box
suites:
- name: timezone
run_list:
- recipe[timezone-ii::rhel]
attributes:
tz: "Asia/Tokyo"
excludes:
- ubuntu-12.04
-
- platforms の driver の box_url を指定してあげないと、kitchen create時にエラー発生。
- provisioner の require_chef_omnibus を指定してあげないと、kitchen setup時にエラー発生。
创建测试用例
测试目录位于以下位置:
test/integration/SUITES_NAME/TESTING_FRAMEWORK/
这次我们决定使用 Serverspec 来进行测试框架。
没有特别的选择原因,只是因为想试试看而已w。
因此,路径是
test/integration/timezone/serverspec/
在此之后,将加载*_spec.rb文件。
那么,让我们来创建一个测试用例来确认时区是否已经设置为”Asia/Tokyo”。
$ mkdir -p test/integration/timezone/serverspec/
$ vi test/integration/timezone/serverspec/change-jp_timezone_spec.rb
require 'serverspec'
# これないと verify 時に"No backend type is specified. Fall back to :exec type."がいっぱい出力される
set :backend, :exec
describe command('date') do
its(:stdout) { should match /JST/ }
end
describe command('strings /etc/localtime') do
its(:stdout) { should match /JST-9/ }
end
describe file('/etc/sysconfig/clock') do
its(:content) { should match(/ZONE=\"Asia\/Tokyo\"/) }
end
考试进行
因为测试用例已经创建了,所以我们要确认实例列表和生命周期中的状态。
$ kitchen list
Instance Driver Provisioner Last Action
timezone-centos-64 Vagrant ChefZero <Not Created>
创建测试对象的客户操作系统。
$ kitchen create timezone-centos-64
$ kitchen list
Instance Driver Provisioner Last Action
timezone-centos-64 Vagrant ChefZero Created
由于是第一次创建,所以需要进行设置。
$ kitchen setup timezone-centos-64
$ kitchen list
Instance Driver Provisioner Last Action
timezone-centos-64 Vagrant ChefZero Set Up
如果想在第二次及以后的操作中将 recipe 应用于实例,请执行”converge”操作。
$ kitchen converge timezone-centos-64
$ kitchen list
Instance Driver Provisioner Last Action
timezone-centos-64 Vagrant ChefZero Converged
我已经准备好了,现在开始进行你期待已久的考试。
$ kitchen verify timezone-centos-64
・・・
Finished in 0.04841 seconds (files took 0.28425 seconds to load)
3 examples, 0 failures
Finished verifying <timezone-centos-64> (4m23.56s).
-----> Kitchen is finished. (4m24.02s)
$ kitchen list
Instance Driver Provisioner Last Action
timezone-centos-64 Vagrant ChefZero Verified
我现在可以检查这个菜谱是否被正确地反映出来了!
暫時的目標是建立初學者的本地測試環境,省去了確認的繁瑣步驟,這樣做有很大的好處。
這樣一來,我們可以輕鬆地確認各種事項是否影響到已經確認過的食譜,看來可以嘗試一些冒險了!