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
Saves the latest created {Dockerspec::Runner::Compose} object.
@return [Docker::Runner::Compose::Base] The saved instance.
@api public
The internal {DockerCompose} object.
@return [DockerCompose] The compose object.
Public Class Methods
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
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
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
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
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
Does not call ready because container is still not ready.
Runs the Docker
Container.
-
Sets up the test context.
-
Runs the container (or
Compose
). -
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
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
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
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
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
Gets the default options configured using `RSpec.configuration`.
@example
self.rspec_options #=> {:container => "webapp", :docker_wait => 30}
@return [Hash] The configuration options.
@api private
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
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
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