Skip to content

Commit

Permalink
feat: add integration tests for tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
restanrm committed May 13, 2022
1 parent 63d9205 commit 31c0062
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions integration_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,128 @@ func (s *IntegrationCLITestSuite) TestPreAuthKeyCommandReusableEphemeral() {
assert.Len(s.T(), listedPreAuthKeys, 2)
}

func (s *IntegrationCLITestSuite) TestNodeTagCommand() {
namespace, err := s.createNamespace("machine-namespace")
assert.Nil(s.T(), err)

machineKeys := []string{
"9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe",
"6abd00bb5fdda622db51387088c68e97e71ce58e7056aa54f592b6a8219d524c",
}
machines := make([]*v1.Machine, len(machineKeys))
assert.Nil(s.T(), err)

for index, machineKey := range machineKeys {
_, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"debug",
"create-node",
"--name",
fmt.Sprintf("machine-%d", index+1),
"--namespace",
namespace.Name,
"--key",
machineKey,
"--output",
"json",
},
[]string{},
)
assert.Nil(s.T(), err)

machineResult, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"nodes",
"--namespace",
namespace.Name,
"register",
"--key",
machineKey,
"--output",
"json",
},
[]string{},
)
assert.Nil(s.T(), err)

var machine v1.Machine
err = json.Unmarshal([]byte(machineResult), &machine)
assert.Nil(s.T(), err)

machines[index] = &machine
}
assert.Len(s.T(), machines, len(machineKeys))

addTagResult, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"nodes",
"tag",
"-i", "1",
"-t", "tag:test",
"--output", "json",
},
[]string{},
)
assert.Nil(s.T(), err)

var machine v1.Machine
err = json.Unmarshal([]byte(addTagResult), &machine)
assert.Nil(s.T(), err)
assert.Equal(s.T(), []string{"tag:test"}, machine.ForcedTags)

// try to set a wrong tag and retrieve the error
_, err = ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"nodes",
"tag",
"-i", "2",
"-t", "wrong-tag",
"--output", "json",
},
[]string{},
)
assert.ErrorContains(s.T(), err, "Invalid tag detected")

// Test list all nodes after added seconds
listAllResult, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"nodes",
"list",
"--output", "json",
},
[]string{},
)
resultMachines := make([]*v1.Machine, len(machineKeys))
assert.Nil(s.T(), err)
json.Unmarshal([]byte(listAllResult), &resultMachines)
found := false
for _, machine := range machines {
if machine.ForcedTags != nil {
for _, tag := range machine.ForcedTags {
if tag == "tag:test" {
found = true
}
}
}
}
assert.Equal(
s.T(),
true,
found,
"should find a machine with the tag 'tag:test' in the list of machines",
)
}

func (s *IntegrationCLITestSuite) TestNodeCommand() {
namespace, err := s.createNamespace("machine-namespace")
assert.Nil(s.T(), err)
Expand Down

0 comments on commit 31c0062

Please sign in to comment.