Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Jan 21, 2025
1 parent a7a3176 commit 4c6a548
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
7 changes: 2 additions & 5 deletions internal/configs/test_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ type TestRun struct {
StateKey string

// Parallel: Indicates if the test run should be executed in parallel.
// This, in combination with the state key, will allow multiple test runs
// to run in parallel, provided 2 test runs do not use the same state key.
// This, in combination with the state key, will determine if the test run
// will be executed in parallel with other test runs.
Parallel bool

NameDeclRange hcl.Range
Expand Down Expand Up @@ -467,9 +467,6 @@ func loadTestFile(body hcl.Body) (*TestFile, hcl.Diagnostics) {

type TestFileConfig struct {
// Parallel: Indicates if test runs should be executed in parallel.
// The decision to execute the runs in parallel will be made based on the
// implicit dependencies between the runs or explicit dependencies defined
// in the run's depends_on attribute.
Parallel bool

DeclRange hcl.Range
Expand Down
12 changes: 6 additions & 6 deletions internal/moduletest/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ type File struct {

Diagnostics tfdiags.Diagnostics

lock sync.Mutex
mu sync.Mutex
}

func NewFile(name string, config *configs.TestFile, runs []*Run) *File {
return &File{
Name: name,
Config: config,
Runs: runs,
lock: sync.Mutex{},
mu: sync.Mutex{},
}
}

func (f *File) UpdateStatus(status Status) {
f.lock.Lock()
defer f.lock.Unlock()
lock := f.Lock()
defer lock()
f.Status = f.Status.Merge(status)
}

func (f *File) Lock() func() {
f.lock.Lock()
return f.lock.Unlock
f.mu.Lock()
return f.mu.Unlock
}
54 changes: 29 additions & 25 deletions internal/moduletest/graph/eval_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ import (
)

// EvalContext is a container for context relating to the evaluation of a
// particular test case, which means a specific "run" block in a .tftest.hcl
// file.
// particular .tftest.hcl file.
// This context is used to track the various values that are available to the
// test suite, both from the test suite itself and from the results of the runs
// within the suite.
// The struct provides concurrency-safe access to the various maps it contains.
type EvalContext struct {
VariableCaches *hcltest.VariableCaches

// priorOutputs is a mapping from run addresses to cty object values
// runOutputs is a mapping from run addresses to cty object values
// representing the collected output values from the module under test.
//
// This is used to allow run blocks to refer back to the output values of
// previous run blocks. It is passed into the Evaluate functions that
// validate the test assertions, and used when calculating values for
// variables within run blocks.
priorOutputs map[addrs.Run]cty.Value
outputsLock sync.Mutex
runOutputs map[addrs.Run]cty.Value
outputsLock sync.Mutex

// configProviders is a cache of config keys mapped to all the providers
// referenced by the given config.
Expand All @@ -57,22 +60,11 @@ type EvalContext struct {
stateLock sync.Mutex
}

// NewEvalContext constructs a new test run evaluation context based on the
// definition of the run itself and on the results of the action the run
// block described.
//
// priorOutputs describes the output values from earlier run blocks, which
// should typically be populated from the second return value from calling
// [EvalContext.Evaluate] on each earlier blocks' [EvalContext].
//
// extraVariableVals, if provided, overlays the input variables that are
// already available in resultScope in case there are additional input
// variables that were defined only for use in the test suite. Any variable
// not defined in extraVariableVals will be evaluated through resultScope
// instead. //TODO: rewrite comments
// NewEvalContext constructs a new graph evaluation context for use in
// evaluating the runs within a test suite.
func NewEvalContext() *EvalContext {
return &EvalContext{
priorOutputs: make(map[addrs.Run]cty.Value),
runOutputs: make(map[addrs.Run]cty.Value),
outputsLock: sync.Mutex{},
configProviders: make(map[string]map[string]bool),
providersLock: sync.Mutex{},
Expand All @@ -82,9 +74,14 @@ func NewEvalContext() *EvalContext {
}
}

// Evaluate processes the assertions inside the provided configs.TestRun against
// EvaluateRun processes the assertions inside the provided configs.TestRun against
// the run results, returning a status, an object value representing the output
// values from the module under test, and diagnostics describing any problems.
//
// extraVariableVals, if provided, overlays the input variables that are
// already available in resultScope in case there are additional input
// variables that were defined only for use in the test suite. Any variable
// not defined in extraVariableVals will be evaluated through resultScope instead.
func (ec *EvalContext) EvaluateRun(run *moduletest.Run, resultScope *lang.Scope, extraVariableVals terraform.InputValues) (moduletest.Status, cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
if run.ModuleConfig == nil {
Expand Down Expand Up @@ -244,14 +241,14 @@ func (ec *EvalContext) EvaluateRun(run *moduletest.Run, resultScope *lang.Scope,
func (ec *EvalContext) SetOutput(run *moduletest.Run, output cty.Value) {
ec.outputsLock.Lock()
defer ec.outputsLock.Unlock()
ec.priorOutputs[run.Addr()] = output
ec.runOutputs[run.Addr()] = output
}

func (ec *EvalContext) GetOutputs() map[addrs.Run]cty.Value {
ec.outputsLock.Lock()
defer ec.outputsLock.Unlock()
outputCopy := make(map[addrs.Run]cty.Value, len(ec.priorOutputs))
for k, v := range ec.priorOutputs {
outputCopy := make(map[addrs.Run]cty.Value, len(ec.runOutputs))
for k, v := range ec.runOutputs {
outputCopy[k] = v
}
return outputCopy
Expand All @@ -264,10 +261,17 @@ func (ec *EvalContext) GetCache(run *moduletest.Run) *hcltest.VariableCache {
return ec.VariableCaches.GetCache(run.Name, run.ModuleConfig)
}

func (ec *EvalContext) GetProviders(run *moduletest.Run) map[string]bool {
// ProviderExists returns true if the provider exists for the run inside the context.
func (ec *EvalContext) ProviderExists(run *moduletest.Run, key string) bool {
ec.providersLock.Lock()
defer ec.providersLock.Unlock()
return ec.configProviders[run.GetModuleConfigID()]
runProviders, ok := ec.configProviders[run.GetModuleConfigID()]
if !ok {
return false
}

found, ok := runProviders[key]
return ok && found
}

func (ec *EvalContext) SetProviders(run *moduletest.Run, providers map[string]bool) {
Expand Down
3 changes: 1 addition & 2 deletions internal/moduletest/graph/transform_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,9 @@ func TransformConfigForTest(ctx *EvalContext, run *moduletest.Run, file *modulet
} else {
// Otherwise, let's copy over and overwrite all providers specified by
// the test file itself.
requiredProviders := ctx.GetProviders(run)
for key, provider := range file.Config.Providers {

if !requiredProviders[key] {
if !ctx.ProviderExists(run, key) {
// Then we don't actually need this provider for this
// configuration, so skip it.
continue
Expand Down

0 comments on commit 4c6a548

Please sign in to comment.