Class: Dockerspec::Runner::Base

Inherits:
Object
  • Object
show all
Includes:
ConfigHelpers
Defined in:
lib/dockerspec/runner/base.rb

Overview

A basic class with the minimal skeleton to create a Runner: Classes to start docker containers.

Direct Known Subclasses

Compose, Docker

Constant Summary collapse

OPTIONS_DEFAULT_KEY =

The option key to set when you pass a string instead of a hash of options.

:ignored

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigHelpers

#parse_log, #stderr, #stdout

Constructor Details

#initialize(*opts) ⇒ Dockerspec::Runner::Base

Constructs a runner class to run Docker images.

Parameters:

  • opts (String, Hash)

    The id/name/file or a list of options.

Options Hash (*opts):

  • :rm (Boolean) — default: calculated

    Whether to remove the Docker container afterwards.

  • :wait (Integer)

    Time to wait before running the tests.

Raises:



65
66
67
68
69
# File 'lib/dockerspec/runner/base.rb', line 65

def initialize(*opts)
  @options = parse_options(opts)
  @engines = EngineList.new(self)
  ObjectSpace.define_finalizer(self, proc { finalize })
end

Instance Attribute Details

#optionsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets the configuration options.

Returns:

  • (Hash)

    The options.



47
48
49
# File 'lib/dockerspec/runner/base.rb', line 47

def options
  @options
end

Instance Method Details

#containerDocker::Container

Gets the internal Docker::Container object.

Returns:

  • (Docker::Container)

    The container.

Raises:



126
127
128
# File 'lib/dockerspec/runner/base.rb', line 126

def container
  raise RunnerError, "#{self.class}#container method must be implemented"
end

#container_nameString

Gets the container name.

Returns:

  • (String)

    Container name.

Raises:

  • (Dockerspec::RunnerError)

    When the #container method is no implemented in the subclass or cannot select the container to test.



140
141
142
# File 'lib/dockerspec/runner/base.rb', line 140

def container_name
  container.json['Name']
end

#finalizeObject

Stops and deletes the Docker Container.

Automatically called when :rm option is enabled.

Returns:

  • void



203
204
205
206
207
# File 'lib/dockerspec/runner/base.rb', line 203

def finalize
  return if options[:rm] == false || container.nil?
  container.stop
  container.delete
end

#idString

Gets the Docker container ID.

Examples:

builder = Dockerspec::Builder.new('.').build
runner = Dockerspec::Runner::Base.new(builder).run
runner.id #=> "b8ba0befc716[...]"

Returns:

  • (String)

    Container ID.

Raises:

  • (Dockerspec::RunnerError)

    When the #container method is no implemented in the subclass or cannot select the container to test.



159
160
161
162
# File 'lib/dockerspec/runner/base.rb', line 159

def id
  return nil unless container.respond_to?(:id)
  container.id
end

#image_idString

Gets the Docker image ID.

Returns:

  • (String)

    Image ID.

Raises:

  • (Dockerspec::RunnerError)

    When the #container method is no implemented in the subclass or cannot select the container to test.



174
175
176
# File 'lib/dockerspec/runner/base.rb', line 174

def image_id
  container.json['Image']
end

#ipaddressString

Gets the Docker Container IP address.

This is used by Engine::Infrataster.

Returns:

  • (String)

    IP address.

Raises:

  • (Dockerspec::RunnerError)

    When the #container method is no implemented in the subclass or cannot select the container to test.



190
191
192
# File 'lib/dockerspec/runner/base.rb', line 190

def ipaddress
  container.json['NetworkSettings']['IPAddress']
end

#restore_rspec_contextObject

Restores the Specinfra backend instance to point to this object's container.

This is used to avoid Serverspec running against the last started container if you are testing multiple containers at the same time.

Returns:

  • void



112
113
114
# File 'lib/dockerspec/runner/base.rb', line 112

def restore_rspec_context
  @engines.restore
end

#runDockerspec::Runner::Base

Runs the Docker Container.

  1. Sets up the test context.
  2. Runs the container (or Compose).
  3. Saves the created underlaying test context.
  4. Sets the container as ready.
  5. Waits the required (configured) time after container has been started.

Examples:

builder = Dockerspec::Builder.new('.')
builder.build
runner = Dockerspec::Runner::Base.new(builder)
runner.run #=> #<Dockerspec::Runner::Base:0x0123>

Returns:

Raises:



93
94
95
96
97
98
99
100
101
# File 'lib/dockerspec/runner/base.rb', line 93

def run
  before_running
  start_time = Time.new.utc
  run_container
  when_running
  when_container_ready
  do_wait((Time.new.utc - start_time).to_i)
  self
end