meta/test/support/ldap_test_environment.ex

63 lines
1.6 KiB
Elixir

defmodule RecycledCloud.LDAPTestEnvironment do
alias RecycledCloud.LDAP
require Logger
@image "code.ungleich.ch:5050/fnux/e-durable-oci-images/openldap-playground:latest"
defp container_runtime_binary do
System.find_executable("podman") || System.find_executable("docker")
end
def start() do
case container_runtime_binary() do
nil ->
Logger.error("Could not find a container runtime (required for LDAP environment). Exiting.")
:error
binary ->
Logger.info("Starting LDAP environment.")
container = container_name()
port = Port.open(
{:spawn_executable, binary},
[:binary, args: [
"run", "--rm", "--name", container, "-p", "3089:389", @image
]])
case wait_for_ldap() do
:ok ->
# Wait for LDAP server to be populated.
# FIXME: poll state instead of taking a nap!
Process.sleep(2000)
{:ok, port, container}
:timeout -> {:error, :timeout}
end
end
end
def stop(name) do
Logger.info "Terminating LDAP Test Environment."
binary = container_runtime_binary()
System.cmd(binary, ["stop", name])
end
defp container_name, do: "ldap-playground-#{System.unique_integer()}"
defp wait_for_ldap(poll_every \\ 1000, max \\ 10, current \\ 0) do
case LDAP.connect() do
%{status: :ok, conn: conn} ->
conn |> LDAP.close()
:ok
%{status: :error, conn: _} ->
unless current >= max do
Process.sleep(poll_every)
wait_for_ldap(poll_every, max, current + 1)
else
:timeout
end
end
end
end