Skip to content

Commit aceb6a7

Browse files
committed
refactor: enhance error reporting for Docker host connection failures
1 parent e938881 commit aceb6a7

10 files changed

Lines changed: 34 additions & 18 deletions

File tree

lib/connection/docker_host_strategy/docker_host_from_env.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ defmodule Testcontainers.DockerHostFromEnvStrategy do
1414
{:ok, docker_host}
1515

1616
{:error, reason} ->
17-
{:error, docker_host_from_env: {reason, strategy.key}}
17+
{:error,
18+
docker_host_from_env:
19+
{:ping_failed, key: strategy.key, value: docker_host, reason: reason}}
1820
end
1921
end
2022
end

lib/connection/docker_host_strategy/docker_host_from_properties.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ defmodule Testcontainers.DockerHostFromPropertiesStrategy do
2020
{:ok, docker_host}
2121

2222
{:error, reason} ->
23-
{:error, testcontainer_host_from_properties: {reason, key}}
23+
{:error,
24+
testcontainer_host_from_properties:
25+
{:ping_failed, key: key, value: docker_host, reason: reason}}
2426
end
2527
end
2628

lib/testcontainers.ex

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -771,12 +771,17 @@ defmodule Testcontainers do
771771
end
772772

773773
defp try_tcp_connect(host, port) do
774-
:gen_tcp.connect(~c"#{host}", port, [
775-
:binary,
776-
active: false,
777-
packet: :line,
778-
send_timeout: 10_000
779-
], 5000)
774+
:gen_tcp.connect(
775+
~c"#{host}",
776+
port,
777+
[
778+
:binary,
779+
active: false,
780+
packet: :line,
781+
send_timeout: 10_000
782+
],
783+
5000
784+
)
780785
end
781786

782787
defp try_container_internal_connect(%Container{ip_address: ip}, internal_port, original_reason)

test/connection/docker_host_strategy/docker_host_from_env_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerHostFromEnvTest do
1313
strategy = %DockerHostFromEnvStrategy{key: "X_DOCKER_HOST"}
1414

1515
{:error,
16-
"Failed to find docker host: [docker_host_from_env: {:econnrefused, \"X_DOCKER_HOST\"}]"} =
16+
"Failed to find docker host: [docker_host_from_env: {:ping_failed, [key: \"X_DOCKER_HOST\", value: \"tcp://localhost:9999\", reason: :econnrefused]}]"} =
1717
DockerHostStrategyEvaluator.run_strategies([strategy], [])
1818
end
1919

test/connection/docker_host_strategy/docker_host_from_properties_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule Testcontainers.Connection.DockerHostStrategy.DockerHostFromPropertiesT
1414
}
1515

1616
{:error,
17-
"Failed to find docker host: [testcontainer_host_from_properties: {:econnrefused, \"tc.host\"}]"} =
17+
"Failed to find docker host: [testcontainer_host_from_properties: {:ping_failed, [key: \"tc.host\", value: \"tcp://localhost:9999\", reason: :econnrefused]}]"} =
1818
DockerHostStrategyEvaluator.run_strategies([strategy], [])
1919
end
2020

test/constants_test.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ defmodule Testcontainers.ConstantsTest do
33

44
test "have correct values" do
55
assert Testcontainers.Constants.container_label() == "org.testcontainers"
6-
assert Testcontainers.Constants.container_session_id_label() == "org.testcontainers.session-id"
6+
7+
assert Testcontainers.Constants.container_session_id_label() ==
8+
"org.testcontainers.session-id"
79

810
assert Testcontainers.Constants.container_reuse_hash_label() ==
911
"org.testcontainers.reuse-hash"

test/docker/auth_test.exs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,18 @@ defmodule Testcontainers.Docker.AuthTest do
7171
end
7272

7373
test "returns nil when the config file is missing" do
74-
missing = Path.join(System.tmp_dir!(), "testcontainers-missing-#{System.unique_integer()}.json")
74+
missing =
75+
Path.join(System.tmp_dir!(), "testcontainers-missing-#{System.unique_integer()}.json")
76+
7577
refute File.exists?(missing)
7678

7779
assert Auth.resolve("library/redis:7", missing) == nil
7880
end
7981

8082
test "returns nil when the config file is invalid JSON" do
81-
path = Path.join(System.tmp_dir!(), "testcontainers-invalid-#{System.unique_integer()}.json")
83+
path =
84+
Path.join(System.tmp_dir!(), "testcontainers-invalid-#{System.unique_integer()}.json")
85+
8286
File.write!(path, "this is not json")
8387

8488
try do

test/testcontainers_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ defmodule TestcontainersTest do
133133
{:ok, container} = Testcontainers.start_container(config, :name_test)
134134

135135
conn = Connection.get_connection() |> Tuple.to_list() |> Kernel.hd()
136+
136137
{:ok, %DockerEngineAPI.Model.ContainerInspectResponse{Name: assigned_name}} =
137138
DockerEngineAPI.Api.Container.container_inspect(conn, container.container_id)
138139

test/util/properties_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ defmodule Testcontainers.Util.PropertiesParserTest do
5555

5656
describe "read_property_file/1" do
5757
test "reads properties from specified file" do
58-
{:ok, props} = PropertiesParser.read_property_file("test/fixtures/.testcontainers.properties")
58+
{:ok, props} =
59+
PropertiesParser.read_property_file("test/fixtures/.testcontainers.properties")
5960

6061
assert is_map(props)
6162
assert props["tc.host"] == "tcp://localhost:9999"
6263
end
6364

6465
test "returns empty map for nonexistent file" do
65-
{:ok, props} = PropertiesParser.read_property_file("/nonexistent/path/.testcontainers.properties")
66+
{:ok, props} =
67+
PropertiesParser.read_property_file("/nonexistent/path/.testcontainers.properties")
6668

6769
assert props == %{}
6870
end

test/wait_strategy/http_wait_strategy_test.exs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ defmodule Testcontainers.HttpWaitStrategyTest do
2828
config =
2929
%Container{image: "nginx:alpine"}
3030
|> Container.with_exposed_port(port)
31-
|> Container.with_waiting_strategy(
32-
HttpWaitStrategy.new("/", port, status_code: 200)
33-
)
31+
|> Container.with_waiting_strategy(HttpWaitStrategy.new("/", port, status_code: 200))
3432

3533
assert {:ok, container} = Testcontainers.start_container(config)
3634
assert :ok = Testcontainers.stop_container(container.container_id)

0 commit comments

Comments
 (0)