Skip to content

Commit d1a3d89

Browse files
committed
Update TestContext_sleepTimeoutExpired to check for canceling within timeframe
Add a timeout to the test to ensure that the terraform apply cancels within a reasonable time of the 5s timeout. Currently, this test is not canceling the terraform apply as expected. In the logs you can see that the test takes 1 min rather than ~5s: ``` --- PASS: TestContext_sleepTimeoutExpired/sleep-0.12.31 (62.13s) ``` ``` === RUN TestContext_sleepTimeoutExpired/sleep-0.12.31 util_test.go:113: [INFO] running Terraform command: /var/folders/6y/gy9gggt14379c_k39vwb50lc0000gn/T/terraform_1378921380/terraform apply -no-color -auto-approve -input=false -lock=true -parallelism=10 -refresh=true util_test.go:103: CLI Output: // truncated ... time_sleep.sleep: Creating... time_sleep.sleep: Still creating... [10s elapsed] time_sleep.sleep: Still creating... [20s elapsed] time_sleep.sleep: Still creating... [30s elapsed] time_sleep.sleep: Still creating... [41s elapsed] time_sleep.sleep: Still creating... [51s elapsed] time_sleep.sleep: Creation complete after 1m0s [id=2022-05-06T17:40:20Z] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. ```
1 parent 7b1dddd commit d1a3d89

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

tfexec/internal/e2etest/errors_test.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,21 @@ func TestContext_sleepTimeoutExpired(t *testing.T) {
205205
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
206206
defer cancel()
207207

208-
err = tf.Apply(ctx)
209-
if err == nil {
210-
t.Fatal("expected error, but didn't find one")
211-
}
212-
213-
if !errors.Is(err, context.DeadlineExceeded) {
214-
t.Fatalf("expected context.DeadlineExceeded, got %T %s", err, err)
208+
errCh := make(chan error)
209+
go func() {
210+
err = tf.Apply(ctx)
211+
if err != nil {
212+
errCh <- err
213+
}
214+
}()
215+
216+
select {
217+
case err := <-errCh:
218+
if !errors.Is(err, context.DeadlineExceeded) {
219+
t.Fatalf("expected context.DeadlineExceeded, got %T %s", err, err)
220+
}
221+
case <-time.After(time.Second * 10):
222+
t.Fatal("terraform apply should have canceled and returned in ~5s")
215223
}
216224
})
217225
}

0 commit comments

Comments
 (0)