Skip to content

Commit

Permalink
Use Docker for isolated local testing
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Apr 8, 2016
1 parent d322ed6 commit 73046cc
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 61 deletions.
15 changes: 2 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,7 @@ We appreciate that. But before you do, please learn our basic rules:

## Bootstrap environment

To get started with `aruba`, you just need to bootstrap the environment by
running the following command.

# Bootstrap environment
script/bootstrap

## Running tests

Make sure you bootstrap the environment first.

# Run the test suite
script/test
See [TESTING.md](TESTING.md) for details on setting up and running Aruba tests.

## Installing your own gems used for development

Expand Down Expand Up @@ -108,7 +97,7 @@ Now release it
git commit -m "Release X.Y.Z"
rake release

Now send a PR to https://github.com/cucumber/website adding an article about the with details of the new release and merge it - an aruba maintainer should normally allowed to merge PRs on `cucumber/website`. A copy of an old announcement can be used as basis for the new article. After this send an email with the link to the article to [email protected].
Now send a PR to https://github.com/cucumber/website adding an article about the with details of the new release and merge it - an aruba maintainer should normally allowed to merge PRs on `cucumber/website`. A copy of an old announcement can be used as basis for the new article. After this send an email with the link to the article to [email protected].

## Gaining Release Karma

Expand Down
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM cucumber/aruba:latest

USER root

# Zsh (just for the sake of a handful of Cucumber scenarios)
RUN apt-get update -qq && apt-get -y install zsh --no-install-recommends && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin

# Python (just for the sake of a handful of Cucumber scenarios)
RUN apt-get update -qq && apt-get -y install python --no-install-recommends && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin

# Java (for javac - also for just a few Cucumber scenarios)
RUN apt-get update -qq && apt-get -y install openjdk-7-jdk --no-install-recommends && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin

# Cache needed gems - for faster test reruns
ADD Gemfile Gemfile.lock aruba.gemspec /home/guest/cache/aruba/
ADD lib/aruba/version.rb /home/guest/cache/aruba/lib/aruba/version.rb
RUN chown -R guest:guest /home/guest/cache
USER guest

# NOTE: It's best to setup your own gem mirror/server, because:
#
# 1. Faster gems installing (especially useful when rebuilding the Docker image)
# 2. Less internet bandwidth used
# 3. Lesser load on rubygems.org servers
#
# Info: http://guides.rubygems.org/run-your-own-gem-server/#

RUN echo '---\n\
:sources:\n\
- http://172.17.0.1:8808\n\
gem: "--no-ri --no-rdoc --source http://172.17.0.1:8808"\n'\
>> /home/guest/.gemrc

# Actually download
RUN bash -l -c "cd cache/aruba && bundle install"


WORKDIR /home/guest/aruba
37 changes: 37 additions & 0 deletions Dockerfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM ubuntu:14.04
MAINTAINER Aruba Maintainers <[email protected]>

# Packages needed to install RVM and run Bundler gem commands
RUN apt-get update -qq && apt-get -y install ca-certificates curl git-core --no-install-recommends && rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin

# Create guest user early (before rvm) so uid:gid are 1000:000
RUN useradd -m -s /bin/bash guest

# Temporarily install RVM as root - just for requirements
RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN curl -L get.rvm.io | bash -s stable
RUN bash -l -c 'rvm requirements 2.2.1'
RUN bash -l -c 'echo yes | rvm implode'

# Fix locale
ENV DEBIAN_FRONTEND noninteractive
RUN dpkg-reconfigure locales && locale-gen en_US.UTF-8 && /usr/sbin/update-locale LANG=en_US.UTF-8
RUN echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen && locale-gen
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
ENV LANGUAGE C.UTF-8

USER guest
ENV HOME /home/guest
WORKDIR /home/guest

# Install RVM as guest
RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
RUN /bin/bash -l -c "echo 'gem: --no-ri --no-rdoc' > ~/.gemrc"
RUN curl -L get.rvm.io | bash -s stable
RUN /bin/bash -l -c "rvm install 2.3.0 && rvm cleanup all"
RUN /bin/bash -l -c "gem install bundler --no-ri --no-rdoc"

ONBUILD ENV USER guest
ONBUILD WORKDIR /home/guest
ONBUILD RUN /bin/bash -l -c "source /home/$USER/.rvm/scripts/rvm"
74 changes: 39 additions & 35 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,56 +48,60 @@ task :test => [ 'travis:lint', :rubocop, :spec, :cucumber, :cucumber_wip]

task :default => :test

require 'uri'
namespace :docker do
image_name = 'cucumber/aruba'
container_name = 'cucumber-aruba-1'
config = YAML.load(IO.read('docker-compose.yml'))['services']

desc 'Build docker image'
task :build, :nocache, :version do |_, args|
args.with_defaults(:version => 'latest')
def build_image(config, application_version, nocache)
build_options = config['build']

nocache = args[:nocache]
application_version = args[:version]
docker_file = 'Dockerfile'
cmdline = %W(docker build --no-cache=#{nocache})

cmdline = []
cmdline << 'docker'
cmdline << 'build'
cmdline << '--no-cache=true' if nocache == 'true'
build_options['args'].each do |key, value|
cmdline << "--build-arg #{key}=#{value}"
end

%w(http_proxy https_proxy HTTP_PROXY HTTPS_PROXY).each do |var|
next unless ENV.key? var
cmdline << "-t #{config['image']}:#{application_version}"
cmdline << "-f #{build_options['dockerfile']}"
cmdline << build_options['context']
sh cmdline.join(' ')
end

proxy_uri = URI(ENV[var])
proxy_uri.host = '172.17.0.1'
cmdline << "--build-arg #{var}=#{proxy_uri}"
end
def expand_volume_paths(volume_paths)
volume_paths.split(':').map { |path| File.expand_path(path) }.join(':')
end

def run_container(config, command)
cmdline = %W(docker run -it --rm --name #{config['container_name']} -w #{config['working_dir']})

volumes = config['volumes'].map { |volume| expand_volume_paths(volume) }
volumes.each { |volume| cmdline << "-v #{volume}" }

cmdline << "-t #{image_name}:#{application_version}"
cmdline << "-f #{docker_file}"
cmdline << File.dirname(docker_file)
cmdline << config['image']
cmdline << (command ? command : config['command'])

puts "Running Docker with arguments:"
puts cmdline.inspect

sh cmdline.join(' ')
end

desc 'Build docker base image'
task :build, :nocache, :version do |_, args|
args.with_defaults(:version => 'latest')
args.with_defaults(:nocache => 'false')
build_image(config['base'], { version: args[:version], nocache: args[:nocache]})
end

desc 'Run docker container'
task :run, :command do |_, task_args|
command = task_args[:command]
tests = config['tests']

args =[]
args << '-it'
args << '--rm'
args << "--name #{container_name}"
args << "-v #{File.expand_path('.')}:/srv/app"
puts "Building/updating test image"
build_image(tests, { version: 'test', nocache: 'false'})

cmdline = []
cmdline << 'docker'
cmdline << 'run'
cmdline.concat args
cmdline << image_name
cmdline << command if command
command = task_args[:command]
command = "bash -l -c #{Shellwords.escape(command)}" if command

sh cmdline.join(' ')
run_container(tests, command)
end
end
34 changes: 34 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# To build the base image (official Aruba base for this), run:
#
# $ docker-compose build base
#
# To run the tests, run:
#
# $ docker-compose run tests
#

version: '2'
services:
base:
image: 'cucumber/aruba'
build:
context: .
dockerfile: Dockerfile.base
args: &proxies
http_proxy: "${http_proxy}"
https_proxy: "${https_proxy}"
HTTP_PROXY: "${HTTP_PROXY}"
HTTPS_PROXY: "${HTTPS_PROXY}"

tests:
image: 'cucumber/aruba_cache'
container_name: 'cucumber-aruba-1'
build:
context: .
dockerfile: Dockerfile
args:
<<: *proxies
working_dir: /home/guest/aruba
command: bash -l -c "./script/bootstrap && ./script/test"
volumes:
- .:/home/guest/aruba
28 changes: 17 additions & 11 deletions script/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ set -e
info_msg="\e[0;32m[INFO]\e[39;49m"
error_msg="\e[0;31mFAILED\e[39;49m"

function output_error_log {
[[ -f error.log ]] && ( cat error.log >&2; rm error.log)
# TODO: these are Unix specific
TMPDIR="${TMPDIR:=/tmp}"
logfile=`mktemp -t aruba_tests.XXXXXXXXXX`

output_error_log() {
[[ -f "$logfile" ]] && ( cat "logfile" >&2; rm -f "$logfile")
}

echo -ne "$info_msg Checking if ruby installed? "
which 'ruby' >/dev/null 2>error.log || ( echo -e "$error_msg\n\nCould not find \`ruby\`. Please install ruby or add it to PATH"; output_error_log; exit 1 )
echo OK
check_for_binary() {
name="$1"

echo -ne "$info_msg Checking if bash installed? "
which 'bash' >/dev/null 2>error.log || ( echo -e "$error_msg\n\nCould not find \`bash\`. Please install bash or add it to PATH"; output_error_log; exit 1 )
echo OK
echo -ne "$info_msg Checking if $name installed? "
which "$name" >/dev/null 2>"$logfile" || ( echo -e "$error_msg\n\nCould not find \`$name\`. Please install $name or add it to PATH"; output_error_log; exit 1 )
echo OK
}

echo -ne "$info_msg Checking if zsh installed? "
which 'zsh' >/dev/null 2>error.log || ( echo -e "$error_msg\n\nCould not find \`zsh\`. Please install zsh or add it to PATH"; output_error_log; exit 1 )
echo OK
check_for_binary "ruby"
check_for_binary "bash"
check_for_binary "zsh"
check_for_binary "python"
check_for_binary "javac"

echo -e "$info_msg rubygem \"bundler\" "
gem install bundler
Expand Down
9 changes: 7 additions & 2 deletions script/test
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#!/bin/sh
#!/usr/bin/env bash

bundle exec rake test $*
if [ "$RUN_IN_DOCKER" == '1' ]; then
bundle exec rake docker:run[$*]
else
bundle exec rake test $*
echo bundle exec rake test $*
fi

0 comments on commit 73046cc

Please sign in to comment.