Integration tests working for taildrop

This commit is contained in:
Juan Font Alonso 2021-09-26 14:22:11 +02:00
parent 05a5f21c3d
commit 83ead36fce

View file

@ -21,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/client/tailscale/apitype"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"inet.af/netaddr" "inet.af/netaddr"
@ -93,13 +94,14 @@ func TestIntegrationTestSuite(t *testing.T) {
} }
} }
func executeCommand(resource *dockertest.Resource, cmd []string) (string, error) { func executeCommand(resource *dockertest.Resource, cmd []string, env []string) (string, error) {
var stdout bytes.Buffer var stdout bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer
exitCode, err := resource.Exec( exitCode, err := resource.Exec(
cmd, cmd,
dockertest.ExecOptions{ dockertest.ExecOptions{
Env: env,
StdOut: &stdout, StdOut: &stdout,
StdErr: &stderr, StdErr: &stderr,
}, },
@ -277,6 +279,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
result, err := executeCommand( result, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "namespaces", "create", namespace}, []string{"headscale", "namespaces", "create", namespace},
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
fmt.Println("headscale create namespace result: ", result) fmt.Println("headscale create namespace result: ", result)
@ -285,6 +288,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
authKey, err := executeCommand( authKey, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "--namespace", namespace, "preauthkeys", "create", "--reusable", "--expiration", "24h"}, []string{"headscale", "--namespace", namespace, "preauthkeys", "create", "--reusable", "--expiration", "24h"},
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -299,6 +303,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
result, err := executeCommand( result, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
fmt.Println("tailscale result: ", result) fmt.Println("tailscale result: ", result)
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -324,6 +329,7 @@ func (s *IntegrationTestSuite) TestListNodes() {
result, err := executeCommand( result, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "--namespace", namespace, "nodes", "list"}, []string{"headscale", "--namespace", namespace, "nodes", "list"},
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -374,6 +380,7 @@ func (s *IntegrationTestSuite) TestStatus() {
result, err := executeCommand( result, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
assert.Nil(t, err) assert.Nil(t, err)
@ -435,6 +442,7 @@ func (s *IntegrationTestSuite) TestPingAllPeers() {
result, err := executeCommand( result, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
assert.Nil(t, err) assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", hostname, result) fmt.Printf("Result for %s: %s\n", hostname, result)
@ -453,6 +461,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err := executeCommand( result, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "nodes", "list", "-o", "json", "--namespace", "shared"}, []string{"headscale", "nodes", "list", "-o", "json", "--namespace", "shared"},
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -465,6 +474,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err := executeCommand( result, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "nodes", "share", "--namespace", "shared", fmt.Sprint(machine.ID), "main"}, []string{"headscale", "nodes", "share", "--namespace", "shared", fmt.Sprint(machine.ID), "main"},
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
@ -474,6 +484,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err = executeCommand( result, err = executeCommand(
&headscale, &headscale,
[]string{"headscale", "nodes", "list", "--namespace", "main"}, []string{"headscale", "nodes", "list", "--namespace", "main"},
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
fmt.Println("Nodelist after sharing", result) fmt.Println("Nodelist after sharing", result)
@ -541,6 +552,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
_, err := executeCommand( _, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
for peername, ip := range ips { for peername, ip := range ips {
@ -552,8 +564,8 @@ func (s *IntegrationTestSuite) TestTailDrop() {
// using `tailscale file cp` - but not in userspace networking mode // using `tailscale file cp` - but not in userspace networking mode
// So curl! // So curl!
peerAPI, ok := apiURLs[ip] peerAPI, ok := apiURLs[ip]
assert.True(t, ok)
command := []string{ command := []string{
"ALL_PROXY=socks5://localhost:1055/",
"curl", "curl",
"-X", "-X",
"PUT", "PUT",
@ -565,6 +577,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
_, err := executeCommand( _, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{"ALL_PROXY=socks5://localhost:1055/"},
) )
assert.Nil(t, err) assert.Nil(t, err)
} }
@ -576,11 +589,12 @@ func (s *IntegrationTestSuite) TestTailDrop() {
command := []string{ command := []string{
"tailscale", "file", "tailscale", "file",
"get", "get",
".", "/tmp/",
} }
_, err := executeCommand( _, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)
for peername, ip := range ips { for peername, ip := range ips {
@ -594,6 +608,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
result, err := executeCommand( result, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
assert.Nil(t, err) assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", peername, result) fmt.Printf("Result for %s: %s\n", peername, result)
@ -613,6 +628,7 @@ func getIPs(tailscales map[string]dockertest.Resource) (map[string]netaddr.IP, e
result, err := executeCommand( result, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -630,11 +646,12 @@ func getIPs(tailscales map[string]dockertest.Resource) (map[string]netaddr.IP, e
func getAPIURLs(tailscales map[string]dockertest.Resource) (map[netaddr.IP]string, error) { func getAPIURLs(tailscales map[string]dockertest.Resource) (map[netaddr.IP]string, error) {
fts := make(map[netaddr.IP]string) fts := make(map[netaddr.IP]string)
for hostname, tailscale := range tailscales { for _, tailscale := range tailscales {
command := []string{"tailscale", "ip"} command := []string{"tailscale", "ip"}
result, err := executeCommand( result, err := executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -644,32 +661,35 @@ func getAPIURLs(tailscales map[string]dockertest.Resource) (map[netaddr.IP]strin
return nil, err return nil, err
} }
command := []string{ command = []string{
"curl", "curl",
"--unix-socket", "--unix-socket",
"/run/tailscale/tailscaled.sock", "/run/tailscale/tailscaled.sock",
"http://localhost/localapi/v0/file-targets", "http://localhost/localapi/v0/file-targets",
} }
result, err := executeCommand( result, err = executeCommand(
&tailscale, &tailscale,
command, command,
[]string{},
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println(ip)
fmt.Println(result)
var pft []apitype.FileTarget var pft []apitype.FileTarget
if err := json.Unmarshal(body, &ft); err != nil { if err := json.Unmarshal([]byte(result), &pft); err != nil {
return nil, fmt.Errorf("invalid JSON: %w", err) return nil, fmt.Errorf("invalid JSON: %w", err)
} }
for _, ft := range pft { for _, ft := range pft {
n := ft.Node n := ft.Node
for _, a := range n.Addresses { for _, a := range n.Addresses { // just add all the addresses
if a.IP() == ip { if _, ok := fts[a.IP()]; !ok {
fts[ip] = ft.PeerAPIURL fts[a.IP()] = ft.PeerAPIURL
break
} }
} }
} }
return fts, nil
} }
fmt.Printf("API URLs: %+v\n", fts)
return fts, nil
} }