Module: Dockerspec::Serverspec::RSpecResources

Defined in:
lib/dockerspec/serverspec/rspec_resources.rb

Overview

Some resources included inside RSpec::Core::ExampleGroup to build and run Docker containers with Serverspec.

RSpec Settings

  • family: The OS family to use

All the RSpec settings are optional.

Examples:

RSpec Settings

RSpec.configure do |config|
  config.family = :debian
end

Instance Method Summary collapse

Instance Method Details

#docker_run(*opts) ⇒ Dockerspec::ServerspecRunner

Runs a docker image and the Serverspec tests against it.

See the Serverspec Resource Types documentation to see the available resources.

By default tries to detect the most appropriate Docker backend: native or LXC.

Examples:

A Basic Example to Test the HTTPd Service

describe docker_build('.', tag: 'myapp') do
  describe docker_run('myapp') do
    describe service('httpd') do
      it { should be_enabled }
      it { should be_running }
    end
    # [...]
  end
end

Avoid Automatic OS Detection to Speed Up the Tests

describe docker_build('.', tag: 'myapp') do
  describe docker_run('myapp', family: :debian) do
    # [...]
  end
end

Using Hash Format

describe docker_build('.', tag: 'myapp') do
  describe docker_run(tag: 'myapp', family: :debian) do
    # [...]
  end
end

Force a Specific Docker Backend

describe docker_build('.', tag: 'myapp') do
  describe docker_run('myapp', backend: :lxc) do
    # [...]
  end
end

Use a Backend Not Included by Default

# specinfra-backend-docker_nsenter gem must be installed by hand
require 'specinfra/backend/docker_nsenter'
describe docker_build('.', tag: 'myapp') do
  describe docker_run('myapp', backend: :nsenter) do
    # [...]
  end
end

Running a Container Image Tag

describe docker_run('debian:8') do
  # [...]
end

Testing FROM Dockerfile Instruction

# FROM debian:8
describe docker_run('myapp') do
  describe file('/etc/debian_version') do
    it { should be_file }
    its(:content) { should match /^8\./ }
  end
  # Another way to check it:
  describe command('lsb_release -ri') do
    its(:stdout) { should match /^Distributor ID:\s+Debian/ }
    its(:stdout) { should match /^Release:\s+8\./ }
  end
end

Testing COPY and ADD Dockerfile Instructions

# COPY docker-entrypoint.sh /entrypoint.sh
describe docker_run('myapp') do
  describe file('/entrypoint.sh') do
    it { should be_file }
    its(:content) { should match /^exec java -jar myapp\.jar/ }
  end
end

Testing RUN Dockerfile Instructions

describe docker_run('myapp') do
  # RUN apt-get install -y wget
  describe package('wget') do
    it { should be_installed }
  end
  # RUN useradd -g myapp -d /opt/myapp myapp
  describe user('myapp') do
    it { should exist }
    it { should belong_to_group 'myapp' }
    it { should have_home_directory '/opt/myapp' }
  end
end

Parameters:

  • opts (String, Hash)

    The :tag or a list of options.

Options Hash (*opts):

  • :tag (String)

    The Docker image tag name to run.

  • :id (String)

    The Docker container ID to use instead of starting a new container.

  • :rm (Boolean) — default: calculated

    Whether to remove the Docker container afterwards.

  • :path (String)

    The environment PATH value of the container.

  • :env (Hash, Array)

    Some ENV instructions to add to the container.

  • :family (Symbol) — default: calculated

    The OS family. It's automatically detected by default, but can be used to speed up the tests. Some possible values: :alpine, :arch, :coreos, :debian, :gentoo, :nixos, :plamo, :poky, :redhat, :suse.

  • :backend (Symbol) — default: calculated

    Docker backend to use: :docker, :lxc.

Returns:

  • (Dockerspec::ServerspecRunner)

    Runner object.

Raises:



160
161
162
163
# File 'lib/dockerspec/serverspec/rspec_resources.rb', line 160

def docker_run(*opts)
  runner = Dockerspec::Serverspec::Runner.new(*opts)
  runner.run
end