Skip to content

Commit

Permalink
Merge pull request #871 from kradalby/integration-ts-interface
Browse files Browse the repository at this point in the history
Integration: make TailscaleClient interface
  • Loading branch information
juanfont authored Oct 22, 2022
2 parents babd303 + e112514 commit 129afdb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
6 changes: 2 additions & 4 deletions integration/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package integration
import (
"net/netip"
"testing"

"github.com/juanfont/headscale/integration/tsic"
)

func TestPingAll(t *testing.T) {
Expand All @@ -26,7 +24,7 @@ func TestPingAll(t *testing.T) {
}

var allIps []netip.Addr
var allClients []*tsic.TailscaleInContainer
var allClients []TailscaleClient

for namespace, count := range spec {
ips, err := scenario.GetIPs(namespace)
Expand Down Expand Up @@ -62,7 +60,7 @@ func TestPingAll(t *testing.T) {
for _, ip := range allIps {
err := client.Ping(ip)
if err != nil {
t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname, err)
t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err)
} else {
success++
}
Expand Down
16 changes: 8 additions & 8 deletions integration/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var (
)

type Namespace struct {
Clients map[string]*tsic.TailscaleInContainer
Clients map[string]TailscaleClient

createWaitGroup sync.WaitGroup
joinWaitGroup sync.WaitGroup
Expand Down Expand Up @@ -118,7 +118,7 @@ func (s *Scenario) Shutdown() error {

for namespaceName, namespace := range s.namespaces {
for _, client := range namespace.Clients {
log.Printf("removing client %s in namespace %s", client.Hostname, namespaceName)
log.Printf("removing client %s in namespace %s", client.Hostname(), namespaceName)
err := client.Shutdown()
if err != nil {
return fmt.Errorf("failed to tear down client: %w", err)
Expand Down Expand Up @@ -179,7 +179,7 @@ func (s *Scenario) CreateNamespace(namespace string) error {
}

s.namespaces[namespace] = &Namespace{
Clients: make(map[string]*tsic.TailscaleInContainer),
Clients: make(map[string]TailscaleClient),
}

return nil
Expand Down Expand Up @@ -214,7 +214,7 @@ func (s *Scenario) CreateTailscaleNodesInNamespace(
log.Printf("failed to add tailscale node: %s", err)
}

namespace.Clients[tsClient.Hostname] = tsClient
namespace.Clients[tsClient.Hostname()] = tsClient
}()
}
namespace.createWaitGroup.Wait()
Expand All @@ -232,7 +232,7 @@ func (s *Scenario) RunTailscaleUp(
for _, client := range namespace.Clients {
namespace.joinWaitGroup.Add(1)

go func(c *tsic.TailscaleInContainer) {
go func(c TailscaleClient) {
defer namespace.joinWaitGroup.Done()

// TODO(kradalby): error handle this
Expand Down Expand Up @@ -264,7 +264,7 @@ func (s *Scenario) WaitForTailscaleSync() error {
for _, client := range namespace.Clients {
namespace.syncWaitGroup.Add(1)

go func(c *tsic.TailscaleInContainer) {
go func(c TailscaleClient) {
defer namespace.syncWaitGroup.Done()

// TODO(kradalby): error handle this
Expand Down Expand Up @@ -333,8 +333,8 @@ func (s *Scenario) GetIPs(namespace string) ([]netip.Addr, error) {
return ips, fmt.Errorf("failed to get ips: %w", errNoNamespaceAvailable)
}

func (s *Scenario) GetClients(namespace string) ([]*tsic.TailscaleInContainer, error) {
var clients []*tsic.TailscaleInContainer
func (s *Scenario) GetClients(namespace string) ([]TailscaleClient, error) {
var clients []TailscaleClient
if ns, ok := s.namespaces[namespace]; ok {
for _, client := range ns.Clients {
clients = append(clients, client)
Expand Down
3 changes: 1 addition & 2 deletions integration/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/juanfont/headscale/integration/dockertestutil"
"github.com/juanfont/headscale/integration/tsic"
)

// This file is intendet to "test the test framework", by proxy it will also test
Expand Down Expand Up @@ -81,7 +80,7 @@ func TestCreateTailscale(t *testing.T) {
}

scenario.namespaces[namespace] = &Namespace{
Clients: make(map[string]*tsic.TailscaleInContainer),
Clients: make(map[string]TailscaleClient),
}

t.Run("create-tailscale", func(t *testing.T) {
Expand Down
18 changes: 18 additions & 0 deletions integration/tailscale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package integration

import (
"net/netip"

"tailscale.com/ipn/ipnstate"
)

type TailscaleClient interface {
Hostname() string
Shutdown() error
Version() string
Up(loginServer, authKey string) error
IPs() ([]netip.Addr, error)
Status() (*ipnstate.Status, error)
WaitForPeers(expected int) error
Ping(ip netip.Addr) error
}
16 changes: 10 additions & 6 deletions integration/tsic/tsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (

type TailscaleInContainer struct {
version string
Hostname string
hostname string

pool *dockertest.Pool
container *dockertest.Resource
Expand Down Expand Up @@ -84,7 +84,7 @@ func New(

return &TailscaleInContainer{
version: version,
Hostname: hostname,
hostname: hostname,

pool: pool,
container: container,
Expand All @@ -96,6 +96,10 @@ func (t *TailscaleInContainer) Shutdown() error {
return t.pool.Purge(t.container)
}

func (t *TailscaleInContainer) Hostname() string {
return t.hostname
}

func (t *TailscaleInContainer) Version() string {
return t.version
}
Expand All @@ -111,11 +115,11 @@ func (t *TailscaleInContainer) Up(
"--authkey",
authKey,
"--hostname",
t.Hostname,
t.hostname,
}

log.Println("Join command:", command)
log.Printf("Running join command for %s\n", t.Hostname)
log.Printf("Running join command for %s\n", t.hostname)
stdout, stderr, err := dockertestutil.ExecuteCommand(
t.container,
command,
Expand All @@ -131,7 +135,7 @@ func (t *TailscaleInContainer) Up(
log.Printf("tailscale join stdout: %s\n", stdout)
}

log.Printf("%s joined\n", t.Hostname)
log.Printf("%s joined\n", t.hostname)

return nil
}
Expand Down Expand Up @@ -234,7 +238,7 @@ func (t *TailscaleInContainer) Ping(ip netip.Addr) error {
if err != nil {
log.Printf(
"failed to run ping command from %s to %s, err: %s",
t.Hostname,
t.hostname,
ip.String(),
err,
)
Expand Down

0 comments on commit 129afdb

Please sign in to comment.