Skip to content

Commit

Permalink
add simple macro and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Oct 12, 2023
1 parent ac05521 commit 56c9fad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 51 deletions.
52 changes: 52 additions & 0 deletions lib/ex_unit.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule TestcontainersElixir.ExUnit do

alias TestcontainersElixir.Reaper
alias DockerEngineAPI.Connection
alias DockerEngineAPI.Api
alias DockerEngineAPI.Model

defmacro container(options \\ []) 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, _} =
conn
|> Api.Container.container_start(container_id)

:ok =
case GenServer.whereis(Reaper) do
nil ->
{:ok, _} = conn |> Reaper.start_link()
Reaper.register({"id", container_id})

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

end

:ok = Reaper.register({"id", container_id})

{:ok, container_id}
end
end
end
19 changes: 11 additions & 8 deletions lib/reaper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
defmodule TestcontainersElixir.Reaper do
use GenServer

alias DockerEngineAPI.Api
alias DockerEngineAPI.Model

@ryuk_image "testcontainers/ryuk:0.5.1"
@ryuk_port 8080

Expand All @@ -25,12 +28,12 @@ defmodule TestcontainersElixir.Reaper do
def init(connection) do
{:ok, _} =
connection
|> DockerEngineAPI.Api.Image.image_create(fromImage: @ryuk_image)
|> Api.Image.image_create(fromImage: @ryuk_image)

{:ok, %DockerEngineAPI.Model.ContainerCreateResponse{Id: container_id} = container} =
{:ok, %Model.ContainerCreateResponse{Id: container_id} = container} =
connection
|> DockerEngineAPI.Api.Container.container_create(
%DockerEngineAPI.Model.ContainerCreateRequest{
|> Api.Container.container_create(
%Model.ContainerCreateRequest{
Image: @ryuk_image,
ExposedPorts: %{"#{@ryuk_port}" => %{}},
HostConfig: %{
Expand All @@ -45,7 +48,7 @@ defmodule TestcontainersElixir.Reaper do

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

{:ok, socket} =
connection
Expand Down Expand Up @@ -77,14 +80,14 @@ defmodule TestcontainersElixir.Reaper do

defp create_ryuk_socket(
connection,
%DockerEngineAPI.Model.ContainerCreateResponse{Id: container_id}
%Model.ContainerCreateResponse{Id: container_id}
) do
port_str = "#{@ryuk_port}/tcp"

{:ok,
%DockerEngineAPI.Model.ContainerInspectResponse{
%Model.ContainerInspectResponse{
NetworkSettings: %{Ports: %{^port_str => [%{"HostPort" => host_port} | _tail]}}
}} = connection |> DockerEngineAPI.Api.Container.container_inspect(container_id)
}} = connection |> Api.Container.container_inspect(container_id)

:gen_tcp.connect(~c"localhost", String.to_integer(host_port), [
:binary,
Expand Down
33 changes: 0 additions & 33 deletions lib/testcontainers_elixir.ex

This file was deleted.

9 changes: 9 additions & 0 deletions test/simple_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule SimpleTest do
use ExUnit.Case
import TestcontainersElixir.ExUnit

test "creates and reapes container" do
{:ok, container_id} = container(image: "nginx:latest", port: 80)
assert is_binary(container_id)
end
end
10 changes: 0 additions & 10 deletions test/testcontainers_elixir_test.exs

This file was deleted.

0 comments on commit 56c9fad

Please sign in to comment.