# Acceptance testing: test a data source

**URL:** <https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940>\
**Category:** Plugin Development\
**Created:** [September 29, 2022, 9:59am UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940 "2022-09-29T09:59:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![juandspy](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juandspy/32/31034_2.png) [@juandspy](https://discuss.hashicorp.com/u/juandspy)\
**Post date:** [September 29, 2022, 9:59am UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940/1 "2022-09-29T09:59:23Z")

</div>

Hi. I’m contributing to a plugin and I’d like to test a data source. I’ve already created some tests for resources, but now I’d like to test a data source. How can this be achieved?

---

<div class="post-metadata">

**Author:** ![bflad](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/bflad/32/922_2.png) [@bflad](https://discuss.hashicorp.com/u/bflad)\
**Post date:** [September 29, 2022, 8:27pm UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940/2 "2022-09-29T20:27:56Z")

</div>

Hi @juandspy 👋 Welcome to the HashiCorp Discuss community!

Acceptance testing a data source is very similar to a resource, but it may depend on what type of data source is being tested. If the source of the data is pre-existing (such as part of the API itself or in a pre-populated test environment), then the test can use a Terraform configuration with only the data source definition and checks in the testing framework can prepend “data.” to the first parameter to functions like `resource.TestCheckResourceAttr()`. For example:

```go
func TestAccExampleDataSource(t *testing.T) {
	resource.Test(t, resource.TestCase{
		// ... other fields ...
		Steps: []resource.TestStep{
			// Read testing
			{
				Config: `data "examplecloud_thing" "test" {}`,
				Check: resource.ComposeAggregateTestCheckFunc(
					resource.TestCheckResourceAttr("data.examplecloud_thing.test", "id", "example-id"),
				),
			},
		},
	})
}

```

If the data source requires a resource to be created first, the Terraform configuration can be setup so the resource is created first and then the data source will be read:

```nohighlight
resource "examplecloud_thing" "test" {}

data "examplecloud_thing" "test" {
  id = examplecloud_thing.test.id
}

```

The configuration reference between the resource and data source must be a _computed_ (known after apply) value to ensure the proper ordering on the first apply. Attempting to use `depends_on` with data sources will always trigger plans that show the data source needing to be read during apply, which makes the testing less useful.

If you’re looking for more comprehensive code examples, you can find example data source testing in the [`terraform-plugin-scaffolding-framework` GitHub repository](https://github.com/hashicorp/terraform-provider-scaffolding-framework/blob/main/internal/provider/example_data_source_test.go) or many large-scale providers such as the [AWS provider](https://github.com/hashicorp/terraform-provider-aws/blob/main/internal/service/imagebuilder/component_data_source_test.go).

The documentation for the acceptance testing framework itself can be found at: [Plugin Development - Acceptance Testing | Terraform by HashiCorp](https://www.terraform.io/plugin/sdkv2/testing/acceptance-tests)

Hope this helps!

---

<div class="post-metadata">

**Author:** ![juandspy](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juandspy/32/31034_2.png) [@juandspy](https://discuss.hashicorp.com/u/juandspy)\
**Post date:** [September 30, 2022, 10:54am UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940/3 "2022-09-30T10:54:52Z")

</div>

Hey @bflad , this is everything I needed 🙂

I created a simple test like this for [terraform-provider-rhoas](https://github.com/redhat-developer/terraform-provider-rhoas):

```go
// TestAccRHOASServiceAccount_DataSource checks the rhoas_service_accounts data source behavior
func TestAccRHOASServiceAccount_DataSource(t *testing.T) {
	var serviceAccount saclient.ServiceAccountData
	randomName := fmt.Sprintf("test-%s", randomString(10))
	config := fmt.Sprintf(`
resource "rhoas_service_account" "%[1]s" {
  name = "%[2]s"
}

data "rhoas_service_account" "test" {
	id = rhoas_service_account.%[1]s.id
}`, serviceAccountID, randomName)

	resource.Test(t, resource.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		CheckDestroy: testAccCheckServiceAccountDestroy,
		Steps: []resource.TestStep{
			{
				Config: config,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckServiceAccountExists(kafkaPath, &serviceAccount),
					resource.TestCheckResourceAttr(
						serviceAccountPath, "name", randomName),
					resource.TestCheckResourceAttr(
						"data.rhoas_service_account.test", "name", randomName),
				),
			},
		},
	})
}

```

However, it fails saying:

```auto
...
2022-09-30T12:49:00.469+0200 [WARN] sdk.helper_schema: Previously configured provider being re-configured. This can cause issues in concurrent testing if the configurations are not equal.: tf_rpc=Configure tf_req_id=7b0fa233-5c35-8893-5234-365708badb11 tf_provider_addr=registry.terraform.io/hashicorp/rhoas
2022-09-30T12:49:00.520+0200 [WARN] sdk.helper_schema: Previously configured provider being re-configured. This can cause issues in concurrent testing if the configurations are not equal.: tf_rpc=Configure tf_req_id=e7adee40-e1bc-c549-d88f-a1b7c568493d tf_provider_addr=registry.terraform.io/hashicorp/rhoas
...
2022-09-30T12:42:18.781+0200 [DEBUG] sdk.helper_resource: Creating sdkv2 provider instance: tf_provider_addr=registry.terraform.io/hashicorp/rhoas test_name=TestAccRHOASServiceAccount_DataSource test_step_number=1 test_terraform_path=/usr/bin/terraform test_working_directory=/tmp/plugintest125814261
2022-09-30T12:42:18.781+0200 [DEBUG] sdk.helper_resource: Created sdkv2 provider instance: test_terraform_path=/usr/bin/terraform tf_provider_addr=registry.terraform.io/hashicorp/rhoas test_working_directory=/tmp/plugintest125814261 test_name=TestAccRHOASServiceAccount_DataSource test_step_number=1
2022-09-30T12:42:18.781+0200 [DEBUG] sdk.helper_resource: Starting sdkv2 provider instance server: test_terraform_path=/usr/bin/terraform test_working_directory=/tmp/plugintest125814261 test_name=TestAccRHOASServiceAccount_DataSource test_step_number=1 tf_provider_addr=registry.terraform.io/hashicorp/rhoas
2022-09-30T12:42:18.781+0200 [DEBUG] sdk.helper_resource: Started sdkv2 provider instance server: test_step_number=1 test_terraform_path=/usr/bin/terraform test_working_directory=/tmp/plugintest125814261 test_name=TestAccRHOASServiceAccount_DataSource tf_provider_addr=registry.terraform.io/hashicorp/rhoas
    testing_new.go:62: no "id" found in attributes
...

```

Do you know what this error means? Also, I’m not sure if the warning is also problematic.

I’ve tried applying this exact configuration using `terraform apply` and it worked:

```nohighlight
terraform {
  required_providers {
    rhoas = {
      source = "registry.terraform.io/redhat-developer/rhoas"
      version = "0.1"
    }
  }
}

provider "rhoas" {}

resource "rhoas_service_account" "test" {
  name = "a-random-name"
}

data "rhoas_service_account" "test" {
  id = rhoas_service_account.test.id
}

```

So it looks like the “id” attribute should exist…

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![juandspy](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juandspy/32/31034_2.png) [@juandspy](https://discuss.hashicorp.com/u/juandspy)\
**Post date:** [September 30, 2022, 11:00am UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940/4 "2022-09-30T11:00:31Z")

</div>

Oh, I’ve just found [this link](https://www.terraform.io/plugin/framework/acctests#implement-id-attribute) from the docs asking for the “id” field to be computed. In [my code](https://github.com/redhat-developer/terraform-provider-rhoas/blob/main/rhoas/serviceaccounts/datasource_serviceaccount.go#L31) it is a required attribute instead. Probably that’s generating the issue…

---

<div class="post-metadata">

**Author:** ![juandspy](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/juandspy/32/31034_2.png) [@juandspy](https://discuss.hashicorp.com/u/juandspy)\
**Post date:** [September 30, 2022, 11:09am UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940/5 "2022-09-30T11:09:00Z")

</div>

Yep, it was that 🫢 . Thanks @bflad !

---

<div class="post-metadata">

**Author:** ![bflad](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/bflad/32/922_2.png) [@bflad](https://discuss.hashicorp.com/u/bflad)\
**Post date:** [September 30, 2022, 3:11pm UTC](https://discuss.hashicorp.com/t/acceptance-testing-test-a-data-source/44940/6 "2022-09-30T15:11:38Z")

</div>

Ah, sorry for not also including the framework-specific acceptance testing page link that mentions the `id` attribute requirement! Hopefully we can find an easy way to remove that requirement. Glad you were able to get this sorted and best of luck with your provider and testing journey. 😄 Please reach out if you need anything else.
