From 91c060ca4422e6274999aff6bca4fece51ac5812 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Wed, 8 Jan 2025 15:46:37 -0500 Subject: [PATCH] test(e2e): Add increased retries due to worker status report changes (#5424) --- .../tests/base/bytes_up_down_empty_test.go | 101 +++++++------ .../e2e/tests/base/bytes_up_down_test.go | 136 +++++++++++------- 2 files changed, 136 insertions(+), 101 deletions(-) diff --git a/testing/internal/e2e/tests/base/bytes_up_down_empty_test.go b/testing/internal/e2e/tests/base/bytes_up_down_empty_test.go index e229f50281..b125e8084f 100644 --- a/testing/internal/e2e/tests/base/bytes_up_down_empty_test.go +++ b/testing/internal/e2e/tests/base/bytes_up_down_empty_test.go @@ -6,10 +6,14 @@ package base_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/target" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" "github.com/stretchr/testify/assert" @@ -17,7 +21,7 @@ import ( ) // TestCliBytesUpDownEmpty uses the cli to verify that the bytes_up/bytes_down fields of a -// session correctly increase when data is transmitting during a session. +// session do not change when no data is transmitted during a session. func TestCliBytesUpDownEmpty(t *testing.T) { e2e.MaybeSkipTest(t) c, err := loadTestConfig() @@ -35,17 +39,13 @@ func TestCliBytesUpDownEmpty(t *testing.T) { }) projectId, err := boundary.CreateProjectCli(t, ctx, orgId) require.NoError(t, err) - hostCatalogId, err := boundary.CreateHostCatalogCli(t, ctx, projectId) - require.NoError(t, err) - hostSetId, err := boundary.CreateHostSetCli(t, ctx, hostCatalogId) - require.NoError(t, err) - hostId, err := boundary.CreateHostCli(t, ctx, hostCatalogId, c.TargetAddress) - require.NoError(t, err) - err = boundary.AddHostToHostSetCli(t, ctx, hostSetId, hostId) - require.NoError(t, err) - targetId, err := boundary.CreateTargetCli(t, ctx, projectId, c.TargetPort) - require.NoError(t, err) - err = boundary.AddHostSourceToTargetCli(t, ctx, targetId, hostSetId) + targetId, err := boundary.CreateTargetCli( + t, + ctx, + projectId, + c.TargetPort, + target.WithAddress(c.TargetAddress), + ) require.NoError(t, err) // Create a session where no additional commands are run @@ -72,43 +72,53 @@ func TestCliBytesUpDownEmpty(t *testing.T) { session := boundary.WaitForSessionCli(t, ctx, projectId) assert.Equal(t, targetId, session.TargetId) - assert.Equal(t, hostId, session.HostId) - // Confirm that bytesUp and bytesDown do not change + // Wait until bytes up and down is greater than 0 + t.Log("Waiting for bytes_up/bytes_down to be greater than 0...") bytesUp := 0 bytesDown := 0 + err = backoff.RetryNotify( + func() error { + 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 newSessionReadResult sessions.SessionReadResult + err = json.Unmarshal(output.Stdout, &newSessionReadResult) + if err != nil { + return backoff.Permanent(err) + } - // Wait until bytes up and down is greater than 0 - 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"), - ) - require.NoError(t, output.Err, string(output.Stderr)) - var newSessionReadResult sessions.SessionReadResult - err = json.Unmarshal(output.Stdout, &newSessionReadResult) - require.NoError(t, err) + if len(newSessionReadResult.Item.Connections) == 0 { + return fmt.Errorf("no connections found in session") + } - 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 + if !(bytesUp > 0 && bytesDown > 0) { + return fmt.Errorf( + "bytes_up: %d, bytes_down: %d, bytes_up or bytes_down is not greater than 0", + bytesUp, + bytesDown, + ) } - } else { - t.Log("No connections found in session. Retrying...") - } - time.Sleep(2 * time.Second) - } - require.Greater(t, bytesUp, 0) - require.Greater(t, bytesDown, 0) + t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown) + 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) - t.Log("Reading bytes_up/bytes_down values...") - for i := 0; i < 3; i++ { - time.Sleep(2 * time.Second) + // Confirm that bytesUp and bytesDown do not change + t.Log("Verifying bytes_up/bytes_down values do not change...") + for i := 0; i < 5; i++ { + time.Sleep(3 * time.Second) output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("sessions", "read", "-id", session.Id, "-format", "json"), @@ -117,14 +127,11 @@ 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) - - if i != 1 { - require.Equal(t, bytesUp, int(newSessionReadResult.Item.Connections[0].BytesUp)) - require.Equal(t, bytesDown, int(newSessionReadResult.Item.Connections[0].BytesDown)) - } + newBytesUp := int(newSessionReadResult.Item.Connections[0].BytesUp) + newBytesDown := int(newSessionReadResult.Item.Connections[0].BytesDown) + t.Logf("bytes_up: %d, bytes_down: %d", newBytesUp, newBytesDown) - t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown) + require.Equal(t, bytesUp, newBytesUp) + require.Equal(t, bytesDown, newBytesDown) } } diff --git a/testing/internal/e2e/tests/base/bytes_up_down_test.go b/testing/internal/e2e/tests/base/bytes_up_down_test.go index a108bdf623..c072132462 100644 --- a/testing/internal/e2e/tests/base/bytes_up_down_test.go +++ b/testing/internal/e2e/tests/base/bytes_up_down_test.go @@ -6,11 +6,15 @@ package base_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/internal/target" "github.com/hashicorp/boundary/testing/internal/e2e" "github.com/hashicorp/boundary/testing/internal/e2e/boundary" "github.com/stretchr/testify/assert" @@ -36,17 +40,13 @@ func TestCliBytesUpDownTransferData(t *testing.T) { }) projectId, err := boundary.CreateProjectCli(t, ctx, orgId) require.NoError(t, err) - hostCatalogId, err := boundary.CreateHostCatalogCli(t, ctx, projectId) - require.NoError(t, err) - hostSetId, err := boundary.CreateHostSetCli(t, ctx, hostCatalogId) - require.NoError(t, err) - hostId, err := boundary.CreateHostCli(t, ctx, hostCatalogId, c.TargetAddress) - require.NoError(t, err) - err = boundary.AddHostToHostSetCli(t, ctx, hostSetId, hostId) - require.NoError(t, err) - targetId, err := boundary.CreateTargetCli(t, ctx, projectId, c.TargetPort) - require.NoError(t, err) - err = boundary.AddHostSourceToTargetCli(t, ctx, targetId, hostSetId) + targetId, err := boundary.CreateTargetCli( + t, + ctx, + projectId, + c.TargetPort, + target.WithAddress(c.TargetAddress), + ) require.NoError(t, err) // Create a session where no additional commands are run @@ -76,60 +76,88 @@ func TestCliBytesUpDownTransferData(t *testing.T) { s := boundary.WaitForSessionCli(t, ctx, projectId) boundary.WaitForSessionStatusCli(t, ctx, s.Id, session.StatusActive.String()) assert.Equal(t, targetId, s.TargetId) - assert.Equal(t, hostId, s.HostId) + // Wait until bytes up and down is greater than 0 + t.Log("Waiting for bytes_up/bytes_down to be greater than 0...") bytesUp := 0 bytesDown := 0 + err = backoff.RetryNotify( + func() error { + output := e2e.RunCommand(ctx, "boundary", + e2e.WithArgs("sessions", "read", "-id", s.Id, "-format", "json"), + ) + if output.Err != nil { + return backoff.Permanent(errors.New(string(output.Stderr))) + } + var newSessionReadResult sessions.SessionReadResult + err = json.Unmarshal(output.Stdout, &newSessionReadResult) + if err != nil { + return backoff.Permanent(err) + } - // Wait until bytes up and down is greater than 0 - 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", 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) + if len(newSessionReadResult.Item.Connections) == 0 { + return fmt.Errorf("no connections found in session") + } - 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 + if !(bytesUp > 0 && bytesDown > 0) { + return fmt.Errorf( + "bytes_up: %d, bytes_down: %d, bytes_up or bytes_down is not greater than 0", + bytesUp, + bytesDown, + ) } - } else { - t.Log("No connections found in session. Retrying...") - } - time.Sleep(2 * time.Second) - } - require.Greater(t, bytesUp, 0) - require.Greater(t, bytesDown, 0) + t.Logf("bytes_up: %d, bytes_down: %d", bytesUp, bytesDown) + 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) // Confirm that bytes up and down increases t.Log("Waiting for bytes_up/bytes_down to increase...") var newBytesUp, newBytesDown int - for i := 0; i < 3; i++ { - output := e2e.RunCommand(ctx, "boundary", - 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) - newBytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp) - newBytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown) - t.Logf("bytes_up: %d, bytes_down: %d", newBytesUp, newBytesDown) - - if newBytesDown > bytesDown { - break - } - - time.Sleep(2 * time.Second) - } - require.GreaterOrEqual(t, newBytesUp, bytesUp) - require.Greater(t, newBytesDown, bytesDown) + err = backoff.RetryNotify( + func() error { + output := e2e.RunCommand(ctx, "boundary", + e2e.WithArgs("sessions", "read", "-id", s.Id, "-format", "json"), + ) + if output.Err != nil { + return backoff.Permanent(errors.New(string(output.Stderr))) + } + var newSessionReadResult sessions.SessionReadResult + err = json.Unmarshal(output.Stdout, &newSessionReadResult) + if err != nil { + return backoff.Permanent(err) + } + + if len(newSessionReadResult.Item.Connections) == 0 { + return fmt.Errorf("no connections found in session") + } + + newBytesUp = int(newSessionReadResult.Item.Connections[0].BytesUp) + newBytesDown = int(newSessionReadResult.Item.Connections[0].BytesDown) + + if !(newBytesDown > bytesDown) || !(newBytesUp > bytesUp) { + return fmt.Errorf( + "bytes_up: %d, bytes_down: %d, bytes_up/bytes_down is not greater than previous value", + newBytesUp, + newBytesDown, + ) + } + + t.Logf("bytes_up: %d, bytes_down: %d", newBytesUp, newBytesDown) + 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) }