From 7168f65965e464ff889de634116e35bf31f6d9a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 13 Jan 2021 07:54:06 +0100 Subject: [PATCH] tests: allow to run container runtime with sudo (docker in CI) --- test/support/ldap_test_environment.ex | 77 ++++++++++++++------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/test/support/ldap_test_environment.ex b/test/support/ldap_test_environment.ex index cc2c228..078b843 100644 --- a/test/support/ldap_test_environment.ex +++ b/test/support/ldap_test_environment.ex @@ -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