test(e2e): Refactor WaitForSessionStatus methods (#3115)

This commit is contained in:
Michael Li authored and GitHub committed 2023-03-27 09:50:31 -04:00
1 parent b2376356dd
commit 28b98348cc
10 files changed
+82 -260

No files matched your search

+10 -37
View File
@@ -61,54 +61,29 @@ func WaitForSessionCli(t testing.TB, ctx context.Context, projectId string) *ses
return session
}
// WaitForSessionToBeActiveCli waits for a session to appear in the session list and returns the
// session information once the session has an active status
func WaitForSessionToBeActiveCli(t testing.TB, ctx context.Context, projectId string) *sessions.Session {
t.Log("Waiting for session to be active...")
var session *sessions.Session
// WaitForSessionStatusCli reads the specified session and waits for its status to match the
// specified status
func WaitForSessionStatusCli(t testing.TB, ctx context.Context, sessionId string, status string) {
t.Logf("Waiting for session to be %s...", status)
err := backoff.RetryNotify(
func() error {
// List sessions
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "list", "-scope-id", projectId, "-format", "json"),
)
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
var sessionListResult sessions.SessionListResult
err := json.Unmarshal(output.Stdout, &sessionListResult)
if err != nil {
return backoff.Permanent(err)
}
// Check if there is one session
sessionCount := len(sessionListResult.Items)
if sessionCount == 0 {
return errors.New("No items are appearing in the session list")
}
t.Logf("Found %d session(s)", sessionCount)
if sessionCount != 1 {
return backoff.Permanent(errors.New("Only one session was expected to be found"))
}
// Check if session is active
session = sessionListResult.Items[0]
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
e2e.WithArgs("sessions", "read", "-id", sessionId, "-format", "json"),
)
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
var sessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &sessionReadResult)
err := json.Unmarshal(output.Stdout, &sessionReadResult)
if err != nil {
return backoff.Permanent(err)
}
s := sessionReadResult.Item
if sessionReadResult.Item.Status != "active" {
return errors.New(fmt.Sprintf("Waiting for session to be active... Expected: %s, Actual: %s",
"active",
if s.Status != status {
return errors.New(fmt.Sprintf("Waiting for session status... Expected: %s, Actual: %s",
status,
sessionReadResult.Item.Status,
))
}
@@ -121,6 +96,4 @@ func WaitForSessionToBeActiveCli(t testing.TB, ctx context.Context, projectId st
},
)
require.NoError(t, err)
return session
}
@@ -80,12 +80,17 @@ func TestCliBytesUpDownEmpty(t *testing.T) {
var newSessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
require.NoError(t, err)
bytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp)
bytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown)
t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown)
if bytesUp > 0 && bytesDown > 0 {
break
if len(newSessionReadResult.Item.Connections) > 0 {
bytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp)
bytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown)
t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown)
if bytesUp > 0 && bytesDown > 0 {
break
}
} else {
t.Log("No connections found in session. Retrying...")
}
time.Sleep(2 * time.Second)
@@ -10,6 +10,7 @@ import (
"time"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -64,10 +65,10 @@ func TestCliBytesUpDownTransferData(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
bytesUp := 0
bytesDown := 0
@@ -76,18 +77,23 @@ func TestCliBytesUpDownTransferData(t *testing.T) {
t.Log("Waiting for bytes_up/bytes_down to be greater than 0...")
for i := 0; i < 3; i++ {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
e2e.WithArgs("sessions", "read", "-id", s.Id, "-format", "json"),
)
require.NoError(t, output.Err, string(output.Stderr))
var newSessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
require.NoError(t, err)
bytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp)
bytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown)
t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown)
if bytesUp > 0 && bytesDown > 0 {
break
if len(newSessionReadResult.Item.Connections) > 0 {
bytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp)
bytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown)
t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown)
if bytesUp > 0 && bytesDown > 0 {
break
}
} else {
t.Log("No connections found in session. Retrying...")
}
time.Sleep(2 * time.Second)
@@ -100,7 +106,7 @@ func TestCliBytesUpDownTransferData(t *testing.T) {
var newBytesUp, newBytesDown int
for i := 0; i < 3; i++ {
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
e2e.WithArgs("sessions", "read", "-id", s.Id, "-format", "json"),
)
require.NoError(t, output.Err, string(output.Stderr))
var newSessionReadResult sessions.SessionReadResult
@@ -5,11 +5,10 @@ package static_test
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -61,27 +60,18 @@ func TestCliSessionCancelAdmin(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Cancel session
t.Log("Canceling session...")
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "cancel", "-id", session.Id),
e2e.WithArgs("sessions", "cancel", "-id", s.Id),
)
require.NoError(t, output.Err, string(output.Stderr))
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
require.NoError(t, output.Err, string(output.Stderr))
var newSessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
require.NoError(t, err)
require.Condition(t, func() bool {
return newSessionReadResult.Item.Status == "canceling" || newSessionReadResult.Item.Status == "terminated"
})
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
// Check output from session
select {
@@ -14,8 +14,8 @@ import (
"github.com/hashicorp/boundary/api/groups"
"github.com/hashicorp/boundary/api/roles"
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/api/users"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -127,27 +127,18 @@ func TestCliSessionCancelGroup(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Cancel session
t.Log("Canceling session...")
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "cancel", "-id", session.Id),
e2e.WithArgs("sessions", "cancel", "-id", s.Id),
)
require.NoError(t, output.Err, string(output.Stderr))
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
require.NoError(t, output.Err, string(output.Stderr))
var newSessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
require.NoError(t, err)
require.Condition(t, func() bool {
return newSessionReadResult.Item.Status == "canceling" || newSessionReadResult.Item.Status == "terminated"
})
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
// Check output from session
select {
@@ -13,8 +13,8 @@ import (
"github.com/hashicorp/boundary/api/accounts"
"github.com/hashicorp/boundary/api/roles"
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/api/users"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -122,27 +122,18 @@ func TestCliSessionCancelUser(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Cancel session
t.Log("Canceling session...")
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "cancel", "-id", session.Id),
e2e.WithArgs("sessions", "cancel", "-id", s.Id),
)
require.NoError(t, output.Err, string(output.Stderr))
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
require.NoError(t, output.Err, string(output.Stderr))
var newSessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &newSessionReadResult)
require.NoError(t, err)
require.Condition(t, func() bool {
return newSessionReadResult.Item.Status == "canceling" || newSessionReadResult.Item.Status == "terminated"
})
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
// Check output from session
select {
@@ -5,14 +5,10 @@ package static_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -91,9 +87,10 @@ func TestCliSessionEndWhenHostSetIsDeleted(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Delete Host Set
t.Log("Deleting host set...")
@@ -101,7 +98,6 @@ func TestCliSessionEndWhenHostSetIsDeleted(t *testing.T) {
require.NoError(t, output.Err, string(output.Stderr))
// Check if session has terminated
t.Log("Waiting for session to be canceling/terminated...")
select {
case output := <-errChan:
// `boundary connect` returns a 255 when cancelled
@@ -110,36 +106,6 @@ func TestCliSessionEndWhenHostSetIsDeleted(t *testing.T) {
t.Fatal("Timed out waiting for session command to exit")
}
err = backoff.RetryNotify(
func() error {
// Check that session has been canceled or terminated
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
var sessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &sessionReadResult)
if err != nil {
return backoff.Permanent(err)
}
if sessionReadResult.Item.Status != "canceling" && sessionReadResult.Item.Status != "terminated" {
return errors.New(fmt.Sprintf("Session has unexpected status. Expected: %s, Actual: %s",
"`canceling` or `terminated`",
sessionReadResult.Item.Status,
))
}
return nil
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5),
func(err error, td time.Duration) {
t.Logf("%s. Retrying...", err.Error())
},
)
require.NoError(t, err)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
t.Log("Session successfully ended after host set was deleted")
}
@@ -5,14 +5,10 @@ package static_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -91,9 +87,10 @@ func TestCliSessionEndWhenHostIsDeleted(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Delete Host
t.Log("Deleting host...")
@@ -101,7 +98,6 @@ func TestCliSessionEndWhenHostIsDeleted(t *testing.T) {
require.NoError(t, output.Err, string(output.Stderr))
// Check if session has terminated
t.Log("Waiting for session to be canceling/terminated...")
select {
case output := <-errChan:
// `boundary connect` returns a 255 when cancelled
@@ -110,36 +106,6 @@ func TestCliSessionEndWhenHostIsDeleted(t *testing.T) {
t.Fatal("Timed out waiting for session command to exit")
}
err = backoff.RetryNotify(
func() error {
// Check that session has been canceled or terminated
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
var sessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &sessionReadResult)
if err != nil {
return backoff.Permanent(err)
}
if sessionReadResult.Item.Status != "canceling" && sessionReadResult.Item.Status != "terminated" {
return errors.New(fmt.Sprintf("Session has unexpected status. Expected: %s, Actual: %s",
"`canceling` or `terminated`",
sessionReadResult.Item.Status,
))
}
return nil
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5),
func(err error, td time.Duration) {
t.Logf("%s. Retrying...", err.Error())
},
)
require.NoError(t, err)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
t.Log("Session successfully ended after host was deleted")
}
@@ -5,14 +5,10 @@ package static_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -91,9 +87,10 @@ func TestCliSessionEndWhenTargetIsDeleted(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Delete Target
t.Log("Deleting target...")
@@ -110,36 +107,6 @@ func TestCliSessionEndWhenTargetIsDeleted(t *testing.T) {
t.Fatal("Timed out waiting for session command to exit")
}
err = backoff.RetryNotify(
func() error {
// Check that session has been canceled or terminated
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
var sessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &sessionReadResult)
if err != nil {
return backoff.Permanent(err)
}
if sessionReadResult.Item.Status != "canceling" && sessionReadResult.Item.Status != "terminated" {
return errors.New(fmt.Sprintf("Session has unexpected status. Expected: %s, Actual: %s",
"`canceling` or `terminated`",
sessionReadResult.Item.Status,
))
}
return nil
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5),
func(err error, td time.Duration) {
t.Logf("%s. Retrying...", err.Error())
},
)
require.NoError(t, err)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
t.Log("Session successfully ended after target was deleted")
}
@@ -5,14 +5,10 @@ package static_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/assert"
@@ -84,9 +80,10 @@ func TestCliSessionEndWhenUserIsDeleted(t *testing.T) {
)
}()
t.Cleanup(cancel)
session := boundary.WaitForSessionToBeActiveCli(t, ctx, newProjectId)
assert.Equal(t, newTargetId, session.TargetId)
assert.Equal(t, newHostId, session.HostId)
s := boundary.WaitForSessionCli(t, ctx, newProjectId)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String())
assert.Equal(t, newTargetId, s.TargetId)
assert.Equal(t, newHostId, s.HostId)
// Delete User
t.Log("Deleting user...")
@@ -103,36 +100,6 @@ func TestCliSessionEndWhenUserIsDeleted(t *testing.T) {
t.Fatal("Timed out waiting for session command to exit")
}
err = backoff.RetryNotify(
func() error {
// Check that session has been canceled or terminated
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"),
)
if output.Err != nil {
return backoff.Permanent(errors.New(string(output.Stderr)))
}
var sessionReadResult sessions.SessionReadResult
err = json.Unmarshal(output.Stdout, &sessionReadResult)
if err != nil {
return backoff.Permanent(err)
}
if sessionReadResult.Item.Status != "canceling" && sessionReadResult.Item.Status != "terminated" {
return errors.New(fmt.Sprintf("Session has unexpected status. Expected: %s, Actual: %s",
"`canceling` or `terminated`",
sessionReadResult.Item.Status,
))
}
return nil
},
backoff.WithMaxRetries(backoff.NewConstantBackOff(3*time.Second), 5),
func(err error, td time.Duration) {
t.Logf("%s. Retrying...", err.Error())
},
)
require.NoError(t, err)
boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusTerminated.String())
t.Log("Session successfully ended after user was deleted")
}