Class: Dockerspec::Engine::Specinfra::Backend

Inherits:
Object
  • Object
show all
Defined in:
lib/dockerspec/engine/specinfra/backend.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ Backend

The Specinfra backend constructor.

Parameters:

  • 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.



45
46
47
# File 'lib/dockerspec/engine/specinfra/backend.rb', line 45

def initialize(backend)
  @backend = backend
end

Instance Method Details

#backend_instance_attribute(name) ⇒ Mixed

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.

Returns:

  • (Mixed)

    The value of the attribute to read.



117
118
119
# File 'lib/dockerspec/engine/specinfra/backend.rb', line 117

def backend_instance_attribute(name)
  backend_instance.instance_variable_get("@#{name}".to_sym)
end

#resetObject

Resets the Specinfra backend.

Returns:

  • void



103
104
105
# File 'lib/dockerspec/engine/specinfra/backend.rb', line 103

def reset
  backend_class.instance_set(nil)
end

#restoreObject

Restores the Specinfra backend instance.

Returns:

  • void



68
69
70
71
72
73
74
# 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) ⇒ Object

Restores the testing context for a container.

Used with Docker Compose to choose the container to test.

Parameters:

  • container_name (String, Symbol)

    The name of the container.

Returns:

  • void



87
88
89
90
91
92
93
94
# 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

#saveObject

Saves the Specinfra backend instance reference internally.

Returns:

  • void



56
57
58
59
# File 'lib/dockerspec/engine/specinfra/backend.rb', line 56

def save
  @saved_backend_name = ::Specinfra.configuration.backend
  @saved_backend_instance = backend_instance
end