2022-10-13 10:03:38 -04:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-10-14 06:17:59 -04:00
|
|
|
"net/netip"
|
2022-10-13 10:03:38 -04:00
|
|
|
"os"
|
2024-01-02 04:41:40 -05:00
|
|
|
"sort"
|
2022-10-13 10:03:38 -04:00
|
|
|
"sync"
|
2024-09-11 06:00:32 -04:00
|
|
|
"testing"
|
2024-04-21 12:28:17 -04:00
|
|
|
"time"
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2023-05-11 03:09:18 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-10-13 10:03:38 -04:00
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
|
|
|
"github.com/juanfont/headscale/integration/tsic"
|
|
|
|
"github.com/ory/dockertest/v3"
|
2023-11-29 09:47:14 -05:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2023-12-09 12:09:24 -05:00
|
|
|
"github.com/samber/lo"
|
2024-09-11 06:00:32 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-08-29 02:33:33 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
2024-02-18 13:31:29 -05:00
|
|
|
"tailscale.com/envknob"
|
2022-10-13 10:03:38 -04:00
|
|
|
)
|
|
|
|
|
2022-10-18 06:21:38 -04:00
|
|
|
const (
|
|
|
|
scenarioHashLength = 6
|
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2024-02-18 13:31:29 -05:00
|
|
|
var usePostgresForTest = envknob.Bool("HEADSCALE_INTEGRATION_POSTGRES")
|
|
|
|
|
2023-11-23 02:31:33 -05:00
|
|
|
func enabledVersions(vs map[string]bool) []string {
|
|
|
|
var ret []string
|
|
|
|
for version, enabled := range vs {
|
|
|
|
if enabled {
|
|
|
|
ret = append(ret, version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-02 04:41:40 -05:00
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(ret)))
|
|
|
|
|
2023-11-23 02:31:33 -05:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:09:10 -04:00
|
|
|
var (
|
|
|
|
errNoHeadscaleAvailable = errors.New("no headscale available")
|
2023-01-17 14:36:46 -05:00
|
|
|
errNoUserAvailable = errors.New("no user available")
|
2023-02-02 10:05:52 -05:00
|
|
|
errNoClientFound = errors.New("client not found")
|
2022-11-14 12:10:29 -05:00
|
|
|
|
|
|
|
// Tailscale started adding TS2021 support in CapabilityVersion>=28 (v1.24.0), but
|
|
|
|
// proper support in Headscale was only added for CapabilityVersion>=39 clients (v1.30.0).
|
2023-11-23 02:31:33 -05:00
|
|
|
tailscaleVersions2021 = map[string]bool{
|
|
|
|
"head": true,
|
|
|
|
"unstable": true,
|
2024-09-24 12:34:20 -04:00
|
|
|
"1.74": true, // CapVer: 106
|
|
|
|
"1.72": true, // CapVer: 104
|
|
|
|
"1.70": true, // CapVer: 102
|
|
|
|
"1.68": true, // CapVer: 97
|
|
|
|
"1.66": true, // CapVer: 95
|
|
|
|
"1.64": true, // CapVer: 90
|
|
|
|
"1.62": true, // CapVer: 88
|
|
|
|
"1.60": true, // CapVer: 87
|
|
|
|
"1.58": true, // CapVer: 85
|
|
|
|
"1.56": true, // Oldest supported version, CapVer: 82
|
|
|
|
"1.54": false, // CapVer: 79
|
|
|
|
"1.52": false, // CapVer: 79
|
|
|
|
"1.50": false, // CapVer: 74
|
|
|
|
"1.48": false, // CapVer: 68
|
|
|
|
"1.46": false, // CapVer: 65
|
2024-08-19 05:41:05 -04:00
|
|
|
"1.44": false, // CapVer: 63
|
2024-09-24 12:34:20 -04:00
|
|
|
"1.42": false, // CapVer: 61
|
2024-08-19 05:41:05 -04:00
|
|
|
"1.40": false, // CapVer: 61
|
|
|
|
"1.38": false, // CapVer: 58
|
2024-02-08 11:28:19 -05:00
|
|
|
"1.36": false, // CapVer: 56
|
2024-01-05 04:41:56 -05:00
|
|
|
"1.34": false, // CapVer: 51
|
|
|
|
"1.32": false, // CapVer: 46
|
2023-11-23 02:31:33 -05:00
|
|
|
"1.30": false,
|
2022-11-14 03:45:01 -05:00
|
|
|
}
|
|
|
|
|
2023-11-23 02:31:33 -05:00
|
|
|
tailscaleVersions2019 = map[string]bool{
|
|
|
|
"1.28": false,
|
|
|
|
"1.26": false,
|
|
|
|
"1.24": false, // Tailscale SSH
|
|
|
|
"1.22": false,
|
|
|
|
"1.20": false,
|
|
|
|
"1.18": false,
|
2022-10-18 06:09:10 -04:00
|
|
|
}
|
2022-11-14 03:45:01 -05:00
|
|
|
|
|
|
|
// tailscaleVersionsUnavailable = []string{
|
|
|
|
// // These versions seem to fail when fetching from apt.
|
2023-08-29 02:33:33 -04:00
|
|
|
// "1.14.6",
|
|
|
|
// "1.12.4",
|
|
|
|
// "1.10.2",
|
|
|
|
// "1.8.7",
|
2022-11-14 08:27:29 -05:00
|
|
|
// }.
|
2022-11-14 03:45:01 -05:00
|
|
|
|
2023-08-31 08:57:43 -04:00
|
|
|
// AllVersions represents a list of Tailscale versions the suite
|
2023-02-03 06:24:27 -05:00
|
|
|
// uses to test compatibility with the ControlServer.
|
|
|
|
//
|
|
|
|
// The list contains two special cases, "head" and "unstable" which
|
|
|
|
// points to the current tip of Tailscale's main branch and the latest
|
|
|
|
// released unstable version.
|
|
|
|
//
|
|
|
|
// The rest of the version represents Tailscale versions that can be
|
|
|
|
// found in Tailscale's apt repository.
|
2023-08-31 08:57:43 -04:00
|
|
|
AllVersions = append(
|
2023-11-23 02:31:33 -05:00
|
|
|
enabledVersions(tailscaleVersions2021),
|
|
|
|
enabledVersions(tailscaleVersions2019)...,
|
2022-11-14 03:45:01 -05:00
|
|
|
)
|
2023-08-31 08:57:43 -04:00
|
|
|
|
|
|
|
// MustTestVersions is the minimum set of versions we should test.
|
2023-08-31 12:37:18 -04:00
|
|
|
// At the moment, this is arbitrarily chosen as:
|
2023-08-31 08:57:43 -04:00
|
|
|
//
|
|
|
|
// - Two unstable (HEAD and unstable)
|
|
|
|
// - Two latest versions
|
2023-12-09 12:09:24 -05:00
|
|
|
// - Two oldest supported version.
|
2023-08-31 08:57:43 -04:00
|
|
|
MustTestVersions = append(
|
2023-11-23 02:31:33 -05:00
|
|
|
AllVersions[0:4],
|
|
|
|
AllVersions[len(AllVersions)-2:]...,
|
2023-08-31 08:57:43 -04:00
|
|
|
)
|
2022-10-18 06:09:10 -04:00
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// User represents a User in the ControlServer and a map of TailscaleClient's
|
|
|
|
// associated with the User.
|
2023-01-17 11:43:44 -05:00
|
|
|
type User struct {
|
2022-10-21 07:17:54 -04:00
|
|
|
Clients map[string]TailscaleClient
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-08-29 02:33:33 -04:00
|
|
|
createWaitGroup errgroup.Group
|
|
|
|
joinWaitGroup errgroup.Group
|
|
|
|
syncWaitGroup errgroup.Group
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// Scenario is a representation of an environment with one ControlServer and
|
|
|
|
// one or more User's and its associated TailscaleClients.
|
|
|
|
// A Scenario is intended to simplify setting up a new testcase for testing
|
|
|
|
// a ControlServer with TailscaleClients.
|
2022-11-02 05:01:21 -04:00
|
|
|
// TODO(kradalby): make control server configurable, test correctness with Tailscale SaaS.
|
2022-10-13 10:03:38 -04:00
|
|
|
type Scenario struct {
|
|
|
|
// TODO(kradalby): support multiple headcales for later, currently only
|
|
|
|
// use one.
|
2022-11-14 08:27:02 -05:00
|
|
|
controlServers *xsync.MapOf[string, ControlServer]
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
users map[string]*User
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
network *dockertest.Network
|
2022-11-14 08:27:02 -05:00
|
|
|
|
2023-09-10 03:17:17 -04:00
|
|
|
mu sync.Mutex
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// NewScenario creates a test Scenario which can be used to bootstraps a ControlServer with
|
|
|
|
// a set of Users and TailscaleClients.
|
2024-04-21 12:28:17 -04:00
|
|
|
func NewScenario(maxWait time.Duration) (*Scenario, error) {
|
2023-05-11 03:09:18 -04:00
|
|
|
hash, err := util.GenerateRandomStringDNSSafe(scenarioHashLength)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not connect to docker: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-21 12:28:17 -04:00
|
|
|
pool.MaxWait = maxWait
|
2022-10-18 05:59:28 -04:00
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
networkName := fmt.Sprintf("hs-%s", hash)
|
|
|
|
if overrideNetworkName := os.Getenv("HEADSCALE_TEST_NETWORK_NAME"); overrideNetworkName != "" {
|
|
|
|
networkName = overrideNetworkName
|
|
|
|
}
|
|
|
|
|
|
|
|
network, err := dockertestutil.GetFirstOrCreateNetwork(pool, networkName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create or get network: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-14 04:38:27 -04:00
|
|
|
// We run the test suite in a docker container that calls a couple of endpoints for
|
|
|
|
// readiness checks, this ensures that we can run the tests with individual networks
|
|
|
|
// and have the client reach the different containers
|
|
|
|
err = dockertestutil.AddContainerToNetwork(pool, network, "headscale-test-suite")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to add test suite container to network: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
return &Scenario{
|
2023-11-29 09:47:14 -05:00
|
|
|
controlServers: xsync.NewMapOf[string, ControlServer](),
|
2023-01-17 14:36:46 -05:00
|
|
|
users: make(map[string]*User),
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-09-11 06:00:32 -04:00
|
|
|
func (s *Scenario) ShutdownAssertNoPanics(t *testing.T) {
|
2022-11-14 08:27:02 -05:00
|
|
|
s.controlServers.Range(func(_ string, control ControlServer) bool {
|
2024-09-11 06:00:32 -04:00
|
|
|
stdoutPath, stderrPath, err := control.Shutdown()
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2022-11-14 08:27:02 -05:00
|
|
|
log.Printf(
|
|
|
|
"Failed to shut down control: %s",
|
|
|
|
fmt.Errorf("failed to tear down control: %w", err),
|
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
2022-11-14 08:27:02 -05:00
|
|
|
|
2024-09-11 06:00:32 -04:00
|
|
|
if t != nil {
|
|
|
|
stdout, err := os.ReadFile(stdoutPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotContains(t, string(stdout), "panic")
|
|
|
|
|
|
|
|
stderr, err := os.ReadFile(stderrPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotContains(t, string(stderr), "panic")
|
|
|
|
}
|
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
return true
|
|
|
|
})
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for userName, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
|
|
|
log.Printf("removing client %s in user %s", client.Hostname(), userName)
|
2022-10-13 10:03:38 -04:00
|
|
|
err := client.Shutdown()
|
|
|
|
if err != nil {
|
2023-08-29 02:33:33 -04:00
|
|
|
log.Printf("failed to tear down client: %s", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 04:38:27 -04:00
|
|
|
if err := s.pool.RemoveNetwork(s.network); err != nil {
|
2023-08-29 02:33:33 -04:00
|
|
|
log.Printf("failed to remove network: %s", err)
|
2022-10-14 04:38:27 -04:00
|
|
|
}
|
2022-10-13 10:03:38 -04:00
|
|
|
|
|
|
|
// TODO(kradalby): This seem redundant to the previous call
|
|
|
|
// if err := s.network.Close(); err != nil {
|
|
|
|
// return fmt.Errorf("failed to tear down network: %w", err)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
2024-09-11 06:00:32 -04:00
|
|
|
// Shutdown shuts down and cleans up all the containers (ControlServer, TailscaleClient)
|
|
|
|
// and networks associated with it.
|
|
|
|
// In addition, it will save the logs of the ControlServer to `/tmp/control` in the
|
|
|
|
// environment running the tests.
|
|
|
|
func (s *Scenario) Shutdown() {
|
|
|
|
s.ShutdownAssertNoPanics(nil)
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// Users returns the name of all users associated with the Scenario.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) Users() []string {
|
|
|
|
users := make([]string, 0)
|
|
|
|
for user := range s.users {
|
|
|
|
users = append(users, user)
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return users
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
/// Headscale related stuff
|
|
|
|
// Note: These functions assume that there is a _single_ headscale instance for now
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// Headscale returns a ControlServer instance based on hsic (HeadscaleInContainer)
|
|
|
|
// If the Scenario already has an instance, the pointer to the running container
|
|
|
|
// will be return, otherwise a new instance will be created.
|
2022-10-13 10:03:38 -04:00
|
|
|
// TODO(kradalby): make port and headscale configurable, multiple instances support?
|
2022-11-14 08:27:02 -05:00
|
|
|
func (s *Scenario) Headscale(opts ...hsic.Option) (ControlServer, error) {
|
2023-09-10 03:17:17 -04:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2022-11-14 08:27:02 -05:00
|
|
|
|
|
|
|
if headscale, ok := s.controlServers.Load("headscale"); ok {
|
|
|
|
return headscale, nil
|
|
|
|
}
|
|
|
|
|
2024-09-03 03:22:17 -04:00
|
|
|
if usePostgresForTest {
|
|
|
|
opts = append(opts, hsic.WithPostgres())
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
headscale, err := hsic.New(s.pool, s.network, opts...)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2022-11-14 08:27:02 -05:00
|
|
|
return nil, fmt.Errorf("failed to create headscale container: %w", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-08-29 02:33:33 -04:00
|
|
|
err = headscale.WaitForRunning()
|
2022-11-10 11:06:10 -05:00
|
|
|
if err != nil {
|
2022-11-14 08:27:02 -05:00
|
|
|
return nil, fmt.Errorf("failed reach headscale container: %w", err)
|
2022-11-10 11:06:10 -05:00
|
|
|
}
|
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
s.controlServers.Store("headscale", headscale)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
return headscale, nil
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// CreatePreAuthKey creates a "pre authentorised key" to be created in the
|
|
|
|
// Headscale instance on behalf of the Scenario.
|
2023-02-02 10:05:52 -05:00
|
|
|
func (s *Scenario) CreatePreAuthKey(
|
|
|
|
user string,
|
|
|
|
reusable bool,
|
|
|
|
ephemeral bool,
|
|
|
|
) (*v1.PreAuthKey, error) {
|
2022-11-14 08:27:02 -05:00
|
|
|
if headscale, err := s.Headscale(); err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
key, err := headscale.CreateAuthKey(user, reusable, ephemeral)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, fmt.Errorf("failed to create user: %w", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return nil, fmt.Errorf("failed to create user: %w", errNoHeadscaleAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// CreateUser creates a User to be created in the
|
|
|
|
// Headscale instance on behalf of the Scenario.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) CreateUser(user string) error {
|
2022-11-14 08:27:02 -05:00
|
|
|
if headscale, err := s.Headscale(); err == nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
err := headscale.CreateUser(user)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to create user: %w", err)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
s.users[user] = &User{
|
2022-10-21 07:17:54 -04:00
|
|
|
Clients: make(map[string]TailscaleClient),
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to create user: %w", errNoHeadscaleAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Client related stuff
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// CreateTailscaleNodesInUser creates and adds a new TailscaleClient to a
|
|
|
|
// User in the Scenario.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) CreateTailscaleNodesInUser(
|
|
|
|
userStr string,
|
2022-10-18 05:59:28 -04:00
|
|
|
requestedVersion string,
|
2022-10-13 10:03:38 -04:00
|
|
|
count int,
|
2022-11-08 10:10:03 -05:00
|
|
|
opts ...tsic.Option,
|
2022-10-13 10:03:38 -04:00
|
|
|
) error {
|
2023-01-17 11:43:44 -05:00
|
|
|
if user, ok := s.users[userStr]; ok {
|
2023-12-09 12:09:24 -05:00
|
|
|
var versions []string
|
2022-10-13 10:03:38 -04:00
|
|
|
for i := 0; i < count; i++ {
|
2022-10-18 05:59:28 -04:00
|
|
|
version := requestedVersion
|
|
|
|
if requestedVersion == "all" {
|
2023-08-31 08:57:43 -04:00
|
|
|
version = MustTestVersions[i%len(MustTestVersions)]
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
2023-12-09 12:09:24 -05:00
|
|
|
versions = append(versions, version)
|
2022-10-18 05:59:28 -04:00
|
|
|
|
2022-11-14 08:27:02 -05:00
|
|
|
headscale, err := s.Headscale()
|
|
|
|
if err != nil {
|
2023-05-02 05:51:30 -04:00
|
|
|
return fmt.Errorf("failed to create tailscale node (version: %s): %w", version, err)
|
2022-11-14 08:27:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cert := headscale.GetCert()
|
|
|
|
hostname := headscale.GetHostname()
|
|
|
|
|
2022-11-08 10:10:03 -05:00
|
|
|
opts = append(opts,
|
|
|
|
tsic.WithHeadscaleTLS(cert),
|
|
|
|
tsic.WithHeadscaleName(hostname),
|
|
|
|
)
|
|
|
|
|
2023-08-29 02:33:33 -04:00
|
|
|
user.createWaitGroup.Go(func() error {
|
2022-11-06 14:22:21 -05:00
|
|
|
tsClient, err := tsic.New(
|
|
|
|
s.pool,
|
|
|
|
version,
|
|
|
|
s.network,
|
2022-11-08 10:10:03 -05:00
|
|
|
opts...,
|
2022-11-06 14:22:21 -05:00
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
if err != nil {
|
2023-08-29 02:33:33 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to create tailscale (%s) node: %w",
|
|
|
|
tsClient.Hostname(),
|
|
|
|
err,
|
|
|
|
)
|
2022-11-06 14:22:21 -05:00
|
|
|
}
|
|
|
|
|
2023-08-29 02:33:33 -04:00
|
|
|
err = tsClient.WaitForNeedsLogin()
|
2022-11-06 14:22:21 -05:00
|
|
|
if err != nil {
|
2023-08-29 02:33:33 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to wait for tailscaled (%s) to need login: %w",
|
|
|
|
tsClient.Hostname(),
|
|
|
|
err,
|
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-09-10 03:17:17 -04:00
|
|
|
s.mu.Lock()
|
2023-01-17 11:43:44 -05:00
|
|
|
user.Clients[tsClient.Hostname()] = tsClient
|
2023-09-10 03:17:17 -04:00
|
|
|
s.mu.Unlock()
|
2023-08-29 02:33:33 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := user.createWaitGroup.Wait(); err != nil {
|
|
|
|
return err
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2024-01-02 04:41:40 -05:00
|
|
|
log.Printf("testing versions %v, MustTestVersions %v", lo.Uniq(versions), MustTestVersions)
|
2023-12-09 12:09:24 -05:00
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to add tailscale node: %w", errNoUserAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// RunTailscaleUp will log in all of the TailscaleClients associated with a
|
|
|
|
// User to the given ControlServer (by URL).
|
2022-10-13 10:03:38 -04:00
|
|
|
func (s *Scenario) RunTailscaleUp(
|
2023-01-17 11:43:44 -05:00
|
|
|
userStr, loginServer, authKey string,
|
2022-10-13 10:03:38 -04:00
|
|
|
) error {
|
2023-01-17 11:43:44 -05:00
|
|
|
if user, ok := s.users[userStr]; ok {
|
|
|
|
for _, client := range user.Clients {
|
2023-08-29 02:33:33 -04:00
|
|
|
c := client
|
|
|
|
user.joinWaitGroup.Go(func() error {
|
|
|
|
return c.Login(loginServer, authKey)
|
|
|
|
})
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
2022-11-13 10:58:20 -05:00
|
|
|
|
2023-08-29 02:33:33 -04:00
|
|
|
if err := user.joinWaitGroup.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 10:03:38 -04:00
|
|
|
|
2023-04-12 03:25:51 -04:00
|
|
|
for _, client := range user.Clients {
|
2023-08-29 02:33:33 -04:00
|
|
|
err := client.WaitForRunning()
|
2023-04-12 03:25:51 -04:00
|
|
|
if err != nil {
|
2023-08-29 02:33:33 -04:00
|
|
|
return fmt.Errorf("%s failed to up tailscale node: %w", client.Hostname(), err)
|
2023-04-12 03:25:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:03:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return fmt.Errorf("failed to up tailscale node: %w", errNoUserAvailable)
|
2022-10-13 10:03:38 -04:00
|
|
|
}
|
2022-10-14 06:17:59 -04:00
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// CountTailscale returns the total number of TailscaleClients in a Scenario.
|
|
|
|
// This is the sum of Users x TailscaleClients.
|
2022-10-18 05:59:28 -04:00
|
|
|
func (s *Scenario) CountTailscale() int {
|
|
|
|
count := 0
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range s.users {
|
|
|
|
count += len(user.Clients)
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WaitForTailscaleSync blocks execution until all the TailscaleClient reports
|
|
|
|
// to have all other TailscaleClients present in their netmap.NetworkMap.
|
2022-10-18 05:59:28 -04:00
|
|
|
func (s *Scenario) WaitForTailscaleSync() error {
|
|
|
|
tsCount := s.CountTailscale()
|
|
|
|
|
2023-12-09 12:09:24 -05:00
|
|
|
err := s.WaitForTailscaleSyncWithPeerCount(tsCount - 1)
|
|
|
|
if err != nil {
|
|
|
|
for _, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
2024-05-24 04:15:34 -04:00
|
|
|
peers, allOnline, _ := client.FailingPeersAsString()
|
|
|
|
if !allOnline {
|
|
|
|
log.Println(peers)
|
|
|
|
}
|
2023-12-09 12:09:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2023-08-29 02:33:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForTailscaleSyncWithPeerCount blocks execution until all the TailscaleClient reports
|
|
|
|
// to have all other TailscaleClients present in their netmap.NetworkMap.
|
|
|
|
func (s *Scenario) WaitForTailscaleSyncWithPeerCount(peerCount int) error {
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
2023-08-29 02:33:33 -04:00
|
|
|
c := client
|
|
|
|
user.syncWaitGroup.Go(func() error {
|
|
|
|
return c.WaitForPeers(peerCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := user.syncWaitGroup.Wait(); err != nil {
|
|
|
|
return err
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-19 17:49:27 -04:00
|
|
|
// CreateHeadscaleEnv is a convenient method returning a complete Headcale
|
2022-10-18 05:59:28 -04:00
|
|
|
// test environment with nodes of all versions, joined to the server with X
|
2023-01-17 11:43:44 -05:00
|
|
|
// users.
|
2022-11-08 10:10:03 -05:00
|
|
|
func (s *Scenario) CreateHeadscaleEnv(
|
2023-01-17 11:43:44 -05:00
|
|
|
users map[string]int,
|
2022-11-08 10:10:03 -05:00
|
|
|
tsOpts []tsic.Option,
|
|
|
|
opts ...hsic.Option,
|
|
|
|
) error {
|
2022-11-14 08:27:02 -05:00
|
|
|
headscale, err := s.Headscale(opts...)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for userName, clientCount := range users {
|
|
|
|
err = s.CreateUser(userName)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
err = s.CreateTailscaleNodesInUser(userName, "all", clientCount, tsOpts...)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
key, err := s.CreatePreAuthKey(userName, true, false)
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
err = s.RunTailscaleUp(userName, headscale.GetEndpoint(), key.GetKey())
|
2022-10-18 05:59:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetIPs returns all netip.Addr of TailscaleClients associated with a User
|
|
|
|
// in a Scenario.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) GetIPs(user string) ([]netip.Addr, error) {
|
2022-10-14 06:17:59 -04:00
|
|
|
var ips []netip.Addr
|
2023-01-17 11:43:44 -05:00
|
|
|
if ns, ok := s.users[user]; ok {
|
2022-10-14 06:17:59 -04:00
|
|
|
for _, client := range ns.Clients {
|
|
|
|
clientIps, err := client.IPs()
|
|
|
|
if err != nil {
|
|
|
|
return ips, fmt.Errorf("failed to get ips: %w", err)
|
|
|
|
}
|
|
|
|
ips = append(ips, clientIps...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return ips, fmt.Errorf("failed to get ips: %w", errNoUserAvailable)
|
2022-10-14 06:17:59 -04:00
|
|
|
}
|
2022-10-18 05:59:28 -04:00
|
|
|
|
2024-04-21 12:19:38 -04:00
|
|
|
// GetClients returns all TailscaleClients associated with a User in a Scenario.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) GetClients(user string) ([]TailscaleClient, error) {
|
2022-10-21 07:17:54 -04:00
|
|
|
var clients []TailscaleClient
|
2023-01-17 11:43:44 -05:00
|
|
|
if ns, ok := s.users[user]; ok {
|
2022-10-18 05:59:28 -04:00
|
|
|
for _, client := range ns.Clients {
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
return clients, nil
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return clients, fmt.Errorf("failed to get clients: %w", errNoUserAvailable)
|
2022-10-18 05:59:28 -04:00
|
|
|
}
|
2022-10-23 06:41:35 -04:00
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// ListTailscaleClients returns a list of TailscaleClients given the Users
|
|
|
|
// passed as parameters.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) ListTailscaleClients(users ...string) ([]TailscaleClient, error) {
|
2022-10-23 06:41:35 -04:00
|
|
|
var allClients []TailscaleClient
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
if len(users) == 0 {
|
|
|
|
users = s.Users()
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range users {
|
|
|
|
clients, err := s.GetClients(user)
|
2022-10-23 06:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allClients = append(allClients, clients...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allClients, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// FindTailscaleClientByIP returns a TailscaleClient associated with an IP address
|
|
|
|
// if it exists.
|
2023-02-02 10:05:52 -05:00
|
|
|
func (s *Scenario) FindTailscaleClientByIP(ip netip.Addr) (TailscaleClient, error) {
|
|
|
|
clients, err := s.ListTailscaleClients()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
ips, _ := client.IPs()
|
|
|
|
for _, ip2 := range ips {
|
|
|
|
if ip == ip2 {
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errNoClientFound
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// ListTailscaleClientsIPs returns a list of netip.Addr based on Users
|
|
|
|
// passed as parameters.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) ListTailscaleClientsIPs(users ...string) ([]netip.Addr, error) {
|
2022-10-23 06:41:35 -04:00
|
|
|
var allIps []netip.Addr
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
if len(users) == 0 {
|
|
|
|
users = s.Users()
|
2022-10-23 06:41:35 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range users {
|
|
|
|
ips, err := s.GetIPs(user)
|
2022-10-23 06:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allIps = append(allIps, ips...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allIps, nil
|
|
|
|
}
|
|
|
|
|
2024-04-21 12:19:38 -04:00
|
|
|
// ListTailscaleClientsFQDNs returns a list of FQDN based on Users
|
2023-02-03 06:24:27 -05:00
|
|
|
// passed as parameters.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *Scenario) ListTailscaleClientsFQDNs(users ...string) ([]string, error) {
|
2022-10-23 06:41:35 -04:00
|
|
|
allFQDNs := make([]string, 0)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
clients, err := s.ListTailscaleClients(users...)
|
2022-10-23 06:41:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, client := range clients {
|
|
|
|
fqdn, err := client.FQDN()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
allFQDNs = append(allFQDNs, fqdn)
|
|
|
|
}
|
|
|
|
|
|
|
|
return allFQDNs, nil
|
|
|
|
}
|
2022-12-22 10:24:37 -05:00
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WaitForTailscaleLogout blocks execution until all TailscaleClients have
|
|
|
|
// logged out of the ControlServer.
|
2023-08-29 02:33:33 -04:00
|
|
|
func (s *Scenario) WaitForTailscaleLogout() error {
|
2023-01-17 11:43:44 -05:00
|
|
|
for _, user := range s.users {
|
|
|
|
for _, client := range user.Clients {
|
2023-08-29 02:33:33 -04:00
|
|
|
c := client
|
|
|
|
user.syncWaitGroup.Go(func() error {
|
2023-08-31 12:37:18 -04:00
|
|
|
return c.WaitForNeedsLogin()
|
2023-08-29 02:33:33 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := user.syncWaitGroup.Wait(); err != nil {
|
|
|
|
return err
|
2022-12-22 10:24:37 -05:00
|
|
|
}
|
|
|
|
}
|
2023-08-29 02:33:33 -04:00
|
|
|
|
|
|
|
return nil
|
2022-12-22 10:24:37 -05:00
|
|
|
}
|