Skip to content

Conversation

@liamcervante
Copy link
Contributor

This PR moves variable evaluation into the Terraform Test graph. This allows variables to reference other variables and run block outputs for the first time. Variable values are now just held in the evaluation context, and can be retrieved directly instead of needing to use the variable cache at all, so the variable cache has been deleted.

There is one test that must be skipped at the moment, because a follow up PR will add providers into the graph and there is a temporary breakage in behaviour until that PR has been completed.

Fixes #34629

@liamcervante liamcervante requested a review from a team as a code owner June 4, 2025 08:34
@liamcervante liamcervante force-pushed the liamcervante/specific-dependencies branch from 53eb2da to 184d7f5 Compare June 4, 2025 08:42
@liamcervante liamcervante force-pushed the liamcervante/test/graph-variables branch from 3debf19 to fc892ad Compare June 4, 2025 08:43
Copy link
Member

@dsa0x dsa0x left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍 , but I have some questions

requiredValues[addr.Name] = value.Value
continue
}
if value, valueDiags := ctx.EvaluateUnparsedVariable(addr.Name, run.ModuleConfig.Module.Variables[addr.Name]); value != nil {
Copy link
Member

@dsa0x dsa0x Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question I asked earlier. Shouldn't these references have been resolved before we get here?

I see that the config here is coming from the module configuration. What if we add those variables into the graph as a VariableDefinition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so the problem with this is that we basically allow users to just provide "external" variables into the testing framework without definitions. I've called them unparsed variables here, as at this point they are essentially just the raw values of variables arriving from either external files, environment variables, or CLI flags.

These external variables are then interpreted differently depending on when they're needed. This makes sense as a particular run block might have a configuration for a variable that it is expecting, and then it's really good that the testing framework just makes the external variables available to that run block without forcing the user to put them in somewhere in the testing file.

But, we did make a (probably bad) decision early on to just let people reference these in the testing file itself, and that's what you're seeing in the place you made the other comment. With the optional variable blocks introduced earlier, we can make people declare what they're using in the test file but we don't want breaking changes by forcing people to do this.

What this means is that we have these unparsed / external variables that are just hanging around and being lazily evaluated. We could add specific nodes into the graph for them, but we'd have to add nodes for the cross product of each run block with each unparsed variable when they're only going to be referenced once anyway.

One short-term solution, could be to reintroduce the variable cache we had before that makes sure these are only evaluated once lazily.

A possible long term solution, might be to add a warning / deprecation notice that says "we're going to stop this behaviour soon, you must add variable definition blocks if you want to do this". Then in 2-3 Terraform versions actually come in and break it.

But, we could do either of those in a follow up PR - what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed explanation. It makes sense to me to make that decision in another PR. The deprecation notice would be nice, even if it takes longer than 2-3 versions to get it removed.

Base automatically changed from liamcervante/specific-dependencies to main June 5, 2025 10:44
@liamcervante liamcervante force-pushed the liamcervante/test/graph-variables branch from 982ab2b to 403e7b4 Compare June 5, 2025 12:01
@liamcervante liamcervante requested a review from dsa0x June 5, 2025 12:01
@liamcervante liamcervante merged commit 53afa2c into main Jun 6, 2025
11 of 12 checks passed
@liamcervante liamcervante deleted the liamcervante/test/graph-variables branch June 6, 2025 08:11
dsa0x pushed a commit that referenced this pull request Jun 6, 2025
#37205)

* terraform test: move variable evaluation into the terraform test graph

* make copyrightfix
@github-actions
Copy link
Contributor

github-actions bot commented Jul 7, 2025

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support locals block inside tftest.hcl Terraform Test file.

2 participants