Skip to content

Commit

Permalink
Integration tests working for taildrop
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfont committed Sep 26, 2021
1 parent 05a5f21 commit 83ead36
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"tailscale.com/client/tailscale/apitype"
"tailscale.com/ipn/ipnstate"

"inet.af/netaddr"
Expand Down Expand Up @@ -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 stderr bytes.Buffer

exitCode, err := resource.Exec(
cmd,
dockertest.ExecOptions{
Env: env,
StdOut: &stdout,
StdErr: &stderr,
},
Expand Down Expand Up @@ -277,6 +279,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
result, err := executeCommand(
&headscale,
[]string{"headscale", "namespaces", "create", namespace},
[]string{},
)
assert.Nil(s.T(), err)
fmt.Println("headscale create namespace result: ", result)
Expand All @@ -285,6 +288,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
authKey, err := executeCommand(
&headscale,
[]string{"headscale", "--namespace", namespace, "preauthkeys", "create", "--reusable", "--expiration", "24h"},
[]string{},
)
assert.Nil(s.T(), err)

Expand All @@ -299,6 +303,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
result, err := executeCommand(
&tailscale,
command,
[]string{},
)
fmt.Println("tailscale result: ", result)
assert.Nil(s.T(), err)
Expand All @@ -324,6 +329,7 @@ func (s *IntegrationTestSuite) TestListNodes() {
result, err := executeCommand(
&headscale,
[]string{"headscale", "--namespace", namespace, "nodes", "list"},
[]string{},
)
assert.Nil(s.T(), err)

Expand Down Expand Up @@ -374,6 +380,7 @@ func (s *IntegrationTestSuite) TestStatus() {
result, err := executeCommand(
&tailscale,
command,
[]string{},
)
assert.Nil(t, err)

Expand Down Expand Up @@ -435,6 +442,7 @@ func (s *IntegrationTestSuite) TestPingAllPeers() {
result, err := executeCommand(
&tailscale,
command,
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", hostname, result)
Expand All @@ -453,6 +461,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err := executeCommand(
&headscale,
[]string{"headscale", "nodes", "list", "-o", "json", "--namespace", "shared"},
[]string{},
)
assert.Nil(s.T(), err)

Expand All @@ -465,6 +474,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err := executeCommand(
&headscale,
[]string{"headscale", "nodes", "share", "--namespace", "shared", fmt.Sprint(machine.ID), "main"},
[]string{},
)
assert.Nil(s.T(), err)

Expand All @@ -474,6 +484,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err = executeCommand(
&headscale,
[]string{"headscale", "nodes", "list", "--namespace", "main"},
[]string{},
)
assert.Nil(s.T(), err)
fmt.Println("Nodelist after sharing", result)
Expand Down Expand Up @@ -541,6 +552,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
_, err := executeCommand(
&tailscale,
command,
[]string{},
)
assert.Nil(s.T(), err)
for peername, ip := range ips {
Expand All @@ -552,8 +564,8 @@ func (s *IntegrationTestSuite) TestTailDrop() {
// using `tailscale file cp` - but not in userspace networking mode
// So curl!
peerAPI, ok := apiURLs[ip]
assert.True(t, ok)
command := []string{
"ALL_PROXY=socks5://localhost:1055/",
"curl",
"-X",
"PUT",
Expand All @@ -565,6 +577,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
_, err := executeCommand(
&tailscale,
command,
[]string{"ALL_PROXY=socks5://localhost:1055/"},
)
assert.Nil(t, err)
}
Expand All @@ -576,11 +589,12 @@ func (s *IntegrationTestSuite) TestTailDrop() {
command := []string{
"tailscale", "file",
"get",
".",
"/tmp/",
}
_, err := executeCommand(
&tailscale,
command,
[]string{},
)
assert.Nil(s.T(), err)
for peername, ip := range ips {
Expand All @@ -594,6 +608,7 @@ func (s *IntegrationTestSuite) TestTailDrop() {
result, err := executeCommand(
&tailscale,
command,
[]string{},
)
assert.Nil(t, err)
fmt.Printf("Result for %s: %s\n", peername, result)
Expand All @@ -613,6 +628,7 @@ func getIPs(tailscales map[string]dockertest.Resource) (map[string]netaddr.IP, e
result, err := executeCommand(
&tailscale,
command,
[]string{},
)
if err != nil {
return nil, err
Expand All @@ -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) {
fts := make(map[netaddr.IP]string)
for hostname, tailscale := range tailscales {
for _, tailscale := range tailscales {
command := []string{"tailscale", "ip"}
result, err := executeCommand(
&tailscale,
command,
[]string{},
)
if err != nil {
return nil, err
Expand All @@ -644,32 +661,35 @@ func getAPIURLs(tailscales map[string]dockertest.Resource) (map[netaddr.IP]strin
return nil, err
}

command := []string{
command = []string{
"curl",
"--unix-socket",
"/run/tailscale/tailscaled.sock",
"http://localhost/localapi/v0/file-targets",
}
result, err := executeCommand(
result, err = executeCommand(
&tailscale,
command,
[]string{},
)
if err != nil {
return nil, err
}
fmt.Println(ip)
fmt.Println(result)
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)
}
for _, ft := range pft {
n := ft.Node
for _, a := range n.Addresses {
if a.IP() == ip {
fts[ip] = ft.PeerAPIURL
break
for _, a := range n.Addresses { // just add all the addresses
if _, ok := fts[a.IP()]; !ok {
fts[a.IP()] = ft.PeerAPIURL
}
}
}
return fts, nil
}
fmt.Printf("API URLs: %+v\n", fts)
return fts, nil
}

0 comments on commit 83ead36

Please sign in to comment.