From fa548c5b960ce0a264505cdd3347d95bc8028ebd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 31 May 2021 09:00:50 -0700 Subject: [PATCH] tstest/integration/vms: fix bindhost lookup (#2012) Don't try to do heuristics on the name. Use the net/interfaces package which we already have to do this sort of stuff. Fixes #2011 Signed-off-by: Brad Fitzpatrick --- tstest/integration/vms/vms_test.go | 35 ++++++++++++------------------ 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/tstest/integration/vms/vms_test.go b/tstest/integration/vms/vms_test.go index 36a209302..fe13dfaca 100644 --- a/tstest/integration/vms/vms_test.go +++ b/tstest/integration/vms/vms_test.go @@ -31,6 +31,7 @@ import ( "github.com/pkg/sftp" "golang.org/x/crypto/ssh" "inet.af/netaddr" + "tailscale.com/net/interfaces" "tailscale.com/tstest" "tailscale.com/tstest/integration" "tailscale.com/tstest/integration/testcontrol" @@ -614,37 +615,29 @@ func copyFile(t *testing.T, cli *sftp.Client, localSrc, remoteDest string) { func deriveBindhost(t *testing.T) string { t.Helper() - ifaces, err := net.Interfaces() + ifName, err := interfaces.DefaultRouteInterface() if err != nil { t.Fatal(err) } - rex := regexp.MustCompile(`^(eth|enp|wlp|wlan)`) - - for _, iface := range ifaces { - t.Logf("found interface %s: %d", iface.Name, iface.Flags&net.FlagUp) - if (iface.Flags & net.FlagUp) == 0 { - continue - } - - if rex.MatchString(iface.Name) { - addrs, err := iface.Addrs() - if err != nil { - t.Fatalf("can't get address for %s: %v", iface.Name, err) - } - - for _, addr := range addrs { - return netaddr.MustParseIPPrefix(addr.String()).IP().String() - } + var ret string + err = interfaces.ForeachInterfaceAddress(func(i interfaces.Interface, prefix netaddr.IPPrefix) { + if ret != "" || i.Name != ifName { + return } + ret = prefix.IP().String() + }) + if ret != "" { + return ret + } + if err != nil { + t.Fatal(err) } - t.Fatal("can't find a bindhost") - return "invalid" + return "unreachable" } func TestDeriveBindhost(t *testing.T) { - t.Skip("broken on some machines; https://github.com/tailscale/tailscale/issues/2011") t.Log(deriveBindhost(t)) }