Module: Dockerspec::RSpecResources

Defined in:
lib/dockerspec/rspec_resources.rb

Overview

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

RSpec Settings

  • dockerfile_path: The dockerfile path.
  • rm_build: Whether to remove the build after the run.
  • log_level: Log level to use by default.

All the RSpec settings are optional.

Examples:

RSpec Settings

RSpec.configure do |config|
  config.log_level = :silent
end

Instance Method Summary collapse

Instance Method Details

#docker_build(*opts) ⇒ Dockerspec::Builder

Builds a Docker image.

The image can be build from a path or from a string.

See the Builder::ConfigHelpers documentation for more information about the available RSpec resource helpers.

Examples:

A Simple Example

describe 'My Dockerfile' do
  describe docker_build('.') do
    it { should have_maintainer /John Doe/ }
    it { should have_cmd ['/bin/dash'] }
    it { should have_expose '80' }
  end
end

A Complete Example

describe docker_build(path: '.') do
  it { should have_maintainer 'John Doe "[email protected]"' }
  it { should have_maintainer(/John Doe/) }
  it { should have_cmd %w(2 2000) }
  it { should have_label 'description' }
  it { should have_label 'description' => 'My Container' }
  it { should have_expose '80' }
  it { should have_expose(/80$/) }
  it { should have_env 'container' }
  it { should have_env 'container' => 'docker' }
  it { should have_env 'CRACKER' => 'RANDOM;PATH=/tmp/bin:/sbin:/bin' }
  it { should have_entrypoint ['sleep'] }
  it { should have_volume '/volume1' }
  it { should have_volume %r{/vol.*2} }
  it { should have_user 'nobody' }
  it { should have_workdir '/opt' }
  it { should have_workdir %r{^/op} }
  it { should have_onbuild 'RUN echo onbuild' }
  it { should have_stopsignal 'SIGTERM' }
end

Checking the Attribute Values Using the its Method

describe docker_build(path: '.') do
  its(:maintainer) { should eq 'John Doe "[email protected]"' }
  its(:cmd) { should eq %w(2 2000) }
  its(:labels) { should include 'description' }
  its(:labels) { should include 'description' => 'My Container' }
  its(:exposes) { should include '80' }
  its(:env) { should include 'container' }
  its(:env) { should include 'container' => 'docker' }
  its(:entrypoint) { should eq ['sleep'] }
  its(:volumes) { should include '/volume1' }
  its(:user) { should eq 'nobody' }
  its(:workdir) { should eq '/opt' }
  its(:onbuilds) { should include 'RUN echo onbuild' }
  its(:stopsignal) { should eq 'SIGTERM' }
end

Checking Its Size and OS

describe docker_build(path: '.') do
  its(:size) { should be < 20 * 2**20 } # 20M
  its(:arch) { should eq 'amd64' }
  its(:os) { should eq 'linux' }
end

Building from a File

describe docker_build(path: '../dockerfiles/Dockerfile-nginx') do
  # [...]
end

Building from a Template

describe docker_build(template: 'Dockerfile1.erb') do
  # [...]
end

Building from a Template with a Context

describe docker_build(
  template: 'Dockerfile1.erb', context: {version: '8'}
) do
  it { should have_maintainer(/John Doe/) }
  it { should have_cmd %w(/bin/sh) }
  # [...]
end

Building from a String

describe docker_build(string: "FROM nginx:1.9\n [...]") do
  # [...]
end

Building from a Docker Image ID

describe docker_build(id: '07d362aea98d') do
  # [...]
end

Building from a Docker Image Name

describe docker_build(id: 'nginx:1.9') do
  # [...]
end

Parameters:

  • opts (String, Hash)

    The :path or a list of options.

Options Hash (*opts):

  • :path (String) — default: '.'

    The directory or file that contains the Dockerfile. By default tries to read it from the DOCKERFILE_PATH environment variable and uses '.' if it is not set.

  • :string (String)

    Use this string as Dockerfile instead of :path. Not set by default.

  • :template (String)

    Use this Erubis template file as Dockerfile.

  • :id (String)

    Use this Docker image ID instead of a Dockerfile.

  • :rm (Boolean)

    Whether to remove the generated docker images after running the tests. By default only removes them if it is running on a CI machine.

  • :context (Hash, Erubis::Context) — default: {}

    Template context used when the :template source is used.

  • :tag (String)

    Repository tag to be applied to the resulting image.

  • :log_level (Fixnum, Symbol)

    Sets the docker library verbosity level. Possible values: :silent or 0 (no output), :ci or 1 (enables some outputs recommended for CI environments), :info or 2 (gives information about main build steps), :debug or 3 (outputs all the provided information in its raw original form).

Returns:

See Also:



174
175
176
177
# File 'lib/dockerspec/rspec_resources.rb', line 174

def docker_build(*opts)
  builder = Dockerspec::Builder.new(*opts)
  builder.build
end

#docker_run(*opts) ⇒ Object

Runs a docker image.

Parameters:

  • opts (Hash)

    List of options.

See Also:



186
187
188
# File 'lib/dockerspec/rspec_resources.rb', line 186

def docker_run(*opts)
  RSpecAssertions.assert_docker_run!(opts)
end