Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions mmv1/third_party/terraform/acctest/test_utils.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

// 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,7 +23,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),
Expand All @@ -35,6 +36,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"}),
),
},
{
Expand All @@ -46,6 +48,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(),
),
},
},
},
Expand Down
Loading