From 28b98348cc9d070f9ce2130227407e025d7bf685 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 27 Mar 2023 09:50:31 -0400 Subject: [PATCH] test(e2e): Refactor WaitForSessionStatus methods (#3115) --- testing/internal/e2e/boundary/session.go | 47 ++++--------------- .../tests/static/bytes_up_down_empty_test.go | 15 ++++-- .../e2e/tests/static/bytes_up_down_test.go | 28 ++++++----- .../tests/static/session_cancel_admin_test.go | 24 +++------- .../tests/static/session_cancel_group_test.go | 23 +++------ .../tests/static/session_cancel_user_test.go | 23 +++------ .../session_end_delete_host_set_test.go | 46 +++--------------- .../static/session_end_delete_host_test.go | 46 +++--------------- .../static/session_end_delete_target_test.go | 45 +++--------------- .../static/session_end_delete_user_test.go | 45 +++--------------- 10 files changed, 82 insertions(+), 260 deletions(-) diff --git a/testing/internal/e2e/boundary/session.go b/testing/internal/e2e/boundary/session.go index b2f07ba01..4a0832c9e 100644 --- a/testing/internal/e2e/boundary/session.go +++ b/testing/internal/e2e/boundary/session.go @@ -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 } diff --git a/testing/internal/e2e/tests/static/bytes_up_down_empty_test.go b/testing/internal/e2e/tests/static/bytes_up_down_empty_test.go index 03ea40637..af8dbc0e6 100644 --- a/testing/internal/e2e/tests/static/bytes_up_down_empty_test.go +++ b/testing/internal/e2e/tests/static/bytes_up_down_empty_test.go @@ -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) diff --git a/testing/internal/e2e/tests/static/bytes_up_down_test.go b/testing/internal/e2e/tests/static/bytes_up_down_test.go index eb1de88dd..cd05f7c9b 100644 --- a/testing/internal/e2e/tests/static/bytes_up_down_test.go +++ b/testing/internal/e2e/tests/static/bytes_up_down_test.go @@ -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 diff --git a/testing/internal/e2e/tests/static/session_cancel_admin_test.go b/testing/internal/e2e/tests/static/session_cancel_admin_test.go index f7bed6549..500b788a8 100644 --- a/testing/internal/e2e/tests/static/session_cancel_admin_test.go +++ b/testing/internal/e2e/tests/static/session_cancel_admin_test.go @@ -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 { diff --git a/testing/internal/e2e/tests/static/session_cancel_group_test.go b/testing/internal/e2e/tests/static/session_cancel_group_test.go index 4eaceb70f..7f16752ae 100644 --- a/testing/internal/e2e/tests/static/session_cancel_group_test.go +++ b/testing/internal/e2e/tests/static/session_cancel_group_test.go @@ -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 { diff --git a/testing/internal/e2e/tests/static/session_cancel_user_test.go b/testing/internal/e2e/tests/static/session_cancel_user_test.go index e5a6c2860..12098082c 100644 --- a/testing/internal/e2e/tests/static/session_cancel_user_test.go +++ b/testing/internal/e2e/tests/static/session_cancel_user_test.go @@ -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 { diff --git a/testing/internal/e2e/tests/static/session_end_delete_host_set_test.go b/testing/internal/e2e/tests/static/session_end_delete_host_set_test.go index e5b1d2ffc..62ca9df57 100644 --- a/testing/internal/e2e/tests/static/session_end_delete_host_set_test.go +++ b/testing/internal/e2e/tests/static/session_end_delete_host_set_test.go @@ -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") } diff --git a/testing/internal/e2e/tests/static/session_end_delete_host_test.go b/testing/internal/e2e/tests/static/session_end_delete_host_test.go index 13ba3ef8e..705b3a1d3 100644 --- a/testing/internal/e2e/tests/static/session_end_delete_host_test.go +++ b/testing/internal/e2e/tests/static/session_end_delete_host_test.go @@ -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") } diff --git a/testing/internal/e2e/tests/static/session_end_delete_target_test.go b/testing/internal/e2e/tests/static/session_end_delete_target_test.go index f6fd0b92d..25a84955c 100644 --- a/testing/internal/e2e/tests/static/session_end_delete_target_test.go +++ b/testing/internal/e2e/tests/static/session_end_delete_target_test.go @@ -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") } diff --git a/testing/internal/e2e/tests/static/session_end_delete_user_test.go b/testing/internal/e2e/tests/static/session_end_delete_user_test.go index 91106b292..e6bd09f49 100644 --- a/testing/internal/e2e/tests/static/session_end_delete_user_test.go +++ b/testing/internal/e2e/tests/static/session_end_delete_user_test.go @@ -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") }