Skip to content

Commit

Permalink
Test if we can join headscale in parallell to speed up
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed Feb 21, 2022
1 parent 69cdfbb commit 8dca405
Showing 1 changed file with 64 additions and 49 deletions.
113 changes: 64 additions & 49 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"path"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -44,6 +45,8 @@ type IntegrationTestSuite struct {
headscale dockertest.Resource

namespaces map[string]TestNamespace

joinWaitGroup sync.WaitGroup
}

func TestIntegrationTestSuite(t *testing.T) {
Expand Down Expand Up @@ -118,7 +121,7 @@ func (s *IntegrationTestSuite) saveLog(
return err
}

fmt.Printf("Saving logs for %s to %s\n", resource.Container.Name, basePath)
log.Printf("Saving logs for %s to %s\n", resource.Container.Name, basePath)

err = ioutil.WriteFile(
path.Join(basePath, resource.Container.Name+".stdout.log"),
Expand All @@ -141,6 +144,34 @@ func (s *IntegrationTestSuite) saveLog(
return nil
}

func (s *IntegrationTestSuite) Join(
endpoint, key, hostname string,
tailscale dockertest.Resource,
) {
defer s.joinWaitGroup.Done()

command := []string{
"tailscale",
"up",
"-login-server",
endpoint,
"--authkey",
key,
"--hostname",
hostname,
}

log.Println("Join command:", command)
log.Printf("Running join command for %s\n", hostname)
_, err := ExecuteCommand(
&tailscale,
command,
[]string{},
)
assert.Nil(s.T(), err)
log.Printf("%s joined\n", hostname)
}

func (s *IntegrationTestSuite) tailscaleContainer(
namespace, identifier, version string,
) (string, *dockertest.Resource) {
Expand Down Expand Up @@ -178,7 +209,7 @@ func (s *IntegrationTestSuite) tailscaleContainer(
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
fmt.Printf("Created %s container\n", hostname)
log.Printf("Created %s container\n", hostname)

return hostname, pts
}
Expand Down Expand Up @@ -221,15 +252,15 @@ func (s *IntegrationTestSuite) SetupSuite() {
Cmd: []string{"headscale", "serve"},
}

fmt.Println("Creating headscale container")
log.Println("Creating headscale container")
if pheadscale, err := s.pool.BuildAndRunWithBuildOptions(headscaleBuildOptions, headscaleOptions, DockerRestartPolicy); err == nil {
s.headscale = *pheadscale
} else {
log.Fatalf("Could not start resource: %s", err)
}
fmt.Println("Created headscale container")
log.Println("Created headscale container")

fmt.Println("Creating tailscale containers")
log.Println("Creating tailscale containers")
for namespace, scales := range s.namespaces {
for i := 0; i < scales.count; i++ {
version := tailscaleVersions[i%len(tailscaleVersions)]
Expand All @@ -243,7 +274,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
}
}

fmt.Println("Waiting for headscale to be ready")
log.Println("Waiting for headscale to be ready")
hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8080/tcp"))

if err := s.pool.Retry(func() error {
Expand All @@ -266,19 +297,19 @@ func (s *IntegrationTestSuite) SetupSuite() {
// https://github.com/stretchr/testify/issues/849
return // fmt.Errorf("Could not connect to headscale: %s", err)
}
fmt.Println("headscale container is ready")
log.Println("headscale container is ready")

for namespace, scales := range s.namespaces {
fmt.Printf("Creating headscale namespace: %s\n", namespace)
log.Printf("Creating headscale namespace: %s\n", namespace)
result, err := ExecuteCommand(
&s.headscale,
[]string{"headscale", "namespaces", "create", namespace},
[]string{},
)
fmt.Println("headscale create namespace result: ", result)
log.Println("headscale create namespace result: ", result)
assert.Nil(s.T(), err)

fmt.Printf("Creating pre auth key for %s\n", namespace)
log.Printf("Creating pre auth key for %s\n", namespace)
preAuthResult, err := ExecuteCommand(
&s.headscale,
[]string{
Expand All @@ -304,33 +335,16 @@ func (s *IntegrationTestSuite) SetupSuite() {

headscaleEndpoint := "http://headscale:8080"

fmt.Printf(
log.Printf(
"Joining tailscale containers to headscale at %s\n",
headscaleEndpoint,
)
for hostname, tailscale := range scales.tailscales {
command := []string{
"tailscale",
"up",
"-login-server",
headscaleEndpoint,
"--authkey",
preAuthKey.Key,
"--hostname",
hostname,
}

fmt.Println("Join command:", command)
fmt.Printf("Running join command for %s\n", hostname)
result, err := ExecuteCommand(
&tailscale,
command,
[]string{},
)
fmt.Println("tailscale result: ", result)
assert.Nil(s.T(), err)
fmt.Printf("%s joined\n", hostname)
s.joinWaitGroup.Add(1)
go s.Join(headscaleEndpoint, preAuthKey.Key, hostname, tailscale)
}

s.joinWaitGroup.Wait()
}

// The nodes need a bit of time to get their updated maps from headscale
Expand All @@ -350,15 +364,15 @@ func (s *IntegrationTestSuite) HandleStats(

func (s *IntegrationTestSuite) TestListNodes() {
for namespace, scales := range s.namespaces {
fmt.Println("Listing nodes")
log.Println("Listing nodes")
result, err := ExecuteCommand(
&s.headscale,
[]string{"headscale", "--namespace", namespace, "nodes", "list"},
[]string{},
)
assert.Nil(s.T(), err)

fmt.Printf("List nodes: \n%s\n", result)
log.Printf("List nodes: \n%s\n", result)

// Chck that the correct count of host is present in node list
lines := strings.Split(result, "\n")
Expand All @@ -381,7 +395,7 @@ func (s *IntegrationTestSuite) TestGetIpAddresses() {
s.T().Run(hostname, func(t *testing.T) {
assert.NotNil(t, ip)

fmt.Printf("IP for %s: %s\n", hostname, ip)
log.Printf("IP for %s: %s\n", hostname, ip)

// c.Assert(ip.Valid(), check.IsTrue)
assert.True(t, ip.Is4() || ip.Is6())
Expand Down Expand Up @@ -410,7 +424,7 @@ func (s *IntegrationTestSuite) TestGetIpAddresses() {
// s.T().Run(hostname, func(t *testing.T) {
// command := []string{"tailscale", "status", "--json"}
//
// fmt.Printf("Getting status for %s\n", hostname)
// log.Printf("Getting status for %s\n", hostname)
// result, err := ExecuteCommand(
// &tailscale,
// command,
Expand Down Expand Up @@ -476,7 +490,7 @@ func (s *IntegrationTestSuite) TestPingAllPeersByAddress() {
ip.String(),
}

fmt.Printf(
log.Printf(
"Pinging from %s to %s (%s)\n",
hostname,
peername,
Expand All @@ -488,7 +502,7 @@ func (s *IntegrationTestSuite) TestPingAllPeersByAddress() {
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", hostname, result)
log.Printf("Result for %s: %s\n", hostname, result)
assert.Contains(t, result, "pong")
})
}
Expand Down Expand Up @@ -534,7 +548,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
)
assert.Nil(s.T(), err)

fmt.Println("Shared node with result: ", result)
log.Println("Shared node with result: ", result)
}

result, err = ExecuteCommand(
Expand All @@ -543,7 +557,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
[]string{},
)
assert.Nil(s.T(), err)
fmt.Println("Nodelist after sharing", result)
log.Println("Nodelist after sharing", result)

// Chck that the correct count of host is present in node list
lines := strings.Split(result, "\n")
Expand Down Expand Up @@ -582,7 +596,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
ip.String(),
}

fmt.Printf(
log.Printf(
"Pinging from %s to %s (%s)\n",
hostname,
peername,
Expand All @@ -594,7 +608,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", hostname, result)
log.Printf("Result for %s: %s\n", hostname, result)
assert.Contains(t, result, "pong")
})
}
Expand All @@ -616,6 +630,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
}
time.Sleep(sleepInverval)
}

return
}

Expand All @@ -638,7 +653,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
fmt.Sprintf("%s:", peername),
}
retry(10, 1*time.Second, func() error {
fmt.Printf(
log.Printf(
"Sending file from %s to %s\n",
hostname,
peername,
Expand Down Expand Up @@ -677,7 +692,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
"ls",
fmt.Sprintf("/tmp/file_from_%s", peername),
}
fmt.Printf(
log.Printf(
"Checking file in %s (%s) from %s (%s)\n",
hostname,
ips[hostname],
Expand All @@ -690,7 +705,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", peername, result)
log.Printf("Result for %s: %s\n", peername, result)
assert.Equal(
t,
fmt.Sprintf("/tmp/file_from_%s\n", peername),
Expand Down Expand Up @@ -720,7 +735,7 @@ func (s *IntegrationTestSuite) TestPingAllPeersByHostname() {
fmt.Sprintf("%s.%s.headscale.net", peername, namespace),
}

fmt.Printf(
log.Printf(
"Pinging using hostname from %s to %s\n",
hostname,
peername,
Expand All @@ -731,7 +746,7 @@ func (s *IntegrationTestSuite) TestPingAllPeersByHostname() {
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", hostname, result)
log.Printf("Result for %s: %s\n", hostname, result)
assert.Contains(t, result, "pong")
})
}
Expand All @@ -754,7 +769,7 @@ func (s *IntegrationTestSuite) TestMagicDNS() {
fmt.Sprintf("%s.%s.headscale.net", peername, namespace),
}

fmt.Printf(
log.Printf(
"Resolving name %s from %s\n",
peername,
hostname,
Expand All @@ -765,7 +780,7 @@ func (s *IntegrationTestSuite) TestMagicDNS() {
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", hostname, result)
log.Printf("Result for %s: %s\n", hostname, result)

for _, ip := range ips {
assert.Contains(t, result, ip.String())
Expand Down

0 comments on commit 8dca405

Please sign in to comment.