Merge pull request #871 from kradalby/integration-ts-interface

Integration: make TailscaleClient interface
This commit is contained in:
Juan Font 2022-10-22 16:22:40 +02:00 committed by GitHub
commit 129afdb157
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 20 deletions

View file

@ -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++
}

View file

@ -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)

View file

@ -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) {

18
integration/tailscale.go Normal file
View file

@ -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
}

View file

@ -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,
)