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: Load terraform.tfvars file from test directory automatically #34008

Closed
liamcervante opened this issue Oct 6, 2023 · 9 comments

Comments

@liamcervante
Copy link
Member

Terraform Version

Terraform v1.6.0

Use Cases

It would be nice to let users specify a set of Terraform variable values for all the tests at once automatically. We can do this by loading a terraform.tfvars file from the testing directory automatically, like we do for the main configuration directory.

Attempted Solutions

You can place the variable values in terraform.tfvars file in the main configuration directory, but this might clash with any default variable values you want to specify for plans or applys against the main config.

Proposal

terraform test loads the default variables from this function here.

We could modify, or duplicate, that function so that it accepts a testing directory and will include two sets of variables one for any tests in the testing directory, and one for any tests within the main configuration directory.

References

No response

@liamcervante liamcervante added enhancement good first issue new new issue not yet triaged terraform test and removed new new issue not yet triaged labels Oct 6, 2023
@JKIPIKA
Copy link
Contributor

JKIPIKA commented Oct 7, 2023

Hi, I would like to be new contributor to the Terraform project, can I work on this issue ?

@liamcervante
Copy link
Member Author

Hi @JKIPIKA, sure thing! I'd be happy to review any PR you raised for this issue.

A caveat / trap to look out for would be that we can execute tests both inside the main configuration directory and within any specified testing directory in the same execution. This means we need to keep track of two sets of variables for the tests, one loaded from the main configuration directory and one loaded for the testing directory.

My approach would be:

  • Add a function called collectVariableValuesForTests() alongside this function.
    • I'd make it accept the current testing directory as an argument, and it should return two sets of variables. One loaded from the testing directory, and one loaded from the configuration directory.
    • The two functions will largely be the same aside from these lines so you want to think about the best way to maximise code sharing between them (probably with a shared third function that both call out to).
  • You can then call the new function from command/test.go to get both sets of variables for all the tests.
  • You then want to pass both sets of variables into the local TestSuiteRunner instead of the current single set.
  • Finally, you want to find where that file was referenced and update them so they pick the correct set of variables based on the currently executing file.
    • You could probably make this easier by adding the global variables directly into TestFileRunner when that is created, and then have the references look at the file runner instead of the suite runner overall.

Hopefully that makes sense! If you're still interested, feel free to raise a PR and tag me, or link it here, and I can take a look. Let me know any other questions!

@JKIPIKA
Copy link
Contributor

JKIPIKA commented Oct 9, 2023

Thank you very much for explanation! I will raise a PR when i will have something worth sharing.

@crw
Copy link
Collaborator

crw commented Oct 9, 2023

@JKIPIKA
Copy link
Contributor

JKIPIKA commented Oct 11, 2023

Hi @crw, i have read contribution guide! I have dev environment ready at my local machine, can you please mark me as assignee to this issue ?

@JKIPIKA
Copy link
Contributor

JKIPIKA commented Nov 20, 2023

Hello, sorry for the delay, the task was actually harder for me than I thought, can you please verify my solution ?

@crw
Copy link
Collaborator

crw commented Nov 28, 2023

@JKIPIKA thanks, let's keep that conversation in the PR. We appreciate your contribution!

@liamcervante
Copy link
Member Author

Fixed in #34341, thanks @JKIPIKA!

Copy link

github-actions bot commented Jan 4, 2024

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, 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 Jan 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants