@@ -508,6 +508,73 @@ func TestCloud_applyWithCloudPlan(t *testing.T) {
508508 }
509509}
510510
511+ func TestCloud_applyAutoApprove_with_CloudPlan (t * testing.T ) {
512+ b , bCleanup := testBackendWithName (t )
513+ defer bCleanup ()
514+
515+ op , configCleanup , done := testOperationApply (t , "./testdata/apply-json" )
516+ defer configCleanup ()
517+ defer done (t )
518+
519+ op .AutoApprove = true
520+ op .UIOut = b .CLI
521+ op .Workspace = testBackendSingleWorkspaceName
522+
523+ mockSROWorkspace (t , b , op .Workspace )
524+
525+ ws , err := b .client .Workspaces .Read (context .Background (), b .Organization , b .WorkspaceMapping .Name )
526+ if err != nil {
527+ t .Fatalf ("Couldn't read workspace: %s" , err )
528+ }
529+
530+ planRun , err := b .plan (context .Background (), context .Background (), op , ws )
531+ if err != nil {
532+ t .Fatalf ("Couldn't perform plan: %s" , err )
533+ }
534+
535+ // Synthesize a cloud plan file with the plan's run ID
536+ pf := & cloudplan.SavedPlanBookmark {
537+ RemotePlanFormat : 1 ,
538+ RunID : planRun .ID ,
539+ Hostname : b .Hostname ,
540+ }
541+ op .PlanFile = planfile .NewWrappedCloud (pf )
542+
543+ // Start spying on the apply output (now that the plan's done)
544+ stream , close := terminal .StreamsForTesting (t )
545+
546+ b .renderer = & jsonformat.Renderer {
547+ Streams : stream ,
548+ Colorize : mockColorize (),
549+ }
550+
551+ // Try apply
552+ run , err := b .Operation (context .Background (), op )
553+ if err != nil {
554+ t .Fatalf ("error starting operation: %v" , err )
555+ }
556+
557+ <- run .Done ()
558+ output := close (t )
559+ if run .Result != backendrun .OperationSuccess {
560+ t .Fatal ("expected apply operation to succeed" )
561+ }
562+ if run .PlanEmpty {
563+ t .Fatalf ("expected plan to not be empty" )
564+ }
565+
566+ gotOut := output .Stdout ()
567+ if ! strings .Contains (gotOut , "1 added, 0 changed, 0 destroyed" ) {
568+ t .Fatalf ("expected apply summary in output: %s" , gotOut )
569+ }
570+
571+ stateMgr , _ := b .StateMgr (testBackendSingleWorkspaceName )
572+ // An error suggests that the state was not unlocked after apply
573+ if _ , err := stateMgr .Lock (statemgr .NewLockInfo ()); err != nil {
574+ t .Fatalf ("unexpected error locking state after apply: %s" , err .Error ())
575+ }
576+ }
577+
511578func TestCloud_applyWithoutRefresh (t * testing.T ) {
512579 b , bCleanup := testBackendWithName (t )
513580 defer bCleanup ()
0 commit comments