2021-11-05 06:10:57 +08:00
|
|
|
//go:build integration
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-18 04:11:51 +08:00
|
|
|
"encoding/json"
|
2021-11-05 06:10:57 +08:00
|
|
|
"fmt"
|
2022-03-04 20:54:59 +08:00
|
|
|
"strings"
|
2021-11-05 06:10:57 +08:00
|
|
|
"time"
|
|
|
|
|
2022-05-18 04:11:51 +08:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-11-05 06:10:57 +08:00
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2022-01-26 06:11:15 +08:00
|
|
|
"inet.af/netaddr"
|
2021-11-05 06:10:57 +08:00
|
|
|
)
|
|
|
|
|
2021-11-15 01:35:49 +08:00
|
|
|
const DOCKER_EXECUTE_TIMEOUT = 10 * time.Second
|
|
|
|
|
2022-01-26 06:11:15 +08:00
|
|
|
var (
|
|
|
|
IpPrefix4 = netaddr.MustParseIPPrefix("100.64.0.0/10")
|
|
|
|
IpPrefix6 = netaddr.MustParseIPPrefix("fd7a:115c:a1e0::/48")
|
2022-03-04 20:26:45 +08:00
|
|
|
|
2022-04-23 16:30:13 +08:00
|
|
|
tailscaleVersions = []string{
|
|
|
|
"head",
|
|
|
|
"unstable",
|
|
|
|
"1.24.0",
|
|
|
|
"1.22.2",
|
|
|
|
"1.20.4",
|
|
|
|
"1.18.2",
|
|
|
|
"1.16.2",
|
|
|
|
"1.14.3",
|
|
|
|
"1.12.3",
|
|
|
|
}
|
2022-01-26 06:11:15 +08:00
|
|
|
)
|
2022-01-15 23:25:38 +08:00
|
|
|
|
2022-03-04 20:26:45 +08:00
|
|
|
type TestNamespace struct {
|
|
|
|
count int
|
|
|
|
tailscales map[string]dockertest.Resource
|
|
|
|
}
|
|
|
|
|
2022-01-15 23:25:38 +08:00
|
|
|
type ExecuteCommandConfig struct {
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteCommandOption func(*ExecuteCommandConfig) error
|
|
|
|
|
|
|
|
func ExecuteCommandTimeout(timeout time.Duration) ExecuteCommandOption {
|
|
|
|
return ExecuteCommandOption(func(conf *ExecuteCommandConfig) error {
|
|
|
|
conf.timeout = timeout
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-13 16:36:45 +08:00
|
|
|
func ExecuteCommand(
|
|
|
|
resource *dockertest.Resource,
|
|
|
|
cmd []string,
|
|
|
|
env []string,
|
2022-01-15 23:25:38 +08:00
|
|
|
options ...ExecuteCommandOption,
|
2021-11-13 16:36:45 +08:00
|
|
|
) (string, error) {
|
2021-11-05 06:10:57 +08:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2022-01-15 23:25:38 +08:00
|
|
|
execConfig := ExecuteCommandConfig{
|
|
|
|
timeout: DOCKER_EXECUTE_TIMEOUT,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range options {
|
|
|
|
if err := opt(&execConfig); err != nil {
|
|
|
|
return "", fmt.Errorf("execute-command/options: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 06:10:57 +08:00
|
|
|
|
|
|
|
type result struct {
|
|
|
|
exitCode int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
resultChan := make(chan result, 1)
|
|
|
|
|
|
|
|
// Run your long running function in it's own goroutine and pass back it's
|
|
|
|
// response into our channel.
|
|
|
|
go func() {
|
|
|
|
exitCode, err := resource.Exec(
|
|
|
|
cmd,
|
|
|
|
dockertest.ExecOptions{
|
2021-11-07 18:40:05 +08:00
|
|
|
Env: append(env, "HEADSCALE_LOG_LEVEL=disabled"),
|
2021-11-05 06:10:57 +08:00
|
|
|
StdOut: &stdout,
|
|
|
|
StdErr: &stderr,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
resultChan <- result{exitCode, err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Listen on our channel AND a timeout channel - which ever happens first.
|
|
|
|
select {
|
|
|
|
case res := <-resultChan:
|
|
|
|
if res.err != nil {
|
|
|
|
return "", res.err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.exitCode != 0 {
|
|
|
|
fmt.Println("Command: ", cmd)
|
|
|
|
fmt.Println("stdout: ", stdout.String())
|
|
|
|
fmt.Println("stderr: ", stderr.String())
|
2021-11-15 01:35:49 +08:00
|
|
|
|
2021-11-05 06:10:57 +08:00
|
|
|
return "", fmt.Errorf("command failed with: %s", stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout.String(), nil
|
2022-01-15 23:25:38 +08:00
|
|
|
case <-time.After(execConfig.timeout):
|
2021-11-15 01:35:49 +08:00
|
|
|
|
2022-01-15 23:25:38 +08:00
|
|
|
return "", fmt.Errorf("command timed out after %s", execConfig.timeout)
|
2021-11-05 06:10:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DockerRestartPolicy(config *docker.HostConfig) {
|
2022-01-15 23:25:38 +08:00
|
|
|
// set AutoRemove to true so that stopped container goes away by itself on error *immediately*.
|
|
|
|
// when set to false, containers remain until the end of the integration test.
|
|
|
|
config.AutoRemove = false
|
2021-11-05 06:10:57 +08:00
|
|
|
config.RestartPolicy = docker.RestartPolicy{
|
|
|
|
Name: "no",
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 23:25:38 +08:00
|
|
|
|
|
|
|
func DockerAllowLocalIPv6(config *docker.HostConfig) {
|
|
|
|
if config.Sysctls == nil {
|
|
|
|
config.Sysctls = make(map[string]string, 1)
|
|
|
|
}
|
|
|
|
config.Sysctls["net.ipv6.conf.all.disable_ipv6"] = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
func DockerAllowNetworkAdministration(config *docker.HostConfig) {
|
|
|
|
config.CapAdd = append(config.CapAdd, "NET_ADMIN")
|
|
|
|
config.Mounts = append(config.Mounts, docker.HostMount{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/dev/net/tun",
|
|
|
|
Target: "/dev/net/tun",
|
|
|
|
})
|
|
|
|
}
|
2022-03-04 20:54:59 +08:00
|
|
|
|
2022-03-19 00:05:28 +08:00
|
|
|
func getDockerBuildOptions(version string) *dockertest.BuildOptions {
|
|
|
|
var tailscaleBuildOptions *dockertest.BuildOptions
|
|
|
|
switch version {
|
2022-03-20 19:30:56 +08:00
|
|
|
case "head":
|
2022-03-19 00:05:28 +08:00
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale-HEAD",
|
|
|
|
ContextDir: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{},
|
|
|
|
}
|
|
|
|
case "unstable":
|
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: "*", // Installs the latest version https://askubuntu.com/a/824926
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_CHANNEL",
|
|
|
|
Value: "unstable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: version,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_CHANNEL",
|
|
|
|
Value: "stable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tailscaleBuildOptions
|
|
|
|
}
|
|
|
|
|
2022-03-04 20:54:59 +08:00
|
|
|
func getIPs(
|
|
|
|
tailscales map[string]dockertest.Resource,
|
|
|
|
) (map[string][]netaddr.IP, error) {
|
|
|
|
ips := make(map[string][]netaddr.IP)
|
|
|
|
for hostname, tailscale := range tailscales {
|
|
|
|
command := []string{"tailscale", "ip"}
|
|
|
|
|
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range strings.Split(result, "\n") {
|
|
|
|
address = strings.TrimSuffix(address, "\n")
|
|
|
|
if len(address) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip, err := netaddr.ParseIP(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ips[hostname] = append(ips[hostname], ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
2022-05-18 04:11:51 +08:00
|
|
|
|
|
|
|
func getDNSNames(
|
|
|
|
headscale *dockertest.Resource,
|
|
|
|
) ([]string, error) {
|
|
|
|
|
|
|
|
listAllResult, err := ExecuteCommand(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var listAll []v1.Machine
|
|
|
|
err = json.Unmarshal([]byte(listAllResult), &listAll)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hostnames := make([]string, len(listAll))
|
|
|
|
|
|
|
|
for index := range listAll {
|
|
|
|
hostnames[index] = listAll[index].GetGivenName()
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostnames, nil
|
|
|
|
}
|
2022-05-19 03:18:03 +08:00
|
|
|
|
|
|
|
func getMagicFQDN(
|
|
|
|
headscale *dockertest.Resource,
|
|
|
|
) ([]string, error) {
|
|
|
|
|
|
|
|
listAllResult, err := ExecuteCommand(
|
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var listAll []v1.Machine
|
|
|
|
err = json.Unmarshal([]byte(listAllResult), &listAll)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hostnames := make([]string, len(listAll))
|
|
|
|
|
|
|
|
for index := range listAll {
|
|
|
|
hostnames[index] = fmt.Sprintf("%s.%s.headscale.net", listAll[index].GetGivenName(), listAll[index].GetNamespace().GetName())
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostnames, nil
|
|
|
|
}
|