Use JSON output and proper datamodel for tailscale status

This commit uses tailscale --json to check status, and unmarshal it into
the proper ipn Status.
This commit is contained in:
Kristoffer Dalby 2021-09-20 22:53:34 +01:00
parent f905812afa
commit 994b4eef17
No known key found for this signature in database
GPG Key ID: 09F62DC067465735

View File

@ -6,6 +6,7 @@ package headscale
import ( import (
"bytes" "bytes"
"context" "context"
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -20,6 +21,7 @@ import (
"github.com/ory/dockertest/v3/docker" "github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"tailscale.com/ipn/ipnstate"
"inet.af/netaddr" "inet.af/netaddr"
) )
@ -282,7 +284,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
fmt.Printf("Creating pre auth key for %s\n", namespace) fmt.Printf("Creating pre auth key for %s\n", namespace)
authKey, err := executeCommand( authKey, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "-n", namespace, "preauthkeys", "create", "--reusable", "--expiration", "24h"}, []string{"headscale", "--namespace", namespace, "preauthkeys", "create", "--reusable", "--expiration", "24h"},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -321,7 +323,7 @@ func (s *IntegrationTestSuite) TestListNodes() {
fmt.Println("Listing nodes") fmt.Println("Listing nodes")
result, err := executeCommand( result, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "-n", namespace, "nodes", "list"}, []string{"headscale", "--namespace", namespace, "nodes", "list"},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -366,7 +368,7 @@ func (s *IntegrationTestSuite) TestStatus() {
for hostname, tailscale := range scales.tailscales { for hostname, tailscale := range scales.tailscales {
s.T().Run(hostname, func(t *testing.T) { s.T().Run(hostname, func(t *testing.T) {
command := []string{"tailscale", "status"} command := []string{"tailscale", "status", "--json"}
fmt.Printf("Getting status for %s\n", hostname) fmt.Printf("Getting status for %s\n", hostname)
result, err := executeCommand( result, err := executeCommand(
@ -374,24 +376,41 @@ func (s *IntegrationTestSuite) TestStatus() {
command, command,
) )
assert.Nil(t, err) assert.Nil(t, err)
// fmt.Printf("Status for %s: %s", hostname, result)
var status ipnstate.Status
err = json.Unmarshal([]byte(result), &status)
assert.Nil(s.T(), err)
// TODO(kradalby): Replace this check with peer length of SAME namespace
// Check if we have as many nodes in status // Check if we have as many nodes in status
// as we have IPs/tailscales // as we have IPs/tailscales
lines := strings.Split(result, "\n") // lines := strings.Split(result, "\n")
assert.Equal(t, len(ips), len(lines)-1) // assert.Equal(t, len(ips), len(lines)-1)
assert.Equal(t, len(scales.tailscales), len(lines)-1) // assert.Equal(t, len(scales.tailscales), len(lines)-1)
peerIps := getIPsfromIPNstate(status)
// Check that all hosts is present in all hosts status // Check that all hosts is present in all hosts status
for ipHostname, ip := range ips { for ipHostname, ip := range ips {
assert.Contains(t, result, ip.String()) if hostname != ipHostname {
assert.Contains(t, result, ipHostname) assert.Contains(t, peerIps, ip)
}
} }
}) })
} }
} }
} }
func getIPsfromIPNstate(status ipnstate.Status) []netaddr.IP {
ips := make([]netaddr.IP, 0)
for _, peer := range status.Peer {
ips = append(ips, peer.TailscaleIPs...)
}
return ips
}
func (s *IntegrationTestSuite) TestPingAllPeers() { func (s *IntegrationTestSuite) TestPingAllPeers() {
for _, scales := range s.namespaces { for _, scales := range s.namespaces {
ips, err := getIPs(scales.tailscales) ips, err := getIPs(scales.tailscales)