Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 12, 2023
1 parent c8691a5 commit 1d8fb9d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
25 changes: 10 additions & 15 deletions lib/ex_unit.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
defmodule TestcontainersElixir.ExUnit do

alias TestcontainersElixir.Reaper
alias DockerEngineAPI.Connection
alias DockerEngineAPI.Api
Expand All @@ -12,6 +11,7 @@ defmodule TestcontainersElixir.ExUnit do
conn = Connection.new(base_url: docker_url)

image = Keyword.get(unquote(options), :image, nil)

{:ok, _} =
conn
|> Api.Image.image_create(fromImage: image)
Expand All @@ -20,19 +20,13 @@ defmodule TestcontainersElixir.ExUnit do

{:ok, %Model.ContainerCreateResponse{Id: container_id}} =
conn
|> Api.Container.container_create(
%Model.ContainerCreateRequest{
Image: image,
ExposedPorts: %{"#{port}" => %{}},
HostConfig: %{
PortBindings: %{"#{port}" => [%{"HostPort" => ""}]}
}
|> Api.Container.container_create(%Model.ContainerCreateRequest{
Image: image,
ExposedPorts: %{"#{port}" => %{}},
HostConfig: %{
PortBindings: %{"#{port}" => [%{"HostPort" => ""}]}
}
)

{:ok, _} =
conn
|> Api.Container.container_start(container_id)
})

:ok =
case GenServer.whereis(Reaper) do
Expand All @@ -42,10 +36,11 @@ defmodule TestcontainersElixir.ExUnit do

_ ->
Reaper.register({"id", container_id})

end

:ok = Reaper.register({"id", container_id})
{:ok, _} =
conn
|> Api.Container.container_start(container_id)

{:ok, container_id}
end
Expand Down
37 changes: 11 additions & 26 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ defmodule TestcontainersElixir.Reaper do
GenServer.call(__MODULE__, {:register, filter})
end

def ping do
try do
GenServer.call(__MODULE__, :ping)
catch
:exit, _reason -> :error
end
end

@impl true
def init(connection) do
{:ok, _} =
Expand All @@ -32,19 +24,17 @@ defmodule TestcontainersElixir.Reaper do

{: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}"]
}
)
|> 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
Expand All @@ -62,11 +52,6 @@ defmodule TestcontainersElixir.Reaper do
{:reply, register_filter(socket, filter), socket}
end

@impl true
def handle_call(:ping, _from, socket) do
{:reply, :ok, socket}
end

defp register_filter(socket, {filter_key, filter_value}) do
:gen_tcp.send(
socket,
Expand Down

0 comments on commit 1d8fb9d

Please sign in to comment.