2021-11-04 16:10:57 -06:00
|
|
|
//go:build integration
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-01-15 08:25:38 -07:00
|
|
|
"inet.af/netaddr"
|
|
|
|
|
2021-11-04 16:10:57 -06:00
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
|
|
|
)
|
|
|
|
|
2021-11-14 10:35:49 -07:00
|
|
|
const DOCKER_EXECUTE_TIMEOUT = 10 * time.Second
|
|
|
|
|
2022-01-15 08:25:38 -07:00
|
|
|
var IpPrefix4 = netaddr.MustParseIPPrefix("100.64.0.0/10")
|
|
|
|
var IpPrefix6 = netaddr.MustParseIPPrefix("fd7a:115c:a1e0::/48")
|
|
|
|
|
|
|
|
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 01:36:45 -07:00
|
|
|
func ExecuteCommand(
|
|
|
|
resource *dockertest.Resource,
|
|
|
|
cmd []string,
|
|
|
|
env []string,
|
2022-01-15 08:25:38 -07:00
|
|
|
options ...ExecuteCommandOption,
|
2021-11-13 01:36:45 -07:00
|
|
|
) (string, error) {
|
2021-11-04 16:10:57 -06:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2022-01-15 08:25:38 -07: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-04 16:10:57 -06: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 03:40:05 -07:00
|
|
|
Env: append(env, "HEADSCALE_LOG_LEVEL=disabled"),
|
2021-11-04 16:10:57 -06: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-14 10:35:49 -07:00
|
|
|
|
2021-11-04 16:10:57 -06:00
|
|
|
return "", fmt.Errorf("command failed with: %s", stderr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout.String(), nil
|
2022-01-15 08:25:38 -07:00
|
|
|
case <-time.After(execConfig.timeout):
|
2021-11-14 10:35:49 -07:00
|
|
|
|
2022-01-15 08:25:38 -07:00
|
|
|
return "", fmt.Errorf("command timed out after %s", execConfig.timeout)
|
2021-11-04 16:10:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DockerRestartPolicy(config *docker.HostConfig) {
|
2022-01-15 08:25:38 -07: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-04 16:10:57 -06:00
|
|
|
config.RestartPolicy = docker.RestartPolicy{
|
|
|
|
Name: "no",
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 08:25:38 -07: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",
|
|
|
|
})
|
|
|
|
}
|