2021-09-20 14:23:18 -04:00
|
|
|
//go:build integration
|
2021-08-08 12:50:32 -04:00
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-08-20 11:50:55 -04:00
|
|
|
"context"
|
2021-09-20 17:53:34 -04:00
|
|
|
"encoding/json"
|
2021-10-04 09:14:12 -04:00
|
|
|
"errors"
|
2021-08-08 12:50:32 -04:00
|
|
|
"fmt"
|
2021-08-20 11:50:55 -04:00
|
|
|
"io/ioutil"
|
2021-08-08 12:50:32 -04:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-08-20 11:50:55 -04:00
|
|
|
"path"
|
2021-08-08 12:50:32 -04:00
|
|
|
"strings"
|
2021-08-13 05:33:19 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-08-08 12:50:32 -04:00
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2021-08-13 05:33:19 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2021-11-14 12:35:49 -05:00
|
|
|
"inet.af/netaddr"
|
2021-09-26 08:22:11 -04:00
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2021-09-20 17:53:34 -04:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-08-08 12:50:32 -04:00
|
|
|
)
|
|
|
|
|
2022-01-13 14:16:12 -05:00
|
|
|
var tailscaleVersions = []string{"1.20.0", "1.18.2", "1.16.2", "1.14.3", "1.12.3"}
|
2021-08-21 04:15:16 -04:00
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
type TestNamespace struct {
|
|
|
|
count int
|
|
|
|
tailscales map[string]dockertest.Resource
|
|
|
|
}
|
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
type IntegrationTestSuite struct {
|
|
|
|
suite.Suite
|
2021-08-20 11:50:55 -04:00
|
|
|
stats *suite.SuiteInformation
|
2021-09-20 15:18:28 -04:00
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
pool dockertest.Pool
|
|
|
|
network dockertest.Network
|
|
|
|
headscale dockertest.Resource
|
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
namespaces map[string]TestNamespace
|
2021-08-13 05:33:19 -04:00
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
func TestIntegrationTestSuite(t *testing.T) {
|
2021-08-20 11:50:55 -04:00
|
|
|
s := new(IntegrationTestSuite)
|
2021-09-20 15:18:28 -04:00
|
|
|
|
|
|
|
s.namespaces = map[string]TestNamespace{
|
|
|
|
"main": {
|
2021-10-16 05:06:33 -04:00
|
|
|
count: 20,
|
2021-09-20 15:18:28 -04:00
|
|
|
tailscales: make(map[string]dockertest.Resource),
|
|
|
|
},
|
|
|
|
"shared": {
|
2021-10-16 05:06:33 -04:00
|
|
|
count: 5,
|
2021-09-20 15:18:28 -04:00
|
|
|
tailscales: make(map[string]dockertest.Resource),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-20 11:50:55 -04: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 15:18:28 -04:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
for _, tailscale := range scales.tailscales {
|
2021-11-04 18:10:57 -04:00
|
|
|
if err := s.pool.Purge(&tailscale); err != nil {
|
2021-09-20 15:18:28 -04:00
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
2021-08-20 11:50:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s.stats.Passed() {
|
2021-11-04 18:10:57 -04:00
|
|
|
err := s.saveLog(&s.headscale, "test_output")
|
2021-08-20 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not save log: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 18:10:57 -04:00
|
|
|
if err := s.pool.Purge(&s.headscale); err != nil {
|
2021-08-20 11:50:55 -04:00
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
if err := s.network.Close(); err != nil {
|
2021-08-20 11:50:55 -04:00
|
|
|
log.Printf("Could not close network: %s\n", err)
|
|
|
|
}
|
2021-08-13 05:33:19 -04:00
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
func (s *IntegrationTestSuite) saveLog(
|
|
|
|
resource *dockertest.Resource,
|
|
|
|
basePath string,
|
|
|
|
) error {
|
2021-08-20 11:50:55 -04:00
|
|
|
err := os.MkdirAll(basePath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
err = s.pool.Client.Logs(
|
2021-08-20 11:50:55 -04: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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Saving logs for %s to %s\n", resource.Container.Name, basePath)
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
err = ioutil.WriteFile(
|
|
|
|
path.Join(basePath, resource.Container.Name+".stdout.log"),
|
|
|
|
[]byte(stdout.String()),
|
|
|
|
0o644,
|
|
|
|
)
|
2021-08-20 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
err = ioutil.WriteFile(
|
|
|
|
path.Join(basePath, resource.Container.Name+".stderr.log"),
|
|
|
|
[]byte(stdout.String()),
|
|
|
|
0o644,
|
|
|
|
)
|
2021-08-20 11:50:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
func (s *IntegrationTestSuite) tailscaleContainer(
|
|
|
|
namespace, identifier, version string,
|
|
|
|
) (string, *dockertest.Resource) {
|
2021-09-20 14:23:18 -04:00
|
|
|
tailscaleBuildOptions := &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: version,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2021-11-13 03:36:45 -05:00
|
|
|
hostname := fmt.Sprintf(
|
|
|
|
"%s-tailscale-%s-%s",
|
|
|
|
namespace,
|
|
|
|
strings.Replace(version, ".", "-", -1),
|
|
|
|
identifier,
|
|
|
|
)
|
2021-09-20 14:23:18 -04:00
|
|
|
tailscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: hostname,
|
2021-11-04 18:10:57 -04:00
|
|
|
Networks: []*dockertest.Network{&s.network},
|
2021-11-13 03:36:45 -05:00
|
|
|
Cmd: []string{
|
2022-01-15 10:25:38 -05:00
|
|
|
"tailscaled", "--tun=tsdev",
|
2021-11-13 03:36:45 -05:00
|
|
|
},
|
2021-09-20 14:23:18 -04:00
|
|
|
}
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
pts, err := s.pool.BuildAndRunWithBuildOptions(
|
|
|
|
tailscaleBuildOptions,
|
|
|
|
tailscaleOptions,
|
|
|
|
DockerRestartPolicy,
|
2022-01-15 10:25:38 -05:00
|
|
|
DockerAllowLocalIPv6,
|
|
|
|
DockerAllowNetworkAdministration,
|
2021-11-13 03:36:45 -05:00
|
|
|
)
|
2021-09-20 14:23:18 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not start resource: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Created %s container\n", hostname)
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-09-20 14:23:18 -04:00
|
|
|
return hostname, pts
|
|
|
|
}
|
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
func (s *IntegrationTestSuite) SetupSuite() {
|
2021-08-08 12:50:32 -04:00
|
|
|
var err error
|
2021-11-15 11:16:04 -05:00
|
|
|
app = Headscale{
|
2021-08-08 12:50:32 -04:00
|
|
|
dbType: "sqlite3",
|
|
|
|
dbString: "integration_test_db.sqlite3",
|
|
|
|
}
|
|
|
|
|
|
|
|
if ppool, err := dockertest.NewPool(""); err == nil {
|
2021-11-04 18:10:57 -04:00
|
|
|
s.pool = *ppool
|
2021-08-08 12:50:32 -04:00
|
|
|
} else {
|
|
|
|
log.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
if pnetwork, err := s.pool.CreateNetwork("headscale-test"); err == nil {
|
|
|
|
s.network = *pnetwork
|
2021-08-08 12:50:32 -04:00
|
|
|
} 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),
|
|
|
|
},
|
2021-11-04 18:10:57 -04:00
|
|
|
Networks: []*dockertest.Network{&s.network},
|
2021-08-13 05:33:19 -04:00
|
|
|
Cmd: []string{"headscale", "serve"},
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Creating headscale container")
|
2021-11-04 18:10:57 -04:00
|
|
|
if pheadscale, err := s.pool.BuildAndRunWithBuildOptions(headscaleBuildOptions, headscaleOptions, DockerRestartPolicy); err == nil {
|
|
|
|
s.headscale = *pheadscale
|
2021-08-08 12:50:32 -04:00
|
|
|
} else {
|
|
|
|
log.Fatalf("Could not start resource: %s", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Created headscale container")
|
|
|
|
|
|
|
|
fmt.Println("Creating tailscale containers")
|
2021-09-20 15:18:28 -04:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
for i := 0; i < scales.count; i++ {
|
|
|
|
version := tailscaleVersions[i%len(tailscaleVersions)]
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
hostname, container := s.tailscaleContainer(
|
|
|
|
namespace,
|
|
|
|
fmt.Sprint(i),
|
|
|
|
version,
|
|
|
|
)
|
2021-09-20 15:18:28 -04:00
|
|
|
scales.tailscales[hostname] = *container
|
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Waiting for headscale to be ready")
|
2021-11-04 18:10:57 -04:00
|
|
|
hostEndpoint := fmt.Sprintf("localhost:%s", s.headscale.GetPort("8080/tcp"))
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
if err := s.pool.Retry(func() error {
|
2021-08-08 12:50:32 -04:00
|
|
|
url := fmt.Sprintf("http://%s/health", hostEndpoint)
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-08-08 12:50:32 -04:00
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-08-08 12:50:32 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("status code not OK")
|
|
|
|
}
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-08-08 12:50:32 -04:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-10-04 09:16:37 -04: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 12:50:32 -04:00
|
|
|
}
|
|
|
|
fmt.Println("headscale container is ready")
|
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
fmt.Printf("Creating headscale namespace: %s\n", namespace)
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
2021-09-20 15:18:28 -04:00
|
|
|
[]string{"headscale", "namespaces", "create", namespace},
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 15:18:28 -04:00
|
|
|
)
|
|
|
|
fmt.Println("headscale create namespace result: ", result)
|
2021-10-29 12:49:44 -04:00
|
|
|
assert.Nil(s.T(), err)
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
fmt.Printf("Creating pre auth key for %s\n", namespace)
|
2021-11-04 18:10:57 -04:00
|
|
|
preAuthResult, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
2021-10-22 12:57:51 -04:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"preauthkeys",
|
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
2021-11-04 18:10:57 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
2021-10-22 12:57:51 -04:00
|
|
|
},
|
2021-11-04 18:10:57 -04:00
|
|
|
[]string{"LOG_LEVEL=error"},
|
2021-09-20 15:18:28 -04:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(preAuthResult), &preAuthKey)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-11-08 15:49:03 -05:00
|
|
|
assert.True(s.T(), preAuthKey.Reusable)
|
2021-11-04 18:10:57 -04:00
|
|
|
|
2021-10-04 10:39:52 -04:00
|
|
|
headscaleEndpoint := "http://headscale:8080"
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
fmt.Printf(
|
|
|
|
"Joining tailscale containers to headscale at %s\n",
|
|
|
|
headscaleEndpoint,
|
|
|
|
)
|
2021-09-20 15:18:28 -04:00
|
|
|
for hostname, tailscale := range scales.tailscales {
|
2021-10-22 12:57:51 -04:00
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"up",
|
|
|
|
"-login-server",
|
|
|
|
headscaleEndpoint,
|
|
|
|
"--authkey",
|
2021-11-04 18:10:57 -04:00
|
|
|
preAuthKey.Key,
|
2021-10-22 12:57:51 -04:00
|
|
|
"--hostname",
|
|
|
|
hostname,
|
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
fmt.Println("Join command:", command)
|
|
|
|
fmt.Printf("Running join command for %s\n", hostname)
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
2021-09-20 15:18:28 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 15:18:28 -04:00
|
|
|
)
|
|
|
|
fmt.Println("tailscale result: ", result)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
fmt.Printf("%s joined\n", hostname)
|
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
2021-08-13 05:33:19 -04: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 11:50:55 -04:00
|
|
|
time.Sleep(60 * time.Second)
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
func (s *IntegrationTestSuite) TearDownSuite() {
|
2021-08-20 11:50:55 -04:00
|
|
|
}
|
2021-08-18 18:17:09 -04:00
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
func (s *IntegrationTestSuite) HandleStats(
|
|
|
|
suiteName string,
|
|
|
|
stats *suite.SuiteInformation,
|
|
|
|
) {
|
2021-08-20 11:50:55 -04:00
|
|
|
s.stats = stats
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
func (s *IntegrationTestSuite) TestListNodes() {
|
2021-09-20 15:18:28 -04:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
fmt.Println("Listing nodes")
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
2021-09-20 17:53:34 -04:00
|
|
|
[]string{"headscale", "--namespace", namespace, "nodes", "list"},
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 15:18:28 -04:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 05:33:19 -04:00
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
fmt.Printf("List nodes: \n%s\n", result)
|
2021-08-13 05:33:19 -04:00
|
|
|
|
2021-09-20 15:18:28 -04: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 12:50:32 -04:00
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
for hostname := range scales.tailscales {
|
|
|
|
assert.Contains(s.T(), result, hostname)
|
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
func (s *IntegrationTestSuite) TestGetIpAddresses() {
|
2021-09-20 15:18:28 -04:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 05:33:19 -04:00
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
for hostname, _ := range scales.tailscales {
|
|
|
|
ips := ips[hostname]
|
|
|
|
for _, ip := range ips {
|
|
|
|
s.T().Run(hostname, func(t *testing.T) {
|
|
|
|
assert.NotNil(t, ip)
|
|
|
|
|
|
|
|
fmt.Printf("IP for %s: %s\n", hostname, ip)
|
|
|
|
|
|
|
|
// 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-09-20 15:18:28 -04:00
|
|
|
}
|
2021-08-13 05:33:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:19 -05:00
|
|
|
// TODO(kradalby): fix this test
|
2022-01-16 08:16:59 -05:00
|
|
|
// We need some way to import ipnstate.Status from multiple go packages.
|
2021-11-27 16:04:19 -05:00
|
|
|
// 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 08:16:59 -05:00
|
|
|
// for _, scales := range s.namespaces {
|
|
|
|
// ips, err := getIPs(scales.tailscales)
|
|
|
|
// assert.Nil(s.T(), err)
|
2021-11-27 16:04:19 -05:00
|
|
|
//
|
2022-01-16 08:16:59 -05:00
|
|
|
// for hostname, tailscale := range scales.tailscales {
|
|
|
|
// s.T().Run(hostname, func(t *testing.T) {
|
|
|
|
// command := []string{"tailscale", "status", "--json"}
|
2021-11-27 16:04:19 -05:00
|
|
|
//
|
2022-01-16 08:16:59 -05:00
|
|
|
// fmt.Printf("Getting status for %s\n", hostname)
|
|
|
|
// result, err := ExecuteCommand(
|
|
|
|
// &tailscale,
|
|
|
|
// command,
|
|
|
|
// []string{},
|
|
|
|
// )
|
|
|
|
// assert.Nil(t, err)
|
2021-11-27 16:04:19 -05:00
|
|
|
//
|
2022-01-16 08:16:59 -05:00
|
|
|
// var status ipnstate.Status
|
|
|
|
// err = json.Unmarshal([]byte(result), &status)
|
|
|
|
// assert.Nil(s.T(), err)
|
2021-11-27 16:04:19 -05:00
|
|
|
//
|
2022-01-16 08:16:59 -05: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)
|
2021-11-27 16:04:19 -05:00
|
|
|
//
|
2022-01-16 08:16:59 -05:00
|
|
|
// peerIps := getIPsfromIPNstate(status)
|
2021-11-27 16:04:19 -05:00
|
|
|
//
|
2022-01-16 08:16:59 -05: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-11-27 16:04:19 -05:00
|
|
|
// }
|
2021-08-13 05:33:19 -04:00
|
|
|
|
2021-09-20 17:53:34 -04: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
|
|
|
|
}
|
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
func (s *IntegrationTestSuite) TestPingAllPeersByAddress() {
|
2021-09-20 15:18:28 -04:00
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-08-13 06:01:23 -04:00
|
|
|
|
2021-09-20 15:18:28 -04:00
|
|
|
for hostname, tailscale := range scales.tailscales {
|
2022-01-16 08:16:59 -05:00
|
|
|
for peername, peerIPs := range ips {
|
|
|
|
for i, ip := range peerIPs {
|
2021-09-20 15:18:28 -04:00
|
|
|
// We currently cant ping ourselves, so skip that.
|
2022-01-16 08:16:59 -05:00
|
|
|
if peername == hostname {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s-%d", hostname, peername, i), func(t *testing.T) {
|
2021-09-20 15:18:28 -04:00
|
|
|
// 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 17:56:48 -04:00
|
|
|
"--c=10",
|
2021-09-20 15:18:28 -04:00
|
|
|
"--until-direct=true",
|
|
|
|
ip.String(),
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
fmt.Printf(
|
2022-01-16 08:16:59 -05:00
|
|
|
"Pinging from %s to %s (%s)\n",
|
2021-11-13 03:36:45 -05:00
|
|
|
hostname,
|
|
|
|
peername,
|
|
|
|
ip,
|
|
|
|
)
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
2021-09-20 15:18:28 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 15:18:28 -04:00
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
assert.Contains(t, result, "pong")
|
2022-01-16 08:16:59 -05:00
|
|
|
})
|
|
|
|
}
|
2021-09-20 15:18:28 -04:00
|
|
|
}
|
2021-08-13 06:01:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-13 05:33:19 -04:00
|
|
|
|
2021-09-20 18:21:25 -04:00
|
|
|
func (s *IntegrationTestSuite) TestSharedNodes() {
|
|
|
|
main := s.namespaces["main"]
|
|
|
|
shared := s.namespaces["shared"]
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
2021-11-13 03:36:45 -05:00
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--namespace",
|
|
|
|
"shared",
|
|
|
|
},
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 18:21:25 -04:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
var machineList []v1.Machine
|
2021-09-20 18:21:25 -04:00
|
|
|
err = json.Unmarshal([]byte(result), &machineList)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
for _, machine := range machineList {
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"share",
|
|
|
|
"--identifier", fmt.Sprint(machine.Id),
|
|
|
|
"--namespace", "main",
|
|
|
|
},
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 18:21:25 -04:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
fmt.Println("Shared node with result: ", result)
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err = ExecuteCommand(
|
|
|
|
&s.headscale,
|
2021-09-20 18:21:25 -04:00
|
|
|
[]string{"headscale", "nodes", "list", "--namespace", "main"},
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-20 18:21:25 -04: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)
|
|
|
|
}
|
2021-10-16 05:36:16 -04:00
|
|
|
|
|
|
|
// TODO(juanfont): We have to find out why do we need to wait
|
2021-10-14 16:37:44 -04:00
|
|
|
time.Sleep(100 * time.Second) // Wait for the nodes to receive updates
|
|
|
|
|
|
|
|
sharedIps, err := getIPs(shared.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-09-20 18:21:25 -04:00
|
|
|
|
2021-10-14 16:37:44 -04:00
|
|
|
for hostname, tailscale := range main.tailscales {
|
2022-01-16 08:16:59 -05:00
|
|
|
for peername, peerIPs := range sharedIps {
|
|
|
|
for i, ip := range peerIPs {
|
2021-10-14 16:37:44 -04:00
|
|
|
// We currently cant ping ourselves, so skip that.
|
2022-01-16 08:16:59 -05:00
|
|
|
if peername == hostname {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s-%d", hostname, peername, i), func(t *testing.T) {
|
2021-10-14 16:37:44 -04:00
|
|
|
// 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=15s",
|
|
|
|
"--c=20",
|
|
|
|
"--until-direct=true",
|
|
|
|
ip.String(),
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
fmt.Printf(
|
2022-01-16 08:16:59 -05:00
|
|
|
"Pinging from %s to %s (%s)\n",
|
2021-11-13 03:36:45 -05:00
|
|
|
hostname,
|
|
|
|
peername,
|
|
|
|
ip,
|
|
|
|
)
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
2021-10-14 16:37:44 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
assert.Contains(t, result, "pong")
|
2022-01-16 08:16:59 -05:00
|
|
|
})
|
|
|
|
}
|
2021-10-14 16:37:44 -04:00
|
|
|
}
|
|
|
|
}
|
2021-09-20 18:21:25 -04:00
|
|
|
}
|
|
|
|
|
2021-09-25 07:12:44 -04:00
|
|
|
func (s *IntegrationTestSuite) TestTailDrop() {
|
|
|
|
for _, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
2021-09-26 06:22:59 -04:00
|
|
|
assert.Nil(s.T(), err)
|
2021-09-25 07:12:44 -04:00
|
|
|
|
2022-01-15 10:28:26 -05:00
|
|
|
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 07:12:44 -04:00
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
command := []string{"touch", fmt.Sprintf("/tmp/file_from_%s", hostname)}
|
2021-11-04 18:10:57 -04:00
|
|
|
_, err := ExecuteCommand(
|
2021-09-25 07:12:44 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2022-01-15 10:28:26 -05:00
|
|
|
[]string{},
|
2021-09-25 07:12:44 -04:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
2022-01-15 10:28:26 -05:00
|
|
|
for peername, _ := range ips {
|
|
|
|
if peername == hostname {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-25 07:12:44 -04:00
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
2022-01-15 10:28:26 -05:00
|
|
|
command := []string{
|
|
|
|
"tailscale", "file", "cp",
|
|
|
|
fmt.Sprintf("/tmp/file_from_%s", hostname),
|
|
|
|
fmt.Sprintf("%s:", peername),
|
2021-09-25 07:12:44 -04:00
|
|
|
}
|
2022-01-15 10:28:26 -05:00
|
|
|
retry(10, 1*time.Second, func() error {
|
|
|
|
fmt.Printf(
|
|
|
|
"Sending file from %s to %s\n",
|
|
|
|
hostname,
|
|
|
|
peername,
|
|
|
|
)
|
|
|
|
_, err := ExecuteCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
ExecuteCommandTimeout(60*time.Second),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
2021-09-25 07:12:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "file",
|
|
|
|
"get",
|
2021-09-26 08:22:11 -04:00
|
|
|
"/tmp/",
|
2021-09-25 07:12:44 -04:00
|
|
|
}
|
2021-11-04 18:10:57 -04:00
|
|
|
_, err := ExecuteCommand(
|
2021-09-25 07:12:44 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-25 07:12:44 -04:00
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
for peername, ip := range ips {
|
2022-01-16 08:16:59 -05:00
|
|
|
if peername == hostname {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-25 07:12:44 -04:00
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
2022-01-16 08:16:59 -05:00
|
|
|
command := []string{
|
|
|
|
"ls",
|
|
|
|
fmt.Sprintf("/tmp/file_from_%s", peername),
|
2021-09-25 07:12:44 -04:00
|
|
|
}
|
2022-01-16 08:16:59 -05:00
|
|
|
fmt.Printf(
|
|
|
|
"Checking file in %s (%s) from %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", peername, result)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
fmt.Sprintf("/tmp/file_from_%s\n", peername),
|
|
|
|
result,
|
|
|
|
)
|
2021-09-25 07:12:44 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
func (s *IntegrationTestSuite) TestPingAllPeersByHostname() {
|
2021-10-04 12:04:08 -04:00
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
2022-01-16 08:16:59 -05:00
|
|
|
for peername, _ := range ips {
|
|
|
|
if peername == hostname {
|
|
|
|
continue
|
|
|
|
}
|
2021-10-04 12:04:08 -04:00
|
|
|
s.T().Run(fmt.Sprintf("%s-%s", hostname, peername), func(t *testing.T) {
|
2022-01-16 08:16:59 -05:00
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=10s",
|
|
|
|
"--c=20",
|
|
|
|
"--until-direct=true",
|
|
|
|
fmt.Sprintf("%s.%s.headscale.net", peername, namespace),
|
|
|
|
}
|
2021-10-02 05:20:42 -04:00
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
fmt.Printf(
|
|
|
|
"Pinging using Hostname from %s to %s\n",
|
|
|
|
hostname,
|
|
|
|
peername,
|
|
|
|
)
|
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
assert.Contains(t, result, "pong")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// * With manual testing, MagicDNS does not respond to AAAA queries. Why?
|
|
|
|
// * Tailscaled only adds a route to the IPv4 (100.100.100.100) address of the MagicDNS service,
|
|
|
|
// event though there is an IPv6 one (fd7a:115c:a1e0::53) as well.
|
|
|
|
func (s *IntegrationTestSuite) TestMagicDNSv4() {
|
|
|
|
for namespace, scales := range s.namespaces {
|
|
|
|
ips, err := getIPs(scales.tailscales)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
for hostname, tailscale := range scales.tailscales {
|
|
|
|
for peername, ips := range ips {
|
|
|
|
if peername == hostname {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.T().Run(fmt.Sprintf("%s-%s-ipv4", hostname, peername), func(t *testing.T) {
|
|
|
|
command := []string{
|
|
|
|
"host", "-4", "-t", "A",
|
|
|
|
fmt.Sprintf("%s.%s.headscale.net", peername, namespace),
|
|
|
|
"100.100.100.100",
|
2021-10-04 12:04:08 -04:00
|
|
|
}
|
2022-01-16 08:16:59 -05:00
|
|
|
|
|
|
|
fmt.Printf(
|
|
|
|
"Resolving name %s (IPv4) from %s over IPv4\n",
|
|
|
|
peername,
|
|
|
|
hostname,
|
|
|
|
)
|
|
|
|
result, err := ExecuteCommand(
|
|
|
|
&tailscale,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
fmt.Printf("Result for %s: %s\n", hostname, result)
|
|
|
|
|
|
|
|
resolved := false
|
|
|
|
for _, ip := range ips {
|
|
|
|
if strings.Contains(result, fmt.Sprintf("has address %s", ip.String())) {
|
|
|
|
resolved = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, true, resolved)
|
2021-10-04 12:04:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-02 05:20:42 -04:00
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
func getIPs(tailscales map[string]dockertest.Resource) (map[string][]netaddr.IP, error) {
|
|
|
|
ips := make(map[string][]netaddr.IP)
|
2021-08-08 12:50:32 -04:00
|
|
|
for hostname, tailscale := range tailscales {
|
|
|
|
command := []string{"tailscale", "ip"}
|
|
|
|
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
2021-08-08 12:50:32 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-08-08 12:50:32 -04:00
|
|
|
)
|
2021-08-13 05:33:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
|
2022-01-16 08:16:59 -05:00
|
|
|
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)
|
2021-08-13 05:33:19 -04:00
|
|
|
}
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-08-13 05:33:19 -04:00
|
|
|
return ips, nil
|
2021-08-08 12:50:32 -04:00
|
|
|
}
|
2021-09-26 06:22:59 -04:00
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
func getAPIURLs(
|
|
|
|
tailscales map[string]dockertest.Resource,
|
|
|
|
) (map[netaddr.IP]string, error) {
|
2021-09-26 06:22:59 -04:00
|
|
|
fts := make(map[netaddr.IP]string)
|
2021-09-26 08:22:11 -04:00
|
|
|
for _, tailscale := range tailscales {
|
2021-09-26 09:17:27 -04:00
|
|
|
command := []string{
|
2021-09-26 06:22:59 -04:00
|
|
|
"curl",
|
|
|
|
"--unix-socket",
|
|
|
|
"/run/tailscale/tailscaled.sock",
|
|
|
|
"http://localhost/localapi/v0/file-targets",
|
|
|
|
}
|
2021-11-04 18:10:57 -04:00
|
|
|
result, err := ExecuteCommand(
|
2021-09-26 06:22:59 -04:00
|
|
|
&tailscale,
|
|
|
|
command,
|
2021-09-26 08:22:11 -04:00
|
|
|
[]string{},
|
2021-09-26 06:22:59 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-26 08:33:01 -04:00
|
|
|
|
2021-09-26 06:22:59 -04:00
|
|
|
var pft []apitype.FileTarget
|
2021-09-26 08:22:11 -04:00
|
|
|
if err := json.Unmarshal([]byte(result), &pft); err != nil {
|
2021-09-26 06:22:59 -04:00
|
|
|
return nil, fmt.Errorf("invalid JSON: %w", err)
|
|
|
|
}
|
|
|
|
for _, ft := range pft {
|
|
|
|
n := ft.Node
|
2021-09-26 08:22:11 -04:00
|
|
|
for _, a := range n.Addresses { // just add all the addresses
|
|
|
|
if _, ok := fts[a.IP()]; !ok {
|
2021-10-04 09:14:12 -04:00
|
|
|
if ft.PeerAPIURL == "" {
|
|
|
|
return nil, errors.New("api url is empty")
|
|
|
|
}
|
2021-09-26 08:22:11 -04:00
|
|
|
fts[a.IP()] = ft.PeerAPIURL
|
2021-09-26 06:22:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-09-26 08:22:11 -04:00
|
|
|
return fts, nil
|
2021-09-26 06:22:59 -04:00
|
|
|
}
|