From 03fe5e9c21dd81b80a1cc313ce292db1060b08ef Mon Sep 17 00:00:00 2001 From: Feng Ruohang Date: Sun, 25 Oct 2026 00:50:41 +0800 Subject: [PATCH] test: isolate endpoint locality fixtures from public DNS Signed-off-by: Feng Ruohang --- cmd/endpoint_test.go | 26 ++++++++++++++++++++++++++ cmd/net_test.go | 1 + 2 files changed, 27 insertions(+) diff --git a/cmd/endpoint_test.go b/cmd/endpoint_test.go index 98b610168..4a149ef91 100644 --- a/cmd/endpoint_test.go +++ b/cmd/endpoint_test.go @@ -18,6 +18,7 @@ package cmd import ( + "context" "fmt" "net" "net/url" @@ -25,10 +26,34 @@ import ( "reflect" "strings" "testing" + + "github.com/minio/dnscache" ) +type endpointTestResolver struct { + dnscache.DNSResolver +} + +func (r endpointTestResolver) LookupHost(ctx context.Context, host string) ([]string, error) { + // These endpoint fixtures test locality and parsing, not public DNS. + for i, name := range []string{"example.org", "example.com", "example.net", "example.edu"} { + if host == name { + return []string{fmt.Sprintf("203.0.113.%d", i+1)}, nil + } + } + return r.DNSResolver.LookupHost(ctx, host) +} + +func useTestEndpointDNS(t *testing.T) { + t.Helper() + previous := globalDNSCache + globalDNSCache = &dnscache.Resolver{Resolver: endpointTestResolver{net.DefaultResolver}} + t.Cleanup(func() { globalDNSCache = previous }) +} + func useStaticTestEndpoints(t *testing.T) { t.Helper() + useTestEndpointDNS(t) // These fixtures deliberately use localhost, including inside containers. previous := orchestrated orchestrated = false @@ -36,6 +61,7 @@ func useStaticTestEndpoints(t *testing.T) { } func TestNewEndpoint(t *testing.T) { + useTestEndpointDNS(t) u2, _ := url.Parse("https://example.org/path") u4, _ := url.Parse("http://192.168.253.200/path") rootSlashFoo, _ := filepath.Abs("/foo") diff --git a/cmd/net_test.go b/cmd/net_test.go index d482cad29..65c0d48c1 100644 --- a/cmd/net_test.go +++ b/cmd/net_test.go @@ -186,6 +186,7 @@ func TestGetAPIEndpoints(t *testing.T) { } func TestCheckLocalServerAddr(t *testing.T) { + useTestEndpointDNS(t) testCases := []struct { serverAddr string expectedErr error