Terraform test framework refreshing data sources during teardown

I’m using Terraform version 1.6.3. I’ve made some test and I use a module to perform data source lookup of resources. Then I refer to those data sources in my assert blocks. Test pass. But then during teardown I see errors. But the errors look like they are due to the data sources performing a refresh on teardown. Have a look here for the full test run.

Full details: https://app.circleci.com/pipelines/github/kohirens/aws-tf-s3-backend/35/workflows/eeea6e7a-c724-49e2-a01b-36fcd2228346/jobs/31

Please overlook the fact that I can use the outputs of run.execute. But this module is very simple and easy to illustrate the issue I’m running into. I have a more complex module that I’m running into the same issue.

Hi @b01, unfortunately this is a known bug relating to the order in which Terraform test destroys the objects created during test executions. For more information you can see here: Terraform Test: helper module data sources returns error while tearing down · Issue #34280 · hashicorp/terraform · GitHub.

This will be fixed as part of the upcoming v1.7 release, but the pattern you’ve used here will only work for some providers in the v1.6 series.

I appreciate that the docs currently list this exact example, but we’re waiting for the next release of the docs page which will include updates to reflect this known limitation.

One potential workaround would be to modify your tests to assert against the state directly in the execute run block. The resources you create might be exposing the same information you receive when the data blocks load.

OK. That is good to know. Yes, I have updated the test to assert directly against the state. The lesson for me here was to NOT write test when you’re sleepy.