Make simple initial test case

This commit makes the initial SSH test a bit simpler:

- Use the same pattern/functions for all clients as other tests
- Only test within _one_ namespace/user to confirm the base case
- Use retry function, same as taildrop, there is some funky going on
  there...

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2022-11-08 15:10:03 +00:00 committed by Kristoffer Dalby
parent cfaa36e51a
commit 3695284286
5 changed files with 105 additions and 65 deletions

View file

@ -2,14 +2,29 @@ package integration
import (
"fmt"
"strings"
"testing"
"time"
"github.com/juanfont/headscale"
)
func TestSSHIntoAll(t *testing.T) {
func TestSSHOneNamespaceAllToAll(t *testing.T) {
IntegrationSkip(t)
retry := func(times int, sleepInverval time.Duration, doWork func() (string, error)) (string, error) {
var err error
for attempts := 0; attempts < times; attempts++ {
result, err := doWork()
if err == nil {
return result, nil
}
time.Sleep(sleepInverval)
}
return "", err
}
scenario, err := NewScenario()
if err != nil {
t.Errorf("failed to create scenario: %s", err)
@ -17,14 +32,12 @@ func TestSSHIntoAll(t *testing.T) {
spec := &HeadscaleSpec{
namespaces: map[string]int{
// Omit versions before 1.24 because they don't support SSH
"namespace1": len(TailscaleVersions) - 4,
"namespace2": len(TailscaleVersions) - 4,
"namespace1": len(TailscaleVersions) - 5,
},
enableSSH: true,
acl: &headscale.ACLPolicy{
Groups: map[string][]string{
"group:integration-test": {"namespace1", "namespace2"},
"group:integration-test": {"namespace1"},
},
ACLs: []headscale.ACL{
{
@ -48,68 +61,73 @@ func TestSSHIntoAll(t *testing.T) {
if err != nil {
t.Errorf("failed to create headscale environment: %s", err)
}
allClients, err := scenario.ListTailscaleClients()
if err != nil {
t.Errorf("failed to get clients: %s", err)
}
err = scenario.WaitForTailscaleSync()
if err != nil {
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
}
for namespace := range spec.namespaces {
// This will essentially fetch and cache all the FQDNs for the given namespace
nsFQDNs, err := scenario.ListTailscaleClientsFQDNs(namespace)
if err != nil {
t.Errorf("failed to get FQDNs: %s", err)
}
_, err = scenario.ListTailscaleClientsFQDNs()
if err != nil {
t.Errorf("failed to get FQDNs: %s", err)
}
nsClients, err := scenario.ListTailscaleClients(namespace)
if err != nil {
t.Errorf("failed to get clients: %s", err)
}
success := 0
for _, client := range nsClients {
currentClientFqdn, _ := client.FQDN()
sshTargets := removeFromSlice(nsFQDNs, currentClientFqdn)
for _, target := range sshTargets {
t.Run(
fmt.Sprintf("%s-%s", currentClientFqdn, target),
func(t *testing.T) {
command := []string{
"ssh", "-o StrictHostKeyChecking=no",
fmt.Sprintf("%s@%s", "ssh-it-user", target),
"'hostname'",
}
result, err := client.Execute(command)
if err != nil {
t.Errorf("failed to execute command over SSH: %s", err)
}
if result != target {
t.Logf("result=%s, target=%s", result, target)
t.Fail()
}
t.Logf("Result for %s: %s\n", target, result)
},
)
for _, client := range allClients {
for _, peer := range allClients {
if client.Hostname() == peer.Hostname() {
continue
}
// t.Logf("%s wants to SSH into %+v", currentClientFqdn, sshTargets)
clientFQDN, _ := client.FQDN()
peerFQDN, _ := peer.FQDN()
t.Run(
fmt.Sprintf("%s-%s", clientFQDN, peerFQDN),
func(t *testing.T) {
command := []string{
"ssh", "-o StrictHostKeyChecking=no", "-o ConnectTimeout=1",
fmt.Sprintf("%s@%s", "ssh-it-user", peer.Hostname()),
"'hostname'",
}
result, err := retry(10, 1*time.Second, func() (string, error) {
return client.Execute(command)
})
if err != nil {
t.Errorf("failed to execute command over SSH: %s", err)
}
if strings.Contains(peer.ID(), result) {
t.Logf(
"failed to get correct container ID from %s, expected: %s, got: %s",
peer.Hostname(),
peer.ID(),
result,
)
t.Fail()
} else {
success++
}
},
)
}
}
t.Logf(
"%d successful pings out of %d",
success,
(len(allClients)*len(allClients))-len(allClients),
)
err = scenario.Shutdown()
if err != nil {
t.Errorf("failed to tear down scenario: %s", err)
}
}
func removeFromSlice(haystack []string, needle string) []string {
for i, value := range haystack {
if needle == value {
return append(haystack[:i], haystack[i+1:]...)
}
}
return haystack
}