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

Can terraform init fully initialize (for tests) if no .tf files exist the current working directory? #35040

Open
brettcurtis opened this issue Apr 19, 2024 · 2 comments
Labels
enhancement new new issue not yet triaged terraform test

Comments

@brettcurtis
Copy link

Terraform Version

Terraform v1.8.1
on linux_amd64
+ provider registry.terraform.io/hashicorp/google v5.25.0

Use Cases

With terraform test you can now run tests from the root of a repo even if the root has no .tf files. A directory structure like this can't run tests unless an empty.tf file is added because we can't fully initialize with terraform init.

image

This is a current test/WA:

❯ terraform test -verbose
╷
│ Error: Module not installed
│ 
│   on tests/default.tftest.hcl line 4, in run "test":
│    4:   module {
│ 
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵
❯ terraform init
Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.
❯ terraform test -verbose
╷
│ Error: Module not installed
│ 
│   on tests/default.tftest.hcl line 4, in run "test":
│    4:   module {
│ 
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵
❯ touch empty.tf
❯ terraform init

Initializing the backend...
Initializing modules...
- test.tests.default.test in tests/fixtures/default
- test.tests.default.test.test in global

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v5.25.0...
- Installed hashicorp/google v5.25.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
❯ terraform test
tests/default.tftest.hcl... in progress
  run "test"... pass
tests/default.tftest.hcl... tearing down
tests/default.tftest.hcl... pass

Success! 1 passed, 0 failed.

Attempted Solutions

add empty.tf, change directory structure.

Proposal

No response

References

No response

@liamcervante
Copy link
Member

Hi @brettcurtis, thanks for the feature request. Is there a reason you can't place the test files / test directory within your global directory and run terraform init and terraform test from there?

With that change you also wouldn't need to create the testing modules you have within the fixtures directory.

@brettcurtis
Copy link
Author

Good questions, we find developers tend to write better modules if the tests are a representation of how the user will consume them. This is why we setup the fixtures the way we do, we also find they provided good examples for consumers of the modules since they do set the system up for testing.

As far as placing the test in global that would be reasonable for this module (simple use case) maybe but more complex modules we'd have tests all over the place and I'm particularly a fan of having a single tests directory structure. Here is an example of a more complex structure for reference: https://github.com/osinfra-io/terraform-google-kubernetes-engine/tree/main/regional. This one will expose some other testing limitations too.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged terraform test
Projects
None yet
Development

No branches or pull requests

2 participants