From a46b879bfe6c0a44507a9fe2e233fd8d27ec931e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Fri, 13 Oct 2023 06:55:23 +0200 Subject: [PATCH] use elixir with and refactor into methods --- lib/ex_unit.ex | 53 ++++++++++++++++++++++++-------------------------- lib/reaper.ex | 53 +++++++++++++++++++++++--------------------------- 2 files changed, 49 insertions(+), 57 deletions(-) diff --git a/lib/ex_unit.ex b/lib/ex_unit.ex index 03f9d73..347f3e7 100644 --- a/lib/ex_unit.ex +++ b/lib/ex_unit.ex @@ -9,40 +9,37 @@ defmodule TestcontainersElixir.ExUnit do quote do docker_url = "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43" conn = Connection.new(base_url: docker_url) - image = Keyword.get(unquote(options), :image, nil) - - {:ok, _} = - conn - |> Api.Image.image_create(fromImage: image) - port = Keyword.get(unquote(options), :port, nil) - {:ok, %Model.ContainerCreateResponse{Id: container_id}} = - conn - |> Api.Container.container_create(%Model.ContainerCreateRequest{ - Image: image, - ExposedPorts: %{"#{port}" => %{}}, - HostConfig: %{ - PortBindings: %{"#{port}" => [%{"HostPort" => ""}]} - } - }) - - :ok = - case GenServer.whereis(Reaper) do - nil -> - {:ok, _} = conn |> Reaper.start_link() - Reaper.register({"id", container_id}) + with {:ok, _} <- Api.Image.image_create(conn, fromImage: image), + {:ok, container} <- simple_container(conn, image, port), + container_id = container."Id", + :ok <- reap_container(conn, container_id), + {:ok, _} <- Api.Container.container_start(conn, container_id) do + {:ok, container_id} + end + end + end - _ -> - Reaper.register({"id", container_id}) - end + def 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}" => [%{"HostPort" => ""}]} + } + }) + end - {:ok, _} = - conn - |> Api.Container.container_start(container_id) + def reap_container(conn, container_id) when is_binary(container_id) do + case GenServer.whereis(Reaper) do + nil -> + {:ok, _} = conn |> Reaper.start_link() + Reaper.register({"id", container_id}) - {:ok, container_id} + _ -> + Reaper.register({"id", container_id}) end end end diff --git a/lib/reaper.ex b/lib/reaper.ex index cd1cdd7..6b04ef2 100644 --- a/lib/reaper.ex +++ b/lib/reaper.ex @@ -18,33 +18,13 @@ defmodule TestcontainersElixir.Reaper do @impl true def init(connection) do - {:ok, _} = - connection - |> Api.Image.image_create(fromImage: @ryuk_image) - - {:ok, %Model.ContainerCreateResponse{Id: container_id} = container} = - connection - |> Api.Container.container_create(%Model.ContainerCreateRequest{ - Image: @ryuk_image, - ExposedPorts: %{"#{@ryuk_port}" => %{}}, - HostConfig: %{ - PortBindings: %{"#{@ryuk_port}" => [%{"HostPort" => ""}]}, - Privileged: true, - # FIXME this will surely not work for all use cases - Binds: ["/var/run/docker.sock:/var/run/docker.sock:rw"] - }, - Env: ["RYUK_PORT=#{@ryuk_port}"] - }) - - {:ok, _} = - connection - |> Api.Container.container_start(container_id) - - {:ok, socket} = - connection - |> create_ryuk_socket(container) - - {:ok, socket} + with {:ok, _} <- Api.Image.image_create(connection, fromImage: @ryuk_image), + {:ok, container} <- create_ryuk_container(connection), + container_id = container."Id", + {:ok, _} <- Api.Container.container_start(connection, container_id), + {:ok, socket} <- create_ryuk_socket(connection, container_id) do + {:ok, socket} + end end @impl true @@ -63,10 +43,25 @@ defmodule TestcontainersElixir.Reaper do :ok end + defp create_ryuk_container(connection) do + Api.Container.container_create(connection, %Model.ContainerCreateRequest{ + Image: @ryuk_image, + ExposedPorts: %{"#{@ryuk_port}" => %{}}, + HostConfig: %{ + PortBindings: %{"#{@ryuk_port}" => [%{"HostPort" => ""}]}, + Privileged: true, + # FIXME this will surely not work for all use cases + Binds: ["/var/run/docker.sock:/var/run/docker.sock:rw"] + }, + Env: ["RYUK_PORT=#{@ryuk_port}"] + }) + end + defp create_ryuk_socket( connection, - %Model.ContainerCreateResponse{Id: container_id} - ) do + container_id + ) + when is_binary(container_id) do port_str = "#{@ryuk_port}/tcp" {:ok,