CheckDestroy Functionality

I have been learning about Terraform Acceptance Testing and I have a question about the CheckDestroy functionality.

I have been using the Hashicups project (https://github.com/hashicorp/terraform-provider-hashicups) as my example and testing. The first item I noticed was that the provider_test.go file was not updated for the v2 sdk. That was an easy fix.

When debugging the test code and entering the resource_order_test.go file, when the testAccCheckHashicupsOrderDestroy test function is called, the state received has no resources available.

According to the documentation, the CheckDestroy method ‘receives the last known Terraform state as input’.

The comment in the code says: 'CheckDestroy is called after the resource is finally destroyed to allow the tester to test that the resource is truly gone’.

The documentation for the CheckDestroy type writes: the ‘TestCheckFunc is the callback type used with acceptance tests to check the state of a resource. The state passed in is the latest state known, or in the case of being after a destroy, it is the last known state when it was created’. TestCheckFunc

According to this, the state being sent to the CheckDestroy function should be the state of the resources when it was created allowing the function logic to test that Terraform actually destroyed the resources. The example code shows that it wants to iterate through the resources and delete them, but at this point, the resources sent in are empty. I viewed other providers (aws, azure, etc…) and their CheckDestroy functions seem to also expect the created resources and then verify that they are no longer valid.

I debugged the sdk code and the ‘runPostTestDestroy’ function in the file ‘https://github.com/hashicorp/terraform-plugin-sdk/blob/master/helper/resource/testing_new.go’, line 18, is the point where the ‘CheckDestroy’ function is called, but the state being sent in is after the destroy. The ‘runNewTest’ method that calls ‘runPostTestDestroy’ is obtaining the ‘statePreDestroy’ on line 57, but it doesn’t send it to the ‘runPostTestDestroy’ where it can be sent to CheckDestroy.

So, either there is an issue with the logic of how the created state is being sent into the ‘CheckDestroy’ method or I am not understanding the functionality of this method. If anyone can review and shed some light on this, I would really appreciate it. Thank you!

1 Like

Hey @enk21, thanks so much for reporting this bug. I saw you also opened a PR, which I really appreciate. Your diagnosis seems right to me, and we’ve merged the PR. I expect it to go out in a release in the near future.