From 80e21981a24955cfb5bbcae4847b3311f4ae0aa5 Mon Sep 17 00:00:00 2001 From: "alvarez.mauriciotm@gmail.com" Date: Wed, 29 Apr 2026 16:00:59 -0700 Subject: [PATCH 1/2] WIP --- .../terraform/acctest/test_utils.go.tmpl | 45 +++++++++++++++++++ .../list_google_service_account_test.go | 7 ++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/acctest/test_utils.go.tmpl b/mmv1/third_party/terraform/acctest/test_utils.go.tmpl index a0545ac0cb64..ec754b7f1a54 100644 --- a/mmv1/third_party/terraform/acctest/test_utils.go.tmpl +++ b/mmv1/third_party/terraform/acctest/test_utils.go.tmpl @@ -20,6 +20,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" @@ -85,6 +87,49 @@ 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) + } +} + +// Check returns a knownvalue.Check that compares against the +// captured value. Fails if Capture has not run yet. +func (c *ListDisplayName) Check() 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/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go b/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go index c3f8ed099e38..18b45d99be1f 100644 --- a/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go +++ b/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go @@ -22,7 +22,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), @@ -35,6 +35,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"}), ), }, { @@ -46,6 +47,10 @@ 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", + listDisplayName.Check(), + ), }, }, }, From 8044a3faccb9e74b3f757bad3efb58496e2b2ea5 Mon Sep 17 00:00:00 2001 From: "alvarez.mauriciotm@gmail.com" Date: Wed, 29 Apr 2026 16:28:25 -0700 Subject: [PATCH 2/2] move list test utils to test_utils.go.tmpl --- mmv1/third_party/terraform/acctest/test_utils.go.tmpl | 4 ++-- .../resourcemanager/list_google_service_account_test.go | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/mmv1/third_party/terraform/acctest/test_utils.go.tmpl b/mmv1/third_party/terraform/acctest/test_utils.go.tmpl index ec754b7f1a54..d4ecc0102735 100644 --- a/mmv1/third_party/terraform/acctest/test_utils.go.tmpl +++ b/mmv1/third_party/terraform/acctest/test_utils.go.tmpl @@ -115,9 +115,9 @@ func (c *ListDisplayName) Capture(resourceAddr string, attrCandidates []string) } } -// Check returns a knownvalue.Check that compares against the +// CheckValue returns a knownvalue.Check that compares against the // captured value. Fails if Capture has not run yet. -func (c *ListDisplayName) Check() knownvalue.Check { +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") diff --git a/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go b/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go index 18b45d99be1f..f9dca1d8cf7c 100644 --- a/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go +++ b/mmv1/third_party/terraform/services/resourcemanager/list_google_service_account_test.go @@ -7,6 +7,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" @@ -49,7 +50,8 @@ func TestAccServiceAccountListResource_queryIdentity(t *testing.T) { querycheck.ExpectLengthAtLeast("google_service_account.all_in_project", 1), querycheck.ExpectResourceDisplayName( "google_service_account.all_in_project", - listDisplayName.Check(), + queryfilter.ByDisplayName(listDisplayName.CheckValue()), + listDisplayName.CheckValue(), ), }, },