tests: allow to run container runtime with sudo (docker in CI)

This commit is contained in:
Timothée Floure 2021-01-13 07:54:06 +01:00
parent 539e1146d1
commit 7168f65965
Signed by: tfloure
GPG key ID: 4502C902C00A1E12

View file

@ -4,46 +4,16 @@ defmodule RecycledCloud.LDAPTestEnvironment do
@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
defp container_name, do: "ldap-playground-#{System.unique_integer()}"
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
defp get_runtime_tuple(args) do
runtime = System.find_executable("podman") || System.find_executable("docker")
case System.get_env("CALL_CONTAINER_RUNTIME_AS_ROOT") do
nil -> {runtime, args}
_ -> {System.find_executable("sudo"), [runtime | args]}
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} ->
@ -59,4 +29,39 @@ defmodule RecycledCloud.LDAPTestEnvironment do
end
end
end
def start() do
container = container_name()
runtime_tuple = get_runtime_tuple([
"run", "--rm", "--name", container, "-p", "3089:389", @image
])
case runtime_tuple do
{nil, _} ->
Logger.error("Could not find a container runtime (required for LDAP environment). Exiting.")
:error
{binary, args} ->
Logger.info("Starting LDAP environment.")
port = Port.open(
{:spawn_executable, binary},
[:binary, args: args]
)
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, args} = get_runtime_tuple(["stop", name])
System.cmd(binary, args)
end
end