Skip to content

Commit d645cad

Browse files
authored
refactor CheckDataSourceStateMatchesResourceStateWithIgnores to use string in place of map (#15357)
1 parent 705d5ff commit d645cad

File tree

34 files changed

+120
-123
lines changed

34 files changed

+120
-123
lines changed

mmv1/templates/terraform/examples/base_configs/datasource_test_file.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ func TestAccDataSource{{ $.Res.ResourceName }}_basic(t *testing.T) {
6666
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
6767
"data.{{ $e.ResourceType $.Res.TerraformName }}.default",
6868
"{{ $e.ResourceType $.Res.TerraformName }}.{{ $e.PrimaryResourceId }}",
69-
map[string]struct{}{
69+
[]string{
7070
{{- range $prop := $.Res.IgnoreReadProperties $e }}
71-
"{{ $prop }}": {},
71+
"{{ $prop }}",
7272
{{- end }}
7373
},
7474
),

mmv1/third_party/terraform/acctest/test_utils.go.tmpl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ import (
2525
)
2626

2727
func CheckDataSourceStateMatchesResourceState(dataSourceName, resourceName string) func(*terraform.State) error {
28-
return CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourceName, map[string]struct{}{})
28+
return CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourceName, []string{})
2929
}
3030

31-
func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourceName string, ignoreFields map[string]struct{}) func(*terraform.State) error {
31+
func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourceName string, fieldsToIgnore []string) func(*terraform.State) error {
3232
return func(s *terraform.State) error {
33+
ignoreFields := make(map[string]struct{}, len(fieldsToIgnore))
34+
for _, field := range fieldsToIgnore {
35+
ignoreFields[field] = struct{}{}
36+
}
37+
3338
ds, ok := s.RootModule().Resources[dataSourceName]
3439
if !ok {
3540
return fmt.Errorf("can't find %s in state", dataSourceName)
@@ -43,15 +48,17 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc
4348
dsAttr := ds.Primary.Attributes
4449
rsAttr := rs.Primary.Attributes
4550

46-
errMsg := ""
51+
var errMsg strings.Builder
52+
4753
// Data sources are often derived from resources, so iterate over the resource fields to
4854
// make sure all fields are accounted for in the data source.
4955
// If a field exists in the data source but not in the resource, its expected value should
5056
// be checked separately.
5157
for k := range rsAttr {
52-
if _, ok := ignoreFields[k]; ok {
58+
if _, ignored := ignoreFields[k]; ignored {
5359
continue
5460
}
61+
5562
if strings.HasPrefix(k, "labels.") || strings.HasPrefix(k, "terraform_labels.") || strings.HasPrefix(k, "effective_labels.") {
5663
continue
5764
}
@@ -60,15 +67,15 @@ func CheckDataSourceStateMatchesResourceStateWithIgnores(dataSourceName, resourc
6067
}
6168
if dsAttr[k] != rsAttr[k] {
6269
// ignore data sources where an empty list is being compared against a null list.
63-
if k[len(k)-1:] == "#" && (dsAttr[k] == "" || dsAttr[k] == "0") && (rsAttr[k] == "" || rsAttr[k] == "0") {
70+
if strings.HasSuffix(k, "#") && (dsAttr[k] == "" || dsAttr[k] == "0") && (rsAttr[k] == "" || rsAttr[k] == "0") {
6471
continue
6572
}
66-
errMsg += fmt.Sprintf("%s is %s; want %s\n", k, dsAttr[k], rsAttr[k])
73+
errMsg.WriteString(fmt.Sprintf("%s is %s; want %s\n", k, dsAttr[k], rsAttr[k]))
6774
}
6875
}
6976

70-
if errMsg != "" {
71-
return errors.New(errMsg)
77+
if errMsg.Len() > 0 {
78+
return errors.New(errMsg.String())
7279
}
7380

7481
return nil

mmv1/third_party/terraform/services/backupdr/data_source_backup_dr_backup_plan_association_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func TestAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(t *testing.T) {
2323
{
2424
Config: testAccDataSourceGoogleBackupDRBackupPlanAssociation_basic(context),
2525
Check: resource.ComposeTestCheckFunc(
26-
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_backup_dr_backup_plan_association.bpa-test", "google_backup_dr_backup_plan_association.bpa", map[string]struct{}{
27-
"resource": {},
26+
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_backup_dr_backup_plan_association.bpa-test", "google_backup_dr_backup_plan_association.bpa", []string{
27+
"resource",
2828
},
2929
),
3030
),

mmv1/third_party/terraform/services/cloudfunctions2/data_source_google_cloudfunctions2_function_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestAccDataSourceGoogleCloudFunctions2Function_basic(t *testing.T) {
2828
// but the "labels" field in resource are user defined labels, which is the reason for the mismatch.
2929
Check: resource.ComposeTestCheckFunc(
3030
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(funcDataNameHttp,
31-
"google_cloudfunctions2_function.function_http_v2", map[string]struct{}{"build_config.0.source.0.storage_source.0.bucket": {}, "build_config.0.source.0.storage_source.0.object": {}, "labels.%": {}, "terraform_labels.%": {}}),
31+
"google_cloudfunctions2_function.function_http_v2", []string{"build_config.0.source.0.storage_source.0.bucket", "build_config.0.source.0.storage_source.0.object", "labels.%", "terraform_labels.%"}),
3232
),
3333
},
3434
},

mmv1/third_party/terraform/services/compute/data_source_google_compute_forwarding_rule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAccDataSourceGoogleForwardingRule(t *testing.T) {
2020
Steps: []resource.TestStep{
2121
{
2222
Config: testAccDataSourceGoogleForwardingRuleConfig(poolName, ruleName),
23-
Check: acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr", map[string]struct{}{"port_range": {}, "target": {}}),
23+
Check: acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_compute_forwarding_rule.my_forwarding_rule", "google_compute_forwarding_rule.foobar-fr", []string{"port_range", "target"}),
2424
},
2525
},
2626
})

mmv1/third_party/terraform/services/compute/data_source_google_compute_instance_template_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ func TestAccInstanceTemplateDatasource_name(t *testing.T) {
1818
{
1919
Config: testAccInstanceTemplate_name(envvar.GetTestProjectFromEnv(), acctest.RandString(t, 10)),
2020
Check: resource.ComposeTestCheckFunc(
21-
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
21+
acctest.CheckDataSourceStateMatchesResourceState(
2222
"data.google_compute_instance_template.default",
2323
"google_compute_instance_template.default",
24-
map[string]struct{}{},
2524
),
2625
),
2726
},
@@ -39,13 +38,11 @@ func TestAccInstanceTemplateDatasource_filter(t *testing.T) {
3938
{
4039
Config: testAccInstanceTemplate_filter(envvar.GetTestProjectFromEnv(), acctest.RandString(t, 10)),
4140
Check: resource.ComposeTestCheckFunc(
42-
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
41+
acctest.CheckDataSourceStateMatchesResourceState(
4342
"data.google_compute_instance_template.default",
4443
"google_compute_instance_template.c",
45-
map[string]struct{}{},
4644
),
47-
),
48-
},
45+
)},
4946
},
5047
})
5148
}
@@ -60,10 +57,9 @@ func TestAccInstanceTemplateDatasource_filter_mostRecent(t *testing.T) {
6057
{
6158
Config: testAccInstanceTemplate_filter_mostRecent(envvar.GetTestProjectFromEnv(), acctest.RandString(t, 10)),
6259
Check: resource.ComposeTestCheckFunc(
63-
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
60+
acctest.CheckDataSourceStateMatchesResourceState(
6461
"data.google_compute_instance_template.default",
6562
"google_compute_instance_template.c",
66-
map[string]struct{}{},
6763
),
6864
),
6965
},
@@ -86,8 +82,8 @@ func TestAccInstanceTemplateDatasource_self_link_unique(t *testing.T) {
8682
"google_compute_instance_template.default",
8783
// we don't compare the id here as we start this test from a self_link_unique url
8884
// and the resource's ID will have the standard format project/projectname/global/instanceTemplates/tf-test-template-random
89-
map[string]struct{}{
90-
"id": {},
85+
[]string{
86+
"id",
9187
},
9288
),
9389
),

mmv1/third_party/terraform/services/compute/data_source_google_compute_region_instance_template_test.go.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestAccRegionInstanceTemplateDatasource_name(t *testing.T) {
2424
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
2525
"data.google_compute_region_instance_template.default",
2626
"google_compute_region_instance_template.default",
27-
map[string]struct{}{},
27+
[]string{},
2828
),
2929
),
3030
},
@@ -45,7 +45,7 @@ func TestAccRegionInstanceTemplateDatasource_filter(t *testing.T) {
4545
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
4646
"data.google_compute_region_instance_template.default",
4747
"google_compute_region_instance_template.c",
48-
map[string]struct{}{},
48+
[]string{},
4949
),
5050
),
5151
},
@@ -66,7 +66,7 @@ func TestAccRegionInstanceTemplateDatasource_filter_mostRecent(t *testing.T) {
6666
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
6767
"data.google_compute_region_instance_template.default",
6868
"google_compute_region_instance_template.c",
69-
map[string]struct{}{},
69+
[]string{},
7070
),
7171
),
7272
},

mmv1/third_party/terraform/services/compute/data_source_google_compute_region_network_endpoint_group_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestAccDataSourceRegionNetworkEndpointGroup_basic(t *testing.T) {
2222
Steps: []resource.TestStep{
2323
{
2424
Config: testAccDataSourceRegionNetworkEndpointGroup_basic(context),
25-
Check: acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_compute_region_network_endpoint_group.cloudrun_neg", "google_compute_region_network_endpoint_group.cloudrun_neg", map[string]struct{}{"name": {}, "region": {}}),
25+
Check: acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_compute_region_network_endpoint_group.cloudrun_neg", "google_compute_region_network_endpoint_group.cloudrun_neg", []string{"name", "region"}),
2626
},
2727
},
2828
})

mmv1/third_party/terraform/services/compute/data_source_google_compute_region_ssl_certificate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func TestAccDataSourceComputeRegionSslCertificate(t *testing.T) {
2121
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
2222
"data.google_compute_region_ssl_certificate.cert",
2323
"google_compute_region_ssl_certificate.foobar",
24-
map[string]struct{}{
25-
"private_key": {},
24+
[]string{
25+
"private_key",
2626
},
2727
),
2828
),

mmv1/third_party/terraform/services/compute/data_source_google_compute_snapshot_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestAccSnapshotDatasource_name(t *testing.T) {
2121
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
2222
"data.google_compute_snapshot.default",
2323
"google_compute_snapshot.default",
24-
map[string]struct{}{"zone": {}},
24+
[]string{"zone"},
2525
),
2626
),
2727
},
@@ -42,7 +42,7 @@ func TestAccSnapshotDatasource_filter(t *testing.T) {
4242
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
4343
"data.google_compute_snapshot.default",
4444
"google_compute_snapshot.c",
45-
map[string]struct{}{"zone": {}},
45+
[]string{"zone"},
4646
),
4747
),
4848
},
@@ -63,7 +63,7 @@ func TestAccSnapshotDatasource_filterMostRecent(t *testing.T) {
6363
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores(
6464
"data.google_compute_snapshot.default",
6565
"google_compute_snapshot.c",
66-
map[string]struct{}{"zone": {}},
66+
[]string{"zone"},
6767
),
6868
),
6969
},

0 commit comments

Comments
 (0)