-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule TestcontainersElixir.CephContainer do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule TestcontainersElixir.Containers do | ||
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) | ||
image = Keyword.get(options, :image, nil) | ||
port = Keyword.get(options, :port, nil) | ||
|
||
with {:ok, _} <- Api.Image.image_create(conn, fromImage: image), | ||
{:ok, container} <- create_simple_container(conn, image, port), | ||
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 | ||
end | ||
|
||
defp stop_container(conn, container_id) when is_binary(container_id) do | ||
with {:ok, _} <- Api.Container.container_kill(conn, container_id), | ||
{:ok, _} <- Api.Container.container_delete(conn, container_id) do | ||
:ok | ||
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) | ||
end | ||
end | ||
|
||
defp reap_container(conn, container_id) when is_binary(container_id) do | ||
case conn |> Reaper.start_link() do | ||
{:error, {:already_started, _}} -> :ok | ||
{:ok, _} -> :ok | ||
end | ||
|
||
Reaper.register({"id", container_id}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
defmodule TestcontainersElixir.ExUnit do | ||
alias TestcontainersElixir.Reaper | ||
alias DockerEngineAPI.Connection | ||
alias DockerEngineAPI.Api | ||
alias DockerEngineAPI.Model | ||
alias TestcontainersElixir.Containers | ||
|
||
def container(options \\ [], on_exit \\ &ExUnit.Callbacks.on_exit/2) do | ||
docker_url = "http+unix://%2Fvar%2Frun%2Fdocker.sock/v1.43" | ||
conn = Connection.new(base_url: docker_url) | ||
image = Keyword.get(options, :image, nil) | ||
port = Keyword.get(options, :port, nil) | ||
|
||
with {:ok, _} <- Api.Image.image_create(conn, fromImage: image), | ||
{:ok, container} <- create_simple_container(conn, image, port), | ||
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 | ||
end | ||
|
||
defp stop_container(conn, container_id) when is_binary(container_id) do | ||
with {:ok, _} <- Api.Container.container_kill(conn, container_id), | ||
{:ok, _} <- Api.Container.container_delete(conn, container_id) do | ||
:ok | ||
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 | ||
TestcontainersElixir.Container.of(response) | ||
end | ||
end | ||
|
||
defp reap_container(conn, container_id) when is_binary(container_id) do | ||
case conn |> Reaper.start_link() do | ||
{:error, {:already_started, _}} -> :ok | ||
{:ok, _} -> :ok | ||
end | ||
|
||
Reaper.register({"id", container_id}) | ||
def container(options \\ []) do | ||
Containers.container(options, &ExUnit.Callbacks.on_exit/2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters