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

resource.TestCheckNoResourceAttr() fails even though the resource is supposed to be deleted #357

Closed
kvaidas opened this issue Jun 26, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@kvaidas
Copy link

kvaidas commented Jun 26, 2024

terraform-plugin-testing version

github.com/hashicorp/terraform-plugin-testing v1.8.0

Relevant provider source code

provider code

Terraform Configuration Files

In the test code, linked above.

Expected Behavior

Test should succeed since the delete resource API call is being sent.
Running this provider using Terraform (as a user) works as expected, resource in the state file also is deleted.

Actual Behavior

Fails with the following error:

resource_test.go:109: Error running post-test destroy, there may be dangling resources: Check 1/1 error: httpclient_resource.test-resource: Attribute 'variables.name' found when not expected

Steps to Reproduce

run TF_ACC=1 go test -v ./...

References

None

@kvaidas kvaidas added the bug Something isn't working label Jun 26, 2024
@austinvalle
Copy link
Member

austinvalle commented Jul 18, 2024

Hey @kvaidas 👋🏻, thanks for providing that example and sorry you're running into trouble here.

The CheckDestroy function is useful for a provider to test that the resource doesn't exist in the remote system, not Terraform state. Since the state value for a resource after destroy will always be null (you don't really need to test this, since Terraform core handles this logic), CheckDestroy passes the last state value prior to the destroy command being run. So your test is actually running on an old version of state, checking that the attribute variables.name was null before the resource was actually destroyed.

Usually this old state in CheckDestroy is used to extract an ID and check the remote system to make sure the resource was fully deleted. Here's an example with the AWS tag resource:

With that being said, I believe CheckDestroy is operating as intended, so I'm going to close this issue. Let me know if I missed anything!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants