58 lines
1.5 KiB
Elixir
58 lines
1.5 KiB
Elixir
defmodule RecycledCloud.LDAPTestEnvironment do
|
|
alias RecycledCloud.LDAP
|
|
require Logger
|
|
|
|
@image "code.ungleich.ch:5050/fnux/e-durable-oci-images/openldap-playground:latest"
|
|
|
|
def start() do
|
|
case System.find_executable("podman") do
|
|
nil ->
|
|
Logger.error("Could not find podman executable (required for LDAP environment). Exiting.")
|
|
|
|
:error
|
|
podman ->
|
|
Logger.info("Starting LDAP environment.")
|
|
container = container_name()
|
|
port = Port.open(
|
|
{:spawn_executable, podman},
|
|
[: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(1000)
|
|
|
|
{:ok, port, container}
|
|
:timeout -> {:error, :timeout}
|
|
end
|
|
end
|
|
end
|
|
|
|
def stop(name) do
|
|
Logger.info "Terminating LDAP Test Environment."
|
|
|
|
podman = System.find_executable("podman")
|
|
System.cmd(podman, ["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
|