From 2c0488da0b13f8430cb6847b5eb5569e54a1f7bb Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 24 Oct 2022 16:40:49 +0200 Subject: [PATCH 1/5] Add Execute helper for controlserver Signed-off-by: Kristoffer Dalby --- integration/control.go | 1 + integration/hsic/hsic.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/integration/control.go b/integration/control.go index 58a4661..33a687c 100644 --- a/integration/control.go +++ b/integration/control.go @@ -6,6 +6,7 @@ import ( type ControlServer interface { Shutdown() error + Execute(command []string) (string, error) GetHealthEndpoint() string GetEndpoint() string WaitForReady() error diff --git a/integration/hsic/hsic.go b/integration/hsic/hsic.go index a6372a5..7db8233 100644 --- a/integration/hsic/hsic.go +++ b/integration/hsic/hsic.go @@ -103,6 +103,29 @@ func (t *HeadscaleInContainer) Shutdown() error { return t.pool.Purge(t.container) } +func (t *HeadscaleInContainer) Execute( + command []string, +) (string, error) { + log.Println("command", command) + log.Printf("running command for %s\n", t.hostname) + stdout, stderr, err := dockertestutil.ExecuteCommand( + t.container, + command, + []string{}, + ) + if err != nil { + log.Printf("command stderr: %s\n", stderr) + + return "", err + } + + if stdout != "" { + log.Printf("command stdout: %s\n", stdout) + } + + return stdout, nil +} + func (t *HeadscaleInContainer) GetIP() string { return t.container.GetIPInNetwork(t.network) } From cb61a490e026f093a8aa6dfce9798e5f01a0fde2 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 24 Oct 2022 16:41:40 +0200 Subject: [PATCH 2/5] Add namespace command test Signed-off-by: Kristoffer Dalby --- integration/cli_test.go | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 integration/cli_test.go diff --git a/integration/cli_test.go b/integration/cli_test.go new file mode 100644 index 0000000..c934686 --- /dev/null +++ b/integration/cli_test.go @@ -0,0 +1,93 @@ +package integration + +import ( + "encoding/json" + "testing" + + v1 "github.com/juanfont/headscale/gen/go/headscale/v1" + "github.com/stretchr/testify/assert" +) + +func executeAndUnmarshal[T any](headscale ControlServer, command []string, result T) error { + str, err := headscale.Execute(command) + if err != nil { + return err + } + + err = json.Unmarshal([]byte(str), result) + if err != nil { + return err + } + + return nil +} + +func TestNamespaceCommand(t *testing.T) { + IntegrationSkip(t) + t.Parallel() + + scenario, err := NewScenario() + assert.NoError(t, err) + + spec := map[string]int{ + "namespace1": 0, + "namespace2": 0, + } + + err = scenario.CreateHeadscaleEnv(spec) + assert.NoError(t, err) + + var listNamespaces []v1.Namespace + err = executeAndUnmarshal(scenario.Headscale(), + []string{ + "headscale", + "namespaces", + "list", + "--output", + "json", + }, + &listNamespaces, + ) + assert.NoError(t, err) + + assert.Equal( + t, + []string{"namespace1", "namespace2"}, + []string{listNamespaces[0].Name, listNamespaces[1].Name}, + ) + + _, err = scenario.Headscale().Execute( + []string{ + "headscale", + "namespaces", + "rename", + "--output", + "json", + "namespace2", + "newname", + }, + ) + assert.NoError(t, err) + + var listAfterRenameNamespaces []v1.Namespace + err = executeAndUnmarshal(scenario.Headscale(), + []string{ + "headscale", + "namespaces", + "list", + "--output", + "json", + }, + &listAfterRenameNamespaces, + ) + assert.NoError(t, err) + + assert.Equal( + t, + []string{"namespace1", "newname"}, + []string{listAfterRenameNamespaces[0].Name, listAfterRenameNamespaces[1].Name}, + ) + + err = scenario.Shutdown() + assert.NoError(t, err) +} From 239ef16ad1fd97611ceac8e76172ac988411f35a Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 24 Oct 2022 16:42:09 +0200 Subject: [PATCH 3/5] Add preauthkey command test Signed-off-by: Kristoffer Dalby --- integration/cli_test.go | 139 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/integration/cli_test.go b/integration/cli_test.go index c934686..b0d9906 100644 --- a/integration/cli_test.go +++ b/integration/cli_test.go @@ -3,6 +3,7 @@ package integration import ( "encoding/json" "testing" + "time" v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/stretchr/testify/assert" @@ -91,3 +92,141 @@ func TestNamespaceCommand(t *testing.T) { err = scenario.Shutdown() assert.NoError(t, err) } + +func TestPreAuthKeyCommand(t *testing.T) { + IntegrationSkip(t) + t.Parallel() + + namespace := "preauthkeyspace" + count := 3 + + scenario, err := NewScenario() + assert.NoError(t, err) + + spec := map[string]int{ + namespace: 0, + } + + err = scenario.CreateHeadscaleEnv(spec) + assert.NoError(t, err) + + keys := make([]*v1.PreAuthKey, count) + assert.NoError(t, err) + + for index := 0; index < count; index++ { + var preAuthKey v1.PreAuthKey + err := executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "create", + "--reusable", + "--expiration", + "24h", + "--output", + "json", + "--tags", + "tag:test1,tag:test2", + }, + &preAuthKey, + ) + assert.NoError(t, err) + + keys[index] = &preAuthKey + } + + assert.Len(t, keys, 3) + + var listedPreAuthKeys []v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "list", + "--output", + "json", + }, + &listedPreAuthKeys, + ) + assert.NoError(t, err) + + // There is one key created by "scenario.CreateHeadscaleEnv" + assert.Len(t, listedPreAuthKeys, 4) + + assert.Equal( + t, + []string{keys[0].Id, keys[1].Id, keys[2].Id}, + []string{listedPreAuthKeys[1].Id, listedPreAuthKeys[2].Id, listedPreAuthKeys[3].Id}, + ) + + assert.NotEmpty(t, listedPreAuthKeys[1].Key) + assert.NotEmpty(t, listedPreAuthKeys[2].Key) + assert.NotEmpty(t, listedPreAuthKeys[3].Key) + + assert.True(t, listedPreAuthKeys[1].Expiration.AsTime().After(time.Now())) + assert.True(t, listedPreAuthKeys[2].Expiration.AsTime().After(time.Now())) + assert.True(t, listedPreAuthKeys[3].Expiration.AsTime().After(time.Now())) + + assert.True( + t, + listedPreAuthKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)), + ) + assert.True( + t, + listedPreAuthKeys[2].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)), + ) + assert.True( + t, + listedPreAuthKeys[3].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)), + ) + + for index := range listedPreAuthKeys { + if index == 0 { + continue + } + + assert.Equal(t, listedPreAuthKeys[index].AclTags, []string{"tag:test1", "tag:test2"}) + } + + // Test key expiry + _, err = scenario.Headscale().Execute( + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "expire", + listedPreAuthKeys[1].Key, + }, + ) + assert.NoError(t, err) + + var listedPreAuthKeysAfterExpire []v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "list", + "--output", + "json", + }, + &listedPreAuthKeysAfterExpire, + ) + assert.NoError(t, err) + + assert.True(t, listedPreAuthKeysAfterExpire[1].Expiration.AsTime().Before(time.Now())) + assert.True(t, listedPreAuthKeysAfterExpire[2].Expiration.AsTime().After(time.Now())) + assert.True(t, listedPreAuthKeysAfterExpire[3].Expiration.AsTime().After(time.Now())) + + err = scenario.Shutdown() + assert.NoError(t, err) +} From 5013187aaffc1e6efa8f827dc0e1b9bbcdd8831c Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 24 Oct 2022 17:03:59 +0200 Subject: [PATCH 4/5] Add some sort stability Signed-off-by: Kristoffer Dalby --- integration/cli_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/integration/cli_test.go b/integration/cli_test.go index b0d9906..fb4f5b6 100644 --- a/integration/cli_test.go +++ b/integration/cli_test.go @@ -2,6 +2,7 @@ package integration import ( "encoding/json" + "sort" "testing" "time" @@ -51,10 +52,13 @@ func TestNamespaceCommand(t *testing.T) { ) assert.NoError(t, err) + result := []string{listNamespaces[0].Name, listNamespaces[1].Name} + sort.Strings(result) + assert.Equal( t, []string{"namespace1", "namespace2"}, - []string{listNamespaces[0].Name, listNamespaces[1].Name}, + result, ) _, err = scenario.Headscale().Execute( @@ -83,10 +87,13 @@ func TestNamespaceCommand(t *testing.T) { ) assert.NoError(t, err) + result = []string{listAfterRenameNamespaces[0].Name, listAfterRenameNamespaces[1].Name} + sort.Strings(result) + assert.Equal( t, []string{"namespace1", "newname"}, - []string{listAfterRenameNamespaces[0].Name, listAfterRenameNamespaces[1].Name}, + result, ) err = scenario.Shutdown() From 7e6ab19270567a8bc1494aad8dff15c2dae470bc Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 24 Oct 2022 17:31:15 +0200 Subject: [PATCH 5/5] Port preauthkey subcommand tests Signed-off-by: Kristoffer Dalby --- integration/cli_test.go | 138 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/integration/cli_test.go b/integration/cli_test.go index fb4f5b6..43e5551 100644 --- a/integration/cli_test.go +++ b/integration/cli_test.go @@ -237,3 +237,141 @@ func TestPreAuthKeyCommand(t *testing.T) { err = scenario.Shutdown() assert.NoError(t, err) } + +func TestPreAuthKeyCommandWithoutExpiry(t *testing.T) { + IntegrationSkip(t) + t.Parallel() + + namespace := "pre-auth-key-without-exp-namespace" + + scenario, err := NewScenario() + assert.NoError(t, err) + + spec := map[string]int{ + namespace: 0, + } + + err = scenario.CreateHeadscaleEnv(spec) + assert.NoError(t, err) + + var preAuthKey v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "create", + "--reusable", + "--output", + "json", + }, + &preAuthKey, + ) + assert.NoError(t, err) + + var listedPreAuthKeys []v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "list", + "--output", + "json", + }, + &listedPreAuthKeys, + ) + assert.NoError(t, err) + + // There is one key created by "scenario.CreateHeadscaleEnv" + assert.Len(t, listedPreAuthKeys, 2) + + assert.True(t, listedPreAuthKeys[1].Expiration.AsTime().After(time.Now())) + assert.True( + t, + listedPreAuthKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Minute*70)), + ) + + err = scenario.Shutdown() + assert.NoError(t, err) +} + +func TestPreAuthKeyCommandReusableEphemeral(t *testing.T) { + IntegrationSkip(t) + t.Parallel() + + namespace := "pre-auth-key-reus-ephm-namespace" + + scenario, err := NewScenario() + assert.NoError(t, err) + + spec := map[string]int{ + namespace: 0, + } + + err = scenario.CreateHeadscaleEnv(spec) + assert.NoError(t, err) + + var preAuthReusableKey v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "create", + "--reusable=true", + "--output", + "json", + }, + &preAuthReusableKey, + ) + assert.NoError(t, err) + + var preAuthEphemeralKey v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "create", + "--ephemeral=true", + "--output", + "json", + }, + &preAuthEphemeralKey, + ) + assert.NoError(t, err) + + assert.True(t, preAuthEphemeralKey.GetEphemeral()) + assert.False(t, preAuthEphemeralKey.GetReusable()) + + var listedPreAuthKeys []v1.PreAuthKey + err = executeAndUnmarshal( + scenario.Headscale(), + []string{ + "headscale", + "preauthkeys", + "--namespace", + namespace, + "list", + "--output", + "json", + }, + &listedPreAuthKeys, + ) + assert.NoError(t, err) + + // There is one key created by "scenario.CreateHeadscaleEnv" + assert.Len(t, listedPreAuthKeys, 3) + + err = scenario.Shutdown() + assert.NoError(t, err) +}