2022-10-13 16:03:38 +02:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-10-14 10:37:13 +02:00
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
2022-10-13 16:03:38 +02:00
|
|
|
)
|
|
|
|
|
2022-11-08 22:33:11 +01:00
|
|
|
// This file is intended to "test the test framework", by proxy it will also test
|
2022-10-14 12:23:35 +02:00
|
|
|
// some Headcsale/Tailscale stuff, but mostly in very simple ways.
|
|
|
|
|
2022-10-14 10:37:13 +02:00
|
|
|
func IntegrationSkip(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if !dockertestutil.IsRunningInContainer() {
|
|
|
|
t.Skip("not running in docker, skipping")
|
|
|
|
}
|
|
|
|
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration tests due to short flag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:01:06 +01:00
|
|
|
// If subtests are parallel, then they will start before setup is run.
|
|
|
|
// This might mean we approach setup slightly wrong, but for now, ignore
|
|
|
|
// the linter
|
|
|
|
// nolint:tparallel
|
2022-10-13 16:03:38 +02:00
|
|
|
func TestHeadscale(t *testing.T) {
|
2022-10-14 10:37:13 +02:00
|
|
|
IntegrationSkip(t)
|
2022-11-22 12:05:58 +01:00
|
|
|
t.Parallel()
|
2022-10-14 12:18:28 +02:00
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
var err error
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user := "test-space"
|
2022-10-14 12:38:34 +02:00
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-13 16:03:38 +02:00
|
|
|
|
|
|
|
t.Run("start-headscale", func(t *testing.T) {
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create start headcale: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
err = headscale.WaitForRunning()
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("headscale failed to become ready: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
t.Run("create-user", func(t *testing.T) {
|
|
|
|
err := scenario.CreateUser(user)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create user: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
if _, ok := scenario.users[user]; !ok {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("user is not in scenario")
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create-auth-key", func(t *testing.T) {
|
2023-01-17 17:43:44 +01:00
|
|
|
_, err := scenario.CreatePreAuthKey(user, true, false)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create preauthkey: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:01:06 +01:00
|
|
|
// If subtests are parallel, then they will start before setup is run.
|
|
|
|
// This might mean we approach setup slightly wrong, but for now, ignore
|
|
|
|
// the linter
|
|
|
|
// nolint:tparallel
|
2022-10-13 16:03:38 +02:00
|
|
|
func TestCreateTailscale(t *testing.T) {
|
2022-10-14 10:37:13 +02:00
|
|
|
IntegrationSkip(t)
|
2022-11-22 12:05:58 +01:00
|
|
|
t.Parallel()
|
2022-10-14 10:37:13 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user := "only-create-containers"
|
2022-10-13 16:03:38 +02:00
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-13 16:03:38 +02:00
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
scenario.users[user] = &User{
|
2022-10-21 13:17:54 +02:00
|
|
|
Clients: make(map[string]TailscaleClient),
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("create-tailscale", func(t *testing.T) {
|
2023-01-17 17:43:44 +01:00
|
|
|
err := scenario.CreateTailscaleNodesInUser(user, "all", 3)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to add tailscale nodes: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
if clients := len(scenario.users[user].Clients); clients != 3 {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("wrong number of tailscale clients: %d != %d", clients, 3)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
2022-10-18 11:59:28 +02:00
|
|
|
|
|
|
|
// TODO(kradalby): Test "all" version logic
|
2022-10-13 16:03:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:01:06 +01:00
|
|
|
// If subtests are parallel, then they will start before setup is run.
|
|
|
|
// This might mean we approach setup slightly wrong, but for now, ignore
|
|
|
|
// the linter
|
|
|
|
// nolint:tparallel
|
2022-10-13 16:03:38 +02:00
|
|
|
func TestTailscaleNodesJoiningHeadcale(t *testing.T) {
|
2022-10-14 10:37:13 +02:00
|
|
|
IntegrationSkip(t)
|
2022-11-22 12:05:58 +01:00
|
|
|
t.Parallel()
|
2022-10-14 10:37:13 +02:00
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
var err error
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
user := "join-node-test"
|
2022-10-13 16:03:38 +02:00
|
|
|
|
2022-10-14 12:18:28 +02:00
|
|
|
count := 1
|
|
|
|
|
2024-04-21 18:28:17 +02:00
|
|
|
scenario, err := NewScenario(dockertestMaxWait())
|
2023-08-29 08:33:33 +02:00
|
|
|
assertNoErr(t, err)
|
|
|
|
defer scenario.Shutdown()
|
2022-10-13 16:03:38 +02:00
|
|
|
|
|
|
|
t.Run("start-headscale", func(t *testing.T) {
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create start headcale: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
err = headscale.WaitForRunning()
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("headscale failed to become ready: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
t.Run("create-user", func(t *testing.T) {
|
|
|
|
err := scenario.CreateUser(user)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create user: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
if _, ok := scenario.users[user]; !ok {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("user is not in scenario")
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create-tailscale", func(t *testing.T) {
|
2024-01-05 10:41:56 +01:00
|
|
|
err := scenario.CreateTailscaleNodesInUser(user, "unstable", count)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to add tailscale nodes: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:43:44 +01:00
|
|
|
if clients := len(scenario.users[user].Clients); clients != count {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("wrong number of tailscale clients: %d != %d", clients, count)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("join-headscale", func(t *testing.T) {
|
2023-01-17 17:43:44 +01:00
|
|
|
key, err := scenario.CreatePreAuthKey(user, true, false)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create preauthkey: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale, err := scenario.Headscale()
|
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to create start headcale: %s", err)
|
2022-11-14 14:27:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scenario.RunTailscaleUp(
|
2023-01-17 17:43:44 +01:00
|
|
|
user,
|
2022-11-14 14:27:02 +01:00
|
|
|
headscale.GetEndpoint(),
|
|
|
|
key.GetKey(),
|
|
|
|
)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to login: %s", err)
|
2022-10-13 16:03:38 +02:00
|
|
|
}
|
2022-10-14 12:18:28 +02:00
|
|
|
})
|
2022-10-13 16:03:38 +02:00
|
|
|
|
2022-10-14 12:18:28 +02:00
|
|
|
t.Run("get-ips", func(t *testing.T) {
|
2023-01-17 17:43:44 +01:00
|
|
|
ips, err := scenario.GetIPs(user)
|
2022-10-14 12:18:28 +02:00
|
|
|
if err != nil {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("failed to get tailscale ips: %s", err)
|
2022-10-14 12:18:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ips) != count*2 {
|
2023-08-29 08:33:33 +02:00
|
|
|
t.Fatalf("got the wrong amount of tailscale ips, %d != %d", len(ips), count*2)
|
2022-10-14 12:18:28 +02:00
|
|
|
}
|
2022-10-13 16:03:38 +02:00
|
|
|
})
|
|
|
|
}
|