headscale/integration_general_test.go

805 lines
19 KiB
Go
Raw Normal View History

//go:build integration_general
package headscale
import (
"bytes"
2021-08-20 09:50:55 -06:00
"context"
"encoding/json"
2021-10-04 07:14:12 -06:00
"errors"
"fmt"
"log"
"net/http"
2022-09-02 01:22:34 -06:00
"net/netip"
"os"
2021-08-20 09:50:55 -06:00
"path"
"strings"
"sync"
2021-08-13 03:33:19 -06:00
"testing"
"time"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
2021-08-13 03:33:19 -06:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
2021-09-26 06:22:11 -06:00
"tailscale.com/client/tailscale/apitype"
"tailscale.com/ipn/ipnstate"
)
2021-08-13 03:33:19 -06:00
type IntegrationTestSuite struct {
suite.Suite
2021-08-20 09:50:55 -06:00
stats *suite.SuiteInformation
pool dockertest.Pool
network dockertest.Network
headscale dockertest.Resource
saveLogs bool
namespaces map[string]TestNamespace
joinWaitGroup sync.WaitGroup
2021-08-13 03:33:19 -06:00
}
2021-08-13 03:33:19 -06:00
func TestIntegrationTestSuite(t *testing.T) {
saveLogs, err := GetEnvBool("HEADSCALE_INTEGRATION_SAVE_LOG")
if err != nil {
saveLogs = false
}
2021-08-20 09:50:55 -06:00
s := new(IntegrationTestSuite)
s.namespaces = map[string]TestNamespace{
2022-02-21 16:04:10 -07:00
"thisspace": {
count: 10,
tailscales: make(map[string]dockertest.Resource),
},
2022-02-21 16:04:10 -07:00
"otherspace": {
count: 2,
tailscales: make(map[string]dockertest.Resource),
},
}
s.saveLogs = saveLogs
2021-08-20 09:50:55 -06: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.
if s.saveLogs {
for _, scales := range s.namespaces {
for _, tailscale := range scales.tailscales {
if err := s.pool.Purge(&tailscale); err != nil {
log.Printf("Could not purge resource: %s\n", err)
}
}
2021-08-20 09:50:55 -06: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)
2021-08-20 09:50:55 -06:00
}
if err := s.network.Close(); err != nil {
log.Printf("Could not close network: %s\n", err)
}
2021-08-20 09:50:55 -06:00
}
2021-08-13 03:33:19 -06:00
}
2021-11-13 01:36:45 -07:00
func (s *IntegrationTestSuite) saveLog(
resource *dockertest.Resource,
basePath string,
) error {
2021-08-20 09:50:55 -06:00
err := os.MkdirAll(basePath, os.ModePerm)
if err != nil {
return err
}
var stdout bytes.Buffer
var stderr bytes.Buffer
err = s.pool.Client.Logs(
2021-08-20 09:50:55 -06:00
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)
2021-08-20 09:50:55 -06:00
2022-08-09 15:21:19 -06:00
err = os.WriteFile(
2021-11-13 01:36:45 -07:00
path.Join(basePath, resource.Container.Name+".stdout.log"),
[]byte(stdout.String()),
0o644,
)
2021-08-20 09:50:55 -06:00
if err != nil {
return err
}
2022-08-09 15:21:19 -06:00
err = os.WriteFile(
2021-11-13 01:36:45 -07:00
path.Join(basePath, resource.Container.Name+".stderr.log"),
[]byte(stdout.String()),
0o644,
)
2021-08-20 09:50:55 -06:00
if err != nil {
return err
}
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)
2022-09-21 09:01:26 -06:00
_, _, 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) {
tailscaleBuildOptions := getDockerBuildOptions(version)
2021-11-13 01:36:45 -07:00
hostname := fmt.Sprintf(
"%s-tailscale-%s-%s",
namespace,
strings.Replace(version, ".", "-", -1),
identifier,
)
tailscaleOptions := &dockertest.RunOptions{
Name: hostname,
Networks: []*dockertest.Network{&s.network},
2021-11-13 01:36:45 -07:00
Cmd: []string{
"tailscaled", "--tun=tsdev",
2021-11-13 01:36:45 -07:00
},
ExposedPorts: []string{"8080/tcp"},
PortBindings: map[docker.Port][]docker.PortBinding{
"8080/tcp": {{HostPort: "8080"}},
},
}
err := s.pool.RemoveContainerByName(hostname)
if err != nil {
s.FailNow(
fmt.Sprintf(
"Could not remove existing container before building test: %s",
err,
),
"",
)
}
pts, err := s.pool.BuildAndRunWithBuildOptions(
2021-11-13 01:36:45 -07:00
tailscaleBuildOptions,
tailscaleOptions,
DockerRestartPolicy,
DockerAllowLocalIPv6,
DockerAllowNetworkAdministration,
2021-11-13 01:36:45 -07:00
)
if err != nil {
2022-03-20 05:31:18 -06:00
log.Fatalf("Could not start tailscale container version %s: %s", version, err)
}
log.Printf("Created %s container\n", hostname)
2021-11-14 10:35:49 -07:00
return hostname, pts
}
2021-08-13 03:33:19 -06:00
func (s *IntegrationTestSuite) SetupSuite() {
var err error
2021-11-15 09:16:04 -07:00
app = Headscale{
dbType: "sqlite3",
dbString: "integration_test_db.sqlite3",
}
if ppool, err := dockertest.NewPool(""); err == nil {
s.pool = *ppool
} else {
s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "")
}
if pnetwork, err := s.pool.CreateNetwork("headscale-test"); err == nil {
s.network = *pnetwork
} else {
s.FailNow(fmt.Sprintf("Could not create network: %s", err), "")
}
headscaleBuildOptions := &dockertest.BuildOptions{
Dockerfile: "Dockerfile",
ContextDir: ".",
}
currentPath, err := os.Getwd()
if err != nil {
s.FailNow(fmt.Sprintf("Could not determine current path: %s", err), "")
}
headscaleOptions := &dockertest.RunOptions{
Name: headscaleHostname,
Mounts: []string{
fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath),
},
Networks: []*dockertest.Network{&s.network},
2021-08-13 03:33:19 -06:00
Cmd: []string{"headscale", "serve"},
}
err = s.pool.RemoveContainerByName(headscaleHostname)
if err != nil {
2022-08-04 02:47:00 -06:00
s.FailNow(
fmt.Sprintf(
"Could not remove existing container before building test: %s",
err,
),
"",
)
}
2022-08-05 09:35:28 -06:00
log.Println("Creating headscale container for core integration tests")
if pheadscale, err := s.pool.BuildAndRunWithBuildOptions(headscaleBuildOptions, headscaleOptions, DockerRestartPolicy); err == nil {
s.headscale = *pheadscale
} else {
2022-08-05 09:35:28 -06:00
s.FailNow(fmt.Sprintf("Could not start headscale container for core integration tests: %s", err), "")
}
2022-08-05 09:35:28 -06:00
log.Println("Created headscale container for core integration tests")
2022-08-05 09:35:28 -06:00
log.Println("Creating tailscale containers for core integration tests")
for namespace, scales := range s.namespaces {
for i := 0; i < scales.count; i++ {
version := tailscaleVersions[i%len(tailscaleVersions)]
2021-11-13 01:36:45 -07:00
hostname, container := s.tailscaleContainer(
namespace,
fmt.Sprint(i),
version,
)
scales.tailscales[hostname] = *container
}
}
2022-08-05 09:35:28 -06:00
log.Println("Waiting for headscale to be ready for core integration tests")
hostEndpoint := fmt.Sprintf("%s:%s",
s.headscale.GetIPInNetwork(&s.network),
s.headscale.GetPort("8080/tcp"))
if err := s.pool.Retry(func() error {
url := fmt.Sprintf("http://%s/health", hostEndpoint)
2021-11-14 10:35:49 -07:00
resp, err := http.Get(url)
if err != nil {
2022-08-05 09:35:28 -06:00
fmt.Printf("headscale for core integration test is not ready: %s\n", err)
return err
}
2021-11-14 10:35:49 -07:00
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code not OK")
}
2021-11-14 10:35:49 -07:00
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 09:35:28 -06:00
log.Println("headscale container is ready for core integration tests")
for namespace, scales := range s.namespaces {
log.Printf("Creating headscale namespace: %s\n", namespace)
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
&s.headscale,
[]string{"headscale", "namespaces", "create", namespace},
2021-09-26 06:22:11 -06:00
[]string{},
)
log.Println("headscale create namespace result: ", result)
assert.Nil(s.T(), err)
log.Printf("Creating pre auth key for %s\n", namespace)
2022-09-21 09:01:26 -06:00
preAuthResult, _, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"--namespace",
namespace,
"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)
headscaleEndpoint := "http://headscale:8080"
log.Printf(
2021-11-13 01:36:45 -07:00
"Joining tailscale containers to headscale at %s\n",
headscaleEndpoint,
)
for hostname, tailscale := range scales.tailscales {
s.joinWaitGroup.Add(1)
go s.Join(headscaleEndpoint, preAuthKey.Key, hostname, tailscale)
}
s.joinWaitGroup.Wait()
}
2021-08-13 03:33:19 -06: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 09:50:55 -06:00
time.Sleep(60 * time.Second)
}
2021-08-13 03:33:19 -06:00
func (s *IntegrationTestSuite) TearDownSuite() {
if !s.saveLogs {
for _, scales := range s.namespaces {
for _, tailscale := range scales.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)
}
if err := s.network.Close(); err != nil {
log.Printf("Could not close network: %s\n", err)
}
}
2021-08-20 09:50:55 -06:00
}
2021-11-13 01:36:45 -07:00
func (s *IntegrationTestSuite) HandleStats(
suiteName string,
stats *suite.SuiteInformation,
) {
2021-08-20 09:50:55 -06:00
s.stats = stats
}
2021-08-13 03:33:19 -06:00
func (s *IntegrationTestSuite) TestListNodes() {
for namespace, scales := range s.namespaces {
log.Println("Listing nodes")
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
&s.headscale,
[]string{"headscale", "--namespace", namespace, "nodes", "list"},
2021-09-26 06:22:11 -06:00
[]string{},
)
assert.Nil(s.T(), err)
2021-08-13 03:33:19 -06:00
log.Printf("List nodes: \n%s\n", result)
2021-08-13 03:33:19 -06: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)
for hostname := range scales.tailscales {
assert.Contains(s.T(), result, hostname)
}
}
}
2021-08-13 03:33:19 -06:00
func (s *IntegrationTestSuite) TestGetIpAddresses() {
for _, scales := range s.namespaces {
ips, err := getIPs(scales.tailscales)
assert.Nil(s.T(), err)
2021-08-13 03:33:19 -06:00
2022-02-11 01:39:00 -07:00
for hostname := range scales.tailscales {
2022-01-16 06:16:59 -07:00
ips := ips[hostname]
for _, ip := range ips {
s.T().Run(hostname, func(t *testing.T) {
assert.NotNil(t, ip)
log.Printf("IP for %s: %s\n", hostname, ip)
2022-01-16 06:16:59 -07:00
// c.Assert(ip.Valid(), check.IsTrue)
assert.True(t, ip.Is4() || ip.Is6())
switch {
case ip.Is4():
assert.True(t, IpPrefix4.Contains(ip))
case ip.Is6():
assert.True(t, IpPrefix6.Contains(ip))
}
})
}
}
2021-08-13 03:33:19 -06:00
}
}
// TODO(kradalby): fix this test
2022-01-16 06:16:59 -07:00
// We need some way to import ipnstate.Status from multiple go packages.
// Currently it will only work with 1.18.x since that is the last
// version we have in go.mod
// func (s *IntegrationTestSuite) TestStatus() {
2022-01-16 06:16:59 -07:00
// for _, scales := range s.namespaces {
// ips, err := getIPs(scales.tailscales)
// assert.Nil(s.T(), err)
//
2022-01-16 06:16:59 -07:00
// for hostname, tailscale := range scales.tailscales {
// s.T().Run(hostname, func(t *testing.T) {
// command := []string{"tailscale", "status", "--json"}
//
// log.Printf("Getting status for %s\n", hostname)
2022-01-16 06:16:59 -07:00
// result, err := ExecuteCommand(
// &tailscale,
// command,
// []string{},
// )
// assert.Nil(t, err)
//
2022-01-16 06:16:59 -07:00
// var status ipnstate.Status
// err = json.Unmarshal([]byte(result), &status)
// assert.Nil(s.T(), err)
//
2022-01-16 06:16:59 -07:00
// // TODO(kradalby): Replace this check with peer length of SAME namespace
// // Check if we have as many nodes in status
// // as we have IPs/tailscales
// // lines := strings.Split(result, "\n")
// // assert.Equal(t, len(ips), len(lines)-1)
// // assert.Equal(t, len(scales.tailscales), len(lines)-1)
//
2022-01-16 06:16:59 -07:00
// peerIps := getIPsfromIPNstate(status)
//
2022-01-16 06:16:59 -07:00
// // Check that all hosts is present in all hosts status
// for ipHostname, ip := range ips {
// if hostname != ipHostname {
// assert.Contains(t, peerIps, ip)
// }
// }
// })
// }
// }
// }
2021-08-13 03:33:19 -06:00
2022-09-02 01:22:34 -06:00
func getIPsfromIPNstate(status ipnstate.Status) []netip.Addr {
ips := make([]netip.Addr, 0)
for _, peer := range status.Peer {
ips = append(ips, peer.TailscaleIPs...)
}
return ips
}
2022-02-21 16:04:10 -07:00
// TODO: Adopt test for cross communication between namespaces
2022-01-16 06:16:59 -07:00
func (s *IntegrationTestSuite) TestPingAllPeersByAddress() {
for _, scales := range s.namespaces {
ips, err := getIPs(scales.tailscales)
assert.Nil(s.T(), err)
2021-08-13 04:01:23 -06:00
for hostname, tailscale := range scales.tailscales {
2022-01-16 06:16:59 -07:00
for peername, peerIPs := range ips {
for i, ip := range peerIPs {
// We currently cant ping ourselves, so skip that.
2022-01-16 06:16:59 -07:00
if peername == hostname {
continue
}
2022-02-11 01:39:00 -07:00
s.T().
Run(fmt.Sprintf("%s-%s-%d", hostname, peername, i), func(t *testing.T) {
// 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=10",
"--until-direct=true",
ip.String(),
}
log.Printf(
2022-02-11 01:39:00 -07:00
"Pinging from %s to %s (%s)\n",
hostname,
peername,
ip,
)
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
2022-02-11 01:39:00 -07:00
&tailscale,
command,
[]string{},
)
assert.Nil(t, err)
log.Printf("Result for %s: %s\n", hostname, result)
2022-02-11 01:39:00 -07:00
assert.Contains(t, result, "pong")
})
2022-01-16 06:16:59 -07:00
}
}
2021-08-13 04:01:23 -06:00
}
}
}
2021-08-13 03:33:19 -06:00
2021-09-25 05:12:44 -06:00
func (s *IntegrationTestSuite) TestTailDrop() {
for _, scales := range s.namespaces {
ips, err := getIPs(scales.tailscales)
assert.Nil(s.T(), err)
retry := func(times int, sleepInverval time.Duration, doWork func() error) (err error) {
for attempts := 0; attempts < times; attempts++ {
err = doWork()
if err == nil {
return
}
time.Sleep(sleepInverval)
}
return
}
2021-09-25 05:12:44 -06:00
for hostname, tailscale := range scales.tailscales {
command := []string{"touch", fmt.Sprintf("/tmp/file_from_%s", hostname)}
2022-09-21 09:01:26 -06:00
_, _, err := ExecuteCommand(
2021-09-25 05:12:44 -06:00
&tailscale,
command,
[]string{},
2021-09-25 05:12:44 -06:00
)
assert.Nil(s.T(), err)
2022-02-11 01:39:00 -07:00
for peername := range ips {
if peername == hostname {
continue
}
2022-08-18 09:53:36 -06:00
2022-09-02 01:22:34 -06:00
var ip4 netip.Addr
2022-08-18 09:53:36 -06:00
for _, ip := range ips[peername] {
if ip.Is4() {
ip4 = ip
break
}
}
2022-09-02 01:22:34 -06:00
if ip4.IsUnspecified() {
2022-08-18 09:53:36 -06:00
panic("no ipv4 address found")
}
2021-09-25 05:12:44 -06:00
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
command := []string{
"tailscale", "file", "cp",
fmt.Sprintf("/tmp/file_from_%s", hostname),
2022-08-18 09:53:36 -06:00
fmt.Sprintf("%s:", ip4),
2021-09-25 05:12:44 -06:00
}
2022-08-18 09:56:01 -06:00
err := retry(10, 1*time.Second, func() error {
log.Printf(
"Sending file from %s to %s\n",
hostname,
peername,
)
2022-09-21 09:01:26 -06:00
_, _, err := ExecuteCommand(
&tailscale,
command,
[]string{},
ExecuteCommandTimeout(60*time.Second),
)
return err
})
2022-08-18 09:56:01 -06:00
assert.Nil(t, err)
2021-09-25 05:12:44 -06:00
})
}
}
for hostname, tailscale := range scales.tailscales {
command := []string{
"tailscale", "file",
"get",
2021-09-26 06:22:11 -06:00
"/tmp/",
2021-09-25 05:12:44 -06:00
}
2022-09-21 09:01:26 -06:00
_, _, err := ExecuteCommand(
2021-09-25 05:12:44 -06:00
&tailscale,
command,
2021-09-26 06:22:11 -06:00
[]string{},
2021-09-25 05:12:44 -06:00
)
assert.Nil(s.T(), err)
for peername, ip := range ips {
2022-01-16 06:16:59 -07:00
if peername == hostname {
continue
}
2021-09-25 05:12:44 -06:00
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
2022-01-16 06:16:59 -07:00
command := []string{
"ls",
fmt.Sprintf("/tmp/file_from_%s", peername),
2021-09-25 05:12:44 -06:00
}
log.Printf(
2022-01-16 06:16:59 -07:00
"Checking file in %s (%s) from %s (%s)\n",
hostname,
ips[hostname][1],
2022-01-16 06:16:59 -07:00
peername,
ip,
)
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
2022-01-16 06:16:59 -07:00
&tailscale,
command,
[]string{},
)
assert.Nil(t, err)
log.Printf("Result for %s: %s\n", peername, result)
2022-01-16 06:16:59 -07:00
assert.Equal(
t,
fmt.Sprintf("/tmp/file_from_%s\n", peername),
result,
)
2021-09-25 05:12:44 -06:00
})
}
}
}
}
2022-01-16 06:16:59 -07:00
func (s *IntegrationTestSuite) TestPingAllPeersByHostname() {
hostnames, err := getMagicFQDN(&s.headscale)
assert.Nil(s.T(), err)
log.Printf("Resolved hostnames: %#v", hostnames)
for _, scales := range s.namespaces {
2021-10-04 10:04:08 -06:00
for hostname, tailscale := range scales.tailscales {
for _, peername := range hostnames {
if strings.Contains(peername, hostname) {
2022-01-16 06:16:59 -07:00
continue
}
2021-10-04 10:04:08 -06:00
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
2022-01-16 06:16:59 -07:00
command := []string{
"tailscale", "ping",
"--timeout=10s",
"--c=20",
"--until-direct=true",
peername,
2022-01-16 06:16:59 -07:00
}
log.Printf(
"Pinging using hostname from %s to %s\n",
2022-01-16 06:16:59 -07:00
hostname,
peername,
)
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
2022-01-16 06:16:59 -07:00
&tailscale,
command,
[]string{},
)
assert.Nil(t, err)
log.Printf("Result for %s: %s\n", hostname, result)
2022-01-16 06:16:59 -07:00
assert.Contains(t, result, "pong")
})
}
}
}
}
func (s *IntegrationTestSuite) TestMagicDNS() {
2022-05-18 13:18:03 -06:00
hostnames, err := getMagicFQDN(&s.headscale)
assert.Nil(s.T(), err)
log.Printf("Resolved hostnames: %#v", hostnames)
2022-05-18 12:14:18 -06:00
2022-05-18 13:18:03 -06:00
for _, scales := range s.namespaces {
ips, err := getIPs(scales.tailscales)
2022-05-18 12:14:18 -06:00
assert.Nil(s.T(), err)
2022-08-16 10:19:04 -06:00
retry := func(times int, sleepInverval time.Duration, doWork func() (string, error)) (result string, err error) {
for attempts := 0; attempts < times; attempts++ {
result, err = doWork()
if err == nil {
return
}
time.Sleep(sleepInverval)
}
return
}
2022-01-16 06:16:59 -07:00
for hostname, tailscale := range scales.tailscales {
2022-05-18 12:14:18 -06:00
for _, peername := range hostnames {
if strings.Contains(peername, hostname) {
2022-01-16 06:16:59 -07:00
continue
}
2022-05-18 13:18:03 -06:00
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
2022-01-16 06:16:59 -07:00
command := []string{
2022-05-18 13:18:03 -06:00
"tailscale", "ip", peername,
2021-10-04 10:04:08 -06:00
}
2022-08-16 10:19:04 -06:00
result, err := retry(10, 1*time.Second, func() (string, error) {
log.Printf(
"Resolving name %s from %s\n",
peername,
hostname,
)
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
2022-08-16 10:19:04 -06:00
&tailscale,
command,
[]string{},
)
return result, err
})
2022-01-16 06:16:59 -07:00
assert.Nil(t, err)
log.Printf("Result for %s: %s\n", hostname, result)
2022-01-16 06:16:59 -07:00
2022-05-18 12:14:18 -06:00
peerBaseName := peername[:len(peername)-MachineGivenNameHashLength-1]
expectedAddresses := ips[peerBaseName]
for _, ip := range expectedAddresses {
assert.Contains(t, result, ip.String())
2022-01-16 06:16:59 -07:00
}
2021-10-04 10:04:08 -06:00
})
}
}
}
}
2021-11-13 01:36:45 -07:00
func getAPIURLs(
tailscales map[string]dockertest.Resource,
2022-09-02 01:22:34 -06:00
) (map[netip.Addr]string, error) {
fts := make(map[netip.Addr]string)
2021-09-26 06:22:11 -06:00
for _, tailscale := range tailscales {
2021-09-26 07:17:27 -06:00
command := []string{
2021-09-26 04:22:59 -06:00
"curl",
"--unix-socket",
"/run/tailscale/tailscaled.sock",
"http://localhost/localapi/v0/file-targets",
}
2022-09-21 09:01:26 -06:00
result, _, err := ExecuteCommand(
2021-09-26 04:22:59 -06:00
&tailscale,
command,
2021-09-26 06:22:11 -06:00
[]string{},
2021-09-26 04:22:59 -06:00
)
if err != nil {
return nil, err
}
2021-09-26 06:33:01 -06:00
2021-09-26 04:22:59 -06:00
var pft []apitype.FileTarget
2021-09-26 06:22:11 -06:00
if err := json.Unmarshal([]byte(result), &pft); err != nil {
2021-09-26 04:22:59 -06:00
return nil, fmt.Errorf("invalid JSON: %w", err)
}
for _, ft := range pft {
n := ft.Node
2021-09-26 06:22:11 -06:00
for _, a := range n.Addresses { // just add all the addresses
2022-09-02 01:22:34 -06:00
if _, ok := fts[a.Addr()]; !ok {
2021-10-04 07:14:12 -06:00
if ft.PeerAPIURL == "" {
return nil, errors.New("api url is empty")
}
2022-09-02 01:22:34 -06:00
fts[a.Addr()] = ft.PeerAPIURL
2021-09-26 04:22:59 -06:00
}
}
}
}
2021-11-14 10:35:49 -07:00
2021-09-26 06:22:11 -06:00
return fts, nil
2021-09-26 04:22:59 -06:00
}