Skip to content

Commit 29b3aed

Browse files
authored
Fix flaky registry test (#2374)
* print the actual error message so we can add it to the list * helper to match common net errors
1 parent 59dc987 commit 29b3aed

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

pkg/docker/docker_client_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,7 @@ func runDockerClientTests(t *testing.T, dockerClient command.Command) {
343343
err = dockerClient.Push(t.Context(), ref.String())
344344
require.Error(t, err, "Push should fail with unreachable registry")
345345

346-
// error message varies between dev and CI host environments, cover them all...
347-
assert.Condition(t, func() bool {
348-
msg := err.Error()
349-
return strings.Contains(msg, "connection refused") || strings.Contains(msg, "EOF")
350-
}, "Error should indicate registry is unreachable")
346+
assert.True(t, isNetworkError(err), "Error should be a network error, got: %q", err.Error())
351347
})
352348

353349
t.Run("missing image", func(t *testing.T) {

pkg/docker/errors.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package docker
22

3-
import "strings"
3+
import (
4+
"errors"
5+
"strings"
6+
)
47

58
// Error messages vary between different backends (dockerd, containerd, podman, orbstack, etc) or even versions of docker.
69
// These helpers normalize the check so callers can handle situations without worrying about the underlying implementation.
@@ -47,3 +50,33 @@ func isMissingDeviceDriverError(err error) bool {
4750
return strings.Contains(msg, "could not select device driver") ||
4851
strings.Contains(msg, "nvidia-container-cli: initialization error")
4952
}
53+
54+
func isNetworkError(err error) bool {
55+
// for both CLI and API clients, network errors are wrapped and lose the net.Error interface
56+
// CLI client: wrapped by exec.Command as exec.ExitError
57+
// API client: wrapped by JSON message stream processing
58+
// Sad as it may be, we rely on string matching for common network error messages
59+
60+
msg := err.Error()
61+
networkErrorStrings := []string{
62+
"connection refused",
63+
"connection reset by peer",
64+
"dial tcp",
65+
"EOF",
66+
"no route to host",
67+
"network is unreachable",
68+
}
69+
70+
for _, errStr := range networkErrorStrings {
71+
if strings.Contains(msg, errStr) {
72+
return true
73+
}
74+
}
75+
76+
// also check wrapped errors
77+
if unwrapped := errors.Unwrap(err); unwrapped != nil {
78+
return isNetworkError(unwrapped)
79+
}
80+
81+
return false
82+
}

0 commit comments

Comments
 (0)