Skip to content

Commit

Permalink
increased readability for docker host errors and ryuk config
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 16, 2024
1 parent d8435d8 commit 79b5ed2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
10 changes: 6 additions & 4 deletions lib/connection/docker_host_strategy/docker_socket_path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ defmodule Testcontainers.DockerSocketPathStrategy do

defimpl Testcontainers.DockerHostStrategy do
alias Testcontainers.DockerUrl
alias Testcontainers.Logger

def execute(strategy, _input) do
Enum.reduce_while(
strategy.socket_paths,
{:error, docker_socket_path: :docker_socket_not_found},
fn path, _acc ->
{:error, {:docker_socket_not_found, []}},
fn path, {:error, {:docker_socket_not_found, tried_paths}} ->
if path != nil && File.exists?(path) do
path_with_scheme = "unix://" <> path

Expand All @@ -28,10 +29,11 @@ defmodule Testcontainers.DockerSocketPathStrategy do
{:halt, {:ok, path_with_scheme}}

{:error, reason} ->
{:cont, {:error, docker_socket_path: {reason, path}}}
Logger.log("Docker socket path #{path} failed: #{reason}")
{:cont, {:error, {:docker_socket_not_found, tried_paths ++ [path]}}}
end
else
{:cont, {:error, docker_socket_path: {:docker_socket_not_found, path}}}
{:cont, {:error, {:docker_socket_not_found, tried_paths ++ [path]}}}
end
end
)
Expand Down
4 changes: 2 additions & 2 deletions lib/connection/docker_host_strategy_evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ defmodule Testcontainers.DockerHostStrategyEvaluator do
success

errors when is_list(errors) ->
{:error, "Failed to find docker host. Errors: #{format_errors(errors)}"}
{:error, "Failed to find docker host: #{format_errors(errors)}"}
end
end

defp format_errors(errors) do
errors
|> Enum.reverse()
|> Enum.map(&inspect/1)
|> Enum.map(fn {:error, error} -> inspect(error) end)
|> Enum.join(", ")
end
end
11 changes: 4 additions & 7 deletions lib/testcontainers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,17 @@ defmodule Testcontainers do
ryuk_config =
Container.new("testcontainers/ryuk:#{Constants.ryuk_version()}")
|> Container.with_exposed_port(8080)
|> Container.with_environment("RYUK_PORT", "8080")
|> then(fn config ->
# docker_host can be anything from an url like https://localhost:1234 to unix:///etc/....
# if its a unix socket, strip the prefix and use the unix socket as the bind mount for Ryuks unix socket
# this enables ryuk to communicate with docker environment it runs in, to stop and remove containers
if String.starts_with?(docker_host, "unix://") do
with %URI{scheme: "unix", path: docker_socket_path} <- URI.parse(docker_host) do
Container.with_bind_mount(
config,
String.replace_prefix(docker_host, "unix://", ""),
docker_socket_path,
"/var/run/docker.sock",
"rw"
)
else
config
_ ->
config
end
end)
|> Container.with_auto_remove(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerHostFromEnvTest do
strategy = %DockerHostFromEnvStrategy{key: "X_DOCKER_HOST"}

{:error,
"Failed to find docker host. Errors: {:error, [docker_host_from_env: {:econnrefused, \"X_DOCKER_HOST\"}]}"} =
"Failed to find docker host: [docker_host_from_env: {:econnrefused, \"X_DOCKER_HOST\"}]"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end

test "should return error if env is not set to a proper url" do
strategy = %DockerHostFromEnvStrategy{key: "NOT_SET"}

{:error,
"Failed to find docker host. Errors: {:error, [docker_host_from_env: {:docker_host_not_found, \"NOT_SET\"}]}"} =
"Failed to find docker host: [docker_host_from_env: {:docker_host_not_found, \"NOT_SET\"}]"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerHostFromPropertiesT
}

{:error,
"Failed to find docker host. Errors: {:error, [testcontainer_host_from_properties: {:econnrefused, \"tc.host\"}]}"} =
"Failed to find docker host: [testcontainer_host_from_properties: {:econnrefused, \"tc.host\"}]"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end

Expand All @@ -27,7 +27,7 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerHostFromPropertiesT
}

{:error,
"Failed to find docker host. Errors: {:error, [testcontainer_host_from_properties: {:property_not_found, \"tc.host\"}]}"} =
"Failed to find docker host: [testcontainer_host_from_properties: {:property_not_found, \"tc.host\"}]"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end

Expand All @@ -40,7 +40,7 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerHostFromPropertiesT
}

{:error,
"Failed to find docker host. Errors: {:error, [testcontainer_host_from_properties: {:property_not_found, \"invalid.host\"}]}"} =
"Failed to find docker host: [testcontainer_host_from_properties: {:property_not_found, \"invalid.host\"}]"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerSocketPathTest do
strategy = %DockerSocketPathStrategy{socket_paths: ["test/fixtures/docker.sock"]}

{:error,
"Failed to find docker host. Errors: {:error, [docker_socket_path: {:enoent, \"test/fixtures/docker.sock\"}]}"} =
"Failed to find docker host: {:docker_socket_not_found, [\"test/fixtures/docker.sock\"]}"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end

test "should return error if docker socket does not exist" do
strategy = %DockerSocketPathStrategy{socket_paths: ["/does/not/exist/at/all"]}

{:error,
"Failed to find docker host. Errors: {:error, [docker_socket_path: {:docker_socket_not_found, \"/does/not/exist/at/all\"}]}"} =
"Failed to find docker host: {:docker_socket_not_found, [\"/does/not/exist/at/all\"]}"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end
end
Expand Down

0 comments on commit 79b5ed2

Please sign in to comment.