From 822241fb9a4223bfd583a84ebfeb6c7fd2308c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Sat, 14 Oct 2023 15:08:25 +0200 Subject: [PATCH] importing containers from excontainers --- lib/api.ex | 40 ++++++++---- lib/{ => container}/ceph_container.ex | 19 +++--- lib/container/mysql_container.ex | 67 ++++++++++++++++++++ lib/container/postgres_container.ex | 65 +++++++++++++++++++ lib/container/redis_container.ex | 38 +++++++++++ lib/http_checker.ex | 63 ------------------ lib/log_checker.ex | 67 -------------------- lib/wait_strategy.ex | 9 +++ lib/wait_strategy/command_wait_strategy.ex | 30 +++++++++ lib/wait_strategy/http_wait_strategy.ex | 74 ++++++++++++++++++++++ lib/wait_strategy/log_wait_strategy.ex | 64 +++++++++++++++++++ lib/{ => wait_strategy}/port_checker.ex | 0 test/ceph_container_test.exs | 3 +- test/httpd_test.exs | 14 ++-- test/nginx_test.exs | 15 ++--- 15 files changed, 392 insertions(+), 176 deletions(-) rename lib/{ => container}/ceph_container.ex (69%) create mode 100644 lib/container/mysql_container.ex create mode 100644 lib/container/postgres_container.ex create mode 100644 lib/container/redis_container.ex delete mode 100644 lib/http_checker.ex delete mode 100644 lib/log_checker.ex create mode 100644 lib/wait_strategy.ex create mode 100644 lib/wait_strategy/command_wait_strategy.ex create mode 100644 lib/wait_strategy/http_wait_strategy.ex create mode 100644 lib/wait_strategy/log_wait_strategy.ex rename lib/{ => wait_strategy}/port_checker.ex (100%) diff --git a/lib/api.ex b/lib/api.ex index eb073cd..f3d45f5 100644 --- a/lib/api.ex +++ b/lib/api.ex @@ -1,16 +1,16 @@ +# SPDX-License-Identifier: MIT defmodule TestcontainersElixir.Docker.Api do + alias TestcontainersElixir.WaitStrategy alias DockerEngineAPI.Model.ContainerCreateRequest alias DockerEngineAPI.Model.ContainerCreateResponse alias DockerEngineAPI.Api alias TestcontainersElixir.Container alias TestcontainersElixir.Reaper - alias TestcontainersElixir.Container alias TestcontainersElixir.Connection - def run(%Container{} = container_config, options \\ []) do - conn = Connection.get_connection() + def run(%Container{} = container_config, options \\ [], conn \\ Connection.get_connection()) do on_exit = Keyword.get(options, :on_exit, nil) - wait_fn = container_config.waiting_strategy + wait_strategy = container_config.waiting_strategy create_request = container_create_request(container_config) with {:ok, _} <- Api.Image.image_create(conn, fromImage: create_request."Image"), @@ -25,17 +25,35 @@ defmodule TestcontainersElixir.Docker.Api do else :ok end), - {:ok, container} <- get_container(conn, container_id), - {:ok, _} <- if(wait_fn != nil, do: wait_fn.(container), else: {:ok, nil}) do + {:ok, container} <- get_container(container_id, conn), + :ok <- + if(wait_strategy != nil, + do: + WaitStrategy.wait_until_container_is_ready(wait_strategy, container.container_id), + else: :ok + ) do {:ok, container} end end - def stdout_logs(container_id) do - conn = Connection.get_connection() + def stdout_logs(container_id, conn \\ Connection.get_connection()) do Api.Container.container_logs(conn, container_id, stdout: true) end + def execute_cmd(container_id, cmd, conn \\ Connection.get_connection()) when is_list(cmd) do + with {:ok, %DockerEngineAPI.Model.IdResponse{Id: container_id}} <- + Api.Exec.container_exec(conn, container_id, %DockerEngineAPI.Model.ExecConfig{Cmd: cmd}) do + {:ok, container_id} + end + end + + def get_container(container_id, conn \\ Connection.get_connection()) + when is_binary(container_id) do + with {:ok, response} <- Api.Container.container_inspect(conn, container_id) do + {:ok, from(response)} + end + end + defp container_create_request(%Container{} = container_config) do %ContainerCreateRequest{ Image: container_config.image, @@ -92,12 +110,6 @@ defmodule TestcontainersElixir.Docker.Api do end end - defp get_container(conn, container_id) when is_binary(container_id) do - with {:ok, response} <- Api.Container.container_inspect(conn, container_id) do - {:ok, from(response)} - end - end - defp reap_container(container_id) when is_binary(container_id) do case Reaper.start_link() do {:error, {:already_started, _}} -> :ok diff --git a/lib/ceph_container.ex b/lib/container/ceph_container.ex similarity index 69% rename from lib/ceph_container.ex rename to lib/container/ceph_container.ex index be0f374..5acb9fa 100644 --- a/lib/ceph_container.ex +++ b/lib/container/ceph_container.ex @@ -1,7 +1,12 @@ # SPDX-License-Identifier: MIT -defmodule TestcontainersElixir.CephContainer do +defmodule TestcontainersElixir.Container.CephContainer do + alias TestcontainersElixir.WaitStrategy.LogWaitStrategy alias TestcontainersElixir.Container - alias TestcontainersElixir.LogChecker + + @waiting_strategy LogWaitStrategy.new( + ~r/.*Bucket 's3:\/\/.*\/' created.*/, + 300_000 + ) def new(options \\ []) do image = Keyword.get(options, :image, "quay.io/ceph/demo:latest-quincy") @@ -20,15 +25,7 @@ defmodule TestcontainersElixir.CephContainer do MON_IP: "127.0.0.1", RGW_NAME: "localhost" }, - waiting_strategy: &waiting_strategy/1 + waiting_strategy: @waiting_strategy ) end - - defp waiting_strategy(container), - do: - LogChecker.wait_for_log( - container.container_id, - ~r/.*Bucket 's3:\/\/.*\/' created.*/, - 300_000 - ) end diff --git a/lib/container/mysql_container.ex b/lib/container/mysql_container.ex new file mode 100644 index 0000000..8c46ac2 --- /dev/null +++ b/lib/container/mysql_container.ex @@ -0,0 +1,67 @@ +# SPDX-License-Identifier: MIT +# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers +# Modified by: Jarl André Hübenthal @ 2023 +defmodule TestcontainersElixir.Container.MySqlContainer do + @moduledoc """ + Functions to build and interact with MySql containers. + """ + + alias TestcontainersElixir.Container + alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy + + @mysql_port 3306 + + @doc """ + Builds a MySql container. + + Uses MySql 8.0 by default, but a custom image can also be set. + + ## Options + + - `username` sets the username for the user + - `password` sets the password for the user + - `database` sets the name of the database + """ + def new(image \\ "mysql:8.0", opts \\ []) do + username = Keyword.get(opts, :username, "test") + password = Keyword.get(opts, :password, "test") + + Container.new( + image, + exposed_ports: [@mysql_port], + environment: %{ + MYSQL_USER: username, + MYSQL_PASSWORD: password, + MYSQL_DATABASE: Keyword.get(opts, :database, "test"), + MYSQL_RANDOM_ROOT_PASSWORD: "yes" + }, + wait_strategy: wait_strategy(username, password) + ) + end + + @doc """ + Returns the port on the _host machine_ where the MySql container is listening. + """ + def port(container), do: Container.mapped_port(container, @mysql_port) + + @doc """ + Returns the connection parameters to connect to the database from the _host machine_. + """ + def connection_parameters(%Container{} = container) do + [ + hostname: "localhost", + port: port(container), + username: container.environment[:MYSQL_USER], + password: container.environment[:MYSQL_PASSWORD], + database: container.environment[:MYSQL_DATABASE] + ] + end + + defp wait_strategy(username, password) do + CommandWaitStrategy.new([ + "sh", + "-c", + "mysqladmin ping --user='#{username}' --password='#{password}' -h localhost | grep 'mysqld is alive'" + ]) + end +end diff --git a/lib/container/postgres_container.ex b/lib/container/postgres_container.ex new file mode 100644 index 0000000..e2d3140 --- /dev/null +++ b/lib/container/postgres_container.ex @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: MIT +# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers +# Modified by: Jarl André Hübenthal @ 2023 +defmodule TestcontainersElixir.Container.PostgresContainer do + @moduledoc """ + Functions to build and interact with PostgreSql containers. + """ + + alias TestcontainersElixir.Container + alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy + + @postgres_port 5432 + @wait_strategy CommandWaitStrategy.new([ + "pg_isready", + "-U", + "test", + "-d", + "test", + "-h", + "localhost" + ]) + + @doc """ + Builds a PostgreSql container. + + Uses PostgreSql 13.1 by default, but a custom image can also be set. + + ## Options + + - `username` sets the username for the user + - `password` sets the password for the user + - `database` sets the name of the database + """ + def new(image \\ "postgres:13.1", opts \\ []) do + Container.new( + image, + exposed_ports: [@postgres_port], + environment: %{ + POSTGRES_USER: Keyword.get(opts, :username, "test"), + POSTGRES_PASSWORD: Keyword.get(opts, :password, "test"), + POSTGRES_DB: Keyword.get(opts, :database, "test") + }, + wait_strategy: @wait_strategy + ) + end + + @doc """ + Returns the port on the _host machine_ where the MySql container is listening. + """ + def port(%Container{} = container), + do: with({:ok, port} <- Container.mapped_port(container, @postgres_port), do: port) + + @doc """ + Returns the connection parameters to connect to the database from the _host machine_. + """ + def connection_parameters(%Container{} = container) do + [ + hostname: "localhost", + port: port(container), + username: container.environment[:POSTGRES_USER], + password: container.environment[:POSTGRES_PASSWORD], + database: container.environment[:POSTGRES_DB] + ] + end +end diff --git a/lib/container/redis_container.ex b/lib/container/redis_container.ex new file mode 100644 index 0000000..efdb299 --- /dev/null +++ b/lib/container/redis_container.ex @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: MIT +# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers +# Modified by: Jarl André Hübenthal @ 2023 +defmodule TestcontainersElixir.Container.RedisContainer do + @moduledoc """ + Functions to build and interact with Redis containers. + """ + + alias TestcontainersElixir.Container + alias TestcontainersElixir.WaitStrategy.CommandWaitStrategy + + @redis_port 6379 + @wait_strategy CommandWaitStrategy.new(["redis-cli", "PING"]) + + @doc """ + Creates a Redis container. + + Runs Redis 6.0 by default, but a custom image can also be set. + """ + def new(image \\ "redis:6.0-alpine", _opts \\ []) do + Container.new( + image, + exposed_ports: [@redis_port], + environment: %{}, + wait_strategy: @wait_strategy + ) + end + + @doc """ + Returns the port on the _host machine_ where the Redis container is listening. + """ + def port(%Container{} = container), do: Container.mapped_port(container, @redis_port) + + @doc """ + Returns the connection url to connect to Redis from the _host machine_. + """ + def connection_url(%Container{} = container), do: "redis://localhost:#{port(container)}/" +end diff --git a/lib/http_checker.ex b/lib/http_checker.ex deleted file mode 100644 index 9195f8e..0000000 --- a/lib/http_checker.ex +++ /dev/null @@ -1,63 +0,0 @@ -# SPDX-License-Identifier: MIT -defmodule TestcontainersElixir.HttpChecker do - @moduledoc """ - A utility module for checking HTTP service availability and readiness. - - The `TestcontainersElixir.HttpChecker` module provides functionality to wait for - an HTTP service to be ready at a specified IP address, port, and path. This ensures - that the HTTP service is not only reachable but also capable of providing HTTP responses, - indicating a degree of readiness. - - The module includes a public function, `wait_for_http/4`, which returns `{:ok, :http_is_ready}` - once the HTTP service is accessible and responding with a 200 status code, and `{:error, :timeout}` - if the service does not become available within the specified timeout period. - """ - - @doc """ - Waits for HTTP to be ready at the specified IP and port. - - ## Params: - - ip: The IP address as a string. - - port: The port number as an integer. - - path: The HTTP path as a string. - - timeout: The maximum time to wait (in milliseconds) as an integer. - """ - def wait_for_http(ip, port, path \\ "/", timeout \\ 5000) - when is_binary(ip) and is_integer(port) and is_binary(path) and is_integer(timeout) do - wait_for_http(ip, port, path, timeout, :os.system_time(:millisecond)) - end - - defp wait_for_http(ip, port, path, timeout, start_time) - when is_binary(ip) and is_integer(port) and is_binary(path) and is_integer(timeout) and - is_integer(start_time) do - if timeout + start_time < :os.system_time(:millisecond) do - {:error, :timeout} - else - case http_request(ip, port, path) do - {:ok, _response} -> - {:ok, :http_is_ready} - - {:error, _reason} -> - # Sleep for 500 ms, then retry - :timer.sleep(500) - wait_for_http(ip, port, path, timeout, start_time) - end - end - end - - defp http_request(ip, port, path) do - url = "http://" <> ip <> ":" <> Integer.to_string(port) <> path - - case :httpc.request(:get, {to_charlist(url), []}, [], []) do - {:ok, {{~c"HTTP/1.1", 200, _reason_phrase}, _headers, _body}} -> - {:ok, :http_ok} - - {:ok, {{~c"HTTP/1.1", status_code, _reason_phrase}, _headers, _body}} - when status_code != 200 -> - {:error, {:unexpected_status_code, status_code}} - - {:error, reason} -> - {:error, reason} - end - end -end diff --git a/lib/log_checker.ex b/lib/log_checker.ex deleted file mode 100644 index ce5a9c7..0000000 --- a/lib/log_checker.ex +++ /dev/null @@ -1,67 +0,0 @@ -# SPDX-License-Identifier: MIT -defmodule TestcontainersElixir.LogChecker do - @moduledoc """ - A module for interacting and verifying logs of a Docker container. - - The main functionality is to wait until a specific log message, - matching a provided regular expression, is emitted by a Docker container. - """ - - alias TestcontainersElixir.Docker - - @doc """ - Waits for a specific log message from a Docker container, - identified by its `container_id`, within a specified `timeout` period. - - The function waits until a log message matching `log_regex` - is found or until the `timeout` has expired, whichever comes first. - - ## Parameters - - - `container_id`: The ID (binary) of the Docker container to inspect the logs. - - `log_regex`: A regular expression (binary) that is expected to be found in the logs. - - `timeout`: (Optional) Time (integer, milliseconds) to wait for the log message. Default is 5000ms. - - ## Examples - - iex> TestcontainersElixir.LogChecker.wait_for_log(conn, "container_id", ~r/some pattern/) - {:ok, :log_is_ready} - - iex> TestcontainersElixir.LogChecker.wait_for_log(conn, "container_id", ~r/some pattern/, 10000) - {:error, :timeout} - - """ - def wait_for_log(container_id, log_regex, timeout \\ 5000) - when is_binary(container_id) and is_integer(timeout) do - wait_for_log( - container_id, - log_regex, - timeout, - :os.system_time(:millisecond) - ) - end - - defp wait_for_log(container_id, log_regex, timeout, start_time) - when is_binary(container_id) and is_integer(timeout) and is_integer(start_time) do - if timeout + start_time < :os.system_time(:millisecond) do - {:error, :timeout} - else - if log_comparison(container_id, log_regex) do - {:ok, :log_is_ready} - else - # Sleep for 500 ms, then retry - :timer.sleep(500) - wait_for_log(container_id, log_regex, timeout, start_time) - end - end - end - - defp log_comparison(container_id, log_regex) do - with {:ok, %{body: stdout_log}} <- Docker.Api.stdout_logs(container_id) do - Regex.match?(log_regex, stdout_log) - else - _ -> - false - end - end -end diff --git a/lib/wait_strategy.ex b/lib/wait_strategy.ex new file mode 100644 index 0000000..7c3a7da --- /dev/null +++ b/lib/wait_strategy.ex @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: MIT +# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers +# Modified by: Jarl André Hübenthal @ 2023 +defprotocol TestcontainersElixir.WaitStrategy do + @moduledoc false + + @spec wait_until_container_is_ready(t, String.t()) :: :ok | {:error, atom()} + def wait_until_container_is_ready(wait_strategy, id_or_name) +end diff --git a/lib/wait_strategy/command_wait_strategy.ex b/lib/wait_strategy/command_wait_strategy.ex new file mode 100644 index 0000000..74d52fe --- /dev/null +++ b/lib/wait_strategy/command_wait_strategy.ex @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: MIT +# Original by: Marco Dallagiacoma @ 2023 in https://github.com/dallagi/excontainers +# Modified by: Jarl André Hübenthal @ 2023 +defmodule TestcontainersElixir.WaitStrategy.CommandWaitStrategy do + @moduledoc """ + Considers container as ready as soon as a command runs successfully inside the container. + """ + defstruct [:command] + + @doc """ + Creates a new CommandWaitStrategy to wait until the given command executes successfully inside the container. + """ + def new(command), do: %__MODULE__{command: command} +end + +defimpl TestcontainersElixir.WaitStrategy, + for: TestcontainersElixir.WaitStrategy.CommandWaitStrategy do + alias TestcontainersElixir.Docker + + def wait_until_container_is_ready(wait_strategy, id_or_name) do + case Docker.Api.execute_cmd(id_or_name, wait_strategy.command) do + {:ok, _id} -> + :ok + + _ -> + :timer.sleep(100) + wait_until_container_is_ready(wait_strategy, id_or_name) + end + end +end diff --git a/lib/wait_strategy/http_wait_strategy.ex b/lib/wait_strategy/http_wait_strategy.ex new file mode 100644 index 0000000..df06721 --- /dev/null +++ b/lib/wait_strategy/http_wait_strategy.ex @@ -0,0 +1,74 @@ +# SPDX-License-Identifier: MIT +defmodule TestcontainersElixir.WaitStrategy.HttpWaitStrategy do + @moduledoc """ + Considers container as ready as soon as a command runs successfully inside the container. + """ + defstruct [:ip, :port, :path, :timeout] + + @doc """ + Creates a new CommandWaitStrategy to wait until the given command executes successfully inside the container. + """ + def new(ip, port, path \\ "/", timeout \\ 5000), + do: %__MODULE__{ip: ip, port: port, path: path, timeout: timeout} +end + +defimpl TestcontainersElixir.WaitStrategy, for: TestcontainersElixir.WaitStrategy.HttpWaitStrategy do + alias TestcontainersElixir.Container + alias TestcontainersElixir.Docker + + @impl true + def wait_until_container_is_ready(wait_strategy, id_or_name) do + with {:ok, %Container{} = container} <- Docker.Api.get_container(id_or_name) do + host_port = Container.mapped_port(container, wait_strategy.port) + + case wait_for_http(wait_strategy.ip, host_port, wait_strategy.path, wait_strategy.timeout) do + {:ok, :http_is_ready} -> + :ok + + other -> + IO.inspect(other) + :timer.sleep(100) + wait_until_container_is_ready(wait_strategy, id_or_name) + end + end + end + + defp wait_for_http(ip, port, path, timeout) + when is_binary(ip) and is_integer(port) and is_binary(path) and is_integer(timeout) do + wait_for_http(ip, port, path, timeout, :os.system_time(:millisecond)) + end + + defp wait_for_http(ip, port, path, timeout, start_time) + when is_binary(ip) and is_integer(port) and is_binary(path) and is_integer(timeout) and + is_integer(start_time) do + if timeout + start_time < :os.system_time(:millisecond) do + {:error, :timeout} + else + case http_request(ip, port, path) do + {:ok, _response} -> + {:ok, :http_is_ready} + + {:error, _reason} -> + # Sleep for 500 ms, then retry + :timer.sleep(500) + wait_for_http(ip, port, path, timeout, start_time) + end + end + end + + defp http_request(ip, port, path) do + url = "http://" <> ip <> ":" <> Integer.to_string(port) <> path + + case :httpc.request(:get, {to_charlist(url), []}, [], []) do + {:ok, {{~c"HTTP/1.1", 200, _reason_phrase}, _headers, _body}} -> + {:ok, :http_ok} + + {:ok, {{~c"HTTP/1.1", status_code, _reason_phrase}, _headers, _body}} + when status_code != 200 -> + {:error, {:unexpected_status_code, status_code}} + + {:error, reason} -> + {:error, reason} + end + end +end diff --git a/lib/wait_strategy/log_wait_strategy.ex b/lib/wait_strategy/log_wait_strategy.ex new file mode 100644 index 0000000..29f475b --- /dev/null +++ b/lib/wait_strategy/log_wait_strategy.ex @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: MIT +defmodule TestcontainersElixir.WaitStrategy.LogWaitStrategy do + @moduledoc """ + Considers container as ready as soon as a command runs successfully inside the container. + """ + defstruct [:log_regex, :timeout] + + @doc """ + Creates a new CommandWaitStrategy to wait until the given command executes successfully inside the container. + """ + def new(log_regex, timeout \\ 5000), + do: %__MODULE__{log_regex: log_regex, timeout: timeout} +end + +defimpl TestcontainersElixir.WaitStrategy, for: TestcontainersElixir.WaitStrategy.LogWaitStrategy do + alias TestcontainersElixir.Docker + + @impl true + def wait_until_container_is_ready(wait_strategy, id_or_name) do + case wait_for_log(id_or_name, wait_strategy.log_regex, wait_strategy.timeout) do + {:ok, :log_is_ready} -> + :ok + + _ -> + :timer.sleep(100) + wait_until_container_is_ready(wait_strategy, id_or_name) + end + end + + def wait_for_log(container_id, log_regex, timeout) + when is_binary(container_id) and is_integer(timeout) do + wait_for_log( + container_id, + log_regex, + timeout, + :os.system_time(:millisecond) + ) + end + + defp wait_for_log(container_id, log_regex, timeout, start_time) + when is_binary(container_id) and is_integer(timeout) and is_integer(start_time) do + if timeout + start_time < :os.system_time(:millisecond) do + {:error, :timeout} + else + if log_comparison(container_id, log_regex) do + {:ok, :log_is_ready} + else + # Sleep for 500 ms, then retry + :timer.sleep(500) + wait_for_log(container_id, log_regex, timeout, start_time) + end + end + end + + defp log_comparison(container_id, log_regex) do + with {:ok, %Tesla.Env{body: stdout_log}} when is_binary(stdout_log) <- + Docker.Api.stdout_logs(container_id) do + Regex.match?(log_regex, stdout_log) + else + _ -> + false + end + end +end diff --git a/lib/port_checker.ex b/lib/wait_strategy/port_checker.ex similarity index 100% rename from lib/port_checker.ex rename to lib/wait_strategy/port_checker.ex diff --git a/test/ceph_container_test.exs b/test/ceph_container_test.exs index 64fe2fb..65e8aa4 100644 --- a/test/ceph_container_test.exs +++ b/test/ceph_container_test.exs @@ -3,8 +3,7 @@ defmodule CephContainerTest do import TestcontainersElixir.ExUnit alias TestcontainersElixir.Container - alias TestcontainersElixir.CephContainer - alias TestcontainersElixir.Container + alias TestcontainersElixir.Container.CephContainer @tag timeout: 300_000 diff --git a/test/httpd_test.exs b/test/httpd_test.exs index c54eb1c..3ef4a7e 100644 --- a/test/httpd_test.exs +++ b/test/httpd_test.exs @@ -2,8 +2,9 @@ defmodule AnotherTest do use ExUnit.Case, async: true import TestcontainersElixir.ExUnit - alias TestcontainersElixir.HttpChecker + alias TestcontainersElixir.Container + alias TestcontainersElixir.WaitStrategy.HttpWaitStrategy test "creates and uses container" do exposed_port = 80 @@ -11,14 +12,9 @@ defmodule AnotherTest do {:ok, container} = Container.new("httpd:latest") |> Container.with_exposed_port(exposed_port) - |> Container.with_waiting_strategy(fn container -> - HttpChecker.wait_for_http( - "127.0.0.1", - Container.mapped_port(container, exposed_port), - "/", - 5000 - ) - end) + |> Container.with_waiting_strategy( + HttpWaitStrategy.new("127.0.0.1", exposed_port, "/", 10000) + ) |> run_container() host_port = Container.mapped_port(container, 80) diff --git a/test/nginx_test.exs b/test/nginx_test.exs index f81e518..b13c6ff 100644 --- a/test/nginx_test.exs +++ b/test/nginx_test.exs @@ -2,8 +2,8 @@ defmodule SimpleTest do use ExUnit.Case, async: true import TestcontainersElixir.ExUnit - alias TestcontainersElixir.Container - alias TestcontainersElixir.HttpChecker + + alias TestcontainersElixir.WaitStrategy.HttpWaitStrategy alias TestcontainersElixir.Container test "creates and uses container" do @@ -12,14 +12,9 @@ defmodule SimpleTest do {:ok, container} = Container.new("nginx:latest") |> Container.with_exposed_port(exposed_port) - |> Container.with_waiting_strategy(fn container -> - HttpChecker.wait_for_http( - "127.0.0.1", - Container.mapped_port(container, exposed_port), - "/", - 5000 - ) - end) + |> Container.with_waiting_strategy( + HttpWaitStrategy.new("127.0.0.1", exposed_port, "/", 10000) + ) |> run_container() host_port = Container.mapped_port(container, exposed_port)