Skip to content

Commit

Permalink
Handle errors in integration test setups
Browse files Browse the repository at this point in the history
Thanks @kev-the-dev

Closes juanfont#1460

Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Aug 29, 2023
1 parent fb203a2 commit 55498f1
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 131 deletions.
2 changes: 1 addition & 1 deletion cmd/headscale/headscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {

zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
Out: os.Stderr,
TimeFormat: time.RFC3339,
NoColor: !colors,
})
Expand Down
43 changes: 23 additions & 20 deletions integration/auth_oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ func TestOIDCExpireNodesBasedOnTokenExpiry(t *testing.T) {
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
scenario.WaitForTailscaleLogout()
err = scenario.WaitForTailscaleLogout()
if err != nil {
t.Errorf("failed to log out tailscale nodes: %s", err)
}

err = scenario.Shutdown()
if err != nil {
Expand All @@ -188,7 +191,7 @@ func (s *AuthOIDCScenario) CreateHeadscaleEnv(
return err
}

err = headscale.WaitForReady()
err = headscale.WaitForRunning()
if err != nil {
return err
}
Expand Down Expand Up @@ -311,13 +314,9 @@ func (s *AuthOIDCScenario) runTailscaleUp(
log.Printf("running tailscale up for user %s", userStr)
if user, ok := s.users[userStr]; ok {
for _, client := range user.Clients {
user.joinWaitGroup.Add(1)

go func(c TailscaleClient) {
defer user.joinWaitGroup.Done()

// TODO(juanfont): error handle this
loginURL, err := c.UpWithLoginURL(loginServer)
c := client
user.joinWaitGroup.Go(func() error {
loginURL, err := c.LoginWithURL(loginServer)
if err != nil {
log.Printf("failed to run tailscale up: %s", err)
}
Expand All @@ -336,9 +335,14 @@ func (s *AuthOIDCScenario) runTailscaleUp(
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, loginURL.String(), nil)
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("%s failed to get login url %s: %s", c.Hostname(), loginURL, err)

return
log.Printf(
"%s failed to get login url %s: %s",
c.Hostname(),
loginURL,
err,
)

return err
}

defer resp.Body.Close()
Expand All @@ -347,24 +351,23 @@ func (s *AuthOIDCScenario) runTailscaleUp(
if err != nil {
log.Printf("%s failed to read response body: %s", c.Hostname(), err)

return
return err
}

log.Printf("Finished request for %s to join tailnet", c.Hostname())
}(client)

err = client.WaitForReady()
if err != nil {
log.Printf("error waiting for client %s to be ready: %s", client.Hostname(), err)
}
return nil
})

log.Printf("client %s is ready", client.Hostname())
}

user.joinWaitGroup.Wait()
if err := user.joinWaitGroup.Wait(); err != nil {
return err
}

for _, client := range user.Clients {
err := client.WaitForReady()
err := client.WaitForRunning()
if err != nil {
log.Printf("client %s was not ready: %s", client.Hostname(), err)

Expand Down
38 changes: 23 additions & 15 deletions integration/auth_web_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ func TestAuthWebFlowLogoutAndRelogin(t *testing.T) {
}
}

scenario.WaitForTailscaleLogout()
err = scenario.WaitForTailscaleLogout()
if err != nil {
t.Errorf("failed to log out tailscale nodes: %s", err)
}

t.Logf("all clients logged out")

Expand Down Expand Up @@ -218,7 +221,7 @@ func (s *AuthWebFlowScenario) CreateHeadscaleEnv(
return err
}

err = headscale.WaitForReady()
err = headscale.WaitForRunning()
if err != nil {
return err
}
Expand Down Expand Up @@ -250,32 +253,37 @@ func (s *AuthWebFlowScenario) runTailscaleUp(
log.Printf("running tailscale up for user %s", userStr)
if user, ok := s.users[userStr]; ok {
for _, client := range user.Clients {
user.joinWaitGroup.Add(1)

go func(c TailscaleClient) {
defer user.joinWaitGroup.Done()

// TODO(juanfont): error handle this
loginURL, err := c.UpWithLoginURL(loginServer)
c := client
user.joinWaitGroup.Go(func() error {
loginURL, err := c.LoginWithURL(loginServer)
if err != nil {
log.Printf("failed to run tailscale up: %s", err)
log.Printf("failed to run tailscale up (%s): %s", c.Hostname(), err)

return err
}

err = s.runHeadscaleRegister(userStr, loginURL)
if err != nil {
log.Printf("failed to register client: %s", err)
log.Printf("failed to register client (%s): %s", c.Hostname(), err)

return err
}
}(client)

err := client.WaitForReady()
return nil
})

err := client.WaitForRunning()
if err != nil {
log.Printf("error waiting for client %s to be ready: %s", client.Hostname(), err)
}
}
user.joinWaitGroup.Wait()

if err := user.joinWaitGroup.Wait(); err != nil {
return err
}

for _, client := range user.Clients {
err := client.WaitForReady()
err := client.WaitForRunning()
if err != nil {
log.Printf("client %s was not ready: %s", client.Hostname(), err)

Expand Down
2 changes: 1 addition & 1 deletion integration/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ControlServer interface {
ConnectToNetwork(network *dockertest.Network) error
GetHealthEndpoint() string
GetEndpoint() string
WaitForReady() error
WaitForRunning() error
CreateUser(user string) error
CreateAuthKey(user string, reusable bool, ephemeral bool) (*v1.PreAuthKey, error)
ListMachinesInUser(user string) ([]*v1.Machine, error)
Expand Down
27 changes: 12 additions & 15 deletions integration/embedded_derp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package integration

import (
"fmt"
"log"
"net/url"
"testing"

Expand Down Expand Up @@ -105,7 +104,7 @@ func (s *EmbeddedDERPServerScenario) CreateHeadscaleEnv(

headscaleURL.Host = fmt.Sprintf("%s:%s", hsServer.GetHostname(), headscaleURL.Port())

err = hsServer.WaitForReady()
err = hsServer.WaitForRunning()
if err != nil {
return err
}
Expand Down Expand Up @@ -186,37 +185,35 @@ func (s *EmbeddedDERPServerScenario) CreateTailscaleIsolatedNodesInUser(

cert := hsServer.GetCert()

user.createWaitGroup.Add(1)

opts = append(opts,
tsic.WithHeadscaleTLS(cert),
)

go func() {
defer user.createWaitGroup.Done()

// TODO(kradalby): error handle this
user.createWaitGroup.Go(func() error {
tsClient, err := tsic.New(
s.pool,
version,
network,
opts...,
)
if err != nil {
// return fmt.Errorf("failed to add tailscale node: %w", err)
log.Printf("failed to create tailscale node: %s", err)
return fmt.Errorf("failed to create tailscale node: %w", err)
}

err = tsClient.WaitForReady()
err = tsClient.WaitForRunning()
if err != nil {
// return fmt.Errorf("failed to add tailscale node: %w", err)
log.Printf("failed to wait for tailscaled: %s", err)
return fmt.Errorf("failed to wait for tailscaled to need login: %w", err)
}

user.Clients[tsClient.Hostname()] = tsClient
}()

return nil
})
}

if err := user.createWaitGroup.Wait(); err != nil {
return err
}
user.createWaitGroup.Wait()

return nil
}
Expand Down
10 changes: 8 additions & 2 deletions integration/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ func TestAuthKeyLogoutAndRelogin(t *testing.T) {
}
}

scenario.WaitForTailscaleLogout()
err = scenario.WaitForTailscaleLogout()
if err != nil {
t.Errorf("failed to log out tailscale nodes: %s", err)
}

t.Logf("all clients logged out")

Expand Down Expand Up @@ -261,7 +264,10 @@ func TestEphemeral(t *testing.T) {
}
}

scenario.WaitForTailscaleLogout()
err = scenario.WaitForTailscaleLogout()
if err != nil {
t.Errorf("failed to log out tailscale nodes: %s", err)
}

t.Logf("all clients logged out")

Expand Down
4 changes: 2 additions & 2 deletions integration/hsic/hsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,9 @@ func (t *HeadscaleInContainer) GetHostname() string {
return t.hostname
}

// WaitForReady blocks until the Headscale instance is ready to
// WaitForRunning blocks until the Headscale instance is ready to
// serve clients.
func (t *HeadscaleInContainer) WaitForReady() error {
func (t *HeadscaleInContainer) WaitForRunning() error {
url := t.GetHealthEndpoint()

log.Printf("waiting for headscale to be ready at %s", url)
Expand Down
Loading

0 comments on commit 55498f1

Please sign in to comment.