Vagrant 环境的自动化测试
Automated testing of Vagrant environments
在许多项目中,我使用 Vagrant 和 Puppet 来获得实时环境的副本以进行开发。有时,尤其是在小项目中,我常常不得不做出改变,我重新配置了 vagrant box,但它失败了!
是否可以使用持续集成测试 Vagrant box?
我需要检查框规定是否没有错误,以及运行一些自定义测试,例如打开网页。
Vagrant 与验收测试的其他选择
在我详细介绍之前,Vagrant 绝对是本地验收测试的选择,因为它非常容易设置。但是,在 CI 环境中进行测试要困难得多,因为您必须设置所有不同的部分才能使其正常工作(Ruby、Vagrant、Virtualbox 等)。 Docker 是一个不错的选择,因为它是轻量级的,而且很多 CI 工具都内置了基于 Docker 的测试(例如 Travis、Gitlab CI、CircleCI ).
我详细 here 关于使用 Docker。它并不完美,因为容器不是真正的机器:您无法测试 sysctl 或交换之类的东西。但它非常适合测试基本的 Puppet 模块(包、配置文件服务)。
关于使用什么来测试 Puppet 代码,您有两个主要选择:
烧杯-rspec
Beaker 是由 Puppet 的发布工程团队编写的工具,用于测试 Puppet Enterprise 堆栈。后来 Beaker-rspec 诞生了,以提供更 rspec 的 Puppet 模块测试体验。
您编写的验收测试如下所示:
require 'spec_helper_acceptance'
describe 'cockpit class' do
context 'default parameters' do
# Using puppet_apply as a helper
it 'should work idempotently with no errors' do
pp = <<-EOS
class { '::cockpit': }
EOS
# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
describe package('cockpit') do
it { is_expected.to be_installed }
end
describe service('cockpit') do
# it { is_expected.to be_enabled }
it { is_expected.to be_running }
end
context 'Cockpit should be running on the default port' do
describe command('sleep 15 && echo "Give Cockpit time to start"') do
its(:exit_status) { should eq 0 }
end
describe command('curl 0.0.0.0:9090/') do
its(:stdout) { should match /Cockpit/ }
end
end
end
end
然后你 运行 对选定的 "hypervisor" 进行测试。所以在你的情况下这将是流浪者,我假设使用 Virtualbox。
您像这样配置主机配置文件:
HOSTS:
centos-72-x64:
roles:
- master
platform: el-7-x86_64
box: puppetlabs/centos-7.2-64-nocm
hypervisor: vagrant
CONFIG:
type: foss
然后使用环境变量调用测试以选择要安装的 Puppet 版本等(它将默认为最新版本的 Puppet 以及您设置为默认值的任何框):
$ PUPPET_INSTALL_VERSION="1.5.2" PUPPET_INSTALL_TYPE=agent BEAKER_set="centos-7-x64" bundle exec rake acceptance
/Users/petersouter/.rbenv/versions/2.3.3/bin/ruby -I/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib:/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-support-3.5.0/lib /Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec spec/acceptance
/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/beaker-rspec-5.3.0/lib/beaker-rspec/helpers/serverspec.rb:43: warning: already initialized constant Module::VALID_OPTIONS_KEYS
/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/specinfra-2.67.2/lib/specinfra/configuration.rb:4: warning: previous definition of VALID_OPTIONS_KEYS was here
Beaker::Hypervisor, found some vagrant boxes to create
==> centos-72-x64: VM not created. Moving on...
Bringing machine 'centos-72-x64' up with 'virtualbox' provider...
==> centos-72-x64: Importing base box 'puppetlabs/centos-7.2-64-nocm'...
有很多输出(我将日志设置为详细,但你也可以让它只在失败时显示),但最终你通过了测试:
这是关于 Beaker 的回答-rspec 我在 Serverfault 上给出的答案:
这里有一些其他链接解释了 Beaker-rspec 和 Puppet:
- https://alexharv074.github.io/2016/05/13/setting-up-puppet-module-testing-from-scratch-part-ii-beaker-for-module-testing.html
- https://simp-project.atlassian.net/wiki/display/SD/Debugging+Acceptance+Tests+Using+Beaker
测试厨房
test-kitchen 实际上是一个 Chef 工具,但有人将其分叉以支持 Puppet(和 Ansible)。我对此没有太多经验,但本质上它的工作方式非常相似:你设置一个配置来测试,比如 Vagrant box,然后以规范文件的形式编写测试:
require 'serverspec'
include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS
RSpec.configure do |c|
c.before :all do
c.path = '/sbin:/usr/sbin'
end
end
describe package('ntp') do
it { should be_installed }
end
describe service('ntp') do
it { should be_running }
end
这里有一些很好的链接总结:
在许多项目中,我使用 Vagrant 和 Puppet 来获得实时环境的副本以进行开发。有时,尤其是在小项目中,我常常不得不做出改变,我重新配置了 vagrant box,但它失败了!
是否可以使用持续集成测试 Vagrant box?
我需要检查框规定是否没有错误,以及运行一些自定义测试,例如打开网页。
Vagrant 与验收测试的其他选择
在我详细介绍之前,Vagrant 绝对是本地验收测试的选择,因为它非常容易设置。但是,在 CI 环境中进行测试要困难得多,因为您必须设置所有不同的部分才能使其正常工作(Ruby、Vagrant、Virtualbox 等)。 Docker 是一个不错的选择,因为它是轻量级的,而且很多 CI 工具都内置了基于 Docker 的测试(例如 Travis、Gitlab CI、CircleCI ).
我详细 here 关于使用 Docker。它并不完美,因为容器不是真正的机器:您无法测试 sysctl 或交换之类的东西。但它非常适合测试基本的 Puppet 模块(包、配置文件服务)。
关于使用什么来测试 Puppet 代码,您有两个主要选择:
烧杯-rspec
Beaker 是由 Puppet 的发布工程团队编写的工具,用于测试 Puppet Enterprise 堆栈。后来 Beaker-rspec 诞生了,以提供更 rspec 的 Puppet 模块测试体验。
您编写的验收测试如下所示:
require 'spec_helper_acceptance'
describe 'cockpit class' do
context 'default parameters' do
# Using puppet_apply as a helper
it 'should work idempotently with no errors' do
pp = <<-EOS
class { '::cockpit': }
EOS
# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
describe package('cockpit') do
it { is_expected.to be_installed }
end
describe service('cockpit') do
# it { is_expected.to be_enabled }
it { is_expected.to be_running }
end
context 'Cockpit should be running on the default port' do
describe command('sleep 15 && echo "Give Cockpit time to start"') do
its(:exit_status) { should eq 0 }
end
describe command('curl 0.0.0.0:9090/') do
its(:stdout) { should match /Cockpit/ }
end
end
end
end
然后你 运行 对选定的 "hypervisor" 进行测试。所以在你的情况下这将是流浪者,我假设使用 Virtualbox。
您像这样配置主机配置文件:
HOSTS:
centos-72-x64:
roles:
- master
platform: el-7-x86_64
box: puppetlabs/centos-7.2-64-nocm
hypervisor: vagrant
CONFIG:
type: foss
然后使用环境变量调用测试以选择要安装的 Puppet 版本等(它将默认为最新版本的 Puppet 以及您设置为默认值的任何框):
$ PUPPET_INSTALL_VERSION="1.5.2" PUPPET_INSTALL_TYPE=agent BEAKER_set="centos-7-x64" bundle exec rake acceptance
/Users/petersouter/.rbenv/versions/2.3.3/bin/ruby -I/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/lib:/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-support-3.5.0/lib /Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.5.4/exe/rspec spec/acceptance
/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/beaker-rspec-5.3.0/lib/beaker-rspec/helpers/serverspec.rb:43: warning: already initialized constant Module::VALID_OPTIONS_KEYS
/Users/petersouter/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/specinfra-2.67.2/lib/specinfra/configuration.rb:4: warning: previous definition of VALID_OPTIONS_KEYS was here
Beaker::Hypervisor, found some vagrant boxes to create
==> centos-72-x64: VM not created. Moving on...
Bringing machine 'centos-72-x64' up with 'virtualbox' provider...
==> centos-72-x64: Importing base box 'puppetlabs/centos-7.2-64-nocm'...
有很多输出(我将日志设置为详细,但你也可以让它只在失败时显示),但最终你通过了测试:
这是关于 Beaker 的回答-rspec 我在 Serverfault 上给出的答案:
这里有一些其他链接解释了 Beaker-rspec 和 Puppet:
- https://alexharv074.github.io/2016/05/13/setting-up-puppet-module-testing-from-scratch-part-ii-beaker-for-module-testing.html
- https://simp-project.atlassian.net/wiki/display/SD/Debugging+Acceptance+Tests+Using+Beaker
测试厨房
test-kitchen 实际上是一个 Chef 工具,但有人将其分叉以支持 Puppet(和 Ansible)。我对此没有太多经验,但本质上它的工作方式非常相似:你设置一个配置来测试,比如 Vagrant box,然后以规范文件的形式编写测试:
require 'serverspec'
include Serverspec::Helper::Exec
include Serverspec::Helper::DetectOS
RSpec.configure do |c|
c.before :all do
c.path = '/sbin:/usr/sbin'
end
end
describe package('ntp') do
it { should be_installed }
end
describe service('ntp') do
it { should be_running }
end
这里有一些很好的链接总结: