@@ -346,7 +346,7 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
346
346
defer sem .Release ()
347
347
348
348
switch v := v .(type ) {
349
- case * graph.NodeTestRun :
349
+ case * graph.NodeTestRun : // NodeTestRun is also executable, so it has to be first.
350
350
file := v .File ()
351
351
run := v .Run ()
352
352
if file .GetStatus () == moduletest .Error {
@@ -374,83 +374,67 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
374
374
if diags .HasErrors () {
375
375
return diags
376
376
}
377
- // continue the execution of the test run.
377
+
378
+ startTime := time .Now ().UTC ()
379
+ runner .run (run , file , startTime )
380
+ runner .Suite .View .Run (run , file , moduletest .Complete , 0 )
381
+ file .UpdateStatus (run .Status )
378
382
case graph.GraphNodeExecutable :
379
383
diags = v .Execute (runner .EvalContext )
380
384
return diags
381
385
default :
382
386
// If the vertex isn't a test run or executable, we'll just skip it.
383
387
return
384
388
}
389
+ return
390
+ }
385
391
386
- // We already know that the vertex is a test run
387
- runNode := v .(* graph.NodeTestRun )
388
-
389
- file := runNode .File ()
390
- run := runNode .Run ()
391
-
392
- key := run .GetStateKey ()
393
- if run .Config .ConfigUnderTest != nil {
394
- if key == moduletest .MainStateIdentifier {
395
- // This is bad. It means somehow the module we're loading has
396
- // the same key as main state and we're about to corrupt things.
397
-
398
- run .Diagnostics = run .Diagnostics .Append (& hcl.Diagnostic {
399
- Severity : hcl .DiagError ,
400
- Summary : "Invalid module source" ,
401
- Detail : fmt .Sprintf ("The source for the selected module evaluated to %s which should not be possible. This is a bug in Terraform - please report it!" , key ),
402
- Subject : run .Config .Module .DeclRange .Ptr (),
403
- })
404
-
405
- run .Status = moduletest .Error
406
- file .UpdateStatus (moduletest .Error )
407
- return
408
- }
409
- }
410
-
411
- startTime := time .Now ().UTC ()
412
- state , updatedState := runner .run (run , file , runner .EvalContext .GetFileState (key ).State )
413
- runDuration := time .Since (startTime )
414
- if updatedState {
415
- // Only update the most recent run and state if the state was
416
- // actually updated by this change. We want to use the run that
417
- // most recently updated the tracked state as the cleanup
418
- // configuration.
419
- runner .EvalContext .SetFileState (key , & graph.TestFileState {
420
- Run : run ,
421
- State : state ,
422
- })
423
- }
392
+ return g .AcyclicGraph .Walk (walkFn )
393
+ }
424
394
395
+ func (runner * TestFileRunner ) run (run * moduletest.Run , file * moduletest.File , startTime time.Time ) {
396
+ log .Printf ("[TRACE] TestFileRunner: executing run block %s/%s" , file .Name , run .Name )
397
+ defer func () {
425
398
// If we got far enough to actually execute the run then we'll give
426
399
// the view some additional metadata about the execution.
427
400
run .ExecutionMeta = & moduletest.RunExecutionMeta {
428
401
Start : startTime ,
429
- Duration : runDuration ,
402
+ Duration : time . Since ( startTime ) ,
430
403
}
431
- runner .Suite .View .Run (run , file , moduletest .Complete , 0 )
432
- file .UpdateStatus (run .Status )
433
- return
434
- }
404
+ }()
435
405
436
- return g .AcyclicGraph .Walk (walkFn )
437
- }
406
+ key := run .GetStateKey ()
407
+ if run .Config .ConfigUnderTest != nil {
408
+ if key == moduletest .MainStateIdentifier {
409
+ // This is bad. It means somehow the module we're loading has
410
+ // the same key as main state and we're about to corrupt things.
438
411
439
- func (runner * TestFileRunner ) run (run * moduletest.Run , file * moduletest.File , state * states.State ) (* states.State , bool ) {
440
- log .Printf ("[TRACE] TestFileRunner: executing run block %s/%s" , file .Name , run .Name )
412
+ run .Diagnostics = run .Diagnostics .Append (& hcl.Diagnostic {
413
+ Severity : hcl .DiagError ,
414
+ Summary : "Invalid module source" ,
415
+ Detail : fmt .Sprintf ("The source for the selected module evaluated to %s which should not be possible. This is a bug in Terraform - please report it!" , key ),
416
+ Subject : run .Config .Module .DeclRange .Ptr (),
417
+ })
418
+
419
+ run .Status = moduletest .Error
420
+ file .UpdateStatus (moduletest .Error )
421
+ return
422
+ }
423
+ }
424
+ state := runner .EvalContext .GetFileState (key ).State
441
425
442
426
config := run .ModuleConfig
443
427
if runner .Suite .Cancelled {
444
428
// Don't do anything, just give up and return immediately.
445
429
// The surrounding functions should stop this even being called, but in
446
430
// case of race conditions or something we can still verify this.
447
- return state , false
431
+ return
448
432
}
449
433
450
434
if runner .Suite .Stopped {
451
435
// Basically the same as above, except we'll be a bit nicer.
452
436
run .Status = moduletest .Skip
453
- return state , false
437
+ return
454
438
}
455
439
456
440
start := time .Now ().UTC ().UnixMilli ()
@@ -459,35 +443,35 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
459
443
run .Diagnostics = run .Diagnostics .Append (run .Config .Validate (config ))
460
444
if run .Diagnostics .HasErrors () {
461
445
run .Status = moduletest .Error
462
- return state , false
446
+ return
463
447
}
464
448
465
449
configDiags := graph .TransformConfigForTest (runner .EvalContext , run , file )
466
450
run .Diagnostics = run .Diagnostics .Append (configDiags )
467
451
if configDiags .HasErrors () {
468
452
run .Status = moduletest .Error
469
- return state , false
453
+ return
470
454
}
471
455
472
456
validateDiags := runner .validate (run , file , start )
473
457
run .Diagnostics = run .Diagnostics .Append (validateDiags )
474
458
if validateDiags .HasErrors () {
475
459
run .Status = moduletest .Error
476
- return state , false
460
+ return
477
461
}
478
462
479
463
references , referenceDiags := run .GetReferences ()
480
464
run .Diagnostics = run .Diagnostics .Append (referenceDiags )
481
465
if referenceDiags .HasErrors () {
482
466
run .Status = moduletest .Error
483
- return state , false
467
+ return
484
468
}
485
469
486
470
variables , variableDiags := runner .GetVariables (run , references , true )
487
471
run .Diagnostics = run .Diagnostics .Append (variableDiags )
488
472
if variableDiags .HasErrors () {
489
473
run .Status = moduletest .Error
490
- return state , false
474
+ return
491
475
}
492
476
493
477
// FilterVariablesToModule only returns warnings, so we don't check the
@@ -498,7 +482,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
498
482
tfCtx , ctxDiags := terraform .NewContext (runner .Suite .Opts )
499
483
run .Diagnostics = run .Diagnostics .Append (ctxDiags )
500
484
if ctxDiags .HasErrors () {
501
- return state , false
485
+ return
502
486
}
503
487
504
488
planScope , plan , planDiags := runner .plan (tfCtx , config , state , run , file , setVariables , references , start )
@@ -508,7 +492,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
508
492
run .Diagnostics = run .Diagnostics .Append (planDiags )
509
493
if planDiags .HasErrors () {
510
494
run .Status = moduletest .Error
511
- return state , false
495
+ return
512
496
}
513
497
514
498
runner .AddVariablesToConfig (run , variables )
@@ -549,8 +533,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
549
533
// Now we've successfully validated this run block, lets add it into
550
534
// our prior run outputs so future run blocks can access it.
551
535
runner .EvalContext .SetOutput (run , outputVals )
552
-
553
- return state , false
536
+ return
554
537
}
555
538
556
539
// Otherwise any error during the planning prevents our apply from
@@ -559,7 +542,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
559
542
run .Diagnostics = run .Diagnostics .Append (planDiags )
560
543
if planDiags .HasErrors () {
561
544
run .Status = moduletest .Error
562
- return state , false
545
+ return
563
546
}
564
547
565
548
// Since we're carrying on an executing the apply operation as well, we're
@@ -586,7 +569,11 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
586
569
run .Status = moduletest .Error
587
570
// Even though the apply operation failed, the graph may have done
588
571
// partial updates and the returned state should reflect this.
589
- return updated , true
572
+ runner .EvalContext .SetFileState (key , & graph.TestFileState {
573
+ Run : run ,
574
+ State : updated ,
575
+ })
576
+ return
590
577
}
591
578
592
579
runner .AddVariablesToConfig (run , variables )
@@ -628,7 +615,15 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
628
615
// our prior run outputs so future run blocks can access it.
629
616
runner .EvalContext .SetOutput (run , outputVals )
630
617
631
- return updated , true
618
+ // Only update the most recent run and state if the state was
619
+ // actually updated by this change. We want to use the run that
620
+ // most recently updated the tracked state as the cleanup
621
+ // configuration.
622
+ runner .EvalContext .SetFileState (key , & graph.TestFileState {
623
+ Run : run ,
624
+ State : updated ,
625
+ })
626
+ return
632
627
}
633
628
634
629
func (runner * TestFileRunner ) validate (run * moduletest.Run , file * moduletest.File , start int64 ) tfdiags.Diagnostics {
0 commit comments