Skip to content

Commit 693b7ac

Browse files
committed
base: make TestValidateAddrs work without a network connection
Looking up an invalid host name may result in a different error depending on whether the test has access to a network connection or not. This patch modifies the test to catch that. Fixes: #109052 Release note: None
1 parent 407f33e commit 693b7ac

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/base/addr_validation_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,17 @@ func TestValidateAddrs(t *testing.T) {
6464
t.Fatal("expected port resolution failure, got no error")
6565
}
6666
portExpectedErr := err.Error()
67-
// For the host name resolution error we can reliably expect "no such host"
68-
// below, but before we test anything we need to ensure we indeed have
69-
// a reliably non-resolvable host name.
70-
_, err = net.DefaultResolver.LookupIPAddr(context.Background(), "nonexistent.example.com")
71-
if err == nil {
72-
t.Fatal("expected host resolution failure, got no error")
67+
// ValidateAddrs uses a net.DefaultResolver for host name resolution.
68+
// Normally, when given a non-existent host name, we expect it to return a
69+
// "no such host" error. However, it may return a different error if there is
70+
// no network access. See
71+
// https://github.com/cockroachdb/cockroach/issues/109052.
72+
noSuchHostErr := func(host string) string {
73+
_, err := net.DefaultResolver.LookupIPAddr(context.Background(), host)
74+
if err == nil { // the supplied host is expected to be reliably non-resolvable
75+
t.Fatal("expected host resolution failure, got no error")
76+
}
77+
return err.Error()
7378
}
7479

7580
// The test cases.
@@ -149,8 +154,8 @@ func TestValidateAddrs(t *testing.T) {
149154
{addrs{"localhost:-1231", "", "", "", "", ""}, "invalid port", addrs{}},
150155
{addrs{"localhost:nonexistent", "", "", "", "", ""}, portExpectedErr, addrs{}},
151156
// Invalid address.
152-
{addrs{"nonexistent.example.com:26257", "", "", "", "", ""}, "no such host", addrs{}},
153-
{addrs{"333.333.333.333:26257", "", "", "", "", ""}, "no such host", addrs{}},
157+
{addrs{"nonexistent.example.com:26257", "", "", "", "", ""}, noSuchHostErr("nonexistent.example.com"), addrs{}},
158+
{addrs{"333.333.333.333:26257", "", "", "", "", ""}, noSuchHostErr("333.333.333.333"), addrs{}},
154159
}
155160

156161
for i, test := range testData {

0 commit comments

Comments
 (0)