2023-04-13 23:10:47 +02:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2023-04-13 23:10:47 +02:00
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
|
|
|
"github.com/juanfont/headscale/integration/hsic"
|
|
|
|
"github.com/juanfont/headscale/integration/tsic"
|
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EmbeddedDERPServerScenario struct {
|
|
|
|
*Scenario
|
|
|
|
|
|
|
|
tsicNetworks map[string]*dockertest.Network
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDERPServerScenario(t *testing.T) {
|
|
|
|
IntegrationSkip(t)
|
|
|
|
// t.Parallel()
|
|
|
|
|
|
|
|
baseScenario, err := NewScenario()
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErr(t, err)
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
scenario := EmbeddedDERPServerScenario{
|
|
|
|
Scenario: baseScenario,
|
|
|
|
tsicNetworks: map[string]*dockertest.Network{},
|
|
|
|
}
|
2023-08-29 08:33:33 +02:00
|
|
|
defer scenario.Shutdown()
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
spec := map[string]int{
|
2024-02-09 07:26:41 +01:00
|
|
|
"user1": 10,
|
|
|
|
// "user1": len(MustTestVersions),
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.CreateHeadscaleEnv(
|
|
|
|
spec,
|
|
|
|
hsic.WithTestName("derpserver"),
|
|
|
|
hsic.WithExtraPorts([]string{"3478/udp"}),
|
2024-04-16 21:37:25 +02:00
|
|
|
hsic.WithEmbeddedDERPServerOnly(),
|
2023-04-13 23:10:47 +02:00
|
|
|
hsic.WithTLS(),
|
|
|
|
hsic.WithHostnameAsServerURL(),
|
|
|
|
)
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErrHeadscaleEnv(t, err)
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
allClients, err := scenario.ListTailscaleClients()
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErrListClients(t, err)
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
allIps, err := scenario.ListTailscaleClientsIPs()
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErrListClientIPs(t, err)
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
err = scenario.WaitForTailscaleSync()
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErrSync(t, err)
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
allHostnames, err := scenario.ListTailscaleClientsFQDNs()
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErrListFQDN(t, err)
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
success := pingDerpAllHelper(t, allClients, allHostnames)
|
|
|
|
|
|
|
|
t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EmbeddedDERPServerScenario) CreateHeadscaleEnv(
|
|
|
|
users map[string]int,
|
|
|
|
opts ...hsic.Option,
|
|
|
|
) error {
|
|
|
|
hsServer, err := s.Headscale(opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
headscaleEndpoint := hsServer.GetEndpoint()
|
|
|
|
headscaleURL, err := url.Parse(headscaleEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
headscaleURL.Host = fmt.Sprintf("%s:%s", hsServer.GetHostname(), headscaleURL.Port())
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
err = hsServer.WaitForRunning()
|
2023-04-13 23:10:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:09:18 +02:00
|
|
|
hash, err := util.GenerateRandomStringDNSSafe(scenarioHashLength)
|
2023-04-13 23:10:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for userName, clientCount := range users {
|
|
|
|
err = s.CreateUser(userName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.CreateTailscaleIsolatedNodesInUser(
|
|
|
|
hash,
|
|
|
|
userName,
|
|
|
|
"all",
|
|
|
|
clientCount,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := s.CreatePreAuthKey(userName, true, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.RunTailscaleUp(userName, headscaleURL.String(), key.GetKey())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EmbeddedDERPServerScenario) CreateTailscaleIsolatedNodesInUser(
|
|
|
|
hash string,
|
|
|
|
userStr string,
|
|
|
|
requestedVersion string,
|
|
|
|
count int,
|
|
|
|
opts ...tsic.Option,
|
|
|
|
) error {
|
2023-04-23 13:02:28 +02:00
|
|
|
hsServer, err := s.Headscale()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:10:47 +02:00
|
|
|
if user, ok := s.users[userStr]; ok {
|
|
|
|
for clientN := 0; clientN < count; clientN++ {
|
|
|
|
networkName := fmt.Sprintf("tsnet-%s-%s-%d",
|
|
|
|
hash,
|
|
|
|
userStr,
|
|
|
|
clientN,
|
|
|
|
)
|
|
|
|
network, err := dockertestutil.GetFirstOrCreateNetwork(
|
|
|
|
s.pool,
|
|
|
|
networkName,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create or get %s network: %w", networkName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.tsicNetworks[networkName] = network
|
|
|
|
|
2023-04-23 13:02:28 +02:00
|
|
|
err = hsServer.ConnectToNetwork(network)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect headscale to %s network: %w", networkName, err)
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:10:47 +02:00
|
|
|
version := requestedVersion
|
|
|
|
if requestedVersion == "all" {
|
2023-08-31 14:57:43 +02:00
|
|
|
version = MustTestVersions[clientN%len(MustTestVersions)]
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 13:02:28 +02:00
|
|
|
cert := hsServer.GetCert()
|
2023-04-13 23:10:47 +02:00
|
|
|
|
|
|
|
opts = append(opts,
|
|
|
|
tsic.WithHeadscaleTLS(cert),
|
|
|
|
)
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
user.createWaitGroup.Go(func() error {
|
2023-04-13 23:10:47 +02:00
|
|
|
tsClient, err := tsic.New(
|
|
|
|
s.pool,
|
|
|
|
version,
|
|
|
|
network,
|
|
|
|
opts...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to create tailscale (%s) node: %w",
|
|
|
|
tsClient.Hostname(),
|
|
|
|
err,
|
|
|
|
)
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
err = tsClient.WaitForNeedsLogin()
|
2023-04-13 23:10:47 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
return fmt.Errorf(
|
|
|
|
"failed to wait for tailscaled (%s) to need login: %w",
|
|
|
|
tsClient.Hostname(),
|
|
|
|
err,
|
|
|
|
)
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-09-10 09:17:17 +02:00
|
|
|
s.mu.Lock()
|
2023-04-13 23:10:47 +02:00
|
|
|
user.Clients[tsClient.Hostname()] = tsClient
|
2023-09-10 09:17:17 +02:00
|
|
|
s.mu.Unlock()
|
2023-08-29 08:33:33 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := user.createWaitGroup.Wait(); err != nil {
|
|
|
|
return err
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
return fmt.Errorf("failed to add tailscale nodes: %w", errNoUserAvailable)
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
func (s *EmbeddedDERPServerScenario) Shutdown() {
|
2023-04-13 23:10:47 +02:00
|
|
|
for _, network := range s.tsicNetworks {
|
|
|
|
err := s.pool.RemoveNetwork(network)
|
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
log.Printf("failed to remove DERP network %s", network.Network.Name)
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
s.Scenario.Shutdown()
|
2023-04-13 23:10:47 +02:00
|
|
|
}
|