summaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-08-30 12:42:13 +0200
committerMartin Polden <mpolden@mpolden.no>2022-08-30 12:42:13 +0200
commitdcebb3d6d0cc8bd0f0b301d992dfd7764e6f74c8 (patch)
treee751ec3d4bcdf4e642b9375d1de2b75d342191ff /client
parentc7bcf3cb739b2a9665212da031cf45cb201d8126 (diff)
Avoid doing real DNS lookups in unit test
Diffstat (limited to 'client')
-rw-r--r--client/go/vespa/detect_hostname.go24
-rw-r--r--client/go/vespa/detect_hostname_test.go19
2 files changed, 27 insertions, 16 deletions
diff --git a/client/go/vespa/detect_hostname.go b/client/go/vespa/detect_hostname.go
index 80f5df2c866..d4d34a5f47d 100644
--- a/client/go/vespa/detect_hostname.go
+++ b/client/go/vespa/detect_hostname.go
@@ -10,6 +10,9 @@ import (
"strings"
)
+type lookupAddrFunc func(addr string) ([]string, error)
+type lookupIPFunc func(host string) ([]net.IP, error)
+
// detect if this host is IPv6-only, in which case we want to pass
// the flag "-Djava.net.preferIPv6Addresses=true" to any java command
func HasOnlyIpV6() bool {
@@ -42,7 +45,9 @@ func HasOnlyIpV6() bool {
// If automatic detection fails, "localhost" will be returned, so
// single-node setups still have a good chance of working.
// Use the enviroment variable VESPA_HOSTNAME to override.
-func FindOurHostname() (string, error) {
+func FindOurHostname() (string, error) { return findOurHostname(net.LookupAddr, net.LookupIP) }
+
+func findOurHostname(lookupAddr lookupAddrFunc, lookupIP lookupIPFunc) (string, error) {
env := os.Getenv("VESPA_HOSTNAME")
if env != "" {
// assumes: env var is already validated and OK
@@ -50,13 +55,10 @@ func FindOurHostname() (string, error) {
}
name, err := os.Hostname()
if err != nil {
- return findOurHostnameFrom("localhost")
- }
- name, err = findOurHostnameFrom(name)
- if strings.HasSuffix(name, ".") {
- name = name[:len(name)-1]
+ return findOurHostnameFrom("localhost", lookupAddr, lookupIP)
}
- return name, err
+ name, err = findOurHostnameFrom(name, lookupAddr, lookupIP)
+ return strings.TrimSuffix(name, "."), err
}
func validateHostname(name string) bool {
@@ -84,20 +86,20 @@ func validateHostname(name string) bool {
return someGood
}
-func findOurHostnameFrom(name string) (string, error) {
+func findOurHostnameFrom(name string, lookupAddr lookupAddrFunc, lookupIP lookupIPFunc) (string, error) {
if strings.Contains(name, ".") && validateHostname(name) {
// it's all good
return name, nil
}
possibles := make([]string, 0, 5)
if name != "" {
- ipAddrs, _ := net.LookupIP(name)
+ ipAddrs, _ := lookupIP(name)
for _, addr := range ipAddrs {
switch {
case addr.IsLoopback():
// skip
case addr.To4() != nil || addr.To16() != nil:
- reverseNames, _ := net.LookupAddr(addr.String())
+ reverseNames, _ := lookupAddr(addr.String())
possibles = append(possibles, reverseNames...)
}
}
@@ -109,7 +111,7 @@ func findOurHostnameFrom(name string) (string, error) {
if ip == nil || ip.IsLoopback() {
continue
}
- reverseNames, _ := net.LookupAddr(ip.String())
+ reverseNames, _ := lookupAddr(ip.String())
possibles = append(possibles, reverseNames...)
}
}
diff --git a/client/go/vespa/detect_hostname_test.go b/client/go/vespa/detect_hostname_test.go
index 398626b708f..e162bcdea8e 100644
--- a/client/go/vespa/detect_hostname_test.go
+++ b/client/go/vespa/detect_hostname_test.go
@@ -3,6 +3,7 @@ package vespa
import (
"fmt"
+ "net"
"os"
"strings"
"testing"
@@ -11,20 +12,28 @@ import (
)
func TestDetectHostname(t *testing.T) {
+ lookupAddr := func(addr string) ([]string, error) {
+ return nil, fmt.Errorf("could not look up %s", addr)
+ }
+ lookupIP := func(host string) ([]net.IP, error) {
+ return nil, fmt.Errorf("no address found for %s", host)
+ }
+
t.Setenv("VESPA_HOSTNAME", "foo.bar")
- got, err := FindOurHostname()
+ got, err := findOurHostname(lookupAddr, lookupIP)
+ assert.Nil(t, err)
assert.Equal(t, "foo.bar", got)
os.Unsetenv("VESPA_HOSTNAME")
- got, err = findOurHostnameFrom("bar.foo.123")
+ got, err = findOurHostnameFrom("bar.foo.123", lookupAddr, lookupIP)
fmt.Fprintln(os.Stderr, "findOurHostname from bar.foo.123 returns:", got, "with error:", err)
assert.NotEqual(t, "", got)
parts := strings.Split(got, ".")
if len(parts) > 1 {
- expanded, err2 := findOurHostnameFrom(parts[0])
+ expanded, err2 := findOurHostnameFrom(parts[0], lookupAddr, lookupIP)
fmt.Fprintln(os.Stderr, "findOurHostname from", parts[0], "returns:", expanded, "with error:", err2)
assert.Equal(t, got, expanded)
}
- got, err = FindOurHostname()
+ got, err = findOurHostname(lookupAddr, lookupIP)
assert.NotEqual(t, "", got)
- fmt.Fprintln(os.Stderr, "FindOurHostname() returns:", got, "with error:", err)
+ fmt.Fprintln(os.Stderr, "findOurHostname() returns:", got, "with error:", err)
}