Skip to content

Commit 6f075c8

Browse files
Added helper to wait for deployment step to finish running
1 parent 78f9620 commit 6f075c8

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

stack_deployment_steps_integration_test.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tfe
66
import (
77
"context"
88
"testing"
9+
"time"
910

1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -211,19 +212,54 @@ func TestStackDeploymentStepsAdvance(t *testing.T) {
211212
assert.NotEmpty(t, steps)
212213

213214
step := steps.Items[0]
215+
step = pollStackDeploymentStepStatus(t, ctx, client, step.ID, "pending_operator")
216+
require.NotNil(t, step)
214217

215218
t.Run("Advance with valid ID", func(t *testing.T) {
216219
err := client.StackDeploymentSteps.Advance(ctx, step.ID)
217220
assert.NoError(t, err)
218221

219-
// Verify that the step status has changed to "pending_operator"
222+
// Verify that the step status has changed to "completed"
220223
sds, err := client.StackDeploymentSteps.Read(ctx, step.ID)
221224
assert.NoError(t, err)
222-
assert.Equal(t, "pending_operator", sds.Status)
225+
assert.Equal(t, "completed", sds.Status)
223226
})
224227

225228
t.Run("Advance with invalid ID", func(t *testing.T) {
226229
err := client.StackDeploymentSteps.Advance(ctx, "")
227230
require.Error(t, err)
228231
})
229232
}
233+
234+
func pollStackDeploymentStepStatus(t *testing.T, ctx context.Context, client *Client, stackDeploymentStepID, status string) (deploymentStep *StackDeploymentStep) {
235+
// pollStackDeploymentStepStatus will poll the given stack deployment step until its status changes or the deadline is reached.
236+
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Minute))
237+
defer cancel()
238+
239+
deadline, _ := ctx.Deadline()
240+
t.Logf("Polling stack deployment step %q for change in status with deadline of %s", stackDeploymentStepID, deadline)
241+
242+
ticker := time.NewTicker(2 * time.Second)
243+
defer ticker.Stop()
244+
245+
var err error
246+
for finished := false; !finished; {
247+
t.Log("...")
248+
select {
249+
case <-ctx.Done():
250+
t.Fatalf("Stack deployment step %s did not have status %q at deadline", stackDeploymentStepID, status)
251+
case <-ticker.C:
252+
deploymentStep, err = client.StackDeploymentSteps.Read(ctx, stackDeploymentStepID)
253+
if err != nil {
254+
t.Fatalf("Failed to read stack deployment step %s: %s", stackDeploymentStepID, err)
255+
}
256+
257+
t.Logf("Stack deployment step %s had status %q", deploymentStep.ID, deploymentStep.Status)
258+
if deploymentStep.Status == status {
259+
finished = true
260+
}
261+
}
262+
}
263+
264+
return
265+
}

0 commit comments

Comments
 (0)