Skip to content

Commit

Permalink
add proper container test
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 13, 2023
1 parent 269a3cd commit 328ea8b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
28 changes: 28 additions & 0 deletions lib/container.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule TestcontainersElixir.Container do
alias DockerEngineAPI.Model.ContainerInspectResponse

defstruct [:container_id, ports: %{}]

def of(%ContainerInspectResponse{
Id: container_id,
NetworkSettings: %{Ports: ports}
}) do
%__MODULE__{
container_id: container_id,
ports:
Enum.reduce(ports, [], fn {key, ports}, acc ->
acc ++
Enum.map(ports, fn %{"HostIp" => host_ip, "HostPort" => host_port} ->
%{exposed_port: key, host_ip: host_ip, host_port: host_port |> String.to_integer()}
end)
end)
}
end

def mapped_port(%__MODULE__{} = container, port) when is_number(port) do
container.ports
|> Enum.filter(fn %{exposed_port: exposed_port} -> exposed_port == "#{port}/tcp" end)
|> List.first()
|> Map.get(:host_port)
end
end
20 changes: 15 additions & 5 deletions lib/ex_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ defmodule TestcontainersElixir.ExUnit do
port = Keyword.get(options, :port, nil)

with {:ok, _} <- Api.Image.image_create(conn, fromImage: image),
{:ok, container} <- simple_container(conn, image, port),
{:ok, container} <- create_simple_container(conn, image, port),
container_id = container."Id",
:ok <- reap_container(conn, container_id),
{:ok, _} <- Api.Container.container_start(conn, container_id),
:ok = on_exit(:stop_container, fn -> stop_container(conn, container_id) end) do
{:ok, container_id}
:ok =
on_exit(:stop_container, fn ->
with :ok <- reap_container(conn, container_id) do
stop_container(conn, container_id)
end
end) do
{:ok, get_container(conn, container_id)}
end
end

Expand All @@ -30,7 +34,7 @@ defmodule TestcontainersElixir.ExUnit do
end
end

defp simple_container(conn, image, port) when is_binary(image) and is_number(port) do
defp create_simple_container(conn, image, port) when is_binary(image) and is_number(port) do
Api.Container.container_create(conn, %Model.ContainerCreateRequest{
Image: image,
ExposedPorts: %{"#{port}" => %{}},
Expand All @@ -40,6 +44,12 @@ defmodule TestcontainersElixir.ExUnit do
})
end

defp get_container(conn, container_id) when is_binary(container_id) do
with {:ok, response} <- Api.Container.container_inspect(conn, container_id) do
TestcontainersElixir.Container.of(response)
end
end

defp reap_container(conn, container_id) when is_binary(container_id) do
case GenServer.whereis(Reaper) do
nil ->
Expand Down
15 changes: 13 additions & 2 deletions test/simple_test.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
defmodule SimpleTest do
use ExUnit.Case
import TestcontainersElixir.ExUnit
alias TestcontainersElixir.Container

test "creates and reaps container" do
{:ok, container_id} = container(image: "nginx:latest", port: 80)
assert is_binary(container_id)
{:ok, container} = container(image: "nginx:latest", port: 80)

port = Container.mapped_port(container, 80)

{:ok, 200, _headers, body_ref} = :hackney.get("http://localhost:#{port}")
{:ok, body} = :hackney.body(body_ref)
body_str = IO.iodata_to_binary(body)

assert String.contains?(
body_str,
"<!DOCTYPE html>\n<html>\n<head>\n<title>Welcome to nginx!</title>\n<style>\nhtml { color-scheme: light dark; }\nbody { width: 35em; margin: 0 auto;\nfont-family: Tahoma, Verdana, Arial, sans-serif; }\n</style>\n</head>\n<body>\n<h1>Welcome to nginx!</h1>\n<p>If you see this page, the nginx web server is successfully installed and\nworking. Further configuration is required.</p>\n\n<p>For online documentation and support please refer to\n<a href=\"http://nginx.org/\">nginx.org</a>.<br/>\nCommercial support is available at\n<a href=\"http://nginx.com/\">nginx.com</a>.</p>\n\n<p><em>Thank you for using nginx.</em></p>\n</body>\n</html>\n"
)
end
end

0 comments on commit 328ea8b

Please sign in to comment.