How can i create two resources in the same config on an acceptance test?

Hey Everyone! I am writing acceptance tests to a Custom Provider, and this resource in particular needs another resource to be created first in order to be created. I tried writing it like:

func TestAccVodSourceResource(t *testing.T) {
	resource.Test(t, resource.TestCase{
		PreCheck:                 func() { testAccPreCheck(t) },
		ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
		Steps: []resource.TestStep{
			// Create and Read testing
			{
				Config: `
resource "awsmt_source_location" "test_source_location_vod"{
		source_location_name = "source_location_vod"
		http_configuration = {
			hc_base_url = "https://ott-mediatailor-test.s3.eu-central-1.amazonaws.com/"
		}
	}
resource "awsmt_vod_source" "test" {
  http_package_configurations = [{
    path = "/"
    source_group = "default"
    type = "HLS"
  }]
  source_location_name = awsmt_vod_source.test.test_vod_source_name.source_location_name
  vod_source_name = "vod_source_example"
}

data "awsmt_vod_source" "data_test" {
  source_location_name = source_location_vod
  vod_source_name = awsmt_vod_source.test.test_vod_source_name.source_location_name
}

output "vod_source_out" {
  value = data.awsmt_vod_source.data_test
}
`,
				Check: resource.ComposeAggregateTestCheckFunc(
					resource.TestCheckResourceAttr("awsmt_vod_source.test", "source_location_name", "test_source_location_vod"),
					resource.TestCheckResourceAttr("awsmt_vod_source.test", "vod_source_name", "vod_source_example"),
					resource.TestCheckResourceAttr("awsmt_vod_source.test", "http_package_configurations.0.path", "/"),
				),
			},

bur I keep getting the following error:

resource_vod_source_test.go:9: Resource specified by ResourceName couldn’t be found: awsmt_vod_source.testawsmt_source_location.test_source_location_vod

how can I create two resources on the same config step?

Hey there @isadoral :wave: ,

There aren’t any restrictions on the Terraform input for Config (outside of some odd behaviors that could happen, mostly regarding the terraform block), so creating another resource in the same config should be fine.

Looking at the config you provided, it is possible the config is invalid, although I can’t confirm without having the exact provider code you’re testing with. One thing you could do is check your Config by putting the content into a .tf file and running terraform validate on it (you’ll need to initialize it, terraform init).

Hey Austin, thanks for the response! I ended up finding out 2 min ago that the problem was actually with the Import Resource test! Not with the first step (the one in this issue). Still haven’t been able to figure out how to test the importing, but this works now :slight_smile:

1 Like