2022-08-09 12:26:12 +02:00
|
|
|
//go:build integration_derp
|
2022-03-05 19:34:06 +01:00
|
|
|
|
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2022-03-08 12:11:51 +01:00
|
|
|
|
|
|
|
"github.com/ccding/go-stun/stun"
|
2022-03-05 19:34:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-08-09 12:26:12 +02:00
|
|
|
namespaceName = "derpnamespace"
|
|
|
|
totalContainers = 3
|
2022-03-05 19:34:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IntegrationDERPTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
stats *suite.SuiteInformation
|
|
|
|
|
|
|
|
pool dockertest.Pool
|
|
|
|
networks map[int]dockertest.Network // so we keep the containers isolated
|
|
|
|
headscale dockertest.Resource
|
2022-06-27 11:56:37 +00:00
|
|
|
saveLogs bool
|
2022-03-05 19:34:06 +01:00
|
|
|
|
|
|
|
tailscales map[string]dockertest.Resource
|
|
|
|
joinWaitGroup sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDERPIntegrationTestSuite(t *testing.T) {
|
2022-06-27 11:56:37 +00:00
|
|
|
saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG")
|
|
|
|
if err != nil {
|
|
|
|
saveLogs = false
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:34:06 +01:00
|
|
|
s := new(IntegrationDERPTestSuite)
|
|
|
|
|
|
|
|
s.tailscales = make(map[string]dockertest.Resource)
|
|
|
|
s.networks = make(map[int]dockertest.Network)
|
2022-06-27 11:56:37 +00:00
|
|
|
s.saveLogs = saveLogs
|
2022-03-05 19:34:06 +01:00
|
|
|
|
|
|
|
suite.Run(t, s)
|
|
|
|
|
|
|
|
// HandleStats, which allows us to check if we passed and save logs
|
|
|
|
// is called after TearDown, so we cannot tear down containers before
|
|
|
|
// we have potentially saved the logs.
|
2022-06-27 11:56:37 +00:00
|
|
|
if s.saveLogs {
|
|
|
|
for _, tailscale := range s.tailscales {
|
|
|
|
if err := s.pool.Purge(&tailscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:56:37 +00:00
|
|
|
if !s.stats.Passed() {
|
|
|
|
err := s.saveLog(&s.headscale, "test_output")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not save log: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := s.pool.Purge(&s.headscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:56:37 +00:00
|
|
|
for _, network := range s.networks {
|
|
|
|
if err := network.Close(); err != nil {
|
|
|
|
log.Printf("Could not close network: %s\n", err)
|
|
|
|
}
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) SetupSuite() {
|
|
|
|
if ppool, err := dockertest.NewPool(""); err == nil {
|
|
|
|
s.pool = *ppool
|
|
|
|
} else {
|
2022-06-27 11:58:16 +00:00
|
|
|
s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "")
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < totalContainers; i++ {
|
|
|
|
if pnetwork, err := s.pool.CreateNetwork(fmt.Sprintf("headscale-derp-%d", i)); err == nil {
|
|
|
|
s.networks[i] = *pnetwork
|
|
|
|
} else {
|
2022-06-27 11:58:16 +00:00
|
|
|
s.FailNow(fmt.Sprintf("Could not create network: %s", err), "")
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
headscaleBuildOptions := &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile",
|
|
|
|
ContextDir: ".",
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
2022-06-27 11:58:16 +00:00
|
|
|
s.FailNow(fmt.Sprintf("Could not determine current path: %s", err), "")
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
headscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: headscaleHostname,
|
|
|
|
Mounts: []string{
|
2022-04-15 18:27:57 +02:00
|
|
|
fmt.Sprintf(
|
|
|
|
"%s/integration_test/etc_embedded_derp:/etc/headscale",
|
|
|
|
currentPath,
|
|
|
|
),
|
2022-03-05 19:34:06 +01:00
|
|
|
},
|
|
|
|
Cmd: []string{"headscale", "serve"},
|
|
|
|
ExposedPorts: []string{"8443/tcp", "3478/udp"},
|
|
|
|
PortBindings: map[docker.Port][]docker.PortBinding{
|
|
|
|
"8443/tcp": {{HostPort: "8443"}},
|
|
|
|
"3478/udp": {{HostPort: "3478"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-27 12:07:30 +00:00
|
|
|
err = s.pool.RemoveContainerByName(headscaleHostname)
|
|
|
|
if err != nil {
|
2022-08-04 10:47:00 +02:00
|
|
|
s.FailNow(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Could not remove existing container before building test: %s",
|
|
|
|
err,
|
|
|
|
),
|
|
|
|
"",
|
|
|
|
)
|
2022-06-27 12:07:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:28 +02:00
|
|
|
log.Println("Creating headscale container for DERP integration tests")
|
2022-03-05 19:34:06 +01:00
|
|
|
if pheadscale, err := s.pool.BuildAndRunWithBuildOptions(headscaleBuildOptions, headscaleOptions, DockerRestartPolicy); err == nil {
|
|
|
|
s.headscale = *pheadscale
|
|
|
|
} else {
|
2022-06-27 11:58:16 +00:00
|
|
|
s.FailNow(fmt.Sprintf("Could not start headscale container: %s", err), "")
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
2022-08-05 17:35:28 +02:00
|
|
|
log.Println("Created headscale container for embedded DERP tests")
|
2022-03-05 19:34:06 +01:00
|
|
|
|
2022-08-05 17:35:28 +02:00
|
|
|
log.Println("Creating tailscale containers for embedded DERP tests")
|
2022-03-05 19:34:06 +01:00
|
|
|
|
|
|
|
for i := 0; i < totalContainers; i++ {
|
|
|
|
version := tailscaleVersions[i%len(tailscaleVersions)]
|
|
|
|
hostname, container := s.tailscaleContainer(
|
|
|
|
fmt.Sprint(i),
|
|
|
|
version,
|
|
|
|
s.networks[i],
|
|
|
|
)
|
|
|
|
s.tailscales[hostname] = *container
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:28 +02:00
|
|
|
log.Println("Waiting for headscale to be ready for embedded DERP tests")
|
2022-03-05 19:34:06 +01:00
|
|
|
hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8443/tcp"))
|
|
|
|
|
|
|
|
if err := s.pool.Retry(func() error {
|
|
|
|
url := fmt.Sprintf("https://%s/health", hostEndpoint)
|
|
|
|
insecureTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
client := &http.Client{Transport: insecureTransport}
|
|
|
|
resp, err := client.Get(url)
|
|
|
|
if err != nil {
|
2022-08-05 17:35:28 +02:00
|
|
|
fmt.Printf("headscale for embedded DERP tests is not ready: %s\n", err)
|
2022-03-05 19:34:06 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("status code not OK")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
// TODO(kradalby): If we cannot access headscale, or any other fatal error during
|
|
|
|
// test setup, we need to abort and tear down. However, testify does not seem to
|
|
|
|
// support that at the moment:
|
|
|
|
// https://github.com/stretchr/testify/issues/849
|
|
|
|
return // fmt.Errorf("Could not connect to headscale: %s", err)
|
|
|
|
}
|
2022-08-05 17:35:28 +02:00
|
|
|
log.Println("headscale container is ready for embedded DERP tests")
|
2022-03-05 19:34:06 +01:00
|
|
|
|
|
|
|
log.Printf("Creating headscale namespace: %s\n", namespaceName)
|
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
|
|
|
[]string{"headscale", "namespaces", "create", namespaceName},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
log.Println("headscale create namespace result: ", result)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
log.Printf("Creating pre auth key for %s\n", namespaceName)
|
|
|
|
preAuthResult, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"--namespace",
|
|
|
|
namespaceName,
|
|
|
|
"preauthkeys",
|
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{"LOG_LEVEL=error"},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(preAuthResult), &preAuthKey)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
assert.True(s.T(), preAuthKey.Reusable)
|
|
|
|
|
2022-04-15 18:27:57 +02:00
|
|
|
headscaleEndpoint := fmt.Sprintf(
|
|
|
|
"https://headscale:%s",
|
|
|
|
s.headscale.GetPort("8443/tcp"),
|
|
|
|
)
|
2022-03-05 19:34:06 +01:00
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"Joining tailscale containers to headscale at %s\n",
|
|
|
|
headscaleEndpoint,
|
|
|
|
)
|
|
|
|
for hostname, tailscale := range s.tailscales {
|
|
|
|
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
|
|
|
|
// TODO: See if we can have a more deterministic wait here.
|
|
|
|
time.Sleep(60 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) 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)
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:27:57 +02:00
|
|
|
func (s *IntegrationDERPTestSuite) tailscaleContainer(
|
|
|
|
identifier, version string,
|
|
|
|
network dockertest.Network,
|
2022-03-05 19:34:06 +01:00
|
|
|
) (string, *dockertest.Resource) {
|
2022-03-18 17:05:28 +01:00
|
|
|
tailscaleBuildOptions := getDockerBuildOptions(version)
|
|
|
|
|
2022-03-05 19:34:06 +01:00
|
|
|
hostname := fmt.Sprintf(
|
|
|
|
"tailscale-%s-%s",
|
|
|
|
strings.Replace(version, ".", "-", -1),
|
|
|
|
identifier,
|
|
|
|
)
|
|
|
|
tailscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: hostname,
|
|
|
|
Networks: []*dockertest.Network{&network},
|
|
|
|
Cmd: []string{
|
|
|
|
"tailscaled", "--tun=tsdev",
|
|
|
|
},
|
|
|
|
|
|
|
|
// expose the host IP address, so we can access it from inside the container
|
2022-04-15 18:27:57 +02:00
|
|
|
ExtraHosts: []string{
|
|
|
|
"host.docker.internal:host-gateway",
|
|
|
|
"headscale:host-gateway",
|
|
|
|
},
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := s.pool.BuildAndRunWithBuildOptions(
|
|
|
|
tailscaleBuildOptions,
|
|
|
|
tailscaleOptions,
|
|
|
|
DockerRestartPolicy,
|
|
|
|
DockerAllowLocalIPv6,
|
|
|
|
DockerAllowNetworkAdministration,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-03-20 12:31:18 +01:00
|
|
|
log.Fatalf("Could not start tailscale container version %s: %s", version, err)
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
log.Printf("Created %s container\n", hostname)
|
|
|
|
|
|
|
|
return hostname, pts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) TearDownSuite() {
|
2022-06-27 11:56:37 +00:00
|
|
|
if !s.saveLogs {
|
|
|
|
for _, tailscale := range s.tailscales {
|
|
|
|
if err := s.pool.Purge(&tailscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.pool.Purge(&s.headscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, network := range s.networks {
|
|
|
|
if err := network.Close(); err != nil {
|
|
|
|
log.Printf("Could not close network: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 19:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) HandleStats(
|
|
|
|
suiteName string,
|
|
|
|
stats *suite.SuiteInformation,
|
|
|
|
) {
|
|
|
|
s.stats = stats
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) saveLog(
|
|
|
|
resource *dockertest.Resource,
|
|
|
|
basePath string,
|
|
|
|
) error {
|
|
|
|
err := os.MkdirAll(basePath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
|
|
|
err = s.pool.Client.Logs(
|
|
|
|
docker.LogsOptions{
|
|
|
|
Context: context.TODO(),
|
|
|
|
Container: resource.Container.ID,
|
|
|
|
OutputStream: &stdout,
|
|
|
|
ErrorStream: &stderr,
|
|
|
|
Tail: "all",
|
|
|
|
RawTerminal: false,
|
|
|
|
Stdout: true,
|
|
|
|
Stderr: true,
|
|
|
|
Follow: false,
|
|
|
|
Timestamps: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Saving logs for %s to %s\n", resource.Container.Name, basePath)
|
|
|
|
|
2022-08-09 23:21:19 +02:00
|
|
|
err = os.WriteFile(
|
2022-03-05 19:34:06 +01:00
|
|
|
path.Join(basePath, resource.Container.Name+".stdout.log"),
|
|
|
|
[]byte(stdout.String()),
|
|
|
|
0o644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-09 23:21:19 +02:00
|
|
|
err = os.WriteFile(
|
2022-03-05 19:34:06 +01:00
|
|
|
path.Join(basePath, resource.Container.Name+".stderr.log"),
|
|
|
|
[]byte(stdout.String()),
|
|
|
|
0o644,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) TestPingAllPeersByHostname() {
|
2022-05-17 22:11:51 +02:00
|
|
|
hostnames, err := getDNSNames(&s.headscale)
|
2022-03-05 19:34:06 +01:00
|
|
|
assert.Nil(s.T(), err)
|
2022-05-17 22:11:51 +02:00
|
|
|
|
2022-05-17 22:02:18 +02:00
|
|
|
log.Printf("Hostnames: %#v\n", hostnames)
|
|
|
|
|
2022-03-05 19:34:06 +01:00
|
|
|
for hostname, tailscale := range s.tailscales {
|
2022-05-17 22:11:51 +02:00
|
|
|
for _, peername := range hostnames {
|
|
|
|
if strings.Contains(peername, hostname) {
|
2022-03-05 19:34:06 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=10s",
|
|
|
|
"--c=5",
|
|
|
|
"--until-direct=false",
|
|
|
|
peername,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(
|
|
|
|
"Pinging using hostname from %s to %s\n",
|
|
|
|
hostname,
|
|
|
|
peername,
|
|
|
|
)
|
|
|
|
log.Println(command)
|
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
log.Printf("Result for %s: %s\n", hostname, result)
|
2022-03-06 23:46:16 +01:00
|
|
|
assert.Contains(t, result, "via DERP(headscale)")
|
2022-03-05 19:34:06 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-08 12:11:51 +01:00
|
|
|
|
|
|
|
func (s *IntegrationDERPTestSuite) TestDERPSTUN() {
|
|
|
|
headscaleSTUNAddr := fmt.Sprintf("localhost:%s", s.headscale.GetPort("3478/udp"))
|
|
|
|
client := stun.NewClient()
|
|
|
|
client.SetVerbose(true)
|
|
|
|
client.SetVVerbose(true)
|
|
|
|
client.SetServerAddr(headscaleSTUNAddr)
|
|
|
|
_, _, err := client.Discover()
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
}
|