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 ( import (
"net/netip" "net/netip"
"testing" "testing"
"github.com/juanfont/headscale/integration/tsic"
) )
func TestPingAll(t *testing.T) { func TestPingAll(t *testing.T) {
@ -26,7 +24,7 @@ func TestPingAll(t *testing.T) {
} }
var allIps []netip.Addr var allIps []netip.Addr
var allClients []*tsic.TailscaleInContainer var allClients []TailscaleClient
for namespace, count := range spec { for namespace, count := range spec {
ips, err := scenario.GetIPs(namespace) ips, err := scenario.GetIPs(namespace)
@ -62,7 +60,7 @@ func TestPingAll(t *testing.T) {
for _, ip := range allIps { for _, ip := range allIps {
err := client.Ping(ip) err := client.Ping(ip)
if err != nil { 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 { } else {
success++ success++
} }

View file

@ -48,7 +48,7 @@ var (
) )
type Namespace struct { type Namespace struct {
Clients map[string]*tsic.TailscaleInContainer Clients map[string]TailscaleClient
createWaitGroup sync.WaitGroup createWaitGroup sync.WaitGroup
joinWaitGroup sync.WaitGroup joinWaitGroup sync.WaitGroup
@ -118,7 +118,7 @@ func (s *Scenario) Shutdown() error {
for namespaceName, namespace := range s.namespaces { for namespaceName, namespace := range s.namespaces {
for _, client := range namespace.Clients { 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() err := client.Shutdown()
if err != nil { if err != nil {
return fmt.Errorf("failed to tear down client: %w", err) 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{ s.namespaces[namespace] = &Namespace{
Clients: make(map[string]*tsic.TailscaleInContainer), Clients: make(map[string]TailscaleClient),
} }
return nil return nil
@ -214,7 +214,7 @@ func (s *Scenario) CreateTailscaleNodesInNamespace(
log.Printf("failed to add tailscale node: %s", err) log.Printf("failed to add tailscale node: %s", err)
} }
namespace.Clients[tsClient.Hostname] = tsClient namespace.Clients[tsClient.Hostname()] = tsClient
}() }()
} }
namespace.createWaitGroup.Wait() namespace.createWaitGroup.Wait()
@ -232,7 +232,7 @@ func (s *Scenario) RunTailscaleUp(
for _, client := range namespace.Clients { for _, client := range namespace.Clients {
namespace.joinWaitGroup.Add(1) namespace.joinWaitGroup.Add(1)
go func(c *tsic.TailscaleInContainer) { go func(c TailscaleClient) {
defer namespace.joinWaitGroup.Done() defer namespace.joinWaitGroup.Done()
// TODO(kradalby): error handle this // TODO(kradalby): error handle this
@ -264,7 +264,7 @@ func (s *Scenario) WaitForTailscaleSync() error {
for _, client := range namespace.Clients { for _, client := range namespace.Clients {
namespace.syncWaitGroup.Add(1) namespace.syncWaitGroup.Add(1)
go func(c *tsic.TailscaleInContainer) { go func(c TailscaleClient) {
defer namespace.syncWaitGroup.Done() defer namespace.syncWaitGroup.Done()
// TODO(kradalby): error handle this // 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) return ips, fmt.Errorf("failed to get ips: %w", errNoNamespaceAvailable)
} }
func (s *Scenario) GetClients(namespace string) ([]*tsic.TailscaleInContainer, error) { func (s *Scenario) GetClients(namespace string) ([]TailscaleClient, error) {
var clients []*tsic.TailscaleInContainer var clients []TailscaleClient
if ns, ok := s.namespaces[namespace]; ok { if ns, ok := s.namespaces[namespace]; ok {
for _, client := range ns.Clients { for _, client := range ns.Clients {
clients = append(clients, client) clients = append(clients, client)

View file

@ -4,7 +4,6 @@ import (
"testing" "testing"
"github.com/juanfont/headscale/integration/dockertestutil" "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 // 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{ scenario.namespaces[namespace] = &Namespace{
Clients: make(map[string]*tsic.TailscaleInContainer), Clients: make(map[string]TailscaleClient),
} }
t.Run("create-tailscale", func(t *testing.T) { 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 { type TailscaleInContainer struct {
version string version string
Hostname string hostname string
pool *dockertest.Pool pool *dockertest.Pool
container *dockertest.Resource container *dockertest.Resource
@ -84,7 +84,7 @@ func New(
return &TailscaleInContainer{ return &TailscaleInContainer{
version: version, version: version,
Hostname: hostname, hostname: hostname,
pool: pool, pool: pool,
container: container, container: container,
@ -96,6 +96,10 @@ func (t *TailscaleInContainer) Shutdown() error {
return t.pool.Purge(t.container) return t.pool.Purge(t.container)
} }
func (t *TailscaleInContainer) Hostname() string {
return t.hostname
}
func (t *TailscaleInContainer) Version() string { func (t *TailscaleInContainer) Version() string {
return t.version return t.version
} }
@ -111,11 +115,11 @@ func (t *TailscaleInContainer) Up(
"--authkey", "--authkey",
authKey, authKey,
"--hostname", "--hostname",
t.Hostname, t.hostname,
} }
log.Println("Join command:", command) 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( stdout, stderr, err := dockertestutil.ExecuteCommand(
t.container, t.container,
command, command,
@ -131,7 +135,7 @@ func (t *TailscaleInContainer) Up(
log.Printf("tailscale join stdout: %s\n", stdout) log.Printf("tailscale join stdout: %s\n", stdout)
} }
log.Printf("%s joined\n", t.Hostname) log.Printf("%s joined\n", t.hostname)
return nil return nil
} }
@ -234,7 +238,7 @@ func (t *TailscaleInContainer) Ping(ip netip.Addr) error {
if err != nil { if err != nil {
log.Printf( log.Printf(
"failed to run ping command from %s to %s, err: %s", "failed to run ping command from %s to %s, err: %s",
t.Hostname, t.hostname,
ip.String(), ip.String(),
err, err,
) )