Packages
testcontainers
0.9.1
2.3.1
2.3.0
2.2.0
2.1.0
2.1.0-rc1
2.0.0
2.0.0-rc3
2.0.0-rc2
2.0.0-rc
1.14.1
1.14.0
1.13.5
1.13.4
1.13.3
1.13.2
1.13.1
1.13.0
1.12.0
1.11.8
1.11.7
1.11.6
1.11.5
1.11.4
1.11.3
1.11.2
1.11.1
1.11.0
1.10.5
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
1.9.0
1.8.4
1.8.3
1.8.2
1.8.1
1.8.0
1.7.0
1.6.0
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.2.11
1.2.10
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-beta.1
1.0.0-beta
0.9.3
0.9.2
0.9.1
0.9.0
Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Current section
Files
Jump to
Current section
Files
lib/container.ex
# SPDX-License-Identifier: MIT
# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers
# Modified by: Jarl André Hübenthal @ 2023
defmodule Testcontainers.Container do
@enforce_keys [:image]
defstruct [
:image,
cmd: nil,
environment: %{},
exposed_ports: [],
wait_strategies: [],
privileged: false,
bind_mounts: [],
labels: %{},
auto_remove: true,
container_id: nil
]
@doc """
A constructor function to make it easier to construct a container
"""
def new(image, opts \\ []) when is_binary(image) do
%__MODULE__{
image: image,
bind_mounts: opts[:bind_mounts] || [],
cmd: opts[:cmd],
environment: opts[:environment] || %{},
exposed_ports: Keyword.get(opts, :exposed_ports, []),
privileged: opts[:privileged] || false,
auto_remove: opts[:auto_remove] || true,
wait_strategies: opts[:wait_strategies] || []
}
end
@doc """
Sets a _waiting strategy_ for the _container_.
"""
def with_waiting_strategy(%__MODULE__{} = config, wait_fn) do
%__MODULE__{config | wait_strategies: [wait_fn | config.wait_strategies]}
end
@doc """
Sets multiple _waiting strategies_ for the _container_.
"""
def with_waiting_strategies(%__MODULE__{} = config, wait_fns) when is_list(wait_fns) do
%__MODULE__{config | wait_strategies: wait_fns ++ config.wait_strategies}
end
@doc """
Sets an _environment variable_ to the _container_.
"""
def with_environment(%__MODULE__{} = config, key, value) do
%__MODULE__{config | environment: Map.put(config.environment, key, value)}
end
@doc """
Adds a _port_ to be exposed on the _container_.
"""
def with_exposed_port(%__MODULE__{} = config, port) do
%__MODULE__{config | exposed_ports: [port | config.exposed_ports]}
end
@doc """
Adds multiple _ports_ to be exposed on the _container_.
"""
def with_exposed_ports(%__MODULE__{} = config, ports) when is_list(ports) do
%__MODULE__{config | exposed_ports: ports ++ config.exposed_ports}
end
@doc """
Sets a file or the directory on the _host machine_ to be mounted into a _container_.
"""
def with_bind_mount(%__MODULE__{} = config, host_src, container_dest, options \\ "ro")
when is_binary(host_src) and is_binary(container_dest) do
new_bind_mount = %{host_src: host_src, container_dest: container_dest, options: options}
%__MODULE__{config | bind_mounts: [new_bind_mount | config.bind_mounts]}
end
@doc """
Sets a label to apply to the container object in docker.
"""
def with_label(%__MODULE__{} = config, key, value) do
%__MODULE__{config | labels: Map.put(config.labels, key, value)}
end
@doc """
Gets the host port on the container for the given exposed port.
"""
def mapped_port(%__MODULE__{} = container, port) when is_number(port) do
container.exposed_ports
|> Enum.filter(fn
%{exposed_port: exposed_port} -> exposed_port == "#{port}/tcp"
port -> port == "#{port}/tcp"
end)
|> List.first(%{})
|> Map.get(:host_port)
end
end