-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from pocke/test-local
Add integration test with `itamae local` command
- Loading branch information
Showing
8 changed files
with
174 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
language: ruby | ||
sudo: required | ||
dist: xenial | ||
services: | ||
- docker | ||
rvm: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'spec_helper' | ||
|
||
describe file('/tmp/cron_stopped') do | ||
it { should be_file } | ||
its(:content) do | ||
expect(subject.content.lines.size).to eq 1 | ||
end | ||
end | ||
|
||
# FIXME: cron service is not running in docker... | ||
# | ||
# root@3450c6da6ea5:/# ps -C cron | ||
# PID TTY TIME CMD | ||
# root@3450c6da6ea5:/# service cron start | ||
# Rather than invoking init scripts through /etc/init.d, use the service(8) | ||
# utility, e.g. service cron start | ||
# | ||
# Since the script you are attempting to invoke has been converted to an | ||
# Upstart job, you may also use the start(8) utility, e.g. start cron | ||
# root@3450c6da6ea5:/# ps -C cron | ||
# PID TTY TIME CMD | ||
# root@3450c6da6ea5:/# | ||
|
||
# describe file('/tmp/cron_running') do | ||
# it { should be_file } | ||
# its(:content) do | ||
# expect(subject.content.lines.size).to eq 2 | ||
# end | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package 'sl' do | ||
version '3.03-17' | ||
end | ||
|
||
###### | ||
|
||
gem_package 'ast' do | ||
version '2.0.0' | ||
options ['--no-ri', '--no-rdoc'] | ||
end | ||
|
||
###### | ||
|
||
service "cron" do | ||
action :stop | ||
end | ||
|
||
execute "ps -C cron > /tmp/cron_stopped; true" | ||
|
||
service "cron" do | ||
action :start | ||
end | ||
|
||
execute "ps -C cron > /tmp/cron_running; true" | ||
|
||
###### | ||
|
||
package "nginx" do | ||
options "--force-yes" | ||
end | ||
|
||
service "nginx" do | ||
action [:enable, :start] | ||
end | ||
|
||
execute "test -f /etc/rc3.d/S20nginx" # test | ||
execute "test $(ps h -C nginx | wc -l) -gt 0" # test | ||
|
||
service "nginx" do | ||
action [:disable, :stop] | ||
end | ||
|
||
execute "test ! -f /etc/rc3.d/S20nginx" # test | ||
execute "test $(ps h -C nginx | wc -l) -eq 0" # test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package 'sl' do | ||
version '3.03-17+b2' | ||
end | ||
|
||
###### | ||
|
||
gem_package 'ast' do | ||
version '2.0.0' | ||
options ['--no-document'] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
desc 'Run integration test on `itamae local` command' | ||
task 'spec:integration:local' do | ||
next if RUBY_DESCRIPTION.include?('dev') | ||
|
||
IntegrationLocalSpecRunner.new( | ||
[ | ||
[ | ||
"spec/integration/recipes/default.rb", | ||
"spec/integration/recipes/default2.rb", | ||
"spec/integration/recipes/redefine.rb", | ||
"spec/integration/recipes/local.rb", | ||
], | ||
[ | ||
"--dry-run", | ||
"spec/integration/recipes/dry_run.rb", | ||
], | ||
], | ||
['spec/integration/default_spec.rb'] | ||
).run | ||
end | ||
|
||
class IntegrationLocalSpecRunner | ||
CONTAINER_NAME = 'itamae' | ||
include FileUtils | ||
|
||
def initialize(suites, specs, ruby_version: RUBY_VERSION.split('.')[0..1].join('.')) | ||
@suites = suites | ||
@specs = specs | ||
@ruby_version = ruby_version | ||
end | ||
|
||
def run | ||
docker_run | ||
prepare | ||
provision | ||
serverspec | ||
clean_docker_container | ||
end | ||
|
||
def docker_run | ||
mount_dir = Pathname(__dir__).join('../').to_s | ||
sh 'docker', 'run', '--privileged', '-d', '--name', CONTAINER_NAME, '-v', "#{mount_dir}:/itamae", "ruby:#{@ruby_version}", 'sleep', '1d' | ||
end | ||
|
||
def prepare | ||
docker_exec 'gem', 'install', 'bundler' | ||
docker_exec 'bundle', 'install', options: %w[--workdir /itamae] | ||
docker_exec 'apt-get', 'update', '-y' | ||
docker_exec 'apt-get', 'install', 'locales', 'sudo', '-y' | ||
docker_exec 'localedef', '-i', 'en_US', '-c', '-f', 'UTF-8', '-A', '/usr/share/locale/locale.alias', 'en_US.UTF-8' | ||
end | ||
|
||
def provision | ||
@suites.each do |suite| | ||
cmd = %W!bundle exec ruby -w bin/itamae local! | ||
cmd << "-l" << (ENV['LOG_LEVEL'] || 'debug') | ||
cmd << "-j" << "spec/integration/recipes/node.json" | ||
cmd += suite | ||
|
||
docker_exec(*cmd, options: %w[--workdir /itamae]) | ||
end | ||
end | ||
|
||
def serverspec | ||
ENV['DOCKER_CONTAINER'] = CONTAINER_NAME | ||
sh('bundle', 'exec', 'rspec', '-I', './spec/integration', *@specs) | ||
end | ||
|
||
def clean_docker_container | ||
sh('docker', 'rm', '-f', CONTAINER_NAME) | ||
end | ||
|
||
def docker_exec(*cmd, options: []) | ||
sh 'docker', 'exec', '--env', 'LANG=en_US.utf8', *options, CONTAINER_NAME, *cmd | ||
end | ||
end |