2022-11-05 04:07:22 -04:00
|
|
|
// nolint
|
2021-11-04 18:45:15 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"github.com/ory/dockertest/v3"
|
2022-10-07 08:29:21 -04:00
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2021-11-04 18:45:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IntegrationCLITestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
stats *suite.SuiteInformation
|
|
|
|
|
|
|
|
pool dockertest.Pool
|
|
|
|
network dockertest.Network
|
|
|
|
headscale dockertest.Resource
|
|
|
|
}
|
|
|
|
|
2022-10-18 08:41:48 -04:00
|
|
|
func TestIntegrationCLITestSuite(t *testing.T) {
|
2022-10-18 08:45:18 -04:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping integration tests due to short flag")
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:45:15 -04:00
|
|
|
s := new(IntegrationCLITestSuite)
|
|
|
|
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationCLITestSuite) SetupTest() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if ppool, err := dockertest.NewPool(""); err == nil {
|
|
|
|
s.pool = *ppool
|
|
|
|
} else {
|
2022-06-27 08:00:21 -04:00
|
|
|
s.FailNow(fmt.Sprintf("Could not connect to docker: %s", err), "")
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2022-10-07 16:10:33 -04:00
|
|
|
network, err := GetFirstOrCreateNetwork(&s.pool, headscaleNetwork)
|
|
|
|
if err != nil {
|
|
|
|
s.FailNow(fmt.Sprintf("Failed to create or get network: %s", err), "")
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
2022-10-07 16:10:33 -04:00
|
|
|
s.network = network
|
2021-11-04 18:45:15 -04:00
|
|
|
|
|
|
|
headscaleBuildOptions := &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile",
|
|
|
|
ContextDir: ".",
|
|
|
|
}
|
|
|
|
|
|
|
|
currentPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
2022-06-27 08:00:21 -04:00
|
|
|
s.FailNow(fmt.Sprintf("Could not determine current path: %s", err), "")
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
headscaleOptions := &dockertest.RunOptions{
|
2022-05-31 04:05:00 -04:00
|
|
|
Name: "headscale-cli",
|
2021-11-04 18:45:15 -04:00
|
|
|
Mounts: []string{
|
|
|
|
fmt.Sprintf("%s/integration_test/etc:/etc/headscale", currentPath),
|
|
|
|
},
|
2022-10-07 16:10:33 -04:00
|
|
|
Cmd: []string{"headscale", "serve"},
|
|
|
|
Networks: []*dockertest.Network{&s.network},
|
2022-10-07 08:29:21 -04:00
|
|
|
ExposedPorts: []string{"8080/tcp"},
|
|
|
|
PortBindings: map[docker.Port][]docker.PortBinding{
|
|
|
|
"8080/tcp": {{HostPort: "8080"}},
|
|
|
|
},
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-27 08:07:30 -04:00
|
|
|
err = s.pool.RemoveContainerByName(headscaleHostname)
|
|
|
|
if err != nil {
|
2022-08-04 04:47:00 -04:00
|
|
|
s.FailNow(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"Could not remove existing container before building test: %s",
|
|
|
|
err,
|
|
|
|
),
|
|
|
|
"",
|
|
|
|
)
|
2022-06-27 08:07:30 -04:00
|
|
|
}
|
|
|
|
|
2022-08-05 11:35:28 -04:00
|
|
|
fmt.Println("Creating headscale container for CLI tests")
|
2021-11-04 18:45:15 -04:00
|
|
|
if pheadscale, err := s.pool.BuildAndRunWithBuildOptions(headscaleBuildOptions, headscaleOptions, DockerRestartPolicy); err == nil {
|
|
|
|
s.headscale = *pheadscale
|
|
|
|
} else {
|
2022-06-27 08:00:21 -04:00
|
|
|
s.FailNow(fmt.Sprintf("Could not start headscale container: %s", err), "")
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
2022-08-05 11:35:28 -04:00
|
|
|
fmt.Println("Created headscale container for CLI tests")
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2022-08-05 11:35:28 -04:00
|
|
|
fmt.Println("Waiting for headscale to be ready for CLI tests")
|
2022-10-07 08:29:21 -04:00
|
|
|
hostEndpoint := fmt.Sprintf("%s:%s",
|
|
|
|
s.headscale.GetIPInNetwork(&s.network),
|
|
|
|
s.headscale.GetPort("8080/tcp"))
|
2021-11-04 18:45:15 -04:00
|
|
|
|
|
|
|
if err := s.pool.Retry(func() error {
|
|
|
|
url := fmt.Sprintf("http://%s/health", hostEndpoint)
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
2022-08-05 11:35:28 -04:00
|
|
|
fmt.Printf("headscale for CLI test is not ready: %s\n", err)
|
2021-11-04 18:45:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("status code not OK")
|
|
|
|
}
|
2021-11-14 12:35:49 -05:00
|
|
|
|
2021-11-04 18:45:15 -04:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
// TODO(kradalby): If we cannot access headscale, or any other fatal error during
|
|
|
|
// test setup, we need to abort and tear down. However, testify does not seem to
|
|
|
|
// support that at the moment:
|
|
|
|
// https://github.com/stretchr/testify/issues/849
|
|
|
|
return // fmt.Errorf("Could not connect to headscale: %s", err)
|
|
|
|
}
|
2022-08-05 11:35:28 -04:00
|
|
|
fmt.Println("headscale container is ready for CLI tests")
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationCLITestSuite) TearDownTest() {
|
|
|
|
if err := s.pool.Purge(&s.headscale); err != nil {
|
|
|
|
log.Printf("Could not purge resource: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.network.Close(); err != nil {
|
|
|
|
log.Printf("Could not close network: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
func (s *IntegrationCLITestSuite) HandleStats(
|
|
|
|
suiteName string,
|
|
|
|
stats *suite.SuiteInformation,
|
|
|
|
) {
|
2021-11-04 18:45:15 -04:00
|
|
|
s.stats = stats
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *IntegrationCLITestSuite) createUser(name string) (*v1.User, error) {
|
2022-09-21 11:01:26 -04:00
|
|
|
result, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 11:43:44 -05:00
|
|
|
"users",
|
2021-11-04 18:45:15 -04:00
|
|
|
"create",
|
|
|
|
name,
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
var user v1.User
|
|
|
|
err = json.Unmarshal([]byte(result), &user)
|
2021-11-04 18:45:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
return &user, nil
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
func (s *IntegrationCLITestSuite) TestUserCommand() {
|
|
|
|
names := []string{"user1", "otherspace", "tasty"}
|
|
|
|
users := make([]*v1.User, len(names))
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
for index, userName := range names {
|
|
|
|
user, err := s.createUser(userName)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
users[index] = user
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Len(s.T(), users, len(names))
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), names[0], users[0].Name)
|
|
|
|
assert.Equal(s.T(), names[1], users[1].Name)
|
|
|
|
assert.Equal(s.T(), names[2], users[2].Name)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Test list users
|
2022-09-21 11:01:26 -04:00
|
|
|
listResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 11:43:44 -05:00
|
|
|
"users",
|
2021-11-04 18:45:15 -04:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
var listedUsers []v1.User
|
|
|
|
err = json.Unmarshal([]byte(listResult), &listedUsers)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), names[0], listedUsers[0].Name)
|
|
|
|
assert.Equal(s.T(), names[1], listedUsers[1].Name)
|
|
|
|
assert.Equal(s.T(), names[2], listedUsers[2].Name)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Test rename user
|
2022-09-21 11:01:26 -04:00
|
|
|
renameResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 11:43:44 -05:00
|
|
|
"users",
|
2021-11-04 18:45:15 -04:00
|
|
|
"rename",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"tasty",
|
|
|
|
"newname",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
var renamedUser v1.User
|
|
|
|
err = json.Unmarshal([]byte(renameResult), &renamedUser)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), renamedUser.Name, "newname")
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Test list after rename users
|
2022-09-21 11:01:26 -04:00
|
|
|
listAfterRenameResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
2023-01-17 11:43:44 -05:00
|
|
|
"users",
|
2021-11-04 18:45:15 -04:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
var listedAfterRenameUsers []v1.User
|
|
|
|
err = json.Unmarshal([]byte(listAfterRenameResult), &listedAfterRenameUsers)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), names[0], listedAfterRenameUsers[0].Name)
|
|
|
|
assert.Equal(s.T(), names[1], listedAfterRenameUsers[1].Name)
|
|
|
|
assert.Equal(s.T(), "newname", listedAfterRenameUsers[2].Name)
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationCLITestSuite) TestPreAuthKeyCommand() {
|
|
|
|
count := 5
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
user, err := s.createUser("pre-auth-key-user")
|
2021-11-04 18:45:15 -04:00
|
|
|
|
|
|
|
keys := make([]*v1.PreAuthKey, count)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2022-09-21 11:01:26 -04:00
|
|
|
preAuthResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
2022-08-25 06:43:15 -04:00
|
|
|
"--tags",
|
|
|
|
"tag:test1,tag:test2",
|
2021-11-04 18:45:15 -04:00
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(preAuthResult), &preAuthKey)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
keys[i] = &preAuthKey
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(s.T(), keys, 5)
|
|
|
|
|
|
|
|
// Test list of keys
|
2022-09-21 11:01:26 -04:00
|
|
|
listResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(listResult), &listedPreAuthKeys)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Equal(s.T(), "1", listedPreAuthKeys[0].Id)
|
|
|
|
assert.Equal(s.T(), "2", listedPreAuthKeys[1].Id)
|
|
|
|
assert.Equal(s.T(), "3", listedPreAuthKeys[2].Id)
|
|
|
|
assert.Equal(s.T(), "4", listedPreAuthKeys[3].Id)
|
|
|
|
assert.Equal(s.T(), "5", listedPreAuthKeys[4].Id)
|
|
|
|
|
|
|
|
assert.NotEmpty(s.T(), listedPreAuthKeys[0].Key)
|
|
|
|
assert.NotEmpty(s.T(), listedPreAuthKeys[1].Key)
|
|
|
|
assert.NotEmpty(s.T(), listedPreAuthKeys[2].Key)
|
|
|
|
assert.NotEmpty(s.T(), listedPreAuthKeys[3].Key)
|
|
|
|
assert.NotEmpty(s.T(), listedPreAuthKeys[4].Key)
|
|
|
|
|
|
|
|
assert.True(s.T(), listedPreAuthKeys[0].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedPreAuthKeys[1].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedPreAuthKeys[2].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedPreAuthKeys[3].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedPreAuthKeys[4].Expiration.AsTime().After(time.Now()))
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedPreAuthKeys[0].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedPreAuthKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedPreAuthKeys[2].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedPreAuthKeys[3].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedPreAuthKeys[4].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
2021-11-08 03:02:01 -05:00
|
|
|
|
2022-08-25 06:43:15 -04:00
|
|
|
// Test that tags are present
|
|
|
|
for i := 0; i < count; i++ {
|
2022-09-07 08:12:29 -04:00
|
|
|
assert.Equal(s.T(), listedPreAuthKeys[i].AclTags, []string{"tag:test1", "tag:test2"})
|
2022-08-25 06:43:15 -04:00
|
|
|
}
|
|
|
|
|
2021-11-04 18:45:15 -04:00
|
|
|
// Expire three keys
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"expire",
|
|
|
|
listedPreAuthKeys[i].Key,
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test list pre auth keys after expire
|
2022-09-21 11:01:26 -04:00
|
|
|
listAfterExpireResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var listedAfterExpirePreAuthKeys []v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(listAfterExpireResult), &listedAfterExpirePreAuthKeys)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpirePreAuthKeys[0].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpirePreAuthKeys[1].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpirePreAuthKeys[2].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpirePreAuthKeys[3].Expiration.AsTime().After(time.Now()),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpirePreAuthKeys[4].Expiration.AsTime().After(time.Now()),
|
|
|
|
)
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2021-11-08 03:02:01 -05:00
|
|
|
func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandWithoutExpiry() {
|
2023-01-17 11:43:44 -05:00
|
|
|
user, err := s.createUser("pre-auth-key-without-exp-user")
|
2021-11-08 03:02:01 -05:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
preAuthResult, _, err := ExecuteCommand(
|
2021-11-08 03:02:01 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-08 03:02:01 -05:00
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(preAuthResult), &preAuthKey)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
// Test list of keys
|
2022-09-21 11:01:26 -04:00
|
|
|
listResult, _, err := ExecuteCommand(
|
2021-11-08 03:02:01 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-08 03:02:01 -05:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(listResult), &listedPreAuthKeys)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listedPreAuthKeys, 1)
|
2021-11-25 19:23:10 -05:00
|
|
|
|
|
|
|
assert.True(s.T(), listedPreAuthKeys[0].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedPreAuthKeys[0].Expiration.AsTime().Before(time.Now().Add(time.Minute*70)),
|
|
|
|
)
|
2021-11-08 03:02:01 -05:00
|
|
|
}
|
|
|
|
|
2021-11-08 15:49:03 -05:00
|
|
|
func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandReusableEphemeral() {
|
2023-01-17 11:43:44 -05:00
|
|
|
user, err := s.createUser("pre-auth-key-reus-ephm-user")
|
2021-11-08 15:49:03 -05:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
preAuthReusableResult, _, err := ExecuteCommand(
|
2021-11-08 15:49:03 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-08 15:49:03 -05:00
|
|
|
"create",
|
|
|
|
"--reusable=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var preAuthReusableKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(preAuthReusableResult), &preAuthReusableKey)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.True(s.T(), preAuthReusableKey.GetReusable())
|
|
|
|
assert.False(s.T(), preAuthReusableKey.GetEphemeral())
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
preAuthEphemeralResult, _, err := ExecuteCommand(
|
2021-11-08 15:49:03 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-08 15:49:03 -05:00
|
|
|
"create",
|
|
|
|
"--ephemeral=true",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var preAuthEphemeralKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(preAuthEphemeralResult), &preAuthEphemeralKey)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.True(s.T(), preAuthEphemeralKey.GetEphemeral())
|
|
|
|
assert.False(s.T(), preAuthEphemeralKey.GetReusable())
|
|
|
|
|
|
|
|
// TODO(kradalby): Evaluate if we need a case to test for reusable and ephemeral
|
|
|
|
// preAuthReusableAndEphemeralResult, err := ExecuteCommand(
|
|
|
|
// &s.headscale,
|
|
|
|
// []string{
|
|
|
|
// "headscale",
|
|
|
|
// "preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
// "--user",
|
|
|
|
// user.Name,
|
2021-11-08 15:49:03 -05:00
|
|
|
// "create",
|
|
|
|
// "--ephemeral",
|
|
|
|
// "--reusable",
|
|
|
|
// "--output",
|
|
|
|
// "json",
|
|
|
|
// },
|
|
|
|
// []string{},
|
|
|
|
// )
|
|
|
|
// assert.NotNil(s.T(), err)
|
|
|
|
|
|
|
|
// Test list of keys
|
2022-09-21 11:01:26 -04:00
|
|
|
listResult, _, err := ExecuteCommand(
|
2021-11-08 15:49:03 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"preauthkeys",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-08 15:49:03 -05:00
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var listedPreAuthKeys []v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(listResult), &listedPreAuthKeys)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listedPreAuthKeys, 2)
|
|
|
|
}
|
|
|
|
|
2022-05-13 05:47:22 -04:00
|
|
|
func (s *IntegrationCLITestSuite) TestNodeTagCommand() {
|
2023-05-01 10:52:48 -04:00
|
|
|
user, err := s.createUser("node-user")
|
2022-05-13 05:47:22 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKeys := []string{
|
2022-11-05 04:07:22 -04:00
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
2022-05-13 05:47:22 -04:00
|
|
|
}
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes := make([]*v1.Node, len(nodeKeys))
|
2022-05-13 05:47:22 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
for index, nodeKey := range nodeKeys {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2022-05-13 05:47:22 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-05-01 10:52:48 -04:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2022-05-13 05:47:22 -04:00
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2022-05-13 05:47:22 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeResult, _, err := ExecuteCommand(
|
2022-05-13 05:47:22 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2022-05-13 05:47:22 -04:00
|
|
|
"register",
|
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2022-05-13 05:47:22 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(nodeResult), &node)
|
2022-05-13 05:47:22 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes[index] = &node
|
2022-05-13 05:47:22 -04:00
|
|
|
}
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Len(s.T(), nodes, len(nodeKeys))
|
2022-05-13 05:47:22 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
addTagResult, _, err := ExecuteCommand(
|
2022-05-13 05:47:22 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"tag",
|
|
|
|
"-i", "1",
|
|
|
|
"-t", "tag:test",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(addTagResult), &node)
|
2022-05-13 05:47:22 -04:00
|
|
|
assert.Nil(s.T(), err)
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), []string{"tag:test"}, node.ForcedTags)
|
2022-05-13 05:47:22 -04:00
|
|
|
|
|
|
|
// try to set a wrong tag and retrieve the error
|
2022-09-21 11:01:26 -04:00
|
|
|
wrongTagResult, _, err := ExecuteCommand(
|
2022-05-13 05:47:22 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"tag",
|
|
|
|
"-i", "2",
|
|
|
|
"-t", "wrong-tag",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
2022-05-13 07:02:40 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
type errOutput struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
var errorOutput errOutput
|
|
|
|
err = json.Unmarshal([]byte(wrongTagResult), &errorOutput)
|
|
|
|
assert.Nil(s.T(), err)
|
2022-07-25 05:25:20 -04:00
|
|
|
assert.Contains(s.T(), errorOutput.Error, "tag must start with the string 'tag:'")
|
2022-05-13 05:47:22 -04:00
|
|
|
|
|
|
|
// Test list all nodes after added seconds
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllResult, _, err := ExecuteCommand(
|
2022-05-13 05:47:22 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output", "json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
2023-05-01 10:52:48 -04:00
|
|
|
resultNodes := make([]*v1.Node, len(nodeKeys))
|
2022-05-13 05:47:22 -04:00
|
|
|
assert.Nil(s.T(), err)
|
2023-05-01 10:52:48 -04:00
|
|
|
json.Unmarshal([]byte(listAllResult), &resultNodes)
|
2022-05-13 05:47:22 -04:00
|
|
|
found := false
|
2023-05-01 10:52:48 -04:00
|
|
|
for _, node := range resultNodes {
|
|
|
|
if node.ForcedTags != nil {
|
|
|
|
for _, tag := range node.ForcedTags {
|
2022-05-13 05:47:22 -04:00
|
|
|
if tag == "tag:test" {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(
|
|
|
|
s.T(),
|
|
|
|
true,
|
|
|
|
found,
|
2023-05-01 10:52:48 -04:00
|
|
|
"should find a node with the tag 'tag:test' in the list of nodes",
|
2022-05-13 05:47:22 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:45:15 -04:00
|
|
|
func (s *IntegrationCLITestSuite) TestNodeCommand() {
|
2023-05-01 10:52:48 -04:00
|
|
|
user, err := s.createUser("node-user")
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
secondUser, err := s.createUser("other-user")
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// Randomly generated node keys
|
|
|
|
nodeKeys := []string{
|
2022-11-05 04:07:22 -04:00
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"nodekey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"nodekey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"nodekey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes := make([]*v1.Node, len(nodeKeys))
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
for index, nodeKey := range nodeKeys {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-05-01 10:52:48 -04:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"register",
|
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(nodeResult), &node)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes[index] = &node
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Len(s.T(), nodes, len(nodeKeys))
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2022-02-21 17:44:08 -05:00
|
|
|
// Test list all nodes after added seconds
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAll []v1.Node
|
2021-11-04 18:45:15 -04:00
|
|
|
err = json.Unmarshal([]byte(listAllResult), &listAll)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listAll, 5)
|
|
|
|
|
|
|
|
assert.Equal(s.T(), uint64(1), listAll[0].Id)
|
|
|
|
assert.Equal(s.T(), uint64(2), listAll[1].Id)
|
|
|
|
assert.Equal(s.T(), uint64(3), listAll[2].Id)
|
|
|
|
assert.Equal(s.T(), uint64(4), listAll[3].Id)
|
|
|
|
assert.Equal(s.T(), uint64(5), listAll[4].Id)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), "node-1", listAll[0].Name)
|
|
|
|
assert.Equal(s.T(), "node-2", listAll[1].Name)
|
|
|
|
assert.Equal(s.T(), "node-3", listAll[2].Name)
|
|
|
|
assert.Equal(s.T(), "node-4", listAll[3].Name)
|
|
|
|
assert.Equal(s.T(), "node-5", listAll[4].Name)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
otherUserNodeKeys := []string{
|
2022-11-05 04:07:22 -04:00
|
|
|
"nodekey:b5b444774186d4217adcec407563a1223929465ee2c68a4da13af0d0185b4f8e",
|
|
|
|
"nodekey:dc721977ac7415aafa87f7d4574cbe07c6b171834a6d37375782bdc1fb6b3584",
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
2023-05-01 10:52:48 -04:00
|
|
|
otherUserNodes := make([]*v1.Node, len(otherUserNodeKeys))
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
for index, nodeKey := range otherUserNodeKeys {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-05-01 10:52:48 -04:00
|
|
|
fmt.Sprintf("otherUser-node-%d", index+1),
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
secondUser.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
secondUser.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"register",
|
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(nodeResult), &node)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
otherUserNodes[index] = &node
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Len(s.T(), otherUserNodes, len(otherUserNodeKeys))
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Test list all nodes after added otherUser
|
|
|
|
listAllWithotherUserResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAllWithotherUser []v1.Node
|
2022-02-21 17:44:08 -05:00
|
|
|
err = json.Unmarshal(
|
2023-01-17 11:43:44 -05:00
|
|
|
[]byte(listAllWithotherUserResult),
|
|
|
|
&listAllWithotherUser,
|
2022-02-21 17:44:08 -05:00
|
|
|
)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// All nodes, nodes + otherUser
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Len(s.T(), listAllWithotherUser, 7)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), uint64(6), listAllWithotherUser[5].Id)
|
|
|
|
assert.Equal(s.T(), uint64(7), listAllWithotherUser[6].Id)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), "otherUser-node-1", listAllWithotherUser[5].Name)
|
|
|
|
assert.Equal(s.T(), "otherUser-node-2", listAllWithotherUser[6].Name)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
// Test list all nodes after added otherUser
|
|
|
|
listOnlyotherUserMachineUserResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
secondUser.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listOnlyotherUserMachineUser []v1.Node
|
2021-11-13 03:36:45 -05:00
|
|
|
err = json.Unmarshal(
|
2023-01-17 11:43:44 -05:00
|
|
|
[]byte(listOnlyotherUserMachineUserResult),
|
|
|
|
&listOnlyotherUserMachineUser,
|
2021-11-13 03:36:45 -05:00
|
|
|
)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Len(s.T(), listOnlyotherUserMachineUser, 2)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), uint64(6), listOnlyotherUserMachineUser[0].Id)
|
|
|
|
assert.Equal(s.T(), uint64(7), listOnlyotherUserMachineUser[1].Id)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2022-02-21 17:44:08 -05:00
|
|
|
assert.Equal(
|
|
|
|
s.T(),
|
2023-05-01 10:52:48 -04:00
|
|
|
"otherUser-node-1",
|
2023-01-17 11:43:44 -05:00
|
|
|
listOnlyotherUserMachineUser[0].Name,
|
2022-02-21 17:44:08 -05:00
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
s.T(),
|
2023-05-01 10:52:48 -04:00
|
|
|
"otherUser-node-2",
|
2023-01-17 11:43:44 -05:00
|
|
|
listOnlyotherUserMachineUser[1].Name,
|
2022-02-21 17:44:08 -05:00
|
|
|
)
|
2021-11-04 18:45:15 -04:00
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// Delete a nodes
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err = ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"delete",
|
|
|
|
"--identifier",
|
2023-05-01 10:52:48 -04:00
|
|
|
// Delete the last added node
|
2021-11-04 18:45:15 -04:00
|
|
|
"4",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
"--force",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// Test: list main user after node is deleted
|
2023-01-17 11:43:44 -05:00
|
|
|
listOnlyMachineUserAfterDeleteResult, _, err := ExecuteCommand(
|
2021-11-04 18:45:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-04 18:45:15 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listOnlyMachineUserAfterDelete []v1.Node
|
2021-11-13 03:36:45 -05:00
|
|
|
err = json.Unmarshal(
|
2023-01-17 11:43:44 -05:00
|
|
|
[]byte(listOnlyMachineUserAfterDeleteResult),
|
|
|
|
&listOnlyMachineUserAfterDelete,
|
2021-11-13 03:36:45 -05:00
|
|
|
)
|
2021-11-04 18:45:15 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Len(s.T(), listOnlyMachineUserAfterDelete, 4)
|
2021-11-04 18:45:15 -04:00
|
|
|
}
|
|
|
|
|
2021-11-21 08:40:44 -05:00
|
|
|
func (s *IntegrationCLITestSuite) TestNodeExpireCommand() {
|
2023-05-01 10:52:48 -04:00
|
|
|
user, err := s.createUser("node-expire-user")
|
2021-11-21 08:40:44 -05:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// Randomly generated node keys
|
|
|
|
nodeKeys := []string{
|
2022-11-05 04:07:22 -04:00
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"nodekey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"nodekey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"nodekey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
2021-11-21 08:40:44 -05:00
|
|
|
}
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes := make([]*v1.Node, len(nodeKeys))
|
2021-11-21 08:40:44 -05:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
for index, nodeKey := range nodeKeys {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2021-11-21 08:40:44 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-05-01 10:52:48 -04:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-21 08:40:44 -05:00
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2021-11-21 08:40:44 -05:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeResult, _, err := ExecuteCommand(
|
2021-11-21 08:40:44 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2021-11-21 08:40:44 -05:00
|
|
|
"register",
|
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2021-11-21 08:40:44 -05:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(nodeResult), &node)
|
2021-11-21 08:40:44 -05:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes[index] = &node
|
2021-11-21 08:40:44 -05:00
|
|
|
}
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Len(s.T(), nodes, len(nodeKeys))
|
2021-11-21 08:40:44 -05:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllResult, _, err := ExecuteCommand(
|
2021-11-21 08:40:44 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAll []v1.Node
|
2021-11-21 08:40:44 -05:00
|
|
|
err = json.Unmarshal([]byte(listAllResult), &listAll)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listAll, 5)
|
|
|
|
|
|
|
|
assert.True(s.T(), listAll[0].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(s.T(), listAll[1].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(s.T(), listAll[2].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(s.T(), listAll[3].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(s.T(), listAll[4].Expiry.AsTime().IsZero())
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2021-11-21 08:40:44 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"expire",
|
|
|
|
"--identifier",
|
|
|
|
fmt.Sprintf("%d", listAll[i].Id),
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
}
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllAfterExpiryResult, _, err := ExecuteCommand(
|
2021-11-21 08:40:44 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAllAfterExpiry []v1.Node
|
2021-11-21 08:40:44 -05:00
|
|
|
err = json.Unmarshal([]byte(listAllAfterExpiryResult), &listAllAfterExpiry)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listAllAfterExpiry, 5)
|
|
|
|
|
|
|
|
assert.True(s.T(), listAllAfterExpiry[0].Expiry.AsTime().Before(time.Now()))
|
|
|
|
assert.True(s.T(), listAllAfterExpiry[1].Expiry.AsTime().Before(time.Now()))
|
|
|
|
assert.True(s.T(), listAllAfterExpiry[2].Expiry.AsTime().Before(time.Now()))
|
|
|
|
assert.True(s.T(), listAllAfterExpiry[3].Expiry.AsTime().IsZero())
|
|
|
|
assert.True(s.T(), listAllAfterExpiry[4].Expiry.AsTime().IsZero())
|
|
|
|
}
|
|
|
|
|
2022-05-16 14:32:56 -04:00
|
|
|
func (s *IntegrationCLITestSuite) TestNodeRenameCommand() {
|
2023-05-01 10:52:48 -04:00
|
|
|
user, err := s.createUser("node-rename-command")
|
2022-05-16 14:32:56 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// Randomly generated node keys
|
|
|
|
nodeKeys := []string{
|
2022-11-05 04:07:22 -04:00
|
|
|
"nodekey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
|
|
|
|
"nodekey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1",
|
|
|
|
"nodekey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507",
|
|
|
|
"nodekey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
|
|
|
|
"nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
|
2022-05-16 14:32:56 -04:00
|
|
|
}
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes := make([]*v1.Node, len(nodeKeys))
|
2022-05-16 14:32:56 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
for index, nodeKey := range nodeKeys {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-05-01 10:52:48 -04:00
|
|
|
fmt.Sprintf("node-%d", index+1),
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2022-05-16 14:32:56 -04:00
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2022-05-16 14:32:56 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeResult, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user.Name,
|
2022-05-16 14:32:56 -04:00
|
|
|
"register",
|
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2022-05-16 14:32:56 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(nodeResult), &node)
|
2022-05-16 14:32:56 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodes[index] = &node
|
2022-05-16 14:32:56 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Len(s.T(), nodes, len(nodeKeys))
|
2022-05-16 14:32:56 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllResult, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAll []v1.Node
|
2022-05-16 14:32:56 -04:00
|
|
|
err = json.Unmarshal([]byte(listAllResult), &listAll)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listAll, 5)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Contains(s.T(), listAll[0].GetGivenName(), "node-1")
|
|
|
|
assert.Contains(s.T(), listAll[1].GetGivenName(), "node-2")
|
|
|
|
assert.Contains(s.T(), listAll[2].GetGivenName(), "node-3")
|
|
|
|
assert.Contains(s.T(), listAll[3].GetGivenName(), "node-4")
|
|
|
|
assert.Contains(s.T(), listAll[4].GetGivenName(), "node-5")
|
2022-05-16 14:32:56 -04:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"rename",
|
|
|
|
"--identifier",
|
|
|
|
fmt.Sprintf("%d", listAll[i].Id),
|
2023-05-01 10:52:48 -04:00
|
|
|
fmt.Sprintf("newnode-%d", i+1),
|
2022-05-16 14:32:56 -04:00
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
}
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllAfterRenameResult, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAllAfterRename []v1.Node
|
2022-05-16 14:32:56 -04:00
|
|
|
err = json.Unmarshal([]byte(listAllAfterRenameResult), &listAllAfterRename)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listAllAfterRename, 5)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), "newnode-1", listAllAfterRename[0].GetGivenName())
|
|
|
|
assert.Equal(s.T(), "newnode-2", listAllAfterRename[1].GetGivenName())
|
|
|
|
assert.Equal(s.T(), "newnode-3", listAllAfterRename[2].GetGivenName())
|
|
|
|
assert.Contains(s.T(), listAllAfterRename[3].GetGivenName(), "node-4")
|
|
|
|
assert.Contains(s.T(), listAllAfterRename[4].GetGivenName(), "node-5")
|
2022-05-16 14:32:56 -04:00
|
|
|
|
|
|
|
// Test failure for too long names
|
2022-09-21 11:01:26 -04:00
|
|
|
result, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"rename",
|
|
|
|
"--identifier",
|
|
|
|
fmt.Sprintf("%d", listAll[4].Id),
|
|
|
|
"testmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaachine12345678901234567890",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
assert.Contains(s.T(), result, "not be over 63 chars")
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllAfterRenameAttemptResult, _, err := ExecuteCommand(
|
2022-05-16 14:32:56 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var listAllAfterRenameAttempt []v1.Node
|
2022-05-16 14:32:56 -04:00
|
|
|
err = json.Unmarshal(
|
|
|
|
[]byte(listAllAfterRenameAttemptResult),
|
|
|
|
&listAllAfterRenameAttempt,
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listAllAfterRenameAttempt, 5)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), "newnode-1", listAllAfterRenameAttempt[0].GetGivenName())
|
|
|
|
assert.Equal(s.T(), "newnode-2", listAllAfterRenameAttempt[1].GetGivenName())
|
|
|
|
assert.Equal(s.T(), "newnode-3", listAllAfterRenameAttempt[2].GetGivenName())
|
|
|
|
assert.Contains(s.T(), listAllAfterRenameAttempt[3].GetGivenName(), "node-4")
|
|
|
|
assert.Contains(s.T(), listAllAfterRenameAttempt[4].GetGivenName(), "node-5")
|
2022-05-16 14:32:56 -04:00
|
|
|
}
|
|
|
|
|
2022-01-25 17:11:15 -05:00
|
|
|
func (s *IntegrationCLITestSuite) TestApiKeyCommand() {
|
|
|
|
count := 5
|
|
|
|
|
|
|
|
keys := make([]string, count)
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2022-09-21 11:01:26 -04:00
|
|
|
apiResult, _, err := ExecuteCommand(
|
2022-01-25 17:11:15 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"create",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
assert.NotEmpty(s.T(), apiResult)
|
|
|
|
|
|
|
|
// var apiKey v1.ApiKey
|
|
|
|
// err = json.Unmarshal([]byte(apiResult), &apiKey)
|
|
|
|
// assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
keys[i] = apiResult
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(s.T(), keys, 5)
|
|
|
|
|
|
|
|
// Test list of keys
|
2022-09-21 11:01:26 -04:00
|
|
|
listResult, _, err := ExecuteCommand(
|
2022-01-25 17:11:15 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var listedApiKeys []v1.ApiKey
|
|
|
|
err = json.Unmarshal([]byte(listResult), &listedApiKeys)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), listedApiKeys, 5)
|
|
|
|
|
|
|
|
assert.Equal(s.T(), uint64(1), listedApiKeys[0].Id)
|
|
|
|
assert.Equal(s.T(), uint64(2), listedApiKeys[1].Id)
|
|
|
|
assert.Equal(s.T(), uint64(3), listedApiKeys[2].Id)
|
|
|
|
assert.Equal(s.T(), uint64(4), listedApiKeys[3].Id)
|
|
|
|
assert.Equal(s.T(), uint64(5), listedApiKeys[4].Id)
|
|
|
|
|
|
|
|
assert.NotEmpty(s.T(), listedApiKeys[0].Prefix)
|
|
|
|
assert.NotEmpty(s.T(), listedApiKeys[1].Prefix)
|
|
|
|
assert.NotEmpty(s.T(), listedApiKeys[2].Prefix)
|
|
|
|
assert.NotEmpty(s.T(), listedApiKeys[3].Prefix)
|
|
|
|
assert.NotEmpty(s.T(), listedApiKeys[4].Prefix)
|
|
|
|
|
|
|
|
assert.True(s.T(), listedApiKeys[0].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedApiKeys[1].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedApiKeys[2].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedApiKeys[3].Expiration.AsTime().After(time.Now()))
|
|
|
|
assert.True(s.T(), listedApiKeys[4].Expiration.AsTime().After(time.Now()))
|
|
|
|
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedApiKeys[0].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedApiKeys[1].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedApiKeys[2].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedApiKeys[3].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedApiKeys[4].Expiration.AsTime().Before(time.Now().Add(time.Hour*26)),
|
|
|
|
)
|
|
|
|
|
|
|
|
expiredPrefixes := make(map[string]bool)
|
|
|
|
|
|
|
|
// Expire three keys
|
|
|
|
for i := 0; i < 3; i++ {
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err := ExecuteCommand(
|
2022-01-25 17:11:15 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"expire",
|
|
|
|
"--prefix",
|
|
|
|
listedApiKeys[i].Prefix,
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
expiredPrefixes[listedApiKeys[i].Prefix] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test list pre auth keys after expire
|
2022-09-21 11:01:26 -04:00
|
|
|
listAfterExpireResult, _, err := ExecuteCommand(
|
2022-01-25 17:11:15 -05:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"apikeys",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
var listedAfterExpireApiKeys []v1.ApiKey
|
|
|
|
err = json.Unmarshal([]byte(listAfterExpireResult), &listedAfterExpireApiKeys)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
for index := range listedAfterExpireApiKeys {
|
|
|
|
if _, ok := expiredPrefixes[listedAfterExpireApiKeys[index].Prefix]; ok {
|
|
|
|
// Expired
|
|
|
|
assert.True(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpireApiKeys[index].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// Not expired
|
|
|
|
assert.False(
|
|
|
|
s.T(),
|
|
|
|
listedAfterExpireApiKeys[index].Expiration.AsTime().Before(time.Now()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-02 06:00:09 -04:00
|
|
|
|
|
|
|
func (s *IntegrationCLITestSuite) TestNodeMoveCommand() {
|
2023-01-17 11:43:44 -05:00
|
|
|
oldUser, err := s.createUser("old-user")
|
2022-05-02 06:00:09 -04:00
|
|
|
assert.Nil(s.T(), err)
|
2023-01-17 11:43:44 -05:00
|
|
|
newUser, err := s.createUser("new-user")
|
2022-05-02 06:00:09 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
// Randomly generated node key
|
|
|
|
nodeKey := "nodekey:688411b767663479632d44140f08a9fde87383adc7cdeb518f62ce28a17ef0aa"
|
2022-05-02 06:00:09 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err = ExecuteCommand(
|
2022-05-02 06:00:09 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"debug",
|
|
|
|
"create-node",
|
|
|
|
"--name",
|
2023-05-01 10:52:48 -04:00
|
|
|
"nomad-node",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
oldUser.Name,
|
2022-05-02 06:00:09 -04:00
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2022-05-02 06:00:09 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeResult, _, err := ExecuteCommand(
|
2022-05-02 06:00:09 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
oldUser.Name,
|
2022-05-02 06:00:09 -04:00
|
|
|
"register",
|
|
|
|
"--key",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeKey,
|
2022-05-02 06:00:09 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var node v1.Node
|
|
|
|
err = json.Unmarshal([]byte(nodeResult), &node)
|
2022-05-02 06:00:09 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), uint64(1), node.Id)
|
|
|
|
assert.Equal(s.T(), "nomad-node", node.Name)
|
|
|
|
assert.Equal(s.T(), node.User.Name, oldUser.Name)
|
2022-05-02 06:00:09 -04:00
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeId := fmt.Sprintf("%d", node.Id)
|
2022-05-02 06:00:09 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
moveToNewNSResult, _, err := ExecuteCommand(
|
2022-05-02 06:00:09 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeId,
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
newUser.Name,
|
2022-05-02 06:00:09 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
err = json.Unmarshal([]byte(moveToNewNSResult), &node)
|
2022-05-02 06:00:09 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), node.User, newUser)
|
2022-05-02 06:00:09 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
listAllNodesResult, _, err := ExecuteCommand(
|
2022-05-02 07:04:24 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
var allNodes []v1.Node
|
2022-05-02 07:04:24 -04:00
|
|
|
err = json.Unmarshal([]byte(listAllNodesResult), &allNodes)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Len(s.T(), allNodes, 1)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), allNodes[0].Id, node.Id)
|
|
|
|
assert.Equal(s.T(), allNodes[0].User, node.User)
|
2023-01-17 11:43:44 -05:00
|
|
|
assert.Equal(s.T(), allNodes[0].User, newUser)
|
2022-05-02 07:04:24 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
moveToNonExistingNSResult, _, err := ExecuteCommand(
|
2022-05-02 06:00:09 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeId,
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
"non-existing-user",
|
2022-05-02 06:00:09 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.Contains(
|
|
|
|
s.T(),
|
|
|
|
string(moveToNonExistingNSResult),
|
2023-01-17 11:43:44 -05:00
|
|
|
"User not found",
|
2022-05-02 06:00:09 -04:00
|
|
|
)
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), node.User, newUser)
|
2022-05-02 06:00:09 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
moveToOldNSResult, _, err := ExecuteCommand(
|
2022-05-02 06:00:09 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeId,
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
oldUser.Name,
|
2022-05-02 06:00:09 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
err = json.Unmarshal([]byte(moveToOldNSResult), &node)
|
2022-05-02 06:00:09 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), node.User, oldUser)
|
2022-05-02 06:00:09 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
moveToSameNSResult, _, err := ExecuteCommand(
|
2022-05-02 06:00:09 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"move",
|
|
|
|
"--identifier",
|
2023-05-01 10:52:48 -04:00
|
|
|
nodeId,
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
oldUser.Name,
|
2022-05-02 06:00:09 -04:00
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
err = json.Unmarshal([]byte(moveToSameNSResult), &node)
|
2022-05-02 06:00:09 -04:00
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
2023-05-01 10:52:48 -04:00
|
|
|
assert.Equal(s.T(), node.User, oldUser)
|
2022-05-02 06:00:09 -04:00
|
|
|
}
|
2022-06-05 07:29:42 -04:00
|
|
|
|
|
|
|
func (s *IntegrationCLITestSuite) TestLoadConfigFromCommand() {
|
|
|
|
// TODO: make sure defaultConfig is not same as altConfig
|
|
|
|
defaultConfig, err := os.ReadFile("integration_test/etc/config.dump.gold.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
altConfig, err := os.ReadFile("integration_test/etc/alt-config.dump.gold.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
2022-08-21 04:22:15 -04:00
|
|
|
altEnvConfig, err := os.ReadFile("integration_test/etc/alt-env-config.dump.gold.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
2022-06-05 07:29:42 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err = ExecuteCommand(
|
2022-06-05 07:29:42 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"dumpConfig",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
defaultDumpConfig, err := os.ReadFile("integration_test/etc/config.dump.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.YAMLEq(s.T(), string(defaultConfig), string(defaultDumpConfig))
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err = ExecuteCommand(
|
2022-06-05 07:29:42 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"-c",
|
|
|
|
"/etc/headscale/alt-config.yaml",
|
|
|
|
"dumpConfig",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
altDumpConfig, err := os.ReadFile("integration_test/etc/config.dump.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.YAMLEq(s.T(), string(altConfig), string(altDumpConfig))
|
2022-08-21 04:22:15 -04:00
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err = ExecuteCommand(
|
2022-08-21 04:22:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"dumpConfig",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"HEADSCALE_CONFIG=/etc/headscale/alt-env-config.yaml",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
altEnvDumpConfig, err := os.ReadFile("integration_test/etc/config.dump.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.YAMLEq(s.T(), string(altEnvConfig), string(altEnvDumpConfig))
|
|
|
|
|
2022-09-21 11:01:26 -04:00
|
|
|
_, _, err = ExecuteCommand(
|
2022-08-21 04:22:15 -04:00
|
|
|
&s.headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"-c",
|
|
|
|
"/etc/headscale/alt-config.yaml",
|
|
|
|
"dumpConfig",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"HEADSCALE_CONFIG=/etc/headscale/alt-env-config.yaml",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
altDumpConfig, err = os.ReadFile("integration_test/etc/config.dump.yaml")
|
|
|
|
assert.Nil(s.T(), err)
|
|
|
|
|
|
|
|
assert.YAMLEq(s.T(), string(altConfig), string(altDumpConfig))
|
2022-06-05 07:29:42 -04:00
|
|
|
}
|