Class: Dockerspec::Serverspec::Runner

Inherits:
Runner
  • Object
show all
Defined in:
lib/dockerspec/serverspec/runner.rb

Overview

Runs a Docker container using Serverspec.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Runner

#id, #image_id

Methods included from Helper::MultipleSourcesDescription

#description

Constructor Details

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

Constructs a Docker Serverspec runner class to run Docker images.

Examples:

From a Docker Container Image Tag

Dockerspec::Serverspec::Runner.new('myapp')
  #=> #<Dockerspec::Serverspec::Runner:0x0124>

From a Docker Container Image Tag Using Hash Format

Dockerspec::Serverspec::Runner.new(tag: 'myapp')
  #=> #<Dockerspec::Serverspec::Runner:0x0124>

From a Running Docker Container ID

Dockerspec::Serverspec::Runner.new(id: 'c51f86c28340')
  #=> #<Dockerspec::Serverspec::Runner:0x0125>

Parameters:

  • opts (String, Hash)

    The :tag or a list of options.

Options Hash (*opts):

  • :tag (String)

    The Docker image tag name to run.

  • :id (String)

    The Docker container ID to use instead of starting a new container.

  • :rm (Boolean) — default: calculated

    Whether to remove the Docker container afterwards.

  • :path (String)

    The environment PATH value of the container.

  • :env (Hash, Array)

    Some ENV instructions to add to the container.

  • :family (Symbol) — default: calculated

    The OS family. It's automatically detected by default, but can be used to speed up the tests. Some possible values: :alpine, :arch, :coreos, :debian, :gentoo, :nixos, :plamo, :poky, :redhat, :suse.

  • :backend (Symbol) — default: calculated

    Docker backend to use: :docker, :lxc.



80
81
82
83
84
# File 'lib/dockerspec/serverspec/runner.rb', line 80

def initialize(*opts)
  super
  @specinfra_backend = nil
  @backend = calculate_docker_backend_name
end

Class Method Details

.restore(metadata) ⇒ Object

Restores the Docker running container instance in the Specinfra internal reference.

Gets the correct Dockerspec::Serverspec::Runner reference from the RSpec metadata.

Examples:

Restore Specinfra Backend

RSpec.configure do |c|
  c.before(:each) do
     = RSpec.current_example.
    Dockerspec::Serverspec::Runner.restore()
  end
end

Parameters:

  • metadata (Hash)

    RSpec metadata.

Returns:

  • void

See Also:



143
144
145
146
147
# File 'lib/dockerspec/serverspec/runner.rb', line 143

def self.restore()
  runner = Helper::RSpecExampleHelpers.search_object(, self)
  return if runner.nil?
  runner.restore
end

Instance Method Details

#finalizeObject

Stops and deletes the Docker Container.

Actually does nothing. Do no delete anything, let Specinfra do that.

Returns:

  • void



117
118
119
# File 'lib/dockerspec/serverspec/runner.rb', line 117

def finalize
  # Do not stop the container
end

#restoreObject

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



158
159
160
# File 'lib/dockerspec/serverspec/runner.rb', line 158

def restore
  @specinfra_backend.restore
end

#runDockerspec::Serverspec::Runner

Runs the Docker Container and sets the Specinfra configuration.

Examples:

builder = Dockerspec::Builder.new('.', tag: 'myapp')
builder.build
runner = Dockerspec::Serverspec::Runner.new('myapp')
runner.run #=> #<Dockerspec::Serverspec::Runner:0x0123>

Returns:



99
100
101
102
103
104
105
106
# File 'lib/dockerspec/serverspec/runner.rb', line 99

def run
  specinfra_setup
  run_container
  specinfra_save
  self
rescue ::Docker::Error::DockerError => e
  DockerExceptionParser.new(e)
end

#to_sString

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.

Generates a description of the object.

Examples:

Running Against a Container Image Tag

self.description #=> "Serverspec on tag: \"debian\""

Running Against a Running Container ID

self.description #=> "Serverspec on id: \"92cc98ab560a\""

Returns:

  • (String)

    The object description.



175
176
177
# File 'lib/dockerspec/serverspec/runner.rb', line 175

def to_s
  description('Serverspec on')
end