class Dockerspec::Engine::Specinfra::Backend

A class to handle the underlying Specinfra backend.

This class saves Specinfra instance in internally and then it is able to recover it from there and setup the running environment accordingly.

This class uses a small hack in the Specinfra class to reset its internal singleton instance.

Public Class Methods

new(backend) click to toggle source

The Specinfra backend constructor.

@param backend [Symbol, Specinfra::Backend::Base, Class] The backend

can be the backend name as a symbol, a Specinfra backend object or
a Specinfra backend class.

@api public

# File lib/dockerspec/engine/specinfra/backend.rb, line 45
def initialize(backend)
  @backend = backend
end

Public Instance Methods

backend_instance_attribute(name) click to toggle source

Gets the internal attribute value from the Specinfra backend object.

Used mainly to get information from the running containers like their name or their IP address.

@return [Mixed] The value of the attribute to read.

@api public

# File lib/dockerspec/engine/specinfra/backend.rb, line 117
def backend_instance_attribute(name)
  backend_instance.instance_variable_get("@#{name}".to_sym)
end
reset() click to toggle source

Resets the Specinfra backend.

@return void

@api public

# File lib/dockerspec/engine/specinfra/backend.rb, line 103
def reset
  backend_class.instance_set(nil)
end
restore() click to toggle source

Restores the Specinfra backend instance.

@return void

@api public

# File lib/dockerspec/engine/specinfra/backend.rb, line 68
def restore
  backend_class.instance_set(@saved_backend_instance)
  if ::Specinfra.configuration.backend != @saved_backend_name
    backend_class.host_reset
    ::Specinfra.configuration.backend = @saved_backend_name
  end
end
restore_container(container_name) click to toggle source

Restores the testing context for a container.

Used with Docker Compose to choose the container to test.

@param container_name [String, Symbol] The name of the container.

@return void

@api public

# File lib/dockerspec/engine/specinfra/backend.rb, line 87
def restore_container(container_name)
  current_container_name =
    ::Specinfra.configuration.docker_compose_container
  return if current_container_name == container_name
  ::Specinfra.configuration.docker_compose_container(container_name)
  # TODO: Save the host family instead of always reseting it:
  backend_class.host_reset
end
save() click to toggle source

Saves the Specinfra backend instance reference internally.

@return void

@api public

# File lib/dockerspec/engine/specinfra/backend.rb, line 56
def save
  @saved_backend_name = ::Specinfra.configuration.backend
  @saved_backend_instance = backend_instance
end

Protected Instance Methods

backend_class() click to toggle source

Returns the current Specinfra backend class.

@return [Class] The Specinfra backend class.

@api private

# File lib/dockerspec/engine/specinfra/backend.rb, line 141
def backend_class
  @backend_class ||= begin
    return @backend.class if @backend.is_a?(::Specinfra::Backend::Base)
    ::Specinfra::Backend.const_get(@backend.to_s.to_camel_case)
  end
end
backend_instance() click to toggle source

Returns the current Specinfra backend object.

@return [Specinfra::Backend::Base] The Specinfra backend object.

@api private

# File lib/dockerspec/engine/specinfra/backend.rb, line 130
def backend_instance
  backend_class.instance
end