Skip to content

Commit

Permalink
Merge pull request #802 from hashicorp/nf/nov23-sv-await-flake
Browse files Browse the repository at this point in the history
Await async state version processing in TestStateVersionsUpload
  • Loading branch information
nfagerlund authored Nov 1, 2023
2 parents 1134435 + b849800 commit 04571b5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
37 changes: 37 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,43 @@ func pollRunStatus(t *testing.T, client *Client, ctx context.Context, r *Run, rs
return r
}

// pollStateVersionStatus will poll the given state version until its status
// matches one of the given statuses or the given context times out.
func pollStateVersionStatus(t *testing.T, client *Client, ctx context.Context, sv *StateVersion, statuses []StateVersionStatus) *StateVersion {
deadline, ok := ctx.Deadline()
if !ok {
t.Logf("No deadline was set to poll state version %q which could result in an infinite loop", sv.ID)
}

t.Logf("Polling state version %q for status included in %q with deadline of %s", sv.ID, statuses, deadline)

ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
var err error

for finished := false; !finished; {
t.Log("...")
select {
case <-ctx.Done():
t.Fatalf("State version %q had status %q at deadline", sv.ID, sv.Status)
case <-ticker.C:
sv, err = client.StateVersions.Read(ctx, sv.ID)
if err != nil {
t.Fatalf("Could not read state version %q: %s", sv.ID, err)
}
t.Logf("State version %q had status %q", sv.ID, sv.Status)
for _, svst := range statuses {
if svst == sv.Status {
finished = true
break
}
}
}
}

return sv
}

// readRun will re-read the given run.
func readRun(t *testing.T, client *Client, ctx context.Context, r *Run) *Run {
t.Logf("Reading run %q", r.ID)
Expand Down
12 changes: 11 additions & 1 deletion state_version_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -144,8 +145,17 @@ func TestStateVersionsUpload(t *testing.T) {
_, err = client.Workspaces.Unlock(ctx, wTest.ID)
require.NoError(t, err)

// TFC does some async processing on state versions, so we must await it
// lest we flake. Should take well less than a minute tho.
timeout := time.Minute / 2

ctxPollSVReady, cancelPollSVReady := context.WithTimeout(ctx, timeout)
defer cancelPollSVReady()

sv = pollStateVersionStatus(t, client, ctxPollSVReady, sv, []StateVersionStatus{StateVersionFinalized})

assert.NotEmpty(t, sv.DownloadURL)
assert.Equal(t, sv.Status, StateVersionFinalized)
assert.Equal(t, StateVersionFinalized, sv.Status)
})

t.Run("cannot provide base64 state parameter when uploading", func(t *testing.T) {
Expand Down

0 comments on commit 04571b5

Please sign in to comment.