Terraform
Acceptance Tests: Query mode
Terraform query is used to search for resources within a given scope and is part of the workflow that enables practitioners to bulk import existing infrastructure into Terraform. For reference information about the query workflow: Terraform CLI Framework SDKv2
Query mode is used for testing list resources by executing a real Terraform query, using the supplied
config as the contents of a .tfquery.hcl query file.
Query checks can be performed on the results of the query to validate various things about the results such as the number of results returned, the presence of a specific result, or the values of resource attributes of a result.
Examples
Testing a list resource with terraform query
The following example shows an acceptance test that validates the presence of a specific query result by specifying the expected resource identity object.
func TestAccThing_query(t *testing.T) {
r.ParallelTest(t, r.TestCase{
ProtoV5ProviderFactories: providerFactories,
Steps: []r.TestStep{
{
Config: `
resource "examplecloud_thing" "test1" {}
resource "examplecloud_thing" "test2" {}
resource "examplecloud_thing" "test3" {}
`,
},
{
Config: `
list "examplecloud_thing" "test" {
provider = examplecloud
config {}
}
`,
Query: true,
QueryResultChecks: []querycheck.QueryResultCheck{
querycheck.ExpectLength("examplecloud_thing.test", 1),
querycheck.ExpectResourceDisplayName("examplecloud_thing.test",
queryfilter.ByResourceIdentity(map[string]knownvalue.Check{
"attr_1": knownvalue.StringExact("foo"),
"attr_2": knownvalue.StringExact("bar"),
}), knownvalue.StringExact("foobarthing"))
}
},
},
})
}