Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform test: allow providers in test files to reference variables #34069

Merged
merged 5 commits into from
Oct 16, 2023

Conversation

liamcervante
Copy link
Member

@liamcervante liamcervante commented Oct 12, 2023

This PR updates the testing framework so that providers defined in testing files can reference variables that are available within the test file.

We've defined a ProviderConfig struct in the moduletest package which wraps a hcl.Body and preprocesses the returned attributes before they're made available to the Terraform graph. We then update our provider definitions so the embedded config uses this as the body instead of the raw body. The Terraform graph executes as normally, just with the providers never returning values that need to be evaluated, because we handled that directly in the hcl.Body.

I've also moved the implementation of the hcl.EvalContext from within the Test command into a dedicated package. This means it can be shared by all the places in the test file that need to evaluate variables. For now, that's just when processing variables in run blocks, and when processing variables in test provider configurations.

One alternative approach I looked at was modifying the Terraform graph so you could provide external variable references, or even providing externally configured providers. The problem I found with this was that we were making sweeping changes within the Terraform graph, and we don't want the actual execution of Terraform to be significantly different during real operations and during test runs - this leads to more chance that a test will pass when a normal run would fail.

A second approach, was to just pass all the variables into the graph alongside the external provider definitions into the graph, so the variables could be located naturally by the graph and the providers initialised. The problem here is that we then start letting tests pass when a variable isn't defined in the config, but only defined within the test file, which isn't a good look.

First part in addressing #34007
We'll follow up by allowing data source blocks to be defined in test files, and these can then be referenced from providers.

Target Release

1.7.0

Draft CHANGELOG entry

ENHANCEMENTS

  • terraform test: Providers defined within test files can now reference variables in their configuration that are defined within the test file.

@liamcervante liamcervante requested a review from a team October 12, 2023 09:59
@liamcervante liamcervante marked this pull request as draft October 12, 2023 12:11
@liamcervante liamcervante removed the request for review from a team October 12, 2023 12:11
@liamcervante liamcervante force-pushed the liamcervante/34007-alternate branch from 24a66f4 to e01e7a2 Compare October 13, 2023 09:33
@liamcervante liamcervante changed the base branch from main to liamcervante/34070 October 13, 2023 09:34
@liamcervante liamcervante marked this pull request as ready for review October 13, 2023 09:43
@liamcervante liamcervante requested a review from a team October 13, 2023 09:44
Base automatically changed from liamcervante/34070 to main October 16, 2023 14:39
@liamcervante liamcervante merged commit 5bbdc4c into main Oct 16, 2023
@liamcervante liamcervante deleted the liamcervante/34007-alternate branch October 16, 2023 14:57
@github-actions
Copy link
Contributor

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

Copy link
Contributor

github-actions bot commented Dec 7, 2023

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 Dec 7, 2023
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.

2 participants