@@ -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"
@@ -216,3 +217,96 @@ func TestStackDeploymentRunsApproveAllPlans(t *testing.T) {
216
217
require .NoError (t , err )
217
218
})
218
219
}
220
+
221
+ func TestStackDeploymentRunsCancel (t * testing.T ) {
222
+ skipUnlessBeta (t )
223
+
224
+ client := testClient (t )
225
+ ctx := context .Background ()
226
+
227
+ orgTest , orgTestCleanup := createOrganization (t , client )
228
+ t .Cleanup (orgTestCleanup )
229
+
230
+ oauthClient , cleanup := createOAuthClient (t , client , orgTest , nil )
231
+ t .Cleanup (cleanup )
232
+
233
+ stack , err := client .Stacks .Create (ctx , StackCreateOptions {
234
+ Project : orgTest .DefaultProject ,
235
+ Name : "test-stack" ,
236
+ VCSRepo : & StackVCSRepoOptions {
237
+ Identifier : "hashicorp-guides/pet-nulls-stack" ,
238
+ OAuthTokenID : oauthClient .OAuthTokens [0 ].ID ,
239
+ Branch : "main" ,
240
+ },
241
+ })
242
+ require .NoError (t , err )
243
+ require .NotNil (t , stack )
244
+
245
+ stackUpdated , err := client .Stacks .UpdateConfiguration (ctx , stack .ID )
246
+ require .NoError (t , err )
247
+ require .NotNil (t , stackUpdated )
248
+
249
+ stack = pollStackDeployments (t , ctx , client , stackUpdated .ID )
250
+ require .NotNil (t , stack .LatestStackConfiguration )
251
+
252
+ // Get the deployment group ID from the stack configuration
253
+ deploymentGroups , err := client .StackDeploymentGroups .List (ctx , stack .LatestStackConfiguration .ID , nil )
254
+ require .NoError (t , err )
255
+ require .NotNil (t , deploymentGroups )
256
+ require .NotEmpty (t , deploymentGroups .Items )
257
+
258
+ deploymentGroupID := deploymentGroups .Items [0 ].ID
259
+
260
+ runList , err := client .StackDeploymentRuns .List (ctx , deploymentGroupID , nil )
261
+ require .NoError (t , err )
262
+ assert .NotNil (t , runList )
263
+
264
+ deploymentRunID := runList .Items [0 ].ID
265
+
266
+ t .Run ("cancel deployment run" , func (t * testing.T ) {
267
+ t .Parallel ()
268
+ err := client .StackDeploymentRuns .ApproveAllPlans (ctx , deploymentRunID )
269
+ require .NoError (t , err )
270
+
271
+ pollStackDeploymentRunForDeployingStatus (t , ctx , client , deploymentRunID )
272
+
273
+ err = client .StackDeploymentRuns .Cancel (ctx , deploymentRunID )
274
+ require .NoError (t , err )
275
+
276
+ dr , err := client .StackDeploymentRuns .Read (ctx , deploymentRunID )
277
+ require .NoError (t , err )
278
+ assert .NotNil (t , dr )
279
+ assert .Equal (t , "abandoned" , dr .Status )
280
+ })
281
+ }
282
+
283
+ func pollStackDeploymentRunForDeployingStatus (t * testing.T , ctx context.Context , client * Client , stackDeploymentRunID string ) {
284
+ t .Helper ()
285
+
286
+ ctx , cancel := context .WithDeadline (ctx , time .Now ().Add (5 * time .Minute ))
287
+ defer cancel ()
288
+
289
+ deadline , _ := ctx .Deadline ()
290
+ t .Logf ("Polling stack deployment run %q for deploying status, with deadline of %s" , stackDeploymentRunID , deadline )
291
+
292
+ ticker := time .NewTicker (2 * time .Second )
293
+ defer ticker .Stop ()
294
+
295
+ for finished := false ; ! finished ; {
296
+ t .Log ("..." )
297
+ select {
298
+ case <- ctx .Done ():
299
+ t .Fatalf ("Stack deployment run %q not deploying at deadline" , stackDeploymentRunID )
300
+ case <- ticker .C :
301
+ var err error
302
+ sdr , err := client .StackDeploymentRuns .Read (ctx , stackDeploymentRunID )
303
+ if err != nil {
304
+ t .Fatalf ("Failed to read stack deployment run %q: %s" , stackDeploymentRunID , err )
305
+ }
306
+
307
+ if sdr .Status == "deploying" {
308
+ finished = true
309
+ }
310
+ }
311
+ }
312
+ }
0 commit comments