2021-09-20 19:23:18 +01:00
|
|
|
//go:build integration
|
2021-08-08 17:50:32 +01:00
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-20 16:50:55 +01:00
|
|
|
"context"
|
2021-09-20 22:53:34 +01:00
|
|
|
"encoding/json"
|
2021-10-04 14:14:12 +01:00
|
|
|
"errors"
|
2021-08-08 17:50:32 +01:00
|
|
|
"fmt"
|
2021-08-20 16:50:55 +01:00
|
|
|
"io/ioutil"
|
2021-08-08 17:50:32 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-08-20 16:50:55 +01:00
|
|
|
"path"
|
2021-08-08 17:50:32 +01:00
|
|
|
"strings"
|
2021-08-13 10:33:19 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
2021-08-08 17:50:32 +01:00
|
|
|
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2021-08-13 10:33:19 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2021-09-26 14:22:11 +02:00
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2021-09-20 22:53:34 +01:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
"inet.af/netaddr"
|
2021-08-08 17:50:32 +01:00
|
|
|
)
|
|
|
|
|
2021-09-20 19:23:18 +01:00
|
|
|
var (
|
|
|
|
integrationTmpDir string
|
|
|
|
ih Headscale
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-09-20 20:18:28 +01:00
|
|
|
pool dockertest.Pool
|
|
|
|
network dockertest.Network
|
|
|
|
headscale dockertest.Resource
|
2021-09-20 19:23:18 +01:00
|
|
|
)
|
2021-08-21 09:15:16 +01:00
|
|
|
|
2021-09-20 19:23:18 +01:00
|
|
|
var tailscaleVersions = []string{"1.14.3", "1.12.3"}
|
2021-08-21 09:15:16 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
type TestNamespace struct {
|
|
|
|
count int
|
|
|
|
tailscales map[string]dockertest.Resource
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
type IntegrationTestSuite struct {
|
|
|
|
suite.Suite
|
2021-08-20 16:50:55 +01:00
|
|
|
stats *suite.SuiteInformation
|
2021-09-20 20:18:28 +01:00
|
|
|
|
|
|
|
namespaces map[string]TestNamespace
|
2021-08-13 10:33:19 +01:00
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
func TestIntegrationTestSuite(t *testing.T) {
|
2021-08-20 16:50:55 +01:00
|
|
|
s := new(IntegrationTestSuite)
|
2021-09-20 20:18:28 +01:00
|
|
|
|
|
|
|
s.namespaces = map[string]TestNamespace{
|
|
|
|
"main": {
|
|
|
|
count: 20,
|
|
|
|
tailscales: make(map[string]dockertest.Resource),
|
|
|
|
},
|
|
|
|
"shared": {
|
|
|
|
count: 5,
|
|
|
|
tailscales: make(map[string]dockertest.Resource),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:50:55 +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.
|
2021-09-20 20:18:28 +01:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
for _, tailscale := range scales.tailscales {
|
|
|
|
if err := pool.Purge(&tailscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
2021-08-20 16:50:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.stats.Passed() {
|
|
|
|
err := saveLog(&headscale, "test_output")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not save log: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := pool.Purge(&headscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := network.Close(); err != nil {
|
|
|
|
log.Printf("Could not close network: %s\n", err)
|
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-09-26 14:22:11 +02:00
|
|
|
func executeCommand(resource *dockertest.Resource, cmd []string, env []string) (string, error) {
|
2021-08-08 17:50:32 +01:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
|
|
|
exitCode, err := resource.Exec(
|
|
|
|
cmd,
|
|
|
|
dockertest.ExecOptions{
|
2021-09-26 14:22:11 +02:00
|
|
|
Env: env,
|
2021-08-08 17:50:32 +01:00
|
|
|
StdOut: &stdout,
|
|
|
|
StdErr: &stderr,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitCode != 0 {
|
|
|
|
fmt.Println("Command: ", cmd)
|
|
|
|
fmt.Println("stdout: ", stdout.String())
|
|
|
|
fmt.Println("stderr: ", stderr.String())
|
|
|
|
return "", fmt.Errorf("command failed with: %s", stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout.String(), nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:50:55 +01:00
|
|
|
func 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 = 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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Saving logs for %s to %s\n", resource.Container.Name, basePath)
|
|
|
|
|
2021-09-20 19:23:18 +01:00
|
|
|
err = ioutil.WriteFile(path.Join(basePath, resource.Container.Name+".stdout.log"), []byte(stdout.String()), 0o644)
|
2021-08-20 16:50:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-20 19:23:18 +01:00
|
|
|
err = ioutil.WriteFile(path.Join(basePath, resource.Container.Name+".stderr.log"), []byte(stdout.String()), 0o644)
|
2021-08-20 16:50:55 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-08 17:50:32 +01:00
|
|
|
func dockerRestartPolicy(config *docker.HostConfig) {
|
|
|
|
// set AutoRemove to true so that stopped container goes away by itself
|
|
|
|
config.AutoRemove = true
|
|
|
|
config.RestartPolicy = docker.RestartPolicy{
|
|
|
|
Name: "no",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
func tailscaleContainer(namespace, identifier, version string) (string, *dockertest.Resource) {
|
2021-09-20 19:23:18 +01:00
|
|
|
tailscaleBuildOptions := &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: version,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-09-20 20:18:28 +01:00
|
|
|
hostname := fmt.Sprintf("%s-tailscale-%s-%s", namespace, strings.Replace(version, ".", "-", -1), identifier)
|
2021-09-20 19:23:18 +01:00
|
|
|
tailscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: hostname,
|
|
|
|
Networks: []*dockertest.Network{&network},
|
|
|
|
Cmd: []string{"tailscaled", "--tun=userspace-networking", "--socks5-server=localhost:1055"},
|
|
|
|
}
|
|
|
|
|
|
|
|
pts, err := pool.BuildAndRunWithBuildOptions(tailscaleBuildOptions, tailscaleOptions, dockerRestartPolicy)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not start resource: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Created %s container\n", hostname)
|
|
|
|
return hostname, pts
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
func (s *IntegrationTestSuite) SetupSuite() {
|
2021-08-08 17:50:32 +01:00
|
|
|
var err error
|
|
|
|
h = Headscale{
|
|
|
|
dbType: "sqlite3",
|
|
|
|
dbString: "integration_test_db.sqlite3",
|
|
|
|
}
|
|
|
|
|
|
|
|
if ppool, err := dockertest.NewPool(""); err == nil {
|
|
|
|
pool = *ppool
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pnetwork, err := pool.CreateNetwork("headscale-test"); err == nil {
|
|
|
|
network = *pnetwork
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Could not create network: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headscaleBuildOptions := &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile",
|
|
|
|
ContextDir: ".",
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not determine current path: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: "headscale",
|
|
|
|
Mounts: []string{
|
|
|
|
fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath),
|
|
|
|
fmt.Sprintf("%s/derp.yaml:/etc/headscale/derp.yaml", currentPath),
|
|
|
|
},
|
|
|
|
Networks: []*dockertest.Network{&network},
|
2021-08-13 10:33:19 +01:00
|
|
|
Cmd: []string{"headscale", "serve"},
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Creating headscale container")
|
|
|
|
if pheadscale, err := pool.BuildAndRunWithBuildOptions(headscaleBuildOptions, headscaleOptions, dockerRestartPolicy); err == nil {
|
|
|
|
headscale = *pheadscale
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Could not start resource: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Created headscale container")
|
|
|
|
|
|
|
|
fmt.Println("Creating tailscale containers")
|
2021-09-20 20:18:28 +01:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
for i := 0; i < scales.count; i++ {
|
|
|
|
version := tailscaleVersions[i%len(tailscaleVersions)]
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
hostname, container := tailscaleContainer(namespace, fmt.Sprint(i), version)
|
|
|
|
scales.tailscales[hostname] = *container
|
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Waiting for headscale to be ready")
|
|
|
|
hostEndpoint := fmt.Sprintf("localhost:%s", headscale.GetPort("8080/tcp"))
|
|
|
|
|
|
|
|
if err := pool.Retry(func() error {
|
|
|
|
url := fmt.Sprintf("http://%s/health", hostEndpoint)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("status code not OK")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-10-04 14:16:37 +01:00
|
|
|
// 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)
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
|
|
|
fmt.Println("headscale container is ready")
|
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
fmt.Printf("Creating headscale namespace: %s\n", namespace)
|
|
|
|
result, err := executeCommand(
|
|
|
|
&headscale,
|
|
|
|
[]string{"headscale", "namespaces", "create", namespace},
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 20:18:28 +01:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
fmt.Println("headscale create namespace result: ", result)
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
fmt.Printf("Creating pre auth key for %s\n", namespace)
|
|
|
|
authKey, err := executeCommand(
|
|
|
|
&headscale,
|
2021-09-20 22:53:34 +01:00
|
|
|
[]string{"headscale", "--namespace", namespace, "preauthkeys", "create", "--reusable", "--expiration", "24h"},
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 20:18:28 +01:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-10-04 14:39:52 +00:00
|
|
|
headscaleEndpoint := "http://headscale:8080"
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
fmt.Printf("Joining tailscale containers to headscale at %s\n", headscaleEndpoint)
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
command := []string{"tailscale", "up", "-login-server", headscaleEndpoint, "--authkey", strings.TrimSuffix(authKey, "\n"), "--hostname", hostname}
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
fmt.Println("Join command:", command)
|
|
|
|
fmt.Printf("Running join command for %s\n", hostname)
|
|
|
|
result, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 20:18:28 +01:00
|
|
|
)
|
|
|
|
fmt.Println("tailscale result: ", result)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
fmt.Printf("%s joined\n", hostname)
|
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
|
|
|
|
// 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.
|
2021-08-20 16:50:55 +01:00
|
|
|
time.Sleep(60 * time.Second)
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
func (s *IntegrationTestSuite) TearDownSuite() {
|
2021-08-20 16:50:55 +01:00
|
|
|
}
|
2021-08-18 23:17:09 +01:00
|
|
|
|
2021-08-20 16:50:55 +01:00
|
|
|
func (s *IntegrationTestSuite) HandleStats(suiteName string, stats *suite.SuiteInformation) {
|
|
|
|
s.stats = stats
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
func (s *IntegrationTestSuite) TestListNodes() {
|
2021-09-20 20:18:28 +01:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
fmt.Println("Listing nodes")
|
|
|
|
result, err := executeCommand(
|
|
|
|
&headscale,
|
2021-09-20 22:53:34 +01:00
|
|
|
[]string{"headscale", "--namespace", namespace, "nodes", "list"},
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 20:18:28 +01:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
fmt.Printf("List nodes: \n%s\n", result)
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
// Chck that the correct count of host is present in node list
|
|
|
|
lines := strings.Split(result, "\n")
|
|
|
|
assert.Equal(s.T(), len(scales.tailscales), len(lines)-2)
|
2021-08-08 17:50:32 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
for hostname := range scales.tailscales {
|
|
|
|
assert.Contains(s.T(), result, hostname)
|
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 10:33:19 +01:00
|
|
|
func (s *IntegrationTestSuite) TestGetIpAddresses() {
|
2021-09-20 20:18:28 +01:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ipPrefix := netaddr.MustParseIPPrefix("100.64.0.0/10")
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
for hostname := range scales.tailscales {
|
|
|
|
s.T().Run(hostname, func(t *testing.T) {
|
2021-10-04 14:17:05 +01:00
|
|
|
ip, ok := ips[hostname]
|
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.NotNil(t, ip)
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
fmt.Printf("IP for %s: %s\n", hostname, ip)
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
// c.Assert(ip.Valid(), check.IsTrue)
|
|
|
|
assert.True(t, ip.Is4())
|
|
|
|
assert.True(t, ipPrefix.Contains(ip))
|
|
|
|
})
|
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TestStatus() {
|
2021-09-20 20:18:28 +01:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
s.T().Run(hostname, func(t *testing.T) {
|
2021-09-20 22:53:34 +01:00
|
|
|
command := []string{"tailscale", "status", "--json"}
|
2021-09-20 20:18:28 +01:00
|
|
|
|
|
|
|
fmt.Printf("Getting status for %s\n", hostname)
|
|
|
|
result, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 20:18:28 +01:00
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2021-09-20 22:53:34 +01:00
|
|
|
var status ipnstate.Status
|
|
|
|
err = json.Unmarshal([]byte(result), &status)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
// TODO(kradalby): Replace this check with peer length of SAME namespace
|
2021-09-20 20:18:28 +01:00
|
|
|
// Check if we have as many nodes in status
|
|
|
|
// as we have IPs/tailscales
|
2021-09-20 22:53:34 +01:00
|
|
|
// lines := strings.Split(result, "\n")
|
|
|
|
// assert.Equal(t, len(ips), len(lines)-1)
|
|
|
|
// assert.Equal(t, len(scales.tailscales), len(lines)-1)
|
|
|
|
|
|
|
|
peerIps := getIPsfromIPNstate(status)
|
2021-09-20 20:18:28 +01:00
|
|
|
|
|
|
|
// Check that all hosts is present in all hosts status
|
|
|
|
for ipHostname, ip := range ips {
|
2021-09-20 22:53:34 +01:00
|
|
|
if hostname != ipHostname {
|
|
|
|
assert.Contains(t, peerIps, ip)
|
|
|
|
}
|
2021-09-20 20:18:28 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:53:34 +01:00
|
|
|
func getIPsfromIPNstate(status ipnstate.Status) []netaddr.IP {
|
|
|
|
ips := make([]netaddr.IP, 0)
|
|
|
|
|
|
|
|
for _, peer := range status.Peer {
|
|
|
|
ips = append(ips, peer.TailscaleIPs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
|
2021-08-13 11:01:23 +01:00
|
|
|
func (s *IntegrationTestSuite) TestPingAllPeers() {
|
2021-09-20 20:18:28 +01:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 11:01:23 +01:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
for peername, ip := range ips {
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
|
|
|
// We currently cant ping ourselves, so skip that.
|
|
|
|
if peername != hostname {
|
|
|
|
// We are only interested in "direct ping" which means what we
|
|
|
|
// might need a couple of more attempts before reaching the node.
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=1s",
|
2021-10-02 22:56:48 +01:00
|
|
|
"--c=10",
|
2021-09-20 20:18:28 +01:00
|
|
|
"--until-direct=true",
|
|
|
|
ip.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Pinging from %s (%s) to %s (%s)\n", hostname, ips[hostname], peername, ip)
|
|
|
|
result, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 20:18:28 +01:00
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
assert.Contains(t, result, "pong")
|
2021-08-18 23:17:09 +01:00
|
|
|
}
|
2021-09-20 20:18:28 +01:00
|
|
|
})
|
|
|
|
}
|
2021-08-13 11:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
|
2021-09-20 23:21:25 +01:00
|
|
|
func (s *IntegrationTestSuite) TestSharedNodes() {
|
|
|
|
main := s.namespaces["main"]
|
|
|
|
shared := s.namespaces["shared"]
|
|
|
|
|
|
|
|
result, err := executeCommand(
|
|
|
|
&headscale,
|
|
|
|
[]string{"headscale", "nodes", "list", "-o", "json", "--namespace", "shared"},
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 23:21:25 +01:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var machineList []Machine
|
|
|
|
err = json.Unmarshal([]byte(result), &machineList)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
for _, machine := range machineList {
|
|
|
|
|
|
|
|
result, err := executeCommand(
|
|
|
|
&headscale,
|
|
|
|
[]string{"headscale", "nodes", "share", "--namespace", "shared", fmt.Sprint(machine.ID), "main"},
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 23:21:25 +01:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
fmt.Println("Shared node with result: ", result)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err = executeCommand(
|
|
|
|
&headscale,
|
|
|
|
[]string{"headscale", "nodes", "list", "--namespace", "main"},
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-20 23:21:25 +01:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
fmt.Println("Nodelist after sharing", result)
|
|
|
|
|
|
|
|
// Chck that the correct count of host is present in node list
|
|
|
|
lines := strings.Split(result, "\n")
|
|
|
|
assert.Equal(s.T(), len(main.tailscales)+len(shared.tailscales), len(lines)-2)
|
|
|
|
|
|
|
|
for hostname := range main.tailscales {
|
|
|
|
assert.Contains(s.T(), result, hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
for hostname := range shared.tailscales {
|
|
|
|
assert.Contains(s.T(), result, hostname)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(kradalby): Figure out why these connections are not set up
|
|
|
|
// // TODO: See if we can have a more deterministic wait here.
|
|
|
|
// time.Sleep(100 * time.Second)
|
|
|
|
|
|
|
|
// mainIps, err := getIPs(main.tailscales)
|
|
|
|
// assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
// sharedIps, err := getIPs(shared.tailscales)
|
|
|
|
// assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
// for hostname, tailscale := range main.tailscales {
|
|
|
|
// for peername, ip := range sharedIps {
|
|
|
|
// s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
|
|
|
// // We currently cant ping ourselves, so skip that.
|
|
|
|
// if peername != hostname {
|
|
|
|
// // We are only interested in "direct ping" which means what we
|
|
|
|
// // might need a couple of more attempts before reaching the node.
|
|
|
|
// command := []string{
|
|
|
|
// "tailscale", "ping",
|
|
|
|
// "--timeout=1s",
|
|
|
|
// "--c=20",
|
|
|
|
// "--until-direct=true",
|
|
|
|
// ip.String(),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fmt.Printf("Pinging from %s (%s) to %s (%s)\n", hostname, mainIps[hostname], peername, ip)
|
|
|
|
// result, err := executeCommand(
|
|
|
|
// &tailscale,
|
|
|
|
// command,
|
|
|
|
// )
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
// fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
// assert.Contains(t, result, "pong")
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:12:44 +02:00
|
|
|
func (s *IntegrationTestSuite) TestTailDrop() {
|
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-09-26 12:22:59 +02:00
|
|
|
apiURLs, err := getAPIURLs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-09-25 13:12:44 +02:00
|
|
|
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
command := []string{"touch", fmt.Sprintf("/tmp/file_from_%s", hostname)}
|
|
|
|
_, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-25 13:12:44 +02:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
for peername, ip := range ips {
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
|
|
|
if peername != hostname {
|
2021-09-26 12:22:59 +02:00
|
|
|
|
|
|
|
// Under normal circumstances, we should be able to send a file
|
|
|
|
// using `tailscale file cp` - but not in userspace networking mode
|
|
|
|
// So curl!
|
|
|
|
peerAPI, ok := apiURLs[ip]
|
2021-09-26 14:22:11 +02:00
|
|
|
assert.True(t, ok)
|
2021-09-26 18:59:23 +02:00
|
|
|
|
2021-09-26 20:23:15 +02:00
|
|
|
// TODO(juanfont): We still have some issues with the test infrastructure, so
|
2021-09-26 18:59:23 +02:00
|
|
|
// lets run curl multiple times until it works.
|
|
|
|
attempts := 0
|
|
|
|
var err error
|
|
|
|
for {
|
|
|
|
command := []string{
|
|
|
|
"curl",
|
|
|
|
"--retry-connrefused",
|
|
|
|
"--retry-delay",
|
|
|
|
"30",
|
|
|
|
"--retry",
|
|
|
|
"10",
|
|
|
|
"--connect-timeout",
|
|
|
|
"60",
|
|
|
|
"-X",
|
|
|
|
"PUT",
|
|
|
|
"--upload-file",
|
|
|
|
fmt.Sprintf("/tmp/file_from_%s", hostname),
|
|
|
|
fmt.Sprintf("%s/v0/put/file_from_%s", peerAPI, hostname),
|
|
|
|
}
|
|
|
|
fmt.Printf("Sending file from %s (%s) to %s (%s)\n", hostname, ips[hostname], peername, ip)
|
|
|
|
_, err = executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-10-04 18:04:08 +02:00
|
|
|
[]string{"ALL_PROXY=socks5://localhost:1055"},
|
2021-09-26 18:59:23 +02:00
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
attempts++
|
|
|
|
if attempts > 10 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:12:44 +02:00
|
|
|
}
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "file",
|
|
|
|
"get",
|
2021-09-26 14:22:11 +02:00
|
|
|
"/tmp/",
|
2021-09-25 13:12:44 +02:00
|
|
|
}
|
|
|
|
_, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-25 13:12:44 +02:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
for peername, ip := range ips {
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
|
|
|
if peername != hostname {
|
|
|
|
command := []string{
|
|
|
|
"ls",
|
|
|
|
fmt.Sprintf("/tmp/file_from_%s", peername),
|
|
|
|
}
|
|
|
|
fmt.Printf("Checking file in %s (%s) from %s (%s)\n", hostname, ips[hostname], peername, ip)
|
|
|
|
result, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-25 13:12:44 +02:00
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", peername, result)
|
|
|
|
assert.Equal(t, result, fmt.Sprintf("/tmp/file_from_%s\n", peername))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 18:04:08 +02:00
|
|
|
func (s *IntegrationTestSuite) TestMagicDNS() {
|
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
for peername, ip := range ips {
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
|
|
|
if peername != hostname {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=10s",
|
|
|
|
"--c=20",
|
|
|
|
"--until-direct=true",
|
|
|
|
fmt.Sprintf("%s.%s.headscale.net", peername, namespace),
|
|
|
|
}
|
2021-10-02 11:20:42 +02:00
|
|
|
|
2021-10-04 18:04:08 +02:00
|
|
|
fmt.Printf("Pinging using Hostname (magicdns) from %s (%s) to %s (%s)\n", hostname, ips[hostname], peername, ip)
|
|
|
|
result, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
assert.Contains(t, result, "pong")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-02 11:20:42 +02:00
|
|
|
|
2021-09-20 20:18:28 +01:00
|
|
|
func getIPs(tailscales map[string]dockertest.Resource) (map[string]netaddr.IP, error) {
|
2021-08-08 17:50:32 +01:00
|
|
|
ips := make(map[string]netaddr.IP)
|
|
|
|
for hostname, tailscale := range tailscales {
|
|
|
|
command := []string{"tailscale", "ip"}
|
|
|
|
|
|
|
|
result, err := executeCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-08-08 17:50:32 +01:00
|
|
|
)
|
2021-08-13 10:33:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
|
|
|
|
ip, err := netaddr.ParseIP(strings.TrimSuffix(result, "\n"))
|
2021-08-13 10:33:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-08 17:50:32 +01:00
|
|
|
|
|
|
|
ips[hostname] = ip
|
|
|
|
}
|
2021-08-13 10:33:19 +01:00
|
|
|
return ips, nil
|
2021-08-08 17:50:32 +01:00
|
|
|
}
|
2021-09-26 12:22:59 +02:00
|
|
|
|
|
|
|
func getAPIURLs(tailscales map[string]dockertest.Resource) (map[netaddr.IP]string, error) {
|
|
|
|
fts := make(map[netaddr.IP]string)
|
2021-09-26 14:22:11 +02:00
|
|
|
for _, tailscale := range tailscales {
|
2021-09-26 15:17:27 +02:00
|
|
|
command := []string{
|
2021-09-26 12:22:59 +02:00
|
|
|
"curl",
|
|
|
|
"--unix-socket",
|
|
|
|
"/run/tailscale/tailscaled.sock",
|
|
|
|
"http://localhost/localapi/v0/file-targets",
|
|
|
|
}
|
2021-09-26 15:17:27 +02:00
|
|
|
result, err := executeCommand(
|
2021-09-26 12:22:59 +02:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 14:22:11 +02:00
|
|
|
[]string{},
|
2021-09-26 12:22:59 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-26 14:33:01 +02:00
|
|
|
|
2021-09-26 12:22:59 +02:00
|
|
|
var pft []apitype.FileTarget
|
2021-09-26 14:22:11 +02:00
|
|
|
if err := json.Unmarshal([]byte(result), &pft); err != nil {
|
2021-09-26 12:22:59 +02:00
|
|
|
return nil, fmt.Errorf("invalid JSON: %w", err)
|
|
|
|
}
|
|
|
|
for _, ft := range pft {
|
|
|
|
n := ft.Node
|
2021-09-26 14:22:11 +02:00
|
|
|
for _, a := range n.Addresses { // just add all the addresses
|
|
|
|
if _, ok := fts[a.IP()]; !ok {
|
2021-10-04 14:14:12 +01:00
|
|
|
if ft.PeerAPIURL == "" {
|
|
|
|
return nil, errors.New("api url is empty")
|
|
|
|
}
|
2021-09-26 14:22:11 +02:00
|
|
|
fts[a.IP()] = ft.PeerAPIURL
|
2021-09-26 12:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 14:22:11 +02:00
|
|
|
return fts, nil
|
2021-09-26 12:22:59 +02:00
|
|
|
}
|