From 97a909866d9b768c1e8247b02e3efad1452da9c4 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 2 Feb 2023 10:14:33 +0100 Subject: [PATCH] Use pingAll helper for all integration pinging Signed-off-by: Kristoffer Dalby --- integration/auth_oidc_test.go | 31 ++++------- integration/auth_web_flow_test.go | 53 +++++++------------ integration/general_test.go | 85 ++++++++++++++----------------- 3 files changed, 68 insertions(+), 101 deletions(-) diff --git a/integration/auth_oidc_test.go b/integration/auth_oidc_test.go index f3ac040d..2b3dbdd9 100644 --- a/integration/auth_oidc_test.go +++ b/integration/auth_oidc_test.go @@ -19,6 +19,7 @@ import ( "github.com/juanfont/headscale/integration/hsic" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" + "github.com/samber/lo" ) const ( @@ -91,7 +92,11 @@ func TestOIDCAuthenticationPingAll(t *testing.T) { t.Errorf("failed wait for tailscale clients to be in sync: %s", err) } - success := pingAll(t, allClients, allIps) + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) err = scenario.Shutdown() @@ -157,7 +162,11 @@ func TestOIDCExpireNodesBasedOnTokenExpiry(t *testing.T) { t.Errorf("failed wait for tailscale clients to be in sync: %s", err) } - success := pingAll(t, allClients, allIps) + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d (before expiry)", success, len(allClients)*len(allIps)) // await all nodes being logged out after OIDC token expiry @@ -359,24 +368,6 @@ func (s *AuthOIDCScenario) runTailscaleUp( return fmt.Errorf("failed to up tailscale node: %w", errNoUserAvailable) } -func pingAll(t *testing.T, clients []TailscaleClient, ips []netip.Addr) int { - t.Helper() - success := 0 - - for _, client := range clients { - for _, ip := range ips { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } - - return success -} - func (s *AuthOIDCScenario) Shutdown() error { err := s.pool.Purge(s.mockOIDC) if err != nil { diff --git a/integration/auth_web_flow_test.go b/integration/auth_web_flow_test.go index df253246..d06a3d29 100644 --- a/integration/auth_web_flow_test.go +++ b/integration/auth_web_flow_test.go @@ -13,6 +13,7 @@ import ( "testing" "github.com/juanfont/headscale/integration/hsic" + "github.com/samber/lo" ) var errParseAuthPage = errors.New("failed to parse auth page") @@ -59,18 +60,11 @@ func TestAuthWebFlowAuthenticationPingAll(t *testing.T) { t.Errorf("failed wait for tailscale clients to be in sync: %s", err) } - success := 0 - for _, client := range allClients { - for _, ip := range allIps { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) err = scenario.Shutdown() @@ -117,18 +111,11 @@ func TestAuthWebFlowLogoutAndRelogin(t *testing.T) { t.Errorf("failed wait for tailscale clients to be in sync: %s", err) } - success := 0 - for _, client := range allClients { - for _, ip := range allIps { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) clientIPs := make(map[TailscaleClient][]netip.Addr) @@ -175,18 +162,11 @@ func TestAuthWebFlowLogoutAndRelogin(t *testing.T) { t.Errorf("failed to get clients: %s", err) } - success = 0 - for _, client := range allClients { - for _, ip := range allIps { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } + allAddrs = lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + success = pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) for _, client := range allClients { @@ -211,7 +191,12 @@ func TestAuthWebFlowLogoutAndRelogin(t *testing.T) { } if !found { - t.Errorf("IPs changed for client %s. Used to be %v now %v", client.Hostname(), clientIPs[client], ips) + t.Errorf( + "IPs changed for client %s. Used to be %v now %v", + client.Hostname(), + clientIPs[client], + ips, + ) } } } diff --git a/integration/general_test.go b/integration/general_test.go index 01f8f143..10a8d033 100644 --- a/integration/general_test.go +++ b/integration/general_test.go @@ -10,6 +10,7 @@ import ( "github.com/juanfont/headscale/integration/hsic" "github.com/juanfont/headscale/integration/tsic" "github.com/rs/zerolog/log" + "github.com/samber/lo" ) func TestPingAllByIP(t *testing.T) { @@ -46,19 +47,11 @@ func TestPingAllByIP(t *testing.T) { t.Errorf("failed wait for tailscale clients to be in sync: %s", err) } - success := 0 - - for _, client := range allClients { - for _, ip := range allIps { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) err = scenario.Shutdown() @@ -148,18 +141,11 @@ func TestAuthKeyLogoutAndRelogin(t *testing.T) { t.Errorf("failed to get clients: %s", err) } - success := 0 - for _, client := range allClients { - for _, ip := range allIps { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) for _, client := range allClients { @@ -184,7 +170,12 @@ func TestAuthKeyLogoutAndRelogin(t *testing.T) { } if !found { - t.Errorf("IPs changed for client %s. Used to be %v now %v", client.Hostname(), clientIPs[client], ips) + t.Errorf( + "IPs changed for client %s. Used to be %v now %v", + client.Hostname(), + clientIPs[client], + ips, + ) } } } @@ -253,18 +244,11 @@ func TestEphemeral(t *testing.T) { t.Errorf("failed to get clients: %s", err) } - success := 0 - for _, client := range allClients { - for _, ip := range allIps { - err := client.Ping(ip.String()) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err) - } else { - success++ - } - } - } + allAddrs := lo.Map(allIps, func(x netip.Addr, index int) string { + return x.String() + }) + success := pingAllHelper(t, allClients, allAddrs) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps)) for _, client := range allClients { @@ -335,18 +319,7 @@ func TestPingAllByHostname(t *testing.T) { t.Errorf("failed to get FQDNs: %s", err) } - success := 0 - - for _, client := range allClients { - for _, hostname := range allHostnames { - err := client.Ping(hostname) - if err != nil { - t.Errorf("failed to ping %s from %s: %s", hostname, client.Hostname(), err) - } else { - success++ - } - } - } + success := pingAllHelper(t, allClients, allHostnames) t.Logf("%d successful pings out of %d", success, len(allClients)*len(allClients)) @@ -582,3 +555,21 @@ func TestResolveMagicDNS(t *testing.T) { t.Errorf("failed to tear down scenario: %s", err) } } + +func pingAllHelper(t *testing.T, clients []TailscaleClient, addrs []string) int { + t.Helper() + success := 0 + + for _, client := range clients { + for _, addr := range addrs { + err := client.Ping(addr) + if err != nil { + t.Errorf("failed to ping %s from %s: %s", addr, client.Hostname(), err) + } else { + success++ + } + } + } + + return success +}