Terraform test using mock but want data source to generate data

I’m starting to try to use a mocked provider in a test where I had been not mocking the aws provider. However, part of what I need to test is that an aws_iam_policy_document data source is generated properly. When my code references data.aws_iam_policy_document.foo.json in the test, it gets a random string.

I’m not sure how I can get that to do what I want it to. This is a test for a child module that has providers passed into it, so I can’t just set the provider for the aws_iam_policy_document data source to not be the mocked one. overriding the json attribute would defeat the purpose.

Hi @grimm26, unfortunately this isn’t currently supported. There is an open feature request in the Github issues for this, if you’d like to track progress towards implementation: Terraform Test Mocks: Add ability to omit targeted resources from mocking · Issue #36189 · hashicorp/terraform · GitHub.

One option would be to push the data source you want to test into it’s own module, with defined inputs and outputs, and then you could write tests directly around that data source to verify the json output is as expected.

Another option you have, that isn’t ideal, is to leave the provider itself unmocked but then manually use override_resource to override all the other resources that you don’t want the provider to handle during this particular test.

Thanks!