@@ -6,6 +6,7 @@ package tfe
6
6
import (
7
7
"context"
8
8
"testing"
9
+ "time"
9
10
10
11
"github.com/stretchr/testify/assert"
11
12
"github.com/stretchr/testify/require"
@@ -211,19 +212,54 @@ func TestStackDeploymentStepsAdvance(t *testing.T) {
211
212
assert .NotEmpty (t , steps )
212
213
213
214
step := steps .Items [0 ]
215
+ step = pollStackDeploymentStepStatus (t , ctx , client , step .ID , "pending_operator" )
216
+ require .NotNil (t , step )
214
217
215
218
t .Run ("Advance with valid ID" , func (t * testing.T ) {
216
219
err := client .StackDeploymentSteps .Advance (ctx , step .ID )
217
220
assert .NoError (t , err )
218
221
219
- // Verify that the step status has changed to "pending_operator "
222
+ // Verify that the step status has changed to "completed "
220
223
sds , err := client .StackDeploymentSteps .Read (ctx , step .ID )
221
224
assert .NoError (t , err )
222
- assert .Equal (t , "pending_operator " , sds .Status )
225
+ assert .Equal (t , "completed " , sds .Status )
223
226
})
224
227
225
228
t .Run ("Advance with invalid ID" , func (t * testing.T ) {
226
229
err := client .StackDeploymentSteps .Advance (ctx , "" )
227
230
require .Error (t , err )
228
231
})
229
232
}
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