Skip to content

Commit

Permalink
Merge pull request #281 from pocke/test-local
Browse files Browse the repository at this point in the history
Add integration test with `itamae local` command
  • Loading branch information
sue445 authored Jan 15, 2019
2 parents c62e6a0 + 3d1042b commit efda69f
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 78 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: ruby
sudo: required
dist: xenial
services:
- docker
rvm:
Expand Down
9 changes: 7 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ require 'rspec/core/rake_task'
require 'tempfile'
require 'net/ssh'

Dir['tasks/*.rb'].each do |file|
require_relative file
end

desc 'Run unit and integration specs.'
task :spec => ['spec:unit', 'spec:integration:all']

Expand All @@ -16,7 +20,7 @@ namespace :spec do
targets = ["ubuntu:trusty"]
container_name = 'itamae'

task :all => targets
task :all => targets + ['spec:integration:local']

targets.each do |target|
desc "Run provision and specs to #{target}"
Expand All @@ -37,6 +41,7 @@ namespace :spec do
"spec/integration/recipes/default.rb",
"spec/integration/recipes/default2.rb",
"spec/integration/recipes/redefine.rb",
"spec/integration/recipes/docker.rb",
],
[
"--dry-run",
Expand Down Expand Up @@ -64,7 +69,7 @@ namespace :spec do
RSpec::Core::RakeTask.new(target.to_sym) do |t|
ENV['DOCKER_CONTAINER'] = container_name
t.ruby_opts = '-I ./spec/integration'
t.pattern = "spec/integration/*_spec.rb"
t.pattern = "spec/integration/[default|docker]_spec.rb"
end
end

Expand Down
38 changes: 7 additions & 31 deletions spec/integration/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,6 @@
its(:content) { should eq("2431") }
end

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

describe file('/tmp-link') do
it { should be_linked_to '/tmp' }
its(:content) do
Expand Down Expand Up @@ -206,15 +178,19 @@
end

describe command('gem list') do
its(:stdout) { should include('rake (11.1.0)') }
its(:stdout) { should match(/^rake \(.*11.1.0.*\)/) }
end

describe command('gem list') do
its(:stdout) { should_not include('test-unit') }
end

describe command('ri Bundler') do
its(:stderr) { should eq("Nothing known about Bundler\n") }
describe command('gem list') do
its(:stdout) { should include('ast (2.0.0)') }
end

describe command('ri AST') do
its(:stderr) { should eq("Nothing known about AST\n") }
end

describe file('/tmp/created_by_definition') do
Expand Down
29 changes: 29 additions & 0 deletions spec/integration/docker_spec.rb
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
45 changes: 0 additions & 45 deletions spec/integration/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@
action :install
end

package 'sl' do
version '3.03-17'
end

package 'resolvconf' do
action :remove
end
Expand All @@ -65,11 +61,6 @@
version '1.2.2'
end

gem_package 'bundler' do
version '1.17.3'
options ['--no-ri', '--no-rdoc']
end

gem_package 'rake' do
version '11.1.0'
end
Expand Down Expand Up @@ -224,42 +215,6 @@
url "https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org%2Fget%3Ffrom%3Ditamae"
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

######

link "/tmp-link" do
to "/tmp"
end
Expand Down
44 changes: 44 additions & 0 deletions spec/integration/recipes/docker.rb
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
10 changes: 10 additions & 0 deletions spec/integration/recipes/local.rb
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
76 changes: 76 additions & 0 deletions tasks/integration_local_spec.rb
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

0 comments on commit efda69f

Please sign in to comment.