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 45eef97
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 143 deletions.
38 changes: 19 additions & 19 deletions integration/auth_oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (s *AuthOIDCScenario) CreateHeadscaleEnv(
return err
}

err = headscale.WaitForReady()
err = headscale.WaitForRunning()
if err != nil {
return err
}
Expand Down Expand Up @@ -311,13 +311,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 +332,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 +348,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
31 changes: 17 additions & 14 deletions integration/auth_web_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *AuthWebFlowScenario) CreateHeadscaleEnv(
return err
}

err = headscale.WaitForReady()
err = headscale.WaitForRunning()
if err != nil {
return err
}
Expand Down Expand Up @@ -250,32 +250,35 @@ 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

Check failure on line 258 in integration/auth_web_flow_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}

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

Check failure on line 264 in integration/auth_web_flow_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

return with no blank line before (nlreturn)
}
}(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
14 changes: 9 additions & 5 deletions integration/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ func TestPingAllByIP(t *testing.T) {
}

spec := map[string]int{
// "user1": 40,
"user1": len(TailscaleVersions),
"user2": len(TailscaleVersions),
"user3": len(TailscaleVersions),
"user4": len(TailscaleVersions),
"user5": len(TailscaleVersions),
}

err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("pingallbyip"))
Expand Down Expand Up @@ -488,7 +492,7 @@ func TestResolveMagicDNS(t *testing.T) {
spec := map[string]int{
// Omit 1.16.2 (-1) because it does not have the FQDN field
"magicdns1": len(TailscaleVersions) - 1,
"magicdns2": len(TailscaleVersions) - 1,
// "magicdns2": len(TailscaleVersions) - 1,
}

err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("magicdns"))
Expand Down Expand Up @@ -553,10 +557,10 @@ func TestResolveMagicDNS(t *testing.T) {
}
}

err = scenario.Shutdown()
if err != nil {
t.Errorf("failed to tear down scenario: %s", err)
}
// err = scenario.Shutdown()
// if err != nil {
// t.Errorf("failed to tear down scenario: %s", err)
// }
}

func TestExpireNode(t *testing.T) {
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 45eef97

Please sign in to comment.