Packages

TestcontainerEx is an Elixir library for integration testing with containerized services. Start, stop, and monitor Docker, Podman, Minikube, Colima, or Apple Container containers with a unified API. Supports custom containers.

Current section

Files

Jump to
testcontainer_ex lib testcontainer_ex connection strategies container_env.ex
Raw

lib/testcontainer_ex/connection/strategies/container_env.ex

defmodule TestcontainerEx.Connection.Strategies.ContainerEnv do
@moduledoc """
Resolves the container engine host from Podman-style environment variables.
Checks `CONTAINER_ENGINE_HOST` first, then `CONTAINER_HOST` for Podman
compatibility.
"""
@behaviour TestcontainerEx.Connection.Strategies.Behaviour
alias TestcontainerEx.Connection.Url
@impl true
def resolve do
case {System.get_env("CONTAINER_ENGINE_HOST"), System.get_env("CONTAINER_HOST")} do
{nil, nil} ->
{:error, {:not_found, "CONTAINER_ENGINE_HOST"}}
{"", nil} ->
{:error, {:empty, "CONTAINER_ENGINE_HOST"}}
{nil, ""} ->
{:error, {:empty, "CONTAINER_HOST"}}
{url, _} when is_binary(url) and url != "" ->
probe(url)
{_, url} when is_binary(url) and url != "" ->
probe(url)
_ ->
{:error, {:empty, "CONTAINER_ENGINE_HOST"}}
end
end
defp probe(url) do
case URI.parse(url) do
%URI{scheme: "unix", path: path} ->
if socket_accessible?(path) do
{:ok, url}
else
{:error, {:socket_not_found, path}}
end
_ ->
case Req.get("#{Url.construct(url)}/_ping") do
{:ok, %{status: 200}} -> {:ok, url}
{:error, reason} -> {:error, {:ping_failed, url, reason}}
end
end
end
defp socket_accessible?(path) do
case File.stat(path) do
{:ok, stat} -> :erlang.band(stat.mode, 0o170000) == 0o140000
_ -> false
end
end
end