diff --git a/google/acctest/test_utils.go b/google/acctest/test_utils.go index 39a47f7e1f0..b0168acd90f 100644 --- a/google/acctest/test_utils.go +++ b/google/acctest/test_utils.go @@ -35,6 +35,8 @@ import ( "github.com/hashicorp/terraform-plugin-mux/tf5muxserver" "github.com/hashicorp/terraform-plugin-testing/echoprovider" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/knownvalue" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/hashicorp/terraform-provider-google/google/envvar" transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" @@ -100,6 +102,48 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc } } +// List test utils + +// ListDisplayName captures a resource's display-name attribute during a +// create step and exposes a knownvalue.Check that asserts list-query results +// match the captured value. +type ListDisplayName struct { + value string +} + +// Capture returns a TestCheckFunc that copies the first non-empty value +// among attrCandidates from resourceAddr's state into the captured value. +// attrCandidates are checked in order from first index to last. +func (c *ListDisplayName) Capture(resourceAddr string, attrCandidates []string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceAddr] + if !ok { + return fmt.Errorf("resource not found in state: %s", resourceAddr) + } + for _, k := range attrCandidates { + if v, ok := rs.Primary.Attributes[k]; ok && v != "" { + c.value = v + return nil + } + } + return fmt.Errorf("no display name attribute found in state for resource %s; tried %v", resourceAddr, attrCandidates) + } +} + +// CheckValue returns a knownvalue.Check that compares against the +// captured value. Fails if Capture has not run yet. +func (c *ListDisplayName) CheckValue() knownvalue.Check { + return knownvalue.StringFunc(func(v string) error { + if c.value == "" { + return fmt.Errorf("display name was not captured from create step") + } + if v != c.value { + return fmt.Errorf("expected display name %q, got %q", c.value, v) + } + return nil + }) +} + // General test utils // MuxedProviders returns the correct test provider (between the sdk version or the framework version) diff --git a/google/services/resourcemanager/list_google_service_account_test.go b/google/services/resourcemanager/list_google_service_account_test.go index 22411494239..82fa349e10a 100644 --- a/google/services/resourcemanager/list_google_service_account_test.go +++ b/google/services/resourcemanager/list_google_service_account_test.go @@ -23,6 +23,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/knownvalue" "github.com/hashicorp/terraform-plugin-testing/querycheck" + "github.com/hashicorp/terraform-plugin-testing/querycheck/queryfilter" "github.com/hashicorp/terraform-plugin-testing/tfversion" "github.com/hashicorp/terraform-provider-google/google/acctest" @@ -38,7 +39,7 @@ func TestAccServiceAccountListResource_queryIdentity(t *testing.T) { accountId := "a" + acctest.RandString(t, 10) project := envvar.GetTestProjectFromEnv() expectedEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project) - + listDisplayName := acctest.ListDisplayName{} acctest.VcrTest(t, resource.TestCase{ TerraformVersionChecks: []tfversion.TerraformVersionCheck{ tfversion.SkipBelow(tfversion.Version1_14_0), @@ -51,6 +52,7 @@ func TestAccServiceAccountListResource_queryIdentity(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("google_service_account.acceptance", "email", expectedEmail), resource.TestCheckResourceAttr("google_service_account.acceptance", "project", project), + listDisplayName.Capture("google_service_account.acceptance", []string{"display_name", "email"}), ), }, { @@ -62,6 +64,11 @@ func TestAccServiceAccountListResource_queryIdentity(t *testing.T) { "project": knownvalue.StringExact(project), }), querycheck.ExpectLengthAtLeast("google_service_account.all_in_project", 1), + querycheck.ExpectResourceDisplayName( + "google_service_account.all_in_project", + queryfilter.ByDisplayName(listDisplayName.CheckValue()), + listDisplayName.CheckValue(), + ), }, }, },