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

Enable TF_SCHEMA_PANIC_ON_ERROR Only During Acceptance Testing #451

Closed
bflad opened this issue May 13, 2020 · 4 comments · Fixed by #462
Closed

Enable TF_SCHEMA_PANIC_ON_ERROR Only During Acceptance Testing #451

bflad opened this issue May 13, 2020 · 4 comments · Fixed by #462
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@bflad
Copy link
Contributor

bflad commented May 13, 2020

SDK version

v2.0.0-rc.1

Use-cases

For a long time, the acceptance testing framework has accepted the TF_SCHEMA_PANIC_ON_ERROR environment variable as a way to tease out state setting and drift detection issues in resources where the (*helper/schema.ResourceData).Set() call is not error checked, e.g.

func ExampleThingRead(d *schema.ResourceData, meta interface{}) error {
  // ...
  d.Set("attr", someInvalidValue)
  // ...
}

In the upcoming SDK v2, the value has been flipped on by default, which is great for catching provider bugs, but unfortunately at the potential expense of very poor user experience. When a resource panics, it makes the resource practically unusable (since this affects refresh) and potentially can leave the user's Terraform state in an errant state (depending on how Terraform CLI handles the panic). Considering that it can be fairly easy for larger providers to miss failing acceptance testing during merges and before releases, this can cause a lot of issues to be submitted and an immediate need to cut a bug fix release. Asking users to run with TF_SCHEMA_PANIC_ON_ERROR disabled could be problematic in its own right.

Proposal

Consider guarding the TF_SCHEMA_PANIC_ON_ERROR behavior to only be enabled by default in the acceptance testing framework. For example, acceptance testing today requires TF_ACC=1 for helper/resource.Test() / helper/resource.ParallelTest() so that could potentially be used.

References

@bflad bflad added the enhancement New feature or request label May 13, 2020
@appilon appilon added this to the v2.0.0 milestone May 13, 2020
@bflad
Copy link
Contributor Author

bflad commented May 13, 2020

I wonder, instead of panicking at all, if maybe the acceptance testing framework could instead capture this issue, allow the testing to continue as normal, then report it at the end of the TestStep as an error?

--- FAIL: TestAccAWSLightsailInstance_basic (43.56s)
    testing.go:630: Error setting state for resource attribute during testing.
        These errors prevent Terraform from saving computed attributes and
        properly performing drift detection on configured values.

        Error: 1 error(s) found:
          * resource: aws_lightsail_instance: ram_size: '' expected type 'int', got unconvertible type 'string'

Panics will likely leave dangling testing resources.

@paultyng
Copy link
Contributor

I think panic is the correct behavior for now, but I do think adding recover at least to the testing framework is a reasonable follow-on issue. I'll write it up, as it could give a bit better test runs for unrelated tests.

@paultyng
Copy link
Contributor

Oh sorry, I misunderstood what you were saying. I think the thing is we would need some hook down in ResourceData.Set that understood this was a test and could call the appropriate output method. I don't think ResourceData has access to the current context.Context, but that may be something to explore.

@appilon appilon self-assigned this May 26, 2020
bflad added a commit to hashicorp/terraform-provider-aws that referenced this issue May 26, 2020
Reference: #9184
Reference: #9190
Reference: #9189
Reference: #13439
Reference: hashicorp/terraform-plugin-sdk#344
Reference: hashicorp/terraform-plugin-sdk#451

The `result_map` attribute was created as a workaround for Terraform 0.11 and earlier not having native JSON decoding abilities. This attribute came with the caveat that it only worked when the JSON was only a flat map of strings, where we otherwise ignore the `(helper/schema.ResourceData).Set()` error and instead return a warning log. In Terraform Plugin SDK v2, this error will now panic in the acceptance testing as these errors are generally indicative of resource bugs, which is unavoidable in this case.

Practitioners should instead opt for the Terraform 0.12 and later `jsondecode()` function against the `result` attribute, which can handle much more arbitrary JSON structures. This has been documented in the `aws_lambda_invocation` data source for awhile now. For JSON structures not compliant with Terraform's JSON decoding, the `result` attribute's string value can still be passed to other processing logic, such as calling the `jq` tool.
bflad added a commit to hashicorp/terraform-provider-aws that referenced this issue May 26, 2020
…3492)

Reference: #9184
Reference: #9190
Reference: #9189
Reference: #13439
Reference: hashicorp/terraform-plugin-sdk#344
Reference: hashicorp/terraform-plugin-sdk#451

The `result_map` attribute was created as a workaround for Terraform 0.11 and earlier not having native JSON decoding abilities. This attribute came with the caveat that it only worked when the JSON was only a flat map of strings, where we otherwise ignore the `(helper/schema.ResourceData).Set()` error and instead return a warning log. In Terraform Plugin SDK v2, this error will now panic in the acceptance testing as these errors are generally indicative of resource bugs, which is unavoidable in this case.

Practitioners should instead opt for the Terraform 0.12 and later `jsondecode()` function against the `result` attribute, which can handle much more arbitrary JSON structures. This has been documented in the `aws_lambda_invocation` data source for awhile now. For JSON structures not compliant with Terraform's JSON decoding, the `result` attribute's string value can still be passed to other processing logic, such as calling the `jq` tool.
@ghost
Copy link

ghost commented Jun 27, 2020

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.

@ghost ghost locked and limited conversation to collaborators Jun 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants