Skip to content

Commit 9dee307

Browse files
authored
Merge pull request #831 from hashicorp/td-diags-checks
Cleans up diags checks in tests
2 parents 3362be8 + ebec4cf commit 9dee307

File tree

3 files changed

+65
-80
lines changed

3 files changed

+65
-80
lines changed

aws_config_test.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ func TestEC2MetadataServiceEndpoint(t *testing.T) {
20882088
EnvironmentVariables map[string]string
20892089
SharedConfigurationFile string
20902090
ExpectedEC2MetadataServiceEndpoint string
2091-
ValidateDiags test.DiagsValidator
2091+
ExpectedDiags diag.Diagnostics
20922092
}{
20932093
"no configuration": {
20942094
Config: &Config{
@@ -2126,10 +2126,12 @@ func TestEC2MetadataServiceEndpoint(t *testing.T) {
21262126
"AWS_METADATA_URL": "https://127.0.0.1:1234",
21272127
},
21282128
ExpectedEC2MetadataServiceEndpoint: "https://127.0.0.1:1234",
2129-
ValidateDiags: test.ExpectWarningDiagValidator(diag.NewWarningDiagnostic(
2130-
"Deprecated Environment Variable",
2131-
`The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`,
2132-
)),
2129+
ExpectedDiags: diag.Diagnostics{
2130+
diag.NewWarningDiagnostic(
2131+
"Deprecated Environment Variable",
2132+
`The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`,
2133+
),
2134+
},
21332135
},
21342136
"envvar overrides deprecated envvar": {
21352137
Config: &Config{
@@ -2141,11 +2143,13 @@ func TestEC2MetadataServiceEndpoint(t *testing.T) {
21412143
"AWS_EC2_METADATA_SERVICE_ENDPOINT": "https://127.0.0.1:1234",
21422144
},
21432145
ExpectedEC2MetadataServiceEndpoint: "https://127.0.0.1:1234",
2144-
ValidateDiags: test.ExpectWarningDiagValidator(diag.NewWarningDiagnostic(
2145-
"Deprecated Environment Variable",
2146-
`The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`+"\n"+
2147-
`"AWS_EC2_METADATA_SERVICE_ENDPOINT" is set to "https://127.0.0.1:1234" and "AWS_METADATA_URL" is set to "https://127.1.1.1:1111". Ignoring "AWS_METADATA_URL".`,
2148-
)),
2146+
ExpectedDiags: diag.Diagnostics{
2147+
diag.NewWarningDiagnostic(
2148+
"Deprecated Environment Variable",
2149+
`The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`+"\n"+
2150+
`"AWS_EC2_METADATA_SERVICE_ENDPOINT" is set to "https://127.0.0.1:1234" and "AWS_METADATA_URL" is set to "https://127.1.1.1:1111". Ignoring "AWS_METADATA_URL".`,
2151+
),
2152+
},
21492153
},
21502154

21512155
"shared configuration file": {
@@ -2210,20 +2214,18 @@ ec2_metadata_service_endpoint = https://127.1.1.1:1111
22102214
ec2_metadata_service_endpoint = https://127.1.1.1:1111
22112215
`,
22122216
ExpectedEC2MetadataServiceEndpoint: "https://127.0.0.1:1234",
2213-
ValidateDiags: test.ExpectWarningDiagValidator(diag.NewWarningDiagnostic(
2214-
"Deprecated Environment Variable",
2215-
`The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`,
2216-
)),
2217+
ExpectedDiags: diag.Diagnostics{
2218+
diag.NewWarningDiagnostic(
2219+
"Deprecated Environment Variable",
2220+
`The environment variable "AWS_METADATA_URL" is deprecated. Use "AWS_EC2_METADATA_SERVICE_ENDPOINT" instead.`,
2221+
),
2222+
},
22172223
},
22182224
}
22192225

22202226
for testName, testCase := range testCases {
22212227
testCase := testCase
22222228

2223-
if testCase.ValidateDiags == nil {
2224-
testCase.ValidateDiags = test.ExpectNoDiags
2225-
}
2226-
22272229
t.Run(testName, func(t *testing.T) {
22282230
servicemocks.InitSessionTestEnv(t)
22292231

@@ -2253,7 +2255,12 @@ ec2_metadata_service_endpoint = https://127.1.1.1:1111
22532255

22542256
_, awsConfig, diags := GetAwsConfig(context.Background(), testCase.Config)
22552257

2256-
testCase.ValidateDiags(t, diags)
2258+
if diff := cmp.Diff(diags, testCase.ExpectedDiags); diff != "" {
2259+
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
2260+
}
2261+
if diags.HasError() {
2262+
return
2263+
}
22572264

22582265
ec2MetadataServiceEndpoint, _, err := awsconfig.ResolveEC2IMDSEndpointConfig(awsConfig.ConfigSources)
22592266
if err != nil {
@@ -2582,7 +2589,7 @@ func TestAssumeRole(t *testing.T) {
25822589
Config *Config
25832590
SharedConfigurationFile string
25842591
ExpectedCredentialsValue aws.Credentials
2585-
ValidateDiags test.DiagsValidator
2592+
ExpectedDiags diag.Diagnostics
25862593
MockStsEndpoints []*servicemocks.MockEndpoint
25872594
}{
25882595
"config": {
@@ -2682,22 +2689,18 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
26822689
SecretKey: servicemocks.MockStaticSecretKey,
26832690
},
26842691
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
2685-
ValidateDiags: test.ExpectDiagValidator(`"role ARN not set" error`, func(d diag.Diagnostic) bool {
2686-
return d.Equal(diag.NewErrorDiagnostic(
2692+
ExpectedDiags: diag.Diagnostics{
2693+
diag.NewErrorDiagnostic(
26872694
"Cannot assume IAM Role",
26882695
"IAM Role ARN not set",
2689-
))
2690-
}),
2696+
),
2697+
},
26912698
},
26922699
}
26932700

26942701
for testName, testCase := range testCases {
26952702
testCase := testCase
26962703

2697-
if testCase.ValidateDiags == nil {
2698-
testCase.ValidateDiags = test.ExpectNoDiags
2699-
}
2700-
27012704
t.Run(testName, func(t *testing.T) {
27022705
servicemocks.InitSessionTestEnv(t)
27032706

@@ -2735,7 +2738,9 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
27352738

27362739
ctx, awsConfig, diags := GetAwsConfig(context.Background(), testCase.Config)
27372740

2738-
testCase.ValidateDiags(t, diags)
2741+
if diff := cmp.Diff(diags, testCase.ExpectedDiags); diff != "" {
2742+
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
2743+
}
27392744
if diags.HasError() {
27402745
return
27412746
}
@@ -3053,7 +3058,6 @@ web_identity_token_file = no-such-file
30533058
if diff := cmp.Diff(diags, testCase.ExpectedDiags); diff != "" {
30543059
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
30553060
}
3056-
30573061
if diags.HasError() {
30583062
return
30593063
}
@@ -3201,7 +3205,7 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
32013205
expectedPartition string
32023206
expectError bool
32033207
mockStsEndpoints []*servicemocks.MockEndpoint
3204-
ValidateDiags test.DiagsValidator
3208+
ExpectedDiags diag.Diagnostics
32053209
}{
32063210
{
32073211
desc: "StandardProvider_Config",
@@ -3259,10 +3263,6 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
32593263
for _, testCase := range testCases {
32603264
tc := testCase
32613265

3262-
if testCase.ValidateDiags == nil {
3263-
testCase.ValidateDiags = test.ExpectNoDiags
3264-
}
3265-
32663266
t.Run(tc.desc, func(t *testing.T) {
32673267
ts := servicemocks.MockAwsApiServer("STS", tc.mockStsEndpoints)
32683268
defer ts.Close()
@@ -3275,7 +3275,12 @@ func TestGetAwsConfigWithAccountIDAndPartition(t *testing.T) {
32753275

32763276
acctID, part, diags := GetAwsAccountIDAndPartition(ctx, awsConfig, tc.config)
32773277

3278-
testCase.ValidateDiags(t, diags)
3278+
if diff := cmp.Diff(diags, testCase.ExpectedDiags); diff != "" {
3279+
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
3280+
}
3281+
if diags.HasError() {
3282+
return
3283+
}
32793284

32803285
if acctID != tc.expectedAcctID {
32813286
t.Errorf("expected account ID (%s), got: %s", tc.expectedAcctID, acctID)

internal/test/validator.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,6 @@ func ExpectDiagValidator(msg string, dv DiagValidator) DiagsValidator {
4141
}
4242
}
4343

44-
func ExpectWarningDiagValidator(expected diag.Diagnostic) DiagsValidator {
45-
return func(t *testing.T, diags diag.Diagnostics) {
46-
// Check for the correct type of error before checking for single diagnostic
47-
if !expectDiagsContainsDiag(diags, expected) {
48-
t.Fatalf("expected Diagnostic matching %#v, got %#v", expected, diags)
49-
}
50-
51-
expectDiagsCount(t, diags, 1)
52-
}
53-
}
54-
5544
func expectDiagsCount(t *testing.T, diags diag.Diagnostics, c int) {
5645
if l := diags.Count(); l != c {
5746
t.Fatalf("Diagnostics: expected %d element, got %d\n%#v", c, l, diags)
@@ -69,15 +58,6 @@ func expectDiagsContainsErr(diags diag.Diagnostics, ev ErrValidator) bool {
6958
return false
7059
}
7160

72-
func expectDiagsContainsDiag(diags diag.Diagnostics, expected diag.Diagnostic) bool {
73-
for _, d := range diags {
74-
if d.Equal(expected) {
75-
return true
76-
}
77-
}
78-
return false
79-
}
80-
8161
func expectDiagsContainsDiagFunc(diags diag.Diagnostics, dv DiagValidator) bool {
8262
for _, d := range diags {
8363
if dv(d) {

v2/awsv1shim/session_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ func TestAssumeRole(t *testing.T) {
19201920
Config *awsbase.Config
19211921
SharedConfigurationFile string
19221922
ExpectedCredentialsValue credentials.Value
1923-
ValidateDiags test.DiagsValidator
1923+
ExpectedDiags diag.Diagnostics
19241924
MockStsEndpoints []*servicemocks.MockEndpoint
19251925
}{
19261926
"config": {
@@ -2020,22 +2020,18 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
20202020
SecretKey: servicemocks.MockStaticSecretKey,
20212021
},
20222022
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleCredentials,
2023-
ValidateDiags: test.ExpectDiagValidator(`"role ARN not set" error`, func(d diag.Diagnostic) bool {
2024-
return d.Equal(diag.NewErrorDiagnostic(
2023+
ExpectedDiags: diag.Diagnostics{
2024+
diag.NewErrorDiagnostic(
20252025
"Cannot assume IAM Role",
20262026
"IAM Role ARN not set",
2027-
))
2028-
}),
2027+
),
2028+
},
20292029
},
20302030
}
20312031

20322032
for testName, testCase := range testCases {
20332033
testCase := testCase
20342034

2035-
if testCase.ValidateDiags == nil {
2036-
testCase.ValidateDiags = test.ExpectNoDiags
2037-
}
2038-
20392035
t.Run(testName, func(t *testing.T) {
20402036
ctx := test.Context(t)
20412037

@@ -2081,7 +2077,9 @@ aws_secret_access_key = SharedConfigurationSourceSecretKey
20812077

20822078
ctx, awsConfig, diags := awsbase.GetAwsConfig(ctx, testCase.Config)
20832079

2084-
testCase.ValidateDiags(t, diags)
2080+
if diff := cmp.Diff(diags, testCase.ExpectedDiags); diff != "" {
2081+
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
2082+
}
20852083
if diags.HasError() {
20862084
return
20872085
}
@@ -2117,7 +2115,7 @@ func TestAssumeRoleWithWebIdentity(t *testing.T) {
21172115
SharedConfigurationFile string
21182116
SetSharedConfigurationFile bool
21192117
ExpectedCredentialsValue credentials.Value
2120-
ValidateDiags test.DiagsValidator
2118+
ExpectedDiags diag.Diagnostics
21212119
MockStsEndpoints []*servicemocks.MockEndpoint
21222120
}{
21232121
"config with inline token": {
@@ -2297,10 +2295,12 @@ web_identity_token_file = no-such-file
22972295
AssumeRoleWithWebIdentity: &awsbase.AssumeRoleWithWebIdentity{},
22982296
},
22992297
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
2300-
ValidateDiags: test.ExpectWarningDiagValidator(diag.NewErrorDiagnostic(
2301-
"Assume Role With Web Identity",
2302-
"Role ARN was not set",
2303-
)),
2298+
ExpectedDiags: diag.Diagnostics{
2299+
diag.NewErrorDiagnostic(
2300+
"Assume Role With Web Identity",
2301+
"Role ARN was not set",
2302+
),
2303+
},
23042304
},
23052305

23062306
"invalid no token": {
@@ -2310,20 +2310,18 @@ web_identity_token_file = no-such-file
23102310
},
23112311
},
23122312
ExpectedCredentialsValue: mockdata.MockStsAssumeRoleWithWebIdentityCredentials,
2313-
ValidateDiags: test.ExpectWarningDiagValidator(diag.NewErrorDiagnostic(
2314-
"Assume Role With Web Identity",
2315-
"One of WebIdentityToken, WebIdentityTokenFile must be set",
2316-
)),
2313+
ExpectedDiags: diag.Diagnostics{
2314+
diag.NewErrorDiagnostic(
2315+
"Assume Role With Web Identity",
2316+
"One of WebIdentityToken, WebIdentityTokenFile must be set",
2317+
),
2318+
},
23172319
},
23182320
}
23192321

23202322
for testName, testCase := range testCases {
23212323
testCase := testCase
23222324

2323-
if testCase.ValidateDiags == nil {
2324-
testCase.ValidateDiags = test.ExpectNoDiags
2325-
}
2326-
23272325
t.Run(testName, func(t *testing.T) {
23282326
ctx := test.Context(t)
23292327

@@ -2410,7 +2408,9 @@ web_identity_token_file = no-such-file
24102408

24112409
ctx, awsConfig, diags := awsbase.GetAwsConfig(ctx, testCase.Config)
24122410

2413-
testCase.ValidateDiags(t, diags)
2411+
if diff := cmp.Diff(diags, testCase.ExpectedDiags); diff != "" {
2412+
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
2413+
}
24142414
if diags.HasError() {
24152415
return
24162416
}

0 commit comments

Comments
 (0)