Skip to content

Instantly share code, notes, and snippets.

@yurinnick
Created April 19, 2016 23:23
Show Gist options
  • Save yurinnick/29c65547171751881c05145a6b1a834c to your computer and use it in GitHub Desktop.
Save yurinnick/29c65547171751881c05145a6b1a834c to your computer and use it in GitHub Desktop.
docker-container-testing-demo
require 'spec_helper'
describe package('apache2') do
it { should be_installed }
end
describe process('apache2') do
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
FROM debian:jessie
RUN apt-get update && \
apt-get install -y --no-install-recommends apache2 net-tools && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/lock/apache2
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_LOG_DIR /var/log/apache2
ENV LANG C
EXPOSE 80
ENTRYPOINT ["/usr/sbin/apache2", "-D", "FOREGROUND"]
require 'rspec'
require 'docker'
images = Dir.glob('images/*').map do |dir|
File.basename(dir) if File.directory?(dir)
end.compact
namespace :build do
desc 'Build all docker images'
task :all => images
images.each do |image|
desc "Build #{image} docker image"
task image.to_sym do
Docker::Image.build_from_dir(File.join('images', image), t: image) do |v|
if (log = JSON.parse(v)) && log.has_key?('stream')
$stdout.puts log['stream']
end
end
end
end
end
namespace :test do
targets = Dir.glob('spec/*').map do |dir|
File.basename(dir) if File.directory?(dir)
end.compact
desc 'Run serverspec for all containers'
task :all => targets
targets.each do |target|
desc "Run serverspec for #{target} containers"
task target do
container = Docker::Container.create(Image: target)
container.start
ENV['CONTAINER_ID'] = container.id
specs = Dir.glob("spec/#{target}/*_spec.rb")
RSpec::Core::Runner.run(specs)
container.delete(force: true)
end
end
end
require 'serverspec'
require 'docker'
set :backend, :docker
set :docker_url, ENV['DOCKER_HOST']
set :docker_container, ENV['CONTAINER_ID']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment