hashicorp / terraform-plugin-testing

Module for testing Terraform providers
Mozilla Public License 2.0
45 stars 11 forks source link

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

Closed kvaidas closed 2 months ago

kvaidas commented 3 months ago

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

austinvalle commented 2 months ago

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!