Skip to content

Commit cadc46d

Browse files
committed
consolidate runner.run
1 parent d8d7b90 commit cadc46d

File tree

2 files changed

+58
-68
lines changed

2 files changed

+58
-68
lines changed

internal/backend/local/test.go

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
346346
defer sem.Release()
347347

348348
switch v := v.(type) {
349-
case *graph.NodeTestRun:
349+
case *graph.NodeTestRun: // NodeTestRun is also executable, so it has to be first.
350350
file := v.File()
351351
run := v.Run()
352352
if file.GetStatus() == moduletest.Error {
@@ -374,83 +374,67 @@ func (runner *TestFileRunner) walkGraph(g *terraform.Graph) tfdiags.Diagnostics
374374
if diags.HasErrors() {
375375
return diags
376376
}
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)
378382
case graph.GraphNodeExecutable:
379383
diags = v.Execute(runner.EvalContext)
380384
return diags
381385
default:
382386
// If the vertex isn't a test run or executable, we'll just skip it.
383387
return
384388
}
389+
return
390+
}
385391

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+
}
424394

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() {
425398
// If we got far enough to actually execute the run then we'll give
426399
// the view some additional metadata about the execution.
427400
run.ExecutionMeta = &moduletest.RunExecutionMeta{
428401
Start: startTime,
429-
Duration: runDuration,
402+
Duration: time.Since(startTime),
430403
}
431-
runner.Suite.View.Run(run, file, moduletest.Complete, 0)
432-
file.UpdateStatus(run.Status)
433-
return
434-
}
404+
}()
435405

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.
438411

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
441425

442426
config := run.ModuleConfig
443427
if runner.Suite.Cancelled {
444428
// Don't do anything, just give up and return immediately.
445429
// The surrounding functions should stop this even being called, but in
446430
// case of race conditions or something we can still verify this.
447-
return state, false
431+
return
448432
}
449433

450434
if runner.Suite.Stopped {
451435
// Basically the same as above, except we'll be a bit nicer.
452436
run.Status = moduletest.Skip
453-
return state, false
437+
return
454438
}
455439

456440
start := time.Now().UTC().UnixMilli()
@@ -459,35 +443,35 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
459443
run.Diagnostics = run.Diagnostics.Append(run.Config.Validate(config))
460444
if run.Diagnostics.HasErrors() {
461445
run.Status = moduletest.Error
462-
return state, false
446+
return
463447
}
464448

465449
configDiags := graph.TransformConfigForTest(runner.EvalContext, run, file)
466450
run.Diagnostics = run.Diagnostics.Append(configDiags)
467451
if configDiags.HasErrors() {
468452
run.Status = moduletest.Error
469-
return state, false
453+
return
470454
}
471455

472456
validateDiags := runner.validate(run, file, start)
473457
run.Diagnostics = run.Diagnostics.Append(validateDiags)
474458
if validateDiags.HasErrors() {
475459
run.Status = moduletest.Error
476-
return state, false
460+
return
477461
}
478462

479463
references, referenceDiags := run.GetReferences()
480464
run.Diagnostics = run.Diagnostics.Append(referenceDiags)
481465
if referenceDiags.HasErrors() {
482466
run.Status = moduletest.Error
483-
return state, false
467+
return
484468
}
485469

486470
variables, variableDiags := runner.GetVariables(run, references, true)
487471
run.Diagnostics = run.Diagnostics.Append(variableDiags)
488472
if variableDiags.HasErrors() {
489473
run.Status = moduletest.Error
490-
return state, false
474+
return
491475
}
492476

493477
// 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
498482
tfCtx, ctxDiags := terraform.NewContext(runner.Suite.Opts)
499483
run.Diagnostics = run.Diagnostics.Append(ctxDiags)
500484
if ctxDiags.HasErrors() {
501-
return state, false
485+
return
502486
}
503487

504488
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
508492
run.Diagnostics = run.Diagnostics.Append(planDiags)
509493
if planDiags.HasErrors() {
510494
run.Status = moduletest.Error
511-
return state, false
495+
return
512496
}
513497

514498
runner.AddVariablesToConfig(run, variables)
@@ -549,8 +533,7 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
549533
// Now we've successfully validated this run block, lets add it into
550534
// our prior run outputs so future run blocks can access it.
551535
runner.EvalContext.SetOutput(run, outputVals)
552-
553-
return state, false
536+
return
554537
}
555538

556539
// 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
559542
run.Diagnostics = run.Diagnostics.Append(planDiags)
560543
if planDiags.HasErrors() {
561544
run.Status = moduletest.Error
562-
return state, false
545+
return
563546
}
564547

565548
// 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
586569
run.Status = moduletest.Error
587570
// Even though the apply operation failed, the graph may have done
588571
// 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
590577
}
591578

592579
runner.AddVariablesToConfig(run, variables)
@@ -628,7 +615,15 @@ func (runner *TestFileRunner) run(run *moduletest.Run, file *moduletest.File, st
628615
// our prior run outputs so future run blocks can access it.
629616
runner.EvalContext.SetOutput(run, outputVals)
630617

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
632627
}
633628

634629
func (runner *TestFileRunner) validate(run *moduletest.Run, file *moduletest.File, start int64) tfdiags.Diagnostics {

internal/moduletest/graph/eval_context.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,6 @@ func diagsForEphemeralResources(refs []*addrs.Reference) (diags tfdiags.Diagnost
320320
func (ec *EvalContext) SetFileState(key string, state *TestFileState) {
321321
ec.stateLock.Lock()
322322
defer ec.stateLock.Unlock()
323-
fileState := ec.FileStates[key]
324-
if fileState != nil {
325-
ec.FileStates[key] = state
326-
return
327-
}
328323
ec.FileStates[key] = &TestFileState{
329324
Run: state.Run,
330325
State: state.State,

0 commit comments

Comments
 (0)