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"
|
|
|
|
"sync"
|
2022-10-18 05:59:28 -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"
|
2022-11-14 08:27:02 -05:00
|
|
|
"github.com/puzpuzpuz/xsync/v2"
|
2023-08-29 02:33:33 -04:00
|
|
|
"golang.org/x/sync/errgroup"
|
2022-10-13 10:03:38 -04:00
|
|
|
)
|
|
|
|
|
2022-10-18 06:21:38 -04:00
|
|
|
const (
|
|
|
|
scenarioHashLength = 6
|
|
|
|
maxWait = 60 * time.Second
|
|
|
|
)
|
2022-10-13 10:03:38 -04:00
|
|
|
|
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).
|
|
|
|
tailscaleVersions2021 = []string{
|
2022-10-18 06:09:10 -04:00
|
|
|
"head",
|
|
|
|
"unstable",
|
2023-08-29 02:33:33 -04:00
|
|
|
"1.48",
|
|
|
|
"1.46",
|
|
|
|
"1.44",
|
|
|
|
"1.42",
|
|
|
|
"1.40",
|
|
|
|
"1.38",
|
|
|
|
"1.36",
|
|
|
|
"1.34",
|
|
|
|
"1.32",
|
|
|
|
"1.30",
|
2022-11-14 03:45:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tailscaleVersions2019 = []string{
|
2023-08-29 02:33:33 -04:00
|
|
|
"1.28",
|
|
|
|
"1.26",
|
2023-08-31 08:57:43 -04:00
|
|
|
"1.24", // Tailscale SSH
|
2023-08-29 02:33:33 -04:00
|
|
|
"1.22",
|
|
|
|
"1.20",
|
|
|
|
"1.18",
|
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(
|
2022-11-14 03:45:01 -05:00
|
|
|
tailscaleVersions2021,
|
|
|
|
tailscaleVersions2019...,
|
|
|
|
)
|
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-08-31 12:37:18 -04:00
|
|
|
// - Two oldest versions.
|
2023-08-31 08:57:43 -04:00
|
|
|
MustTestVersions = append(
|
|
|
|
tailscaleVersions2021[0:4],
|
2023-08-31 12:37:18 -04:00
|
|
|
tailscaleVersions2019[len(tailscaleVersions2019)-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
|
|
|
|
|
|
|
headscaleLock 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.
|
2022-10-13 10:03:38 -04:00
|
|
|
func NewScenario() (*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)
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:19:43 -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{
|
2022-11-14 08:27:02 -05:00
|
|
|
controlServers: xsync.NewMapOf[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
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05: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.
|
2023-08-29 02:33:33 -04:00
|
|
|
func (s *Scenario) Shutdown() {
|
2022-11-14 08:27:02 -05:00
|
|
|
s.controlServers.Range(func(_ string, control ControlServer) bool {
|
2023-04-27 10:57:11 -04:00
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
s.headscaleLock.Lock()
|
|
|
|
defer s.headscaleLock.Unlock()
|
|
|
|
|
|
|
|
if headscale, ok := s.controlServers.Load("headscale"); ok {
|
|
|
|
return headscale, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
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-01-17 11:43:44 -05:00
|
|
|
user.Clients[tsClient.Hostname()] = tsClient
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-08-29 02:33:33 -04:00
|
|
|
return s.WaitForTailscaleSyncWithPeerCount(tsCount - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// CreateHeadscaleEnv is a conventient 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
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetIPs 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
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// ListTailscaleClientsIPs returns a list of FQDN based on Users
|
|
|
|
// 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
|
|
|
}
|