Class: Dockerspec::Runner::Compose

Inherits:
Base
  • Object
show all
Includes:
Helper::MultipleSourcesDescription
Defined in:
lib/dockerspec/runner/compose.rb

Overview

This class runs Docker Compose (without using Serverspec for that).

This class is used mainly when you are not using Serverspec to run the tests.

Direct Known Subclasses

Serverspec::Compose

Constant Summary collapse

OPTIONS_DEFAULT_KEY =

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

Returns:

  • (Symbol)

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

:file

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::MultipleSourcesDescription

#description

Methods inherited from Base

#id, #image_id, #ipaddress, #restore_rspec_context

Methods included from ConfigHelpers

#parse_log, #stderr, #stdout

Constructor Details

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

Constructs a runner class to run Docker Compose.

Examples:

From a Directory

Dockerspec::Runner::Compose.new('directory1')
  #=> #<Dockerspec::Runner::Compose:0x0124>

From a YAML File

Dockerspec::Runner::Compose.new('data/docker-compose.yml')
  #=> #<Dockerspec::Runner::Compose:0x0124>

From a Directory or File Using Hash Format

Dockerspec::Runner::Compose.new(file: 'file.yml')
  #=> #<Dockerspec::Runner::Compose:0x0124>

Parameters:

  • opts (String, Hash)

    The :file or a list of options.

Options Hash (*opts):

  • :file (String)

    The compose YAML file or a directory containing the 'docker-compose.yml' file.

  • :rm (Boolean) — default: calculated

    Whether to remove the Docker

  • :wait (Integer)

    Time to wait before running the tests.

Raises:



89
90
91
92
93
94
# File 'lib/dockerspec/runner/compose.rb', line 89

def initialize(*opts)
  Compose.current_instance = self
  @container_options = {}
  super
  setup_from_file(file)
end

Class Attribute Details

.current_instanceDocker::Runner::Compose::Base

Saves the latest created Dockerspec::Runner::Compose object.

Returns:

  • (Docker::Runner::Compose::Base)

    The saved instance.



42
43
44
# File 'lib/dockerspec/runner/compose.rb', line 42

def current_instance
  @current_instance
end

Instance Attribute Details

#composeDockerCompose (readonly)

The internal DockerCompose object.

Returns:

  • (DockerCompose)

    The compose object.



58
59
60
# File 'lib/dockerspec/runner/compose.rb', line 58

def compose
  @compose
end

Instance Method Details

#containerDocker::Container

Gets the selected container object.

This method is used in Base to get information from the container: ID, image ID, …

Returns:

  • (Docker::Container)

    The container object.

Raises:



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/dockerspec/runner/compose.rb', line 178

def container
  if container_name.nil?
    raise RunnerError,
          'Use `its_container` to select a container to test.'
  end
  compose_container = compose.containers[container_name]
  if compose_container.nil?
    raise RunnerError, "Container not found: #{compose_container.inspect}"
  end
  compose_container.container
end

#container_nameString?

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 selected container name.

Returns:

  • (String, nil)

    The container name.



160
161
162
163
# File 'lib/dockerspec/runner/compose.rb', line 160

def container_name
  return nil if @options[:container].nil?
  @options[:container].to_s
end

#finalizeObject

Stops and deletes the Docker Compose containers.

Automatically called when :rm option is enabled.

Returns:

  • void



219
220
221
222
223
# File 'lib/dockerspec/runner/compose.rb', line 219

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

#optionsObject

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.

Returns general and container specific options merged.

Returns:

  • void



148
149
150
151
# File 'lib/dockerspec/runner/compose.rb', line 148

def options
  container_name = @options[:container]
  @container_options[container_name] || @options
end

#runDockerspec::Runner::Compose

Does not call ready because container is still not ready.

Runs the Docker Container.

  1. Sets up the test context.
  2. Runs the container (or Compose).
  3. Saves the created underlaying test context.

Returns:

Raises:

See Also:

  • #select_conainer


112
113
114
115
116
117
118
119
# File 'lib/dockerspec/runner/compose.rb', line 112

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

#select_container(name, opts = nil) ⇒ Object

Selects the container to test and sets its configuration options.

Also sets the selected container as ready in the underlaying test engines.

Parameters:

  • name (Symbol, String)

    The container name.

  • opts (Hash) (defaults to: nil)

    Container configuration options.

Returns:

  • void



135
136
137
138
139
# File 'lib/dockerspec/runner/compose.rb', line 135

def select_container(name, opts = nil)
  @options[:container] = name
  @container_options[name] = @options.merge(opts) if opts.is_a?(Hash)
  when_container_ready
end

#to_sString

Gets a descriptions of the object.

Examples:

Running from a Compose File

r = Dockerspec::Runner::Compose.new('docker-compose.yml')
r.to_s #=> "Docker Compose Run from file: \"docker-compose.yml\""

Running from a Compose Directory

r = Dockerspec::Runner::Compose.new('docker_images')
r.to_s #=> "Docker Compose Run from file: "\
       #   "\"docker_images/docker-compose.yml\""

Returns:

  • (String)

    The object description.



206
207
208
# File 'lib/dockerspec/runner/compose.rb', line 206

def to_s
  description('Docker Compose Run from')
end