Adds docs for tests - run references in test files (#2607)

Signed-off-by: Ilia Gogotchuri <ilia.gogotchuri0@gmail.com>
This commit is contained in:
Ilia Gogotchuri
2025-03-18 18:38:35 +04:00
committed by GitHub
parent 44d07cea4b
commit 39d65540af
3 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
run "setup" {
module {
source = "./setup"
}
}
run "test" {
variables {
filename_from_setup = run.setup.filename
}
# more assertions to run
}

View File

@@ -0,0 +1,3 @@
output "filename" {
value = "some_file.txt"
}

View File

@@ -23,6 +23,8 @@ import ProviderAliasMain from '!!raw-loader!./examples/provider_alias/main.tf'
import ProviderAliasTest from '!!raw-loader!./examples/provider_alias/main.tftest.hcl'
import VariablesMain from '!!raw-loader!./examples/variables/main.tf'
import VariablesTest from '!!raw-loader!./examples/variables/main.tftest.hcl'
import RunInVariablesMain from '!!raw-loader!./examples/run_in_variables/mod/main.tf'
import RunInVariablesTest from '!!raw-loader!./examples/run_in_variables/main.tftest.hcl'
import ExpectFailureVariablesMain from '!!raw-loader!./examples/expect_failures_variables/main.tf'
import ExpectFailureVariablesTest from '!!raw-loader!./examples/expect_failures_variables/main.tftest.hcl'
import ExpectFailureResourcesMain from '!!raw-loader!./examples/expect_failures_resources/main.tf'
@@ -298,6 +300,19 @@ For example:
<TabItem value="main" label="main.tf"><CodeBlock language={"hcl"}>{VariablesMain}</CodeBlock></TabItem>
</Tabs>
#### The `run` block outputs for variables
It is also possible to use the earlier-executed `run` block module outputs to set another `run` block `variables` values.
This can be useful when you need to pass values between different test cases.
<Tabs>
<TabItem value="test" label="main.tftest.hcl" default>
<CodeBlock language={"hcl"}>{RunInVariablesTest}</CodeBlock>
</TabItem>
<TabItem value="main" label="mod/main.tf"><CodeBlock language={"hcl"}>{RunInVariablesMain}</CodeBlock></TabItem>
</Tabs>
### The `run.expect_failures` list
In some cases you may want to test deliberate failures of your code, for example to ensure your validation is working.
@@ -413,6 +428,33 @@ of the file.
</TabItem>
</Tabs>
#### References to `run` module outputs and variables in `provider` blocks
You can reference `var` (variables) and the `run` block module outputs in `provider` blocks to set up test providers based on dynamic configuration.
```hcl
variables {
region = "us-west-2"
}
provider "aws" {
region = var.region
access_key = run.setup.access_key
secret_key = run.setup.secret_key
}
run "setup" {
# `mod` module has outputs access_key and secret_key
module {
source = "./mod"
}
# Actual run block behavior, such as asserts, are skipped for simplicity
}
```
### The `mock_provider` blocks
A `mock_provider` block allows you to replace provider configuration by a mocked one. In such scenario,