test: isolate endpoint locality fixtures from public DNS

Signed-off-by: Feng Ruohang <rh@vonng.com>
This commit is contained in:
Feng Ruohang
2026-10-25 00:50:41 +08:00
parent 0b1c21cb4b
commit 03fe5e9c21
2 changed files with 27 additions and 0 deletions
+26
View File
@@ -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")
+1
View File
@@ -186,6 +186,7 @@ func TestGetAPIEndpoints(t *testing.T) {
}
func TestCheckLocalServerAddr(t *testing.T) {
useTestEndpointDNS(t)
testCases := []struct {
serverAddr string
expectedErr error