Skip to content

Commit 1c09e58

Browse files
authored
Fix diagnostic comparison issues in s3 backend tests (#37509)
* Fix S3 backend test affected by making the Workspaces method return errors via diagnostics * Address diagnostics comparison issues in test by ensuring expected diagnostics are defined in the context of the config they're triggered by * Fix failing test case `TestBackendConfig_EC2MetadataEndpoint/envvar_invalid_mode` by making `diagnosticBase` struct comparable * Add compile-time checks that diagnostic types fulfil interfaces * Stop diagnosticBase implementing ComparableDiagnostic, re-add S3-specific comparer code to s3 package * Update tests to use the S3-specific comparer again * Fix test case missed in refactoring
1 parent e854d83 commit 1c09e58

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

internal/backend/remote-state/s3/backend_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func TestBackendConfig_InvalidRegion(t *testing.T) {
362362
confDiags := b.Configure(configSchema)
363363
diags = diags.Append(confDiags)
364364

365-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
365+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
366366
t.Errorf("unexpected diagnostics difference: %s", diff)
367367
}
368368
})
@@ -528,7 +528,7 @@ func TestBackendConfig_DynamoDBEndpoint(t *testing.T) {
528528
raw, diags := testBackendConfigDiags(t, New(), backend.TestWrapConfig(config))
529529
b := raw.(*Backend)
530530

531-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
531+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
532532
t.Errorf("unexpected diagnostics difference: %s", diff)
533533
}
534534

@@ -616,7 +616,8 @@ func TestBackendConfig_IAMEndpoint(t *testing.T) {
616616
pathString(cty.GetAttrPath("iam_endpoint")),
617617
pathString(cty.GetAttrPath("endpoints").GetAttr("iam")),
618618
),
619-
)},
619+
),
620+
},
620621
},
621622
"envvar": {
622623
vars: map[string]string{
@@ -660,7 +661,7 @@ func TestBackendConfig_IAMEndpoint(t *testing.T) {
660661

661662
_, diags := testBackendConfigDiags(t, New(), backend.TestWrapConfig(config))
662663

663-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
664+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
664665
t.Errorf("unexpected diagnostics difference: %s", diff)
665666
}
666667
})
@@ -736,7 +737,8 @@ func TestBackendConfig_S3Endpoint(t *testing.T) {
736737
pathString(cty.GetAttrPath("endpoint")),
737738
pathString(cty.GetAttrPath("endpoints").GetAttr("s3")),
738739
),
739-
)},
740+
),
741+
},
740742
},
741743
"envvar": {
742744
vars: map[string]string{
@@ -783,7 +785,7 @@ func TestBackendConfig_S3Endpoint(t *testing.T) {
783785
raw, diags := testBackendConfigDiags(t, New(), backend.TestWrapConfig(config))
784786
b := raw.(*Backend)
785787

786-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
788+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
787789
t.Errorf("unexpected diagnostics difference: %s", diff)
788790
}
789791

@@ -943,7 +945,7 @@ func TestBackendConfig_EC2MetadataEndpoint(t *testing.T) {
943945
raw, diags := testBackendConfigDiags(t, New(), backend.TestWrapConfig(config))
944946
b := raw.(*Backend)
945947

946-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
948+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
947949
t.Errorf("unexpected diagnostics difference: %s", diff)
948950
}
949951

@@ -1435,7 +1437,7 @@ func TestBackendConfig_PrepareConfigValidation(t *testing.T) {
14351437

14361438
_, valDiags := b.PrepareConfig(populateSchema(t, b.ConfigSchema(), tc.config))
14371439

1438-
if diff := cmp.Diff(valDiags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
1440+
if diff := cmp.Diff(valDiags, tc.expectedDiags, diagnosticComparer); diff != "" {
14391441
t.Errorf("unexpected diagnostics difference: %s", diff)
14401442
}
14411443
})
@@ -2497,7 +2499,7 @@ func TestBackendConfigKmsKeyId(t *testing.T) {
24972499
diags = diags.Append(confDiags)
24982500
}
24992501

2500-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
2502+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
25012503
t.Fatalf("unexpected diagnostics difference: %s", diff)
25022504
}
25032505

@@ -2627,7 +2629,7 @@ func TestBackendSSECustomerKey(t *testing.T) {
26272629
diags = diags.Append(confDiags)
26282630
}
26292631

2630-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
2632+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
26312633
t.Fatalf("unexpected diagnostics difference: %s", diff)
26322634
}
26332635

@@ -3287,7 +3289,7 @@ func TestAssumeRole_PrepareConfigValidation(t *testing.T) {
32873289
var diags tfdiags.Diagnostics
32883290
validateNestedAttribute(assumeRoleSchema, config, path, &diags)
32893291

3290-
if diff := cmp.Diff(diags, tc.expectedDiags, tfdiags.DiagnosticComparer); diff != "" {
3292+
if diff := cmp.Diff(diags, tc.expectedDiags, diagnosticComparer); diff != "" {
32913293
t.Errorf("unexpected diagnostics difference: %s", diff)
32923294
}
32933295
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package s3
5+
6+
import (
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform/internal/tfdiags"
9+
)
10+
11+
var diagnosticComparer cmp.Option = cmp.Comparer(diagnosticComparerS3)
12+
13+
// diagnosticComparerS3 is a Comparer function for use with cmp.Diff to compare two tfdiags.Diagnostic values
14+
func diagnosticComparerS3(l, r tfdiags.Diagnostic) bool {
15+
if l.Severity() != r.Severity() {
16+
return false
17+
}
18+
if l.Description() != r.Description() {
19+
return false
20+
}
21+
22+
lp := tfdiags.GetAttribute(l)
23+
rp := tfdiags.GetAttribute(r)
24+
if len(lp) != len(rp) {
25+
return false
26+
}
27+
return lp.Equals(rp)
28+
}

internal/tfdiags/contextual.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ func GetAttribute(d Diagnostic) cty.Path {
9999
return nil
100100
}
101101

102+
var _ Diagnostic = &attributeDiagnostic{}
103+
var _ ComparableDiagnostic = &attributeDiagnostic{}
104+
102105
type attributeDiagnostic struct {
103106
diagnosticBase
104107
attrPath cty.Path
@@ -384,6 +387,9 @@ func WholeContainingBody(severity Severity, summary, detail string) Diagnostic {
384387
}
385388
}
386389

390+
var _ Diagnostic = &wholeBodyDiagnostic{}
391+
var _ ComparableDiagnostic = &wholeBodyDiagnostic{}
392+
387393
type wholeBodyDiagnostic struct {
388394
diagnosticBase
389395
subject *SourceRange // populated only after ElaborateFromConfigBody

internal/tfdiags/diagnostic_base.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ type diagnosticBase struct {
1515
address string
1616
}
1717

18+
var _ Diagnostic = &diagnosticBase{}
19+
20+
// diagnosticBase doesn't implement ComparableDiagnostic because the lack of source data
21+
// means separate diagnostics might be falsely identified as equal. This poses a user-facing
22+
// risk if deduplication of diagnostics removes a diagnostic that's incorrectly been identified
23+
// as a duplicate via comparison.
24+
1825
func (d diagnosticBase) Severity() Severity {
1926
return d.severity
2027
}

internal/tfdiags/hcl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type hclDiagnostic struct {
1313
}
1414

1515
var _ Diagnostic = hclDiagnostic{}
16+
var _ ComparableDiagnostic = hclDiagnostic{}
1617

1718
func (d hclDiagnostic) Severity() Severity {
1819
switch d.diag.Severity {

internal/tfdiags/rpc_friendly.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type rpcFriendlyDiag struct {
1515
Context_ *SourceRange
1616
}
1717

18+
var _ Diagnostic = &rpcFriendlyDiag{}
19+
var _ ComparableDiagnostic = &rpcFriendlyDiag{}
20+
1821
// rpcFriendlyDiag transforms a given diagnostic so that is more friendly to
1922
// RPC.
2023
//

0 commit comments

Comments
 (0)