Skip to content

Commit

Permalink
Port integration tests routes CLI to v2
Browse files Browse the repository at this point in the history
Fix options signature
  • Loading branch information
juanfont authored and kradalby committed Dec 6, 2022
1 parent 946d38e commit 52862b8
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 168 deletions.
12 changes: 12 additions & 0 deletions cmd/headscale/cli/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ var listRoutesCmd = &cobra.Command{
return
}

if output != "" {
SuccessOutput(response.Routes, "", output)

return
}

routes = response.Routes
} else {
response, err := client.GetMachineRoutes(ctx, &v1.GetMachineRoutesRequest{
Expand All @@ -92,6 +98,12 @@ var listRoutesCmd = &cobra.Command{
return
}

if output != "" {
SuccessOutput(response.Routes, "", output)

return
}

routes = response.Routes
}

Expand Down
1 change: 1 addition & 0 deletions grpcv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ func (api headscaleV1APIServer) DebugCreateMachine(

HostInfo: HostInfo(hostinfo),
}

nodeKey := key.NodePublic{}
err = nodeKey.UnmarshalText([]byte(request.GetKey()))
if err != nil {
Expand Down
143 changes: 143 additions & 0 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package integration

import (
"encoding/json"
"fmt"
"sort"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -389,3 +391,144 @@ func TestPreAuthKeyCommandReusableEphemeral(t *testing.T) {
err = scenario.Shutdown()
assert.NoError(t, err)
}

func TestEnablingRoutes(t *testing.T) {
IntegrationSkip(t)
t.Parallel()

namespace := "enable-routing"

scenario, err := NewScenario()
assert.NoError(t, err)

spec := map[string]int{
namespace: 3,
}

err = scenario.CreateHeadscaleEnv(spec, []tsic.Option{}, hsic.WithTestName("clienableroute"))
assert.NoError(t, err)

allClients, err := scenario.ListTailscaleClients()
if err != nil {
t.Errorf("failed to get clients: %s", err)
}

err = scenario.WaitForTailscaleSync()
if err != nil {
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
}

headscale, err := scenario.Headscale()
assert.NoError(t, err)

// advertise routes using the up command
for i, client := range allClients {
routeStr := fmt.Sprintf("10.0.%d.0/24", i)
hostname, _ := client.FQDN()
_, _, err = client.Execute([]string{
"tailscale",
"up",
fmt.Sprintf("--advertise-routes=%s", routeStr),
"-login-server", headscale.GetEndpoint(),
"--hostname", hostname,
})
assert.NoError(t, err)
}

err = scenario.WaitForTailscaleSync()
if err != nil {
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
}

var routes []*v1.Route
err = executeAndUnmarshal(
headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
&routes,
)

assert.NoError(t, err)
assert.Len(t, routes, 3)

for _, route := range routes {
assert.Equal(t, route.Advertised, true)
assert.Equal(t, route.Enabled, false)
assert.Equal(t, route.IsPrimary, false)
}

for _, route := range routes {
_, err = headscale.Execute(
[]string{
"headscale",
"routes",
"enable",
"--route",
strconv.Itoa(int(route.Id)),
})
assert.NoError(t, err)
}

var enablingRoutes []*v1.Route
err = executeAndUnmarshal(
headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
&enablingRoutes,
)
assert.NoError(t, err)

for _, route := range enablingRoutes {
assert.Equal(t, route.Advertised, true)
assert.Equal(t, route.Enabled, true)
assert.Equal(t, route.IsPrimary, true)
}

routeIDToBeDisabled := enablingRoutes[0].Id

_, err = headscale.Execute(
[]string{
"headscale",
"routes",
"disable",
"--route",
strconv.Itoa(int(routeIDToBeDisabled)),
})
assert.NoError(t, err)

var disablingRoutes []*v1.Route
err = executeAndUnmarshal(
headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
&disablingRoutes,
)
assert.NoError(t, err)

for _, route := range disablingRoutes {
assert.Equal(t, true, route.Advertised)

if route.Id == routeIDToBeDisabled {
assert.Equal(t, route.Enabled, false)
assert.Equal(t, route.IsPrimary, false)
} else {
assert.Equal(t, route.Enabled, true)
assert.Equal(t, route.IsPrimary, true)
}
}
}
168 changes: 0 additions & 168 deletions integration_cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net/http"
"os"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -1244,173 +1243,6 @@ func (s *IntegrationCLITestSuite) TestNodeRenameCommand() {
assert.Contains(s.T(), listAllAfterRenameAttempt[4].GetGivenName(), "machine-5")
}

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

// Randomly generated machine keys
machineKey := "nodekey:9b2ffa7e08cc421a3d2cca9012280f6a236fd0de0b4ce005b30a98ad930306fe"

_, _, err = ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"debug",
"create-node",
"--name",
"route-machine",
"--namespace",
namespace.Name,
"--key",
machineKey,
"--route",
"10.0.0.0/8",
"--route",
"192.168.1.0/24",
"--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)

assert.Equal(s.T(), uint64(1), machine.Id)
assert.Equal(s.T(), "route-machine", machine.Name)

listAllResult, _, err := ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
[]string{},
)
assert.Nil(s.T(), err)

var routes []v1.Route
err = json.Unmarshal([]byte(listAllResult), &routes)
assert.Nil(s.T(), err)

assert.Len(s.T(), routes, 2)
assert.Equal(s.T(), routes[0].Enabled, false)
assert.Equal(s.T(), routes[1].Enabled, false)

routeIDToEnable := routes[1].Id

_, _, err = ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"enable",
"--output",
"json",
"--route",
strconv.FormatUint(routeIDToEnable, 10),
},
[]string{},
)
assert.Nil(s.T(), err)

listAllResult, _, err = ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
[]string{},
)
assert.Nil(s.T(), err)

assert.Nil(s.T(), err)

err = json.Unmarshal([]byte(listAllResult), &routes)
assert.Nil(s.T(), err)

assert.Len(s.T(), routes, 2)

for _, route := range routes {
if route.Id == routeIDToEnable {
assert.Equal(s.T(), route.Enabled, true)
assert.Equal(s.T(), route.IsPrimary, true)
} else {
assert.Equal(s.T(), route.Enabled, false)
}
}

// Enable only one route, effectively disabling one of the routes
_, _, err = ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"disable",
"--output",
"json",
"--route",
strconv.FormatUint(routeIDToEnable, 10),
},
[]string{},
)
assert.Nil(s.T(), err)

listAllResult, _, err = ExecuteCommand(
&s.headscale,
[]string{
"headscale",
"routes",
"list",
"--output",
"json",
},
[]string{},
)
assert.Nil(s.T(), err)

assert.Nil(s.T(), err)

err = json.Unmarshal([]byte(listAllResult), &routes)
assert.Nil(s.T(), err)

assert.Len(s.T(), routes, 2)

for _, route := range routes {
if route.Id == routeIDToEnable {
assert.Equal(s.T(), route.Enabled, false)
assert.Equal(s.T(), route.IsPrimary, false)
} else {
assert.Equal(s.T(), route.Enabled, false)
}
}
}

func (s *IntegrationCLITestSuite) TestApiKeyCommand() {
count := 5

Expand Down

0 comments on commit 52862b8

Please sign in to comment.