fixup CLI tests

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2025-01-17 15:32:21 +01:00
parent 2d4500f60f
commit b93c0744a3
No known key found for this signature in database
5 changed files with 72 additions and 68 deletions

View file

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"tailscale.com/types/key"
) )
const ( const (
@ -79,7 +79,7 @@ var createNodeCmd = &cobra.Command{
) )
} }
machineKey, err := cmd.Flags().GetString("key") registrationID, err := cmd.Flags().GetString("key")
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -88,8 +88,7 @@ var createNodeCmd = &cobra.Command{
) )
} }
var mkey key.MachinePublic _, err = types.RegistrationIDFromString(registrationID)
err = mkey.UnmarshalText([]byte(machineKey))
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -108,7 +107,7 @@ var createNodeCmd = &cobra.Command{
} }
request := &v1.DebugCreateNodeRequest{ request := &v1.DebugCreateNodeRequest{
Key: machineKey, Key: registrationID,
Name: name, Name: name,
User: user, User: user,
Routes: routes, Routes: routes,

View file

@ -122,7 +122,7 @@ var registerNodeCmd = &cobra.Command{
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
machineKey, err := cmd.Flags().GetString("key") registrationID, err := cmd.Flags().GetString("key")
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -132,7 +132,7 @@ var registerNodeCmd = &cobra.Command{
} }
request := &v1.RegisterNodeRequest{ request := &v1.RegisterNodeRequest{
Key: machineKey, Key: registrationID,
User: user, User: user,
} }

View file

@ -843,11 +843,10 @@ func (api headscaleV1APIServer) DebugCreateNode(
return nil, err return nil, err
} }
nodeKey := key.NewNode()
newNode := types.RegisterNode{ newNode := types.RegisterNode{
Node: types.Node{ Node: types.Node{
NodeKey: nodeKey.Public(), NodeKey: key.NewNode().Public(),
MachineKey: key.NewMachine().Public(),
Hostname: request.GetName(), Hostname: request.GetName(),
User: *user, User: *user,

View file

@ -139,6 +139,14 @@ func NewRegistrationID() (RegistrationID, error) {
return RegistrationID(rid), nil return RegistrationID(rid), nil
} }
func MustRegistrationID() RegistrationID {
rid, err := NewRegistrationID()
if err != nil {
panic(err)
}
return rid
}
func RegistrationIDFromString(str string) (RegistrationID, error) { func RegistrationIDFromString(str string) (RegistrationID, error) {
if len(str) != RegistrationIDLength { if len(str) != RegistrationIDLength {
return "", fmt.Errorf("registration ID must be %d characters long", RegistrationIDLength) return "", fmt.Errorf("registration ID must be %d characters long", RegistrationIDLength)

View file

@ -12,6 +12,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts" "github.com/google/go-cmp/cmp/cmpopts"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/policy" "github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/integration/hsic" "github.com/juanfont/headscale/integration/hsic"
"github.com/juanfont/headscale/integration/tsic" "github.com/juanfont/headscale/integration/tsic"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -812,14 +813,14 @@ func TestNodeTagCommand(t *testing.T) {
headscale, err := scenario.Headscale() headscale, err := scenario.Headscale()
assertNoErr(t, err) assertNoErr(t, err)
machineKeys := []string{ regIDs := []string{
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe", types.MustRegistrationID().String(),
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c", types.MustRegistrationID().String(),
} }
nodes := make([]*v1.Node, len(machineKeys)) nodes := make([]*v1.Node, len(regIDs))
assert.Nil(t, err) assert.Nil(t, err)
for index, machineKey := range machineKeys { for index, regID := range regIDs {
_, err := headscale.Execute( _, err := headscale.Execute(
[]string{ []string{
"headscale", "headscale",
@ -830,7 +831,7 @@ func TestNodeTagCommand(t *testing.T) {
"--user", "--user",
"user1", "user1",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -847,7 +848,7 @@ func TestNodeTagCommand(t *testing.T) {
"user1", "user1",
"register", "register",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -857,7 +858,7 @@ func TestNodeTagCommand(t *testing.T) {
nodes[index] = &node nodes[index] = &node
} }
assert.Len(t, nodes, len(machineKeys)) assert.Len(t, nodes, len(regIDs))
var node v1.Node var node v1.Node
err = executeAndUnmarshal( err = executeAndUnmarshal(
@ -889,7 +890,7 @@ func TestNodeTagCommand(t *testing.T) {
assert.ErrorContains(t, err, "tag must start with the string 'tag:'") assert.ErrorContains(t, err, "tag must start with the string 'tag:'")
// Test list all nodes after added seconds // Test list all nodes after added seconds
resultMachines := make([]*v1.Node, len(machineKeys)) resultMachines := make([]*v1.Node, len(regIDs))
err = executeAndUnmarshal( err = executeAndUnmarshal(
headscale, headscale,
[]string{ []string{
@ -1054,18 +1055,17 @@ func TestNodeCommand(t *testing.T) {
headscale, err := scenario.Headscale() headscale, err := scenario.Headscale()
assertNoErr(t, err) assertNoErr(t, err)
// Pregenerated machine keys regIDs := []string{
machineKeys := []string{ types.MustRegistrationID().String(),
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe", types.MustRegistrationID().String(),
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c", types.MustRegistrationID().String(),
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507", types.MustRegistrationID().String(),
"mkey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1", types.MustRegistrationID().String(),
"mkey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
} }
nodes := make([]*v1.Node, len(machineKeys)) nodes := make([]*v1.Node, len(regIDs))
assert.Nil(t, err) assert.Nil(t, err)
for index, machineKey := range machineKeys { for index, regID := range regIDs {
_, err := headscale.Execute( _, err := headscale.Execute(
[]string{ []string{
"headscale", "headscale",
@ -1076,7 +1076,7 @@ func TestNodeCommand(t *testing.T) {
"--user", "--user",
"node-user", "node-user",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1093,7 +1093,7 @@ func TestNodeCommand(t *testing.T) {
"node-user", "node-user",
"register", "register",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1104,7 +1104,7 @@ func TestNodeCommand(t *testing.T) {
nodes[index] = &node nodes[index] = &node
} }
assert.Len(t, nodes, len(machineKeys)) assert.Len(t, nodes, len(regIDs))
// Test list all nodes after added seconds // Test list all nodes after added seconds
var listAll []v1.Node var listAll []v1.Node
@ -1135,14 +1135,14 @@ func TestNodeCommand(t *testing.T) {
assert.Equal(t, "node-4", listAll[3].GetName()) assert.Equal(t, "node-4", listAll[3].GetName())
assert.Equal(t, "node-5", listAll[4].GetName()) assert.Equal(t, "node-5", listAll[4].GetName())
otherUserMachineKeys := []string{ otherUserRegIDs := []string{
"mkey:b5b444774186d4217adcec407563a1223929465ee2c68a4da13af0d0185b4f8e", types.MustRegistrationID().String(),
"mkey:dc721977ac7415aafa87f7d4574cbe07c6b171834a6d37375782bdc1fb6b3584", types.MustRegistrationID().String(),
} }
otherUserMachines := make([]*v1.Node, len(otherUserMachineKeys)) otherUserMachines := make([]*v1.Node, len(otherUserRegIDs))
assert.Nil(t, err) assert.Nil(t, err)
for index, machineKey := range otherUserMachineKeys { for index, regID := range otherUserRegIDs {
_, err := headscale.Execute( _, err := headscale.Execute(
[]string{ []string{
"headscale", "headscale",
@ -1153,7 +1153,7 @@ func TestNodeCommand(t *testing.T) {
"--user", "--user",
"other-user", "other-user",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1170,7 +1170,7 @@ func TestNodeCommand(t *testing.T) {
"other-user", "other-user",
"register", "register",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1181,7 +1181,7 @@ func TestNodeCommand(t *testing.T) {
otherUserMachines[index] = &node otherUserMachines[index] = &node
} }
assert.Len(t, otherUserMachines, len(otherUserMachineKeys)) assert.Len(t, otherUserMachines, len(otherUserRegIDs))
// Test list all nodes after added otherUser // Test list all nodes after added otherUser
var listAllWithotherUser []v1.Node var listAllWithotherUser []v1.Node
@ -1294,17 +1294,16 @@ func TestNodeExpireCommand(t *testing.T) {
headscale, err := scenario.Headscale() headscale, err := scenario.Headscale()
assertNoErr(t, err) assertNoErr(t, err)
// Pregenerated machine keys regIDs := []string{
machineKeys := []string{ types.MustRegistrationID().String(),
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe", types.MustRegistrationID().String(),
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c", types.MustRegistrationID().String(),
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507", types.MustRegistrationID().String(),
"mkey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1", types.MustRegistrationID().String(),
"mkey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084",
} }
nodes := make([]*v1.Node, len(machineKeys)) nodes := make([]*v1.Node, len(regIDs))
for index, machineKey := range machineKeys { for index, regID := range regIDs {
_, err := headscale.Execute( _, err := headscale.Execute(
[]string{ []string{
"headscale", "headscale",
@ -1315,7 +1314,7 @@ func TestNodeExpireCommand(t *testing.T) {
"--user", "--user",
"node-expire-user", "node-expire-user",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1332,7 +1331,7 @@ func TestNodeExpireCommand(t *testing.T) {
"node-expire-user", "node-expire-user",
"register", "register",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1343,7 +1342,7 @@ func TestNodeExpireCommand(t *testing.T) {
nodes[index] = &node nodes[index] = &node
} }
assert.Len(t, nodes, len(machineKeys)) assert.Len(t, nodes, len(regIDs))
var listAll []v1.Node var listAll []v1.Node
err = executeAndUnmarshal( err = executeAndUnmarshal(
@ -1421,18 +1420,17 @@ func TestNodeRenameCommand(t *testing.T) {
headscale, err := scenario.Headscale() headscale, err := scenario.Headscale()
assertNoErr(t, err) assertNoErr(t, err)
// Pregenerated machine keys regIDs := []string{
machineKeys := []string{ types.MustRegistrationID().String(),
"mkey:cf7b0fd05da556fdc3bab365787b506fd82d64a70745db70e00e86c1b1c03084", types.MustRegistrationID().String(),
"mkey:8bc13285cee598acf76b1824a6f4490f7f2e3751b201e28aeb3b07fe81d5b4a1", types.MustRegistrationID().String(),
"mkey:f08305b4ee4250b95a70f3b7504d048d75d899993c624a26d422c67af0422507", types.MustRegistrationID().String(),
"mkey:6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c", types.MustRegistrationID().String(),
"mkey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
} }
nodes := make([]*v1.Node, len(machineKeys)) nodes := make([]*v1.Node, len(regIDs))
assert.Nil(t, err) assert.Nil(t, err)
for index, machineKey := range machineKeys { for index, regID := range regIDs {
_, err := headscale.Execute( _, err := headscale.Execute(
[]string{ []string{
"headscale", "headscale",
@ -1443,7 +1441,7 @@ func TestNodeRenameCommand(t *testing.T) {
"--user", "--user",
"node-rename-command", "node-rename-command",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1460,7 +1458,7 @@ func TestNodeRenameCommand(t *testing.T) {
"node-rename-command", "node-rename-command",
"register", "register",
"--key", "--key",
machineKey, regID,
"--output", "--output",
"json", "json",
}, },
@ -1471,7 +1469,7 @@ func TestNodeRenameCommand(t *testing.T) {
nodes[index] = &node nodes[index] = &node
} }
assert.Len(t, nodes, len(machineKeys)) assert.Len(t, nodes, len(regIDs))
var listAll []v1.Node var listAll []v1.Node
err = executeAndUnmarshal( err = executeAndUnmarshal(
@ -1589,7 +1587,7 @@ func TestNodeMoveCommand(t *testing.T) {
assertNoErr(t, err) assertNoErr(t, err)
// Randomly generated node key // Randomly generated node key
machineKey := "mkey:688411b767663479632d44140f08a9fde87383adc7cdeb518f62ce28a17ef0aa" regID := types.MustRegistrationID()
_, err = headscale.Execute( _, err = headscale.Execute(
[]string{ []string{
@ -1601,7 +1599,7 @@ func TestNodeMoveCommand(t *testing.T) {
"--user", "--user",
"old-user", "old-user",
"--key", "--key",
machineKey, regID.String(),
"--output", "--output",
"json", "json",
}, },
@ -1618,7 +1616,7 @@ func TestNodeMoveCommand(t *testing.T) {
"old-user", "old-user",
"register", "register",
"--key", "--key",
machineKey, regID.String(),
"--output", "--output",
"json", "json",
}, },