Skip to content

Commit

Permalink
use elixir with and refactor into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 13, 2023
1 parent f177ba8 commit a46b879
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 57 deletions.
53 changes: 25 additions & 28 deletions lib/ex_unit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 24 additions & 29 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit a46b879

Please sign in to comment.