Class: Kitchen::Docker::Container

Inherits:
Object
  • Object
show all
Includes:
Helpers::CliHelper, Helpers::ContainerHelper, Helpers::FileHelper, Helpers::ImageHelper
Defined in:
lib/kitchen/docker/container.rb,
lib/kitchen/docker/container/linux.rb,
lib/kitchen/docker/container/windows.rb

Overview

Base class for the container the instance under test runs in.

Holds the behaviour that is the same on every platform -- checking whether the container exists, removing it, working out its address, and copying files in. Linux and Windows add how the image is built and how commands are run, which share almost nothing.

Direct Known Subclasses

Linux, Windows

Defined Under Namespace

Classes: Linux, Windows

Constant Summary

Constants included from Helpers::ContainerHelper

Helpers::ContainerHelper::COPIED_MARKER

Instance Method Summary collapse

Methods included from Helpers::ImageHelper

#build_image, #image_exists?, #image_in_use?, #parse_image_id, #remove_image

Methods included from Helpers::ContainerHelper

#container_env_variables, #container_exec, #container_exists?, #container_ip_address, #container_running?, #copy_file_to_container, #create_dir_on_container, #dockerfile_path, #dockerfile_proxy_config, #dockerfile_template, #file_on_container?, #ip_address?, #parse_container_id, #proxy_env_vars, #remote_socket?, #remove_container, #replace_env_variables, #run_container, #socket_uri, #verify_file_copied

Methods included from Helpers::CliHelper

#build_copy_command, #build_env_variable_args, #build_exec_command, #build_powershell_command, #build_run_command, #config_to_options, #dev_null, #docker_command, #docker_shell_opts, #docker_sudo_opts, #run_command, #shell_escape

Methods included from Helpers::FileHelper

#create_temp_file

Constructor Details

#initialize(config) ⇒ Container

Returns a new instance of Container.

Parameters:

  • config (Hash)

    the driver or transport configuration



34
35
36
# File 'lib/kitchen/docker/container.rb', line 34

def initialize(config)
  @config = config
end

Instance Method Details

#create(state) ⇒ void

This method returns an undefined value.

Checks the container named in state and records the login user.

A state file naming a container that no longer exists is an error rather than something to build over, because the stale id usually means the container was removed behind Test Kitchen's back and silently creating a new one would hide that.

A container that exists but has stopped is also an error, and a separate one: it is still there to be cleaned up, so the message points at kitchen destroy rather than claiming the container is gone.

Parameters:

  • state (Hash)

    mutable instance state; gains username

Raises:

  • (Kitchen::ActionFailed)

    if state names a container that is gone, or one that exists but is not running



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kitchen/docker/container.rb', line 53

def create(state)
  if container_exists?(state)
    unless container_running?(state)
      raise ActionFailed, "Container ID #{state[:container_id]} was found in the kitchen state data, " \
                          "but the container is not running. Run `kitchen destroy` to remove it."
    end

    info("Container ID #{state[:container_id]} already exists.")
  elsif state[:container_id]
    raise ActionFailed, "Container ID #{state[:container_id]} was found in the kitchen state data, " \
                        "but the container does not exist."
  end

  state[:username] = @config[:username]
end

#destroy(state) ⇒ void

This method returns an undefined value.

Removes the container, and its image when remove_images is set.

Parameters:

  • state (Hash)

    instance state naming the container



73
74
75
76
77
78
79
80
# File 'lib/kitchen/docker/container.rb', line 73

def destroy(state)
  info("[Docker] Destroying Docker container #{state[:container_id]}") if state[:container_id]
  remove_container(state) if container_exists?(state)

  if @config[:remove_images] && state[:image_id]
    remove_image(state) if image_exists?(state)
  end
end

#hostname(state) ⇒ String

Works out the address Test Kitchen should connect to.

A remote Docker socket means the container is reachable at the socket's own host; use_internal_docker_network means its container IP; anything else is a published port on localhost.

Parameters:

  • state (Hash)

    instance state naming the container

Returns:

  • (String)

    a hostname or IP address



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kitchen/docker/container.rb', line 90

def hostname(state)
  hostname = "localhost"

  if remote_socket?
    hostname = socket_uri.host
  elsif @config[:use_internal_docker_network]
    hostname = container_ip_address(state)
  end

  hostname
end

#upload(locals, remote) ⇒ Array<String>

Copies local files into the container.

Parameters:

  • locals (String, Array<String>)

    one path or several

  • remote (String)

    destination path inside the container

Returns:

  • (Array<String>)

    the files copied



107
108
109
110
111
112
113
114
115
116
# File 'lib/kitchen/docker/container.rb', line 107

def upload(locals, remote)
  files = locals
  files = Array(locals) unless locals.is_a?(Array)

  files.each do |file|
    copy_file_to_container(@config, file, remote)
  end

  files
end