Skip to content

Instantly share code, notes, and snippets.

@sawanoboly
Last active December 25, 2015 06:09
Show Gist options
  • Save sawanoboly/6929466 to your computer and use it in GitHub Desktop.
Save sawanoboly/6929466 to your computer and use it in GitHub Desktop.
serverspecで完結するテスト駆動インフラ構築 (Test-Driven-Infrastructure with serverspec) ref: http://qiita.com/sawanoboly/items/f2e60d69a336c426ef97
require 'spec_helper'
describe command('which chef-solo') do
it {
begin
should return_exit_status(0)
rescue => e
puts e.message
puts "======= Installing Omnibus Chef..."
cmd = Serverspec::Type::Command.new('curl -L https://www.opscode.com/chef/install.sh | sudo bash')
expect(cmd.return_exit_status?(0)).to be_true
end
}
end
describe package('git') do
it {
begin
should be_installed
rescue => e
puts e.message
puts "======= installing git..."
cmd = serverspec::type::command.new('apt-get update && apt-get install -y git')
expect(cmd.return_exit_status?(0)).to be_true
end
}
end
describe file('/var/chef/cookbooks/.git') do
it {
begin
should be_directory
rescue => e
puts e.message
puts "======= initializing cookbooks directory..."
cmd = Serverspec::Type::Command.new('mkdir -p /var/chef/cookbooks')
expect(cmd.return_exit_status?(0)).to be_true
cmd = Serverspec::Type::Command.new('git init /var/chef/cookbooks')
expect(cmd.return_exit_status?(0)).to be_true
cmd = Serverspec::Type::Command.new('touch /var/chef/cookbooks/.gitkeep')
expect(cmd.return_exit_status?(0)).to be_true
cmd = Serverspec::Type::Command.new('cd /var/chef/cookbooks && git add . && git commit .gitkeep -m "empty init"')
expect(cmd.return_exit_status?(0)).to be_true
end
}
end
describe package('rabbitmq-server') do
it {
begin
should be_installed
rescue => e
puts e.message
puts "======= installing rabbitmq by chef..."
cmd = Serverspec::Type::Command.new('knife cookbook site install rabbitmq')
expect(cmd.return_exit_status?(0)).to be_true
cmd = Serverspec::Type::Command.new('chef-solo -o "rabbitmq"')
expect(cmd.return_exit_status?(0)).to be_true
end
}
end
require 'spec_helper'
describe package('nginx') do
it {
begin
should be_installed
rescue => e
puts e.message
puts "======= Installing Nginx..."
cmd = Serverspec::Type::Command.new('apt-get update && apt-get install -y nginx')
expect(cmd.return_exit_status?(0)).to be_true
end
}
end
require 'spec_helper'
describe package('nginx') do
it { should be_installed }
end
$ rspec -fd
Package "nginx"
expected Package "nginx" to be installed
======= Installing Nginx...
should be true
Finished in 13.42 seconds
1 example, 0 failures
Failures:
1) Package "nginx"
Failure/Error: it { should be_installed }
dpkg -s nginx && ! dpkg -s nginx | grep -E '^Status: .+ not-installed$'
Package `nginx' is not installed and no info is available.
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.
expected Package "nginx" to be installed
# ./spec/xxx.xxx.xxx.xxx/nginx_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 1.01 seconds
1 examples, 1 failures
$ rspec -fd
Package "nginx"
should be installed
Finished in 1.03 seconds
1 example, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment