Skip to content

Commit

Permalink
Added integration tests for ephemeral nodes
Browse files Browse the repository at this point in the history
Fetch the machines from headscale
  • Loading branch information
juanfont committed Dec 28, 2022
1 parent a81a5d5 commit 127f9b9
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
2 changes: 1 addition & 1 deletion integration/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ControlServer interface {
GetEndpoint() string
WaitForReady() error
CreateNamespace(namespace string) error
CreateAuthKey(namespace string) (*v1.PreAuthKey, error)
CreateAuthKey(namespace string, reusable bool, ephemeral bool) (*v1.PreAuthKey, error)
ListMachinesInNamespace(namespace string) ([]*v1.Machine, error)
GetCert() []byte
GetHostname() string
Expand Down
105 changes: 104 additions & 1 deletion integration/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestAuthKeyLogoutAndRelogin(t *testing.T) {
}

for namespaceName := range spec {
key, err := scenario.CreatePreAuthKey(namespaceName)
key, err := scenario.CreatePreAuthKey(namespaceName, true, false)
if err != nil {
t.Errorf("failed to create pre-auth key for namespace %s: %s", namespaceName, err)
}
Expand Down Expand Up @@ -197,6 +197,109 @@ func TestAuthKeyLogoutAndRelogin(t *testing.T) {
}
}

func TestEphemeral(t *testing.T) {
IntegrationSkip(t)
t.Parallel()

scenario, err := NewScenario()
if err != nil {
t.Errorf("failed to create scenario: %s", err)
}

spec := map[string]int{
"namespace1": len(TailscaleVersions),
"namespace2": len(TailscaleVersions),
}

headscale, err := scenario.Headscale(hsic.WithTestName("ephemeral"))
if err != nil {
t.Errorf("failed to create headscale environment: %s", err)
}

for namespaceName, clientCount := range spec {
err = scenario.CreateNamespace(namespaceName)
if err != nil {
t.Errorf("failed to create namespace %s: %s", namespaceName, err)
}

err = scenario.CreateTailscaleNodesInNamespace(namespaceName, "all", clientCount, []tsic.Option{}...)
if err != nil {
t.Errorf("failed to create tailscale nodes in namespace %s: %s", namespaceName, err)
}

key, err := scenario.CreatePreAuthKey(namespaceName, true, true)
if err != nil {
t.Errorf("failed to create pre-auth key for namespace %s: %s", namespaceName, err)
}

err = scenario.RunTailscaleUp(namespaceName, headscale.GetEndpoint(), key.GetKey())
if err != nil {
t.Errorf("failed to run tailscale up for namespace %s: %s", namespaceName, err)
}
}

err = scenario.WaitForTailscaleSync()
if err != nil {
t.Errorf("failed wait for tailscale clients to be in sync: %s", err)
}

allClients, err := scenario.ListTailscaleClients()
if err != nil {
t.Errorf("failed to get clients: %s", err)
}

allIps, err := scenario.ListTailscaleClientsIPs()
if err != nil {
t.Errorf("failed to get clients: %s", err)
}

success := 0
for _, client := range allClients {
for _, ip := range allIps {
err := client.Ping(ip.String())
if err != nil {
t.Errorf("failed to ping %s from %s: %s", ip, client.Hostname(), err)
} else {
success++
}
}
}

t.Logf("%d successful pings out of %d", success, len(allClients)*len(allIps))

for _, client := range allClients {
err := client.Logout()
if err != nil {
t.Errorf("failed to logout client %s: %s", client.Hostname(), err)
}
}

scenario.WaitForTailscaleLogout()

t.Logf("all clients logged out")

for namespaceName := range spec {
machines, err := headscale.ListMachinesInNamespace(namespaceName)
if err != nil {
log.Error().
Err(err).
Str("namespace", namespaceName).
Msg("Error listing machines in namespace")

return
}

if len(machines) != 0 {
t.Errorf("expected no machines, got %d in namespace %s", len(machines), namespaceName)
}
}

err = scenario.Shutdown()
if err != nil {
t.Errorf("failed to tear down scenario: %s", err)
}
}

func TestPingAllByHostname(t *testing.T) {
IntegrationSkip(t)
t.Parallel()
Expand Down
11 changes: 10 additions & 1 deletion integration/hsic/hsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,29 @@ func (t *HeadscaleInContainer) CreateNamespace(

func (t *HeadscaleInContainer) CreateAuthKey(
namespace string,
reusable bool,
ephemeral bool,
) (*v1.PreAuthKey, error) {
command := []string{
"headscale",
"--namespace",
namespace,
"preauthkeys",
"create",
"--reusable",
"--expiration",
"24h",
"--output",
"json",
}

if reusable {
command = append(command, "--reusable")
}

if ephemeral {
command = append(command, "--ephemeral")
}

result, _, err := dockertestutil.ExecuteCommand(
t.container,
command,
Expand Down
6 changes: 3 additions & 3 deletions integration/scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ func (s *Scenario) Headscale(opts ...hsic.Option) (ControlServer, error) {
return headscale, nil
}

func (s *Scenario) CreatePreAuthKey(namespace string) (*v1.PreAuthKey, error) {
func (s *Scenario) CreatePreAuthKey(namespace string, reusable bool, ephemeral bool) (*v1.PreAuthKey, error) {
if headscale, err := s.Headscale(); err == nil {
key, err := headscale.CreateAuthKey(namespace)
key, err := headscale.CreateAuthKey(namespace, reusable, ephemeral)
if err != nil {
return nil, fmt.Errorf("failed to create namespace: %w", err)
}
Expand Down Expand Up @@ -368,7 +368,7 @@ func (s *Scenario) CreateHeadscaleEnv(
return err
}

key, err := s.CreatePreAuthKey(namespaceName)
key, err := s.CreatePreAuthKey(namespaceName, true, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions integration/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestHeadscale(t *testing.T) {
})

t.Run("create-auth-key", func(t *testing.T) {
_, err := scenario.CreatePreAuthKey(namespace)
_, err := scenario.CreatePreAuthKey(namespace, true, false)
if err != nil {
t.Errorf("failed to create preauthkey: %s", err)
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestTailscaleNodesJoiningHeadcale(t *testing.T) {
})

t.Run("join-headscale", func(t *testing.T) {
key, err := scenario.CreatePreAuthKey(namespace)
key, err := scenario.CreatePreAuthKey(namespace, true, false)
if err != nil {
t.Errorf("failed to create preauthkey: %s", err)
}
Expand Down

0 comments on commit 127f9b9

Please sign in to comment.