Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring, hopefully to somehing better #4

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/ceph_container.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
# SPDX-License-Identifier: Apache-2.0
defmodule TestcontainersElixir.CephContainer do
alias TestcontainersElixir.LogChecker
alias DockerEngineAPI.Model

def create_container(options \\ []) do
image = Keyword.get(options, :image, "quay.io/ceph/demo:latest-quincy")
access_key = Keyword.get(options, :access_key, "demo")
secret_key = Keyword.get(options, :secret_key, "demo")
bucket = Keyword.get(options, :bucket, "demo")

%Model.ContainerCreateRequest{
Image: image,
ExposedPorts: %{"3300" => %{}, "8080" => %{}},
HostConfig: %{
PortBindings: %{
"3300" => [%{"HostIp" => "0.0.0.0", "HostPort" => ""}],
"8080" => [%{"HostIp" => "0.0.0.0", "HostPort" => ""}]
}
},
Env: [
"CEPH_DEMO_UID=demo",
"CEPH_DEMO_BUCKET=#{bucket}",
"CEPH_DEMO_ACCESS_KEY=#{access_key}",
"CEPH_DEMO_SECRET_KEY=#{secret_key}",
"CEPH_PUBLIC_NETWORK=0.0.0.0/0",
"MON_IP=127.0.0.1",
"RGW_NAME=localhost"
]
}
end

def waiting_strategy(conn, container),
do:
LogChecker.wait_for_log(
conn,
container.container_id,
~r/.*Bucket 's3:\/\/.*\/' created.*/,
300_000
)
end
6 changes: 3 additions & 3 deletions lib/container.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ defmodule TestcontainersElixir.Container do
%__MODULE__{
container_id: container_id,
ports:
Enum.reduce(ports, [], fn {key, ports}, acc ->
Enum.reduce(ports || [], [], fn {key, ports}, acc ->
acc ++
Enum.map(ports, fn %{"HostIp" => host_ip, "HostPort" => host_port} ->
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)
Expand All @@ -23,7 +23,7 @@ defmodule TestcontainersElixir.Container do
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()
|> List.first(%{})
|> Map.get(:host_port)
end
end
37 changes: 19 additions & 18 deletions lib/containers.ex
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# SPDX-License-Identifier: Apache-2.0
defmodule TestcontainersElixir.Containers do
alias TestcontainersElixir.PortChecker
alias DockerEngineAPI.Api
alias DockerEngineAPI.Model
alias DockerEngineAPI.Connection
alias TestcontainersElixir.Reaper
alias TestcontainersElixir.Container

def container(options \\ [], on_exit) do
docker_url = "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43"
conn = Connection.new(base_url: docker_url)
def container(options) do
conn = Keyword.get_lazy(options, :conn, &get_static_connection/0)
image = Keyword.get(options, :image, nil)
port = Keyword.get(options, :port, nil)
on_exit = Keyword.get(options, :on_exit, fn _, _ -> :ok end)
container_factory = Keyword.get(options, :container_factory)

waiting_strategy =
Keyword.get(options, :waiting_strategy, fn _ ->
PortChecker.wait_for_port("127.0.0.1", port)
end)

with {:ok, _} <- Api.Image.image_create(conn, fromImage: image),
{:ok, container} <- create_simple_container(conn, image, port),
{:ok, container} <- Api.Container.container_create(conn, container_factory.(options)),
container_id = container."Id",
{:ok, _} <- Api.Container.container_start(conn, 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),
{:ok, container} <- get_container(conn, container_id),
{:ok, _} <- waiting_strategy.(conn, container) do
{:ok, container}
end
end

Expand All @@ -33,19 +41,9 @@ defmodule TestcontainersElixir.Containers do
end
end

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}" => %{}},
HostConfig: %{
PortBindings: %{"#{port}/tcp" => [%{"HostIp" => "0.0.0.0", "HostPort" => ""}]}
}
})
end

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

Expand All @@ -57,4 +55,7 @@ defmodule TestcontainersElixir.Containers do

Reaper.register({"id", container_id})
end

defp get_static_connection,
do: Connection.new(base_url: "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43")
end
35 changes: 33 additions & 2 deletions lib/ex_unit.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
# SPDX-License-Identifier: Apache-2.0
defmodule TestcontainersElixir.ExUnit do
alias TestcontainersElixir.CephContainer
alias TestcontainersElixir.Containers
alias DockerEngineAPI.Model

def container(options \\ []) do
Containers.container(options, &ExUnit.Callbacks.on_exit/2)
def ceph_container(options \\ []) do
Containers.container(
options
|> Keyword.merge(
on_exit: Keyword.get(options, :on_exit, &ExUnit.Callbacks.on_exit/2),
container_factory: &CephContainer.create_container/1,
waiting_strategy: &CephContainer.waiting_strategy/2
)
)
end

def generic_container(options \\ []) do
Containers.container(
options
|> Keyword.merge(
on_exit: Keyword.get(options, :on_exit, &ExUnit.Callbacks.on_exit/2),
container_factory: fn _ ->
%Model.ContainerCreateRequest{
Image: Keyword.get(options, :image),
ExposedPorts: %{"#{Keyword.get(options, :port)}" => %{}},
HostConfig: %{
PortBindings: %{
"#{Keyword.get(options, :port)}" => [
%{"HostIp" => "0.0.0.0", "HostPort" => ""}
]
}
}
}
end
)
)
end
end
16 changes: 16 additions & 0 deletions test/ceph_container_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule CephContainerTest do
use ExUnit.Case, async: true

import TestcontainersElixir.ExUnit
alias DockerEngineAPI.Api.Container
alias TestcontainersElixir.Container

@tag timeout: 300_000

test "creates and starts ceph container" do
{:ok, container} =
ceph_container(image: "quay.io/ceph/demo:latest")

assert is_number(Container.mapped_port(container, 8080))
end
end
17 changes: 13 additions & 4 deletions test/httpd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ defmodule AnotherTest do
use ExUnit.Case, async: true

import TestcontainersElixir.ExUnit

alias TestcontainersElixir.HttpChecker
alias TestcontainersElixir.Container

test "creates and uses container" do
{:ok, container} = container(image: "httpd:latest", port: 80)
{:ok, container} =
generic_container(
image: "httpd:latest",
port: 80,
waiting_strategy: fn _, container ->
HttpChecker.wait_for_http(
"127.0.0.1",
Container.mapped_port(container, 80),
"/",
5000
)
end
)

port = Container.mapped_port(container, 80)

{:ok, :http_is_ready} = HttpChecker.wait_for_http("127.0.0.1", port, "/", 5000)

{:ok, 200, _headers, body_ref} = :hackney.request(:get, "http://127.0.0.1:#{port}")
{:ok, body} = :hackney.body(body_ref)
body_str = IO.iodata_to_binary(body)
Expand Down
26 changes: 13 additions & 13 deletions test/nginx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ defmodule SimpleTest do
use ExUnit.Case, async: true

import TestcontainersElixir.ExUnit

alias TestcontainersElixir.HttpChecker
alias TestcontainersElixir.Container

test "creates and uses container" do
{:ok, container} = container(image: "nginx:latest", port: 80)
{:ok, container} =
generic_container(
image: "nginx:latest",
port: 80,
waiting_strategy: fn _, container ->
HttpChecker.wait_for_http(
"127.0.0.1",
Container.mapped_port(container, 80),
"/",
5000
)
end
)

port = Container.mapped_port(container, 80)

# # this is just work in progress, the log ready check below needs it
# # but we want to move this logic inside the container function above
# # and make a better system for passing in waiting strategies
# docker_url = "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43"
# conn = DockerEngineAPI.Connection.new(base_url: docker_url)

# {:ok, :log_is_ready} =
# LogChecker.wait_for_log(conn, container.container_id, ~r/.*nginx\/.*/, 10000)

{:ok, :http_is_ready} = HttpChecker.wait_for_http("127.0.0.1", port, "/", 5000)

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