From 16d0baf8ac3ae4f216a73ab7e1122b95d0d7bad2 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Mon, 13 Jan 2025 12:21:02 +0000 Subject: [PATCH 1/3] backport of commit 215414cda13bea0b39b28c9f775f96c308e75478 --- website/docs/language/tests/mocking.mdx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/website/docs/language/tests/mocking.mdx b/website/docs/language/tests/mocking.mdx index 2d42729e9d3a..14ab4288cec7 100644 --- a/website/docs/language/tests/mocking.mdx +++ b/website/docs/language/tests/mocking.mdx @@ -171,10 +171,12 @@ Overrides can be used with both real and mocked providers and will provide the c ### Overrides Syntax -All override blocks contain a `target` attribute, which should specify the resource, data source, or module to override. The `override_module` blocks contain an `outputs` attribute, while the `override_resource` and `override_data` blocks contain a `values` attribute. +All override blocks contain a `target` attribute, which should specify the resource, data source, or module to override. The `override_module` blocks contain an `outputs` attribute, while the `override_resource` and `override_data` blocks contain a `values` attribute. In addition, the override blocks support an optional `override_during` block that specifies when the computed values should be generated. The `outputs` and `values` attributes are optional and if not specified, Terraform will generate values for them automatically. +The `override_during` attribute requires either a `plan` or `apply` keyword value. If set to `plan`, Terraform will generate values during the `plan` phase and reuse these values during the `apply` phase. If set to `apply` or not specified, Terraform will generate values for override block during the `apply` phase, returning `(known after apply)` for the values during the `plan` phase. + The following example demonstrates the override blocks at various different scopes and levels. The main configuration calls the `./modules/s3_data` module to read a file from an S3 bucket, and then creates a `local_file` from the data returned from the module. ```hcl From 843a489023df15f218c5e598b2a52dde809f7a67 Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Tue, 14 Jan 2025 10:43:49 +0000 Subject: [PATCH 2/3] backport of commit b9015d8b3d37335ae63d83badc96ab1b3d8c3b0b --- website/docs/language/tests/mocking.mdx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/website/docs/language/tests/mocking.mdx b/website/docs/language/tests/mocking.mdx index 14ab4288cec7..6c8679aa536b 100644 --- a/website/docs/language/tests/mocking.mdx +++ b/website/docs/language/tests/mocking.mdx @@ -105,6 +105,16 @@ Mocked providers only generate data for computed attributes. All required resour An example of this is the `bucket` attribute in the `aws_s3_bucket` resource. A real AWS provider will generate a bucket name if one is not specified. A mocked AWS provider will do the same, and only generate a value if one is not already specified in the configuration. +By default, Terraform will generate data during the `apply` operation while returning `(known after apply)` values during the `plan` operation. This behavior can be overridden with the `override_during` attribute in the `mock_provider` block: + +```hcl +mock_provider "aws" { + override_during = plan +} +``` + +The above `aws` provider will generate the data during the `plan` operation and reuse the same data during the `apply` operation. The `override_during` attribute accepts either `plan` or `apply` as values. + ### Mock Provider data You can specify specific values for targeted resources and data sources. in a `mock_provider` block, you can write any number of `mock_resource` and `mock_data` blocks. Both the `mock_resource` and `mock_data` blocks accept a type argument that should match the resource or data source you want to provide values for. They also accept a `defaults` object attribute that you can use to specify the values that should be returned for specific attributes. @@ -127,7 +137,7 @@ mock_provider "aws" { } ``` -In the above example, Terraform uses the supplied value for `arn` attributes in S3 buckets instead of generating a random string. Computed attributes not provided an explicit default will simply fall back to the generic data generation rules. +In the above example, Terraform uses the supplied value for `arn` attributes in S3 buckets instead of generating a random string. Computed attributes not provided an explicit default will simply fall back to the generic data generation rules. The `override_during` attribute can also be used in the `mock_resource` and `mock_data` blocks to specify when the values should be generated at an individual resource level. If not specified, the values will be generated using the rules inherited from the `mock_provider` block. If specified, the local value will override any value specified in the `mock_provider` block. You can also share mock provider data between tests by writing dedicated mock data files and using the `source` attribute in the `mock_provider` block. Mock data files have `.tfmock.hcl` or `.tfmock.json` extension, and can contain `mock_resource` and `mock_data` blocks as if they were defined in the `mock_provider` block directly. @@ -171,11 +181,9 @@ Overrides can be used with both real and mocked providers and will provide the c ### Overrides Syntax -All override blocks contain a `target` attribute, which should specify the resource, data source, or module to override. The `override_module` blocks contain an `outputs` attribute, while the `override_resource` and `override_data` blocks contain a `values` attribute. In addition, the override blocks support an optional `override_during` block that specifies when the computed values should be generated. - -The `outputs` and `values` attributes are optional and if not specified, Terraform will generate values for them automatically. +All override blocks contain a `target` attribute, which should specify the resource, data source, or module to override. The `override_module` blocks contain an `outputs` attribute, while the `override_resource` and `override_data` blocks contain a `values` attribute. In addition, the override blocks also support the same `override_during` attribute seen within the `mock_provider` block. -The `override_during` attribute requires either a `plan` or `apply` keyword value. If set to `plan`, Terraform will generate values during the `plan` phase and reuse these values during the `apply` phase. If set to `apply` or not specified, Terraform will generate values for override block during the `apply` phase, returning `(known after apply)` for the values during the `plan` phase. +The `outputs` and `values` attributes are optional and if not specified, Terraform will generate values for them automatically. The `override_during` attribute is also optional, and if specified it will override the `override_during` attribute in the `mock_provider` block. If not specified, the behavior will be inherited from the `mock_provider` block. The following example demonstrates the override blocks at various different scopes and levels. The main configuration calls the `./modules/s3_data` module to read a file from an S3 bucket, and then creates a `local_file` from the data returned from the module. From 5179893a01125c87d7a7c5e1bd79da1a180cfe7e Mon Sep 17 00:00:00 2001 From: Liam Cervante Date: Thu, 16 Jan 2025 09:06:09 +0000 Subject: [PATCH 3/3] backport of commit ed10f3049da3cfa75067226eb3c0a89f2b905189 --- website/docs/language/tests/mocking.mdx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/website/docs/language/tests/mocking.mdx b/website/docs/language/tests/mocking.mdx index 6c8679aa536b..6ab6919a6aea 100644 --- a/website/docs/language/tests/mocking.mdx +++ b/website/docs/language/tests/mocking.mdx @@ -105,7 +105,7 @@ Mocked providers only generate data for computed attributes. All required resour An example of this is the `bucket` attribute in the `aws_s3_bucket` resource. A real AWS provider will generate a bucket name if one is not specified. A mocked AWS provider will do the same, and only generate a value if one is not already specified in the configuration. -By default, Terraform will generate data during the `apply` operation while returning `(known after apply)` values during the `plan` operation. This behavior can be overridden with the `override_during` attribute in the `mock_provider` block: +By default, Terraform generates data during the `apply` operation and returns `(known after apply)` values during the `plan` operation. You can override this behavior with the `override_during` attribute in the `mock_provider` block: ```hcl mock_provider "aws" { @@ -113,11 +113,11 @@ mock_provider "aws" { } ``` -The above `aws` provider will generate the data during the `plan` operation and reuse the same data during the `apply` operation. The `override_during` attribute accepts either `plan` or `apply` as values. +The above `aws` provider generates the data during the `plan` operation and reuses the same data during the `apply` operation. The `override_during` attribute accepts either `plan` or `apply` as values. ### Mock Provider data -You can specify specific values for targeted resources and data sources. in a `mock_provider` block, you can write any number of `mock_resource` and `mock_data` blocks. Both the `mock_resource` and `mock_data` blocks accept a type argument that should match the resource or data source you want to provide values for. They also accept a `defaults` object attribute that you can use to specify the values that should be returned for specific attributes. +You can specify specific values for targeted resources and data sources. In a `mock_provider` block, you can write any number of `mock_resource` and `mock_data` blocks. Both the `mock_resource` and `mock_data` blocks accept a type argument that should match the resource or data source you want to provide values for. They also accept a `defaults` object attribute that you can use to specify the values that should be returned for specific attributes. The following example demonstrates providing a set `arn` value for all AWS S3 bucket resources and data sources: @@ -137,7 +137,7 @@ mock_provider "aws" { } ``` -In the above example, Terraform uses the supplied value for `arn` attributes in S3 buckets instead of generating a random string. Computed attributes not provided an explicit default will simply fall back to the generic data generation rules. The `override_during` attribute can also be used in the `mock_resource` and `mock_data` blocks to specify when the values should be generated at an individual resource level. If not specified, the values will be generated using the rules inherited from the `mock_provider` block. If specified, the local value will override any value specified in the `mock_provider` block. +In the above example, Terraform uses the supplied value for `arn` attributes in S3 buckets instead of generating a random string. Computed attributes not provided an explicit default will simply fall back to the generic data generation rules. You can also use the `override_during` attribute in the `mock_resource` and `mock_data` blocks to specify when Terraform should generate the values for an individual resource. If you do not not specify the `override_during` attribute, Terraform generates the values using the rules inherited from the `mock_provider` block. If specified, the local value overrides any value specified in the `mock_provider` block. You can also share mock provider data between tests by writing dedicated mock data files and using the `source` attribute in the `mock_provider` block. Mock data files have `.tfmock.hcl` or `.tfmock.json` extension, and can contain `mock_resource` and `mock_data` blocks as if they were defined in the `mock_provider` block directly. @@ -181,9 +181,9 @@ Overrides can be used with both real and mocked providers and will provide the c ### Overrides Syntax -All override blocks contain a `target` attribute, which should specify the resource, data source, or module to override. The `override_module` blocks contain an `outputs` attribute, while the `override_resource` and `override_data` blocks contain a `values` attribute. In addition, the override blocks also support the same `override_during` attribute seen within the `mock_provider` block. +All override blocks contain a `target` attribute, which should specify the resource, data source, or module to override. The `override_module` blocks contain an `outputs` attribute, while the `override_resource` and `override_data` blocks contain a `values` attribute. The override blocks also support the `override_during` attribute. -The `outputs` and `values` attributes are optional and if not specified, Terraform will generate values for them automatically. The `override_during` attribute is also optional, and if specified it will override the `override_during` attribute in the `mock_provider` block. If not specified, the behavior will be inherited from the `mock_provider` block. +The `outputs` and `values` attributes are optional and if not specified, Terraform will generate values for them automatically. The `override_during` attribute is also optional, and if specified it will override the `override_during` attribute in the `mock_provider` block. If not specified, Terraform inherits the behavior from the `mock_provider` block. The following example demonstrates the override blocks at various different scopes and levels. The main configuration calls the `./modules/s3_data` module to read a file from an S3 bucket, and then creates a `local_file` from the data returned from the module.