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-10-14 12:23:35 +02:00
|
|
|
// This file is intendet to "test the test framework", by proxy it will also test
|
|
|
|
// 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-10-13 16:03:38 +02:00
|
|
|
func TestHeadscale(t *testing.T) {
|
2022-10-14 10:37:13 +02:00
|
|
|
IntegrationSkip(t)
|
2022-10-14 12:18:28 +02:00
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
var err error
|
|
|
|
|
2022-10-14 12:38:34 +02:00
|
|
|
namespace := "test-space"
|
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("start-headscale", func(t *testing.T) {
|
|
|
|
err = scenario.StartHeadscale()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create start headcale: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:10 +01:00
|
|
|
err = scenario.MustHeadscale().WaitForReady()
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("headscale failed to become ready: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create-namespace", func(t *testing.T) {
|
2022-10-14 12:38:34 +02:00
|
|
|
err := scenario.CreateNamespace(namespace)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create namespace: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-10-14 12:38:34 +02:00
|
|
|
if _, ok := scenario.namespaces[namespace]; !ok {
|
2022-10-13 16:03:38 +02:00
|
|
|
t.Errorf("namespace is not in scenario")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create-auth-key", func(t *testing.T) {
|
2022-10-14 12:38:34 +02:00
|
|
|
_, err := scenario.CreatePreAuthKey(namespace)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create preauthkey: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateTailscale(t *testing.T) {
|
2022-10-14 10:37:13 +02:00
|
|
|
IntegrationSkip(t)
|
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
namespace := "only-create-containers"
|
|
|
|
|
2022-10-18 11:59:28 +02:00
|
|
|
scenario, err := NewScenario()
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
scenario.namespaces[namespace] = &Namespace{
|
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) {
|
2022-10-18 11:59:28 +02:00
|
|
|
err := scenario.CreateTailscaleNodesInNamespace(namespace, "all", 3)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to add tailscale nodes: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if clients := len(scenario.namespaces[namespace].Clients); clients != 3 {
|
|
|
|
t.Errorf("wrong number of tailscale clients: %d != %d", clients, 3)
|
|
|
|
}
|
2022-10-18 11:59:28 +02:00
|
|
|
|
|
|
|
// TODO(kradalby): Test "all" version logic
|
2022-10-13 16:03:38 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTailscaleNodesJoiningHeadcale(t *testing.T) {
|
2022-10-14 10:37:13 +02:00
|
|
|
IntegrationSkip(t)
|
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
namespace := "join-node-test"
|
|
|
|
|
2022-10-14 12:18:28 +02:00
|
|
|
count := 1
|
|
|
|
|
2022-10-13 16:03:38 +02:00
|
|
|
scenario, err := NewScenario()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create scenario: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("start-headscale", func(t *testing.T) {
|
|
|
|
err = scenario.StartHeadscale()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create start headcale: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:10 +01:00
|
|
|
headscale := scenario.MustHeadscale()
|
2022-10-13 16:03:38 +02:00
|
|
|
err = headscale.WaitForReady()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("headscale failed to become ready: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create-namespace", func(t *testing.T) {
|
|
|
|
err := scenario.CreateNamespace(namespace)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create namespace: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := scenario.namespaces[namespace]; !ok {
|
|
|
|
t.Errorf("namespace is not in scenario")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("create-tailscale", func(t *testing.T) {
|
2022-10-14 12:18:28 +02:00
|
|
|
err := scenario.CreateTailscaleNodesInNamespace(namespace, "1.30.2", count)
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to add tailscale nodes: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-10-14 12:18:28 +02:00
|
|
|
if clients := len(scenario.namespaces[namespace].Clients); clients != count {
|
|
|
|
t.Errorf("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) {
|
|
|
|
key, err := scenario.CreatePreAuthKey(namespace)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create preauthkey: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:06:10 +01:00
|
|
|
err = scenario.RunTailscaleUp(namespace, scenario.MustHeadscale().GetEndpoint(), key.GetKey())
|
2022-10-13 16:03:38 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to login: %s", err)
|
|
|
|
}
|
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) {
|
|
|
|
ips, err := scenario.GetIPs(namespace)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get tailscale ips: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ips) != count*2 {
|
|
|
|
t.Errorf("got the wrong amount of tailscale ips, %d != %d", len(ips), count*2)
|
|
|
|
}
|
2022-10-13 16:03:38 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
err = scenario.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to tear down scenario: %s", err)
|
|
|
|
}
|
|
|
|
}
|