class Dockerspec::Runner::Compose

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.

Constants

OPTIONS_DEFAULT_KEY

@return [Symbol] The option key to set when you pass a string instead

of a hash of options.

Attributes

current_instance[RW]

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

@return [Docker::Runner::Compose::Base] The saved instance.

@api public

compose[R]

The internal {DockerCompose} object.

@return [DockerCompose] The compose object.

Public Class Methods

new(*opts) click to toggle source

Constructs a runner class to run Docker Compose.

@example From a Directory

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

@example From a YAML File

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

@example From a Directory or File Using Hash Format

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

@param opts [String, Hash] The `:file` or a list of options.

@option opts [String] :file The compose YAML file or a directory

containing the `'docker-compose.yml'` file.

@option opts [Boolean] :rm (calculated) Whether to remove the Docker @option opts [Integer] :wait Time to wait before running the tests.

@return [Dockerspec::Runner::Compose] Runner object.

@raise [Dockerspec::DockerRunArgumentError] Raises this exception when

some required options are missing.

@api public

Calls superclass method Dockerspec::Runner::Base::new
# File lib/dockerspec/runner/compose.rb, line 89
def initialize(*opts)
  Compose.current_instance = self
  @container_options = {}
  super
  setup_from_file(file)
end

Public Instance Methods

container() click to toggle source

Gets the selected container object.

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

@return [Docker::Container] The container object.

@raise [Dockerspec::RunnerError] When cannot select the container to

test.

@api public

# 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_name() click to toggle source

Gets the selected container name.

@return [String, nil] The container name.

@api private

# File lib/dockerspec/runner/compose.rb, line 160
def container_name
  return nil if @options[:container].nil?
  @options[:container].to_s
end
finalize() click to toggle source

Stops and deletes the Docker Compose containers.

Automatically called when `:rm` option is enabled.

@return void

@api public

# File lib/dockerspec/runner/compose.rb, line 219
def finalize
  return if options[:rm] == false || compose.nil?
  compose.stop
  compose.delete
end
options() click to toggle source

Returns general and container specific options merged.

@return void

@api private

# File lib/dockerspec/runner/compose.rb, line 148
def options
  container_name = @options[:container]
  @container_options[container_name] || @options
end
run() click to toggle source

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.

@return [Dockerspec::Runner::Compose] Runner object.

@raise [Dockerspec::DockerError] For underlaying docker errors.

@see select_conainer

@api public

# 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) click to toggle source

Selects the container to test and sets its configuration options.

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

@param name [Symbol, String] The container name.

@param opts [Hash] Container configuration options.

@return void

@api public

# 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_s() click to toggle source

Gets a descriptions of the object.

@example Running from a Compose File

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

@example 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\""

@return [String] The object description.

@api public

# File lib/dockerspec/runner/compose.rb, line 206
def to_s
  description('Docker Compose Run from')
end

Protected Instance Methods

assert_options!(opts) click to toggle source

Ensures that the passed options are correct.

Currently this only checks that you passed the `:file` argument.

@return void

@raise [Dockerspec::DockerRunArgumentError] Raises this exception when

the required fields are missing.

@api private

# File lib/dockerspec/runner/compose.rb, line 291
def assert_options!(opts)
  return if opts[:file].is_a?(String)
  raise DockerRunArgumentError, 'You need to pass the `:file` option '\
    'to the #docker_compose method.'
end
file() click to toggle source

Gets the full path of the Docker Compose YAML file.

It adds `'docker-compose.yml'` if you pass a directory.

@return [String] The file path.

@api private

# File lib/dockerspec/runner/compose.rb, line 236
def file
  @file ||=
    if File.directory?(options[source])
      File.join(options[source], 'docker-compose.yml')
    else
      options[source]
    end
end
rspec_options() click to toggle source

Gets the default options configured using `RSpec.configuration`.

@example

self.rspec_options #=> {:container => "webapp", :docker_wait => 30}

@return [Hash] The configuration options.

@api private

Calls superclass method Dockerspec::Runner::Base#rspec_options
# File lib/dockerspec/runner/compose.rb, line 272
def rspec_options
  config = ::RSpec.configuration
  super.tap do |opts|
    opts[:container] = config.container_name if config.container_name?
  end
end
run_container() click to toggle source

Runs Docker Compose.

@return void

@api private

# File lib/dockerspec/runner/compose.rb, line 317
def run_container
  Dir.chdir(::File.dirname(file)) { compose.start }
end
setup_from_file(file) click to toggle source

Saves the build internally.

@param file [String] The configuration file.

@return void

@api private

# File lib/dockerspec/runner/compose.rb, line 306
def setup_from_file(file)
  @compose = ::DockerCompose.load(file)
end
source() click to toggle source

Gets the source to start the container from.

Possible values: `:file`.

@example Start the Container from a YAML Configuration File

self.source #=> :file

@return [Symbol] The source.

@api private

# File lib/dockerspec/runner/compose.rb, line 257
def source
  return @source unless @source.nil?
  @source = %i(file).find { |from| options.key?(from) }
end