Skip to content

Commit

Permalink
reorganize and improve error logging for docker host strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 16, 2024
1 parent ae8a358 commit d8435d8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 23 deletions.
3 changes: 1 addition & 2 deletions lib/connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ defmodule Testcontainers.Connection do
defp get_docker_host do
strategies = [
%DockerHostFromPropertiesStrategy{key: "tc.host"},
%DockerHostFromEnvStrategy{},
%DockerSocketPathStrategy{socket_paths: ["/var/run/docker.sock"]},
%DockerHostFromPropertiesStrategy{key: "docker.host"},
%DockerHostFromEnvStrategy{},
%DockerSocketPathStrategy{}
]

Expand Down
6 changes: 3 additions & 3 deletions lib/connection/docker_host_strategy/docker_host_from_env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ defmodule Testcontainers.DockerHostFromEnvStrategy do
{:ok, docker_host}

{:error, reason} ->
{:error, docker_host_from_env: reason}
{:error, docker_host_from_env: {reason, strategy.key}}
end
end
end

defp get_docker_host(strategy) do
case System.get_env(strategy.key) do
nil ->
{:error, docker_host_from_env: :docker_host_not_found}
{:error, docker_host_from_env: {:docker_host_not_found, strategy.key}}

"" ->
{:error, docker_host_from_env: :docker_host_empty}
{:error, docker_host_from_env: {:docker_host_empty, strategy.key}}

docker_host when is_binary(docker_host) ->
{:ok, docker_host}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ defmodule Testcontainers.DockerHostFromPropertiesStrategy do
def execute(strategy, _input) do
with {:ok, properties} <- PropertiesParser.read_property_file(strategy.filename),
docker_host <- Map.fetch(properties, strategy.key),
do: handle_docker_host(docker_host)
do: handle_docker_host(docker_host, strategy.key)
end

defp handle_docker_host({:ok, docker_host}) when is_binary(docker_host) do
defp handle_docker_host({:ok, docker_host}, key) when is_binary(docker_host) do
case DockerUrl.test_docker_host(docker_host) do
:ok ->
{:ok, docker_host}

{:error, reason} ->
{:error, testcontainer_host_from_properties: reason}
{:error, testcontainer_host_from_properties: {reason, key}}
end
end

defp handle_docker_host(:error),
do: {:error, testcontainer_host_from_properties: :property_not_found}
defp handle_docker_host(:error, key),
do: {:error, testcontainer_host_from_properties: {:property_not_found, key}}
end
end
13 changes: 7 additions & 6 deletions lib/connection/docker_host_strategy/docker_socket_path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
defmodule Testcontainers.DockerSocketPathStrategy do
@moduledoc false

@rootless_docker_socket_paths [
System.get_env("XDG_RUNTIME_DIR"),
@docker_socket_paths [
Path.expand("~/.docker/run/docker.sock"),
Path.expand("~/.docker/desktop/docker.sock"),
"/run/user/#{:os.getpid()}/docker.sock"
"/run/user/#{:os.getpid()}/podman/podman.sock",
"/run/user/#{:os.getpid()}/docker.sock",
"/var/run/docker.sock"
]

defstruct socket_paths: @rootless_docker_socket_paths
defstruct socket_paths: @docker_socket_paths

defimpl Testcontainers.DockerHostStrategy do
alias Testcontainers.DockerUrl
Expand All @@ -27,10 +28,10 @@ defmodule Testcontainers.DockerSocketPathStrategy do
{:halt, {:ok, path_with_scheme}}

{:error, reason} ->
{:cont, {:error, docker_socket_path: reason}}
{:cont, {:error, docker_socket_path: {reason, path}}}
end
else
{:cont, {:error, docker_socket_path: :docker_socket_not_found}}
{:cont, {:error, docker_socket_path: {:docker_socket_not_found, path}}}
end
end
)
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]}"} =
"Failed to find docker host. Errors: {:error, [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]}"} =
"Failed to find docker host. Errors: {:error, [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]}"} =
"Failed to find docker host. Errors: {:error, [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]}"} =
"Failed to find docker host. Errors: {:error, [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]}"} =
"Failed to find docker host. Errors: {:error, [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 @@ -8,15 +8,16 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerSocketPathTest do
test "should return :enoent if docker socket exists but is not a real socket" do
strategy = %DockerSocketPathStrategy{socket_paths: ["test/fixtures/docker.sock"]}

{:error, "Failed to find docker host. Errors: {:error, [docker_socket_path: :enoent]}"} =
{:error,
"Failed to find docker host. Errors: {:error, [docker_socket_path: {:enoent, \"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]}"} =
"Failed to find docker host. Errors: {:error, [docker_socket_path: {:docker_socket_not_found, \"/does/not/exist/at/all\"}]}"} =
DockerHostStrategyEvaluator.run_strategies([strategy], [])
end
end
Expand Down

0 comments on commit d8435d8

Please sign in to comment.