Skip to content

Commit

Permalink
Add support to network host mode (#107)
Browse files Browse the repository at this point in the history
* Add support to network host mode

* Add reusable test helper function open_port?
  • Loading branch information
sleipnir authored Jul 3, 2024
1 parent 5df5266 commit f691792
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ defmodule Testcontainers.Container do
bind_volumes: [],
labels: %{},
auto_remove: false,
container_id: nil
container_id: nil,
network_mode: nil
]

@doc """
Expand Down Expand Up @@ -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.
"""
Expand Down
3 changes: 2 additions & 1 deletion lib/docker/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/container_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions test/generic_container_test.exs
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions test/support/test_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f691792

Please sign in to comment.