From 8c4744acd980682cce5ff51b08462ec279d05646 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 21 Oct 2022 13:17:38 +0200 Subject: [PATCH 1/2] make TailscaleClient interface Signed-off-by: Kristoffer Dalby --- integration/tailscale.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 integration/tailscale.go diff --git a/integration/tailscale.go b/integration/tailscale.go new file mode 100644 index 00000000..b163c290 --- /dev/null +++ b/integration/tailscale.go @@ -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 +} From c9823ce347868e4a80660181c140a8882ef0e9f7 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 21 Oct 2022 13:17:54 +0200 Subject: [PATCH 2/2] Use TailscaleClient interface instead of tsic Signed-off-by: Kristoffer Dalby --- integration/general_test.go | 6 ++---- integration/scenario.go | 16 ++++++++-------- integration/scenario_test.go | 3 +-- integration/tsic/tsic.go | 16 ++++++++++------ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/integration/general_test.go b/integration/general_test.go index 2e7689ac..d5e014a4 100644 --- a/integration/general_test.go +++ b/integration/general_test.go @@ -3,8 +3,6 @@ package integration import ( "net/netip" "testing" - - "github.com/juanfont/headscale/integration/tsic" ) func TestPingAll(t *testing.T) { @@ -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) @@ -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++ } diff --git a/integration/scenario.go b/integration/scenario.go index ebe3aff2..4676fe4e 100644 --- a/integration/scenario.go +++ b/integration/scenario.go @@ -48,7 +48,7 @@ var ( ) type Namespace struct { - Clients map[string]*tsic.TailscaleInContainer + Clients map[string]TailscaleClient createWaitGroup sync.WaitGroup joinWaitGroup sync.WaitGroup @@ -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) @@ -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 @@ -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() @@ -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 @@ -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 @@ -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) diff --git a/integration/scenario_test.go b/integration/scenario_test.go index c1a51f30..2d8cb1a5 100644 --- a/integration/scenario_test.go +++ b/integration/scenario_test.go @@ -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 @@ -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) { diff --git a/integration/tsic/tsic.go b/integration/tsic/tsic.go index f3188135..f21dccae 100644 --- a/integration/tsic/tsic.go +++ b/integration/tsic/tsic.go @@ -29,7 +29,7 @@ var ( type TailscaleInContainer struct { version string - Hostname string + hostname string pool *dockertest.Pool container *dockertest.Resource @@ -84,7 +84,7 @@ func New( return &TailscaleInContainer{ version: version, - Hostname: hostname, + hostname: hostname, pool: pool, container: container, @@ -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 } @@ -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, @@ -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 } @@ -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, )