diff --git a/lib/container.ex b/lib/container.ex index a64dd8d..e0a593f 100644 --- a/lib/container.ex +++ b/lib/container.ex @@ -20,7 +20,8 @@ defmodule Testcontainers.Container do bind_volumes: [], labels: %{}, auto_remove: false, - container_id: nil + container_id: nil, + network_mode: nil ] @doc """ @@ -151,6 +152,13 @@ defmodule Testcontainers.Container do %__MODULE__{config | auth: registry_auth_token} end + @doc """ + Sets a network mode to apply to the container object in docker. + """ + def with_network_mode(%__MODULE__{} = config, mode) when is_binary(mode) do + %__MODULE__{config | network_mode: mode} + end + @doc """ Gets the host port on the container for the given exposed port. """ diff --git a/lib/docker/api.ex b/lib/docker/api.ex index 283ab9f..405a311 100644 --- a/lib/docker/api.ex +++ b/lib/docker/api.ex @@ -174,7 +174,8 @@ defmodule Testcontainers.Docker.Api do PortBindings: map_port_bindings(container_config), Privileged: container_config.privileged, Binds: map_binds(container_config), - Mounts: map_volumes(container_config) + Mounts: map_volumes(container_config), + NetworkMode: container_config.network_mode } } end diff --git a/test/container_test.exs b/test/container_test.exs index b4c10dd..35bb942 100644 --- a/test/container_test.exs +++ b/test/container_test.exs @@ -73,6 +73,18 @@ defmodule Testcontainers.ContainerTest do end end + describe "with_network_mode/2" do + test "returns the network host type" do + container = Container.new("my-image") |> Container.with_network_mode("host") + assert container.network_mode == "host" + end + + test "returns nil if the network mode is not set" do + container = Container.new("my-image") + assert container.network_mode == nil + end + end + describe "with_auth/3" do test "sets the authentication token for the container" do container = Container.new("my-image") diff --git a/test/generic_container_test.exs b/test/generic_container_test.exs index 0f3f2e2..98b1e9c 100644 --- a/test/generic_container_test.exs +++ b/test/generic_container_test.exs @@ -1,9 +1,19 @@ defmodule Testcontainers.GenericContainerTest do use ExUnit.Case, async: true + import TestHelper, only: [port_open?: 2] + test "can start and stop generic container" do config = %Testcontainers.Container{image: "redis:latest"} assert {:ok, container} = Testcontainers.start_container(config) assert :ok = Testcontainers.stop_container(container.container_id) end + + test "can start and stop generic container with network mode set to host" do + config = %Testcontainers.Container{image: "redis:latest", network_mode: "host"} + assert {:ok, container} = Testcontainers.start_container(config) + Process.sleep(5000) + assert :ok = port_open?("127.0.0.1", 6379) + assert :ok = Testcontainers.stop_container(container.container_id) + end end diff --git a/test/support/test_helper.ex b/test/support/test_helper.ex index 89b4190..3176c16 100644 --- a/test/support/test_helper.ex +++ b/test/support/test_helper.ex @@ -68,4 +68,18 @@ defmodule TestHelper do do_wait_for_lambda(lambda, retries - 1, interval, counter + 1) end end + + @doc """ + test if specific tcp port is open on localhost host address. + """ + def port_open?(address, port) when is_binary(address) do + case :gen_tcp.connect(to_charlist(address), port, [:binary, active: false], 1000) do + {:ok, socket} -> + :gen_tcp.close(socket) + :ok + + _ -> + :error + end + end end