-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |