headscale/integration/ssh_test.go

135 lines
2.8 KiB
Go
Raw Normal View History

2022-10-26 05:31:30 -06:00
package integration
import (
"fmt"
"strings"
2022-10-26 05:31:30 -06:00
"testing"
"time"
2022-10-26 05:31:30 -06:00
"github.com/juanfont/headscale"
)
func TestSSHOneNamespaceAllToAll(t *testing.T) {
2022-10-26 05:31:30 -06:00
IntegrationSkip(t)
2022-11-08 14:33:11 -07:00
retry := func(times int, sleepInterval time.Duration, doWork func() (string, error)) (string, error) {
var err error
for attempts := 0; attempts < times; attempts++ {
2022-11-08 14:33:11 -07:00
var result string
result, err = doWork()
if err == nil {
return result, nil
}
2022-11-08 14:33:11 -07:00
time.Sleep(sleepInterval)
}
return "", err
}
2022-10-26 05:31:30 -06:00
scenario, err := NewScenario()
if err != nil {
t.Errorf("failed to create scenario: %s", err)
}
spec := &HeadscaleSpec{
namespaces: map[string]int{
"namespace1": len(TailscaleVersions) - 5,
2022-10-26 05:31:30 -06:00
},
enableSSH: true,
acl: &headscale.ACLPolicy{
Groups: map[string][]string{
"group:integration-test": {"namespace1"},
2022-10-26 05:31:30 -06:00
},
ACLs: []headscale.ACL{
{
Action: "accept",
Sources: []string{"*"},
Destinations: []string{"*:*"},
},
},
SSHs: []headscale.SSH{
{
Action: "accept",
Sources: []string{"group:integration-test"},
Destinations: []string{"group:integration-test"},
Users: []string{"ssh-it-user"},
},
},
},
}
err = scenario.CreateHeadscaleEnv(spec)
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)
}
2022-10-26 05:31:30 -06:00
err = scenario.WaitForTailscaleSync()
if err != nil {
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
}
_, err = scenario.ListTailscaleClientsFQDNs()
if err != nil {
t.Errorf("failed to get FQDNs: %s", err)
}
2022-10-26 05:31:30 -06:00
success := 0
2022-10-26 05:31:30 -06:00
for _, client := range allClients {
for _, peer := range allClients {
if client.Hostname() == peer.Hostname() {
continue
2022-10-26 05:31:30 -06:00
}
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++
}
},
)
2022-10-26 05:31:30 -06:00
}
}
t.Logf(
"%d successful pings out of %d",
success,
(len(allClients)*len(allClients))-len(allClients),
)
2022-10-26 05:31:30 -06:00
err = scenario.Shutdown()
if err != nil {
t.Errorf("failed to tear down scenario: %s", err)
}
}