Testcontainers.ToxiproxyContainer (testcontainers v1.14.1)

Copy Markdown View Source

Provides functionality for creating and managing Toxiproxy container configurations.

Toxiproxy is a framework for simulating network conditions. It's made specifically to work in testing, CI and development environments, supporting deterministic tampering with connections, but with support for randomized chaos and customization.

Summary

Functions

Returns the URI for the Toxiproxy API.

Configures the ToxiproxyEx library to use this container.

Returns the control port number (for the Toxiproxy HTTP API).

Creates a proxy in Toxiproxy that routes traffic from a container port to an upstream service.

Creates a proxy for another container on the same network.

Retrieves the default Docker image for the Toxiproxy container.

Deletes a proxy from Toxiproxy.

Returns the first proxy port number.

Lists all proxies configured in Toxiproxy.

Returns the mapped control port on the host for the running container.

Creates a new ToxiproxyContainer struct with default configurations.

Returns the number of proxy ports reserved.

Resets Toxiproxy, removing all toxics and re-enabling all proxies.

Overrides the default image used for the Toxiproxy container.

Set the reuse flag to reuse the container if it is already running.

Overrides the default wait timeout used for the Toxiproxy container.

Functions

api_url(container)

Returns the URI for the Toxiproxy API.

This can be used with ToxiproxyEx:

ToxiproxyContainer.api_url(container)
|> then(&Application.put_env(:toxiproxy_ex, :host, &1))

configure_toxiproxy_ex(container)

Configures the ToxiproxyEx library to use this container.

This sets the :toxiproxy_ex application environment to point to the running container's API endpoint.

Example

{:ok, toxiproxy} = Testcontainers.start_container(ToxiproxyContainer.new())
:ok = ToxiproxyContainer.configure_toxiproxy_ex(toxiproxy)

# Now ToxiproxyEx will use this container
ToxiproxyEx.get!("my_proxy") |> ToxiproxyEx.down!(fn -> ... end)

control_port()

Returns the control port number (for the Toxiproxy HTTP API).

create_proxy(container, name, upstream, opts \\ [])

Creates a proxy in Toxiproxy that routes traffic from a container port to an upstream service.

Parameters

  • container - The running Toxiproxy container
  • name - A unique name for the proxy
  • upstream - The upstream address in format "host:port" (as seen from Toxiproxy container)
  • opts - Optional keyword list:
    • :listen_port - Specific port to listen on (default: auto-allocated from 8666+)

create_proxy_for_container(toxiproxy, name, target_container, target_port, opts \\ [])

Creates a proxy for another container on the same network.

This is a convenience function that creates a proxy using the target container's hostname and port.

Parameters

  • toxiproxy - The running Toxiproxy container
  • name - A unique name for the proxy
  • target_container - The target container to proxy to
  • target_port - The port on the target container
  • opts - Optional keyword list (see create_proxy/4)

default_image()

Retrieves the default Docker image for the Toxiproxy container.

delete_proxy(container, name)

Deletes a proxy from Toxiproxy.

first_proxy_port()

Returns the first proxy port number.

list_proxies(container)

Lists all proxies configured in Toxiproxy.

Returns a map of proxy names to their configurations.

mapped_control_port(container)

Returns the mapped control port on the host for the running container.

new()

Creates a new ToxiproxyContainer struct with default configurations.

proxy_port_count()

Returns the number of proxy ports reserved.

reset(container)

Resets Toxiproxy, removing all toxics and re-enabling all proxies.

with_image(config, image)

Overrides the default image used for the Toxiproxy container.

with_reuse(config, reuse)

Set the reuse flag to reuse the container if it is already running.

with_wait_timeout(config, wait_timeout)

Overrides the default wait timeout used for the Toxiproxy container.