Skip to content

Commit 9136be3

Browse files
committed
test(aws): fix lint — switch mock tests to aws_test package
- Move all six new mock test files from `package aws` to `package aws_test` to match the project convention (testpackage linter) - Alias imports following the existing pattern: SDK as `awsSDK`, terratest helpers as `aws` - Add blank lines before return statements / after if-blocks to satisfy wsl_v5 and goimports No test coverage or behavior changed; all 25 subtests still pass.
1 parent 10e20fa commit 9136be3

6 files changed

Lines changed: 105 additions & 61 deletions

File tree

modules/aws/acm_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package aws
1+
package aws_test
22

33
import (
44
"context"
55
"errors"
66
"testing"
77

8-
"github.com/aws/aws-sdk-go-v2/aws"
8+
awsSDK "github.com/aws/aws-sdk-go-v2/aws"
99
"github.com/aws/aws-sdk-go-v2/service/acm"
1010
"github.com/aws/aws-sdk-go-v2/service/acm/types"
1111
"github.com/stretchr/testify/require"
12+
13+
aws "github.com/gruntwork-io/terratest/modules/aws"
1214
)
1315

14-
// mockAcmClient is a test double for AcmAPI that returns canned responses.
16+
// mockAcmClient is a test double for aws.AcmAPI that returns canned responses.
1517
type mockAcmClient struct {
1618
ListCertificatesOutput *acm.ListCertificatesOutput
1719
ListCertificatesErr error
@@ -21,6 +23,7 @@ func (m *mockAcmClient) ListCertificates(_ context.Context, _ *acm.ListCertifica
2123
if m.ListCertificatesErr != nil {
2224
return nil, m.ListCertificatesErr
2325
}
26+
2427
return m.ListCertificatesOutput, nil
2528
}
2629

@@ -36,8 +39,8 @@ func TestGetAcmCertificateArnWithClientContextE(t *testing.T) {
3639

3740
twoCerts := &acm.ListCertificatesOutput{
3841
CertificateSummaryList: []types.CertificateSummary{
39-
{DomainName: aws.String(domain1), CertificateArn: aws.String(arn1)},
40-
{DomainName: aws.String(domain2), CertificateArn: aws.String(arn2)},
42+
{DomainName: awsSDK.String(domain1), CertificateArn: awsSDK.String(arn1)},
43+
{DomainName: awsSDK.String(domain2), CertificateArn: awsSDK.String(arn2)},
4144
},
4245
}
4346

@@ -78,14 +81,15 @@ func TestGetAcmCertificateArnWithClientContextE(t *testing.T) {
7881
t.Run(name, func(t *testing.T) {
7982
t.Parallel()
8083

81-
arn, err := GetAcmCertificateArnWithClientContextE(t, context.Background(), tc.client, tc.query)
84+
arn, err := aws.GetAcmCertificateArnWithClientContextE(t, context.Background(), tc.client, tc.query)
8285
if tc.expectErr {
8386
require.Error(t, err)
87+
8488
return
8589
}
90+
8691
require.NoError(t, err)
8792
require.Equal(t, tc.expectedArn, arn)
8893
})
8994
}
9095
}
91-

modules/aws/cloudwatch_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package aws
1+
package aws_test
22

33
import (
44
"context"
55
"errors"
66
"testing"
77

8-
"github.com/aws/aws-sdk-go-v2/aws"
8+
awsSDK "github.com/aws/aws-sdk-go-v2/aws"
99
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
1010
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
1111
"github.com/stretchr/testify/require"
12+
13+
aws "github.com/gruntwork-io/terratest/modules/aws"
1214
)
1315

14-
// mockCloudWatchLogsClient is a test double for CloudWatchLogsAPI that returns canned responses.
16+
// mockCloudWatchLogsClient is a test double for aws.CloudWatchLogsAPI that returns canned responses.
1517
type mockCloudWatchLogsClient struct {
1618
GetLogEventsOutput *cloudwatchlogs.GetLogEventsOutput
1719
GetLogEventsErr error
@@ -21,6 +23,7 @@ func (m *mockCloudWatchLogsClient) GetLogEvents(_ context.Context, _ *cloudwatch
2123
if m.GetLogEventsErr != nil {
2224
return nil, m.GetLogEventsErr
2325
}
26+
2427
return m.GetLogEventsOutput, nil
2528
}
2629

@@ -36,9 +39,9 @@ func TestGetCloudWatchLogEntriesWithClientContextE(t *testing.T) {
3639
client: &mockCloudWatchLogsClient{
3740
GetLogEventsOutput: &cloudwatchlogs.GetLogEventsOutput{
3841
Events: []types.OutputLogEvent{
39-
{Message: aws.String("first line")},
40-
{Message: aws.String("second line")},
41-
{Message: aws.String("third line")},
42+
{Message: awsSDK.String("first line")},
43+
{Message: awsSDK.String("second line")},
44+
{Message: awsSDK.String("third line")},
4245
},
4346
},
4447
},
@@ -60,11 +63,13 @@ func TestGetCloudWatchLogEntriesWithClientContextE(t *testing.T) {
6063
t.Run(name, func(t *testing.T) {
6164
t.Parallel()
6265

63-
got, err := GetCloudWatchLogEntriesWithClientContextE(t, context.Background(), tc.client, "stream", "group")
66+
got, err := aws.GetCloudWatchLogEntriesWithClientContextE(t, context.Background(), tc.client, "stream", "group")
6467
if tc.wantErr {
6568
require.Error(t, err)
69+
6670
return
6771
}
72+
6873
require.NoError(t, err)
6974
require.Equal(t, tc.expected, got)
7075
})

modules/aws/dynamodb_test.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package aws
1+
package aws_test
22

33
import (
44
"context"
55
"errors"
66
"testing"
77

8-
"github.com/aws/aws-sdk-go-v2/aws"
8+
awsSDK "github.com/aws/aws-sdk-go-v2/aws"
99
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
1010
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
1111
"github.com/stretchr/testify/require"
12+
13+
aws "github.com/gruntwork-io/terratest/modules/aws"
1214
)
1315

14-
// mockDynamoDBClient is a test double for DynamoDBAPI.
16+
// mockDynamoDBClient is a test double for aws.DynamoDBAPI.
1517
type mockDynamoDBClient struct {
1618
DescribeTableOutput *dynamodb.DescribeTableOutput
1719
DescribeTableErr error
@@ -24,25 +26,30 @@ type mockDynamoDBClient struct {
2426
}
2527

2628
func (m *mockDynamoDBClient) DescribeTable(_ context.Context, params *dynamodb.DescribeTableInput, _ ...func(*dynamodb.Options)) (*dynamodb.DescribeTableOutput, error) {
27-
m.lastDescribeTableName = aws.ToString(params.TableName)
29+
m.lastDescribeTableName = awsSDK.ToString(params.TableName)
30+
2831
if m.DescribeTableErr != nil {
2932
return nil, m.DescribeTableErr
3033
}
34+
3135
return m.DescribeTableOutput, nil
3236
}
3337

3438
func (m *mockDynamoDBClient) DescribeTimeToLive(_ context.Context, _ *dynamodb.DescribeTimeToLiveInput, _ ...func(*dynamodb.Options)) (*dynamodb.DescribeTimeToLiveOutput, error) {
3539
if m.DescribeTimeToLiveErr != nil {
3640
return nil, m.DescribeTimeToLiveErr
3741
}
42+
3843
return m.DescribeTimeToLiveOutput, nil
3944
}
4045

4146
func (m *mockDynamoDBClient) ListTagsOfResource(_ context.Context, params *dynamodb.ListTagsOfResourceInput, _ ...func(*dynamodb.Options)) (*dynamodb.ListTagsOfResourceOutput, error) {
42-
m.lastListTagsResourceArn = aws.ToString(params.ResourceArn)
47+
m.lastListTagsResourceArn = awsSDK.ToString(params.ResourceArn)
48+
4349
if m.ListTagsOfResourceErr != nil {
4450
return nil, m.ListTagsOfResourceErr
4551
}
52+
4653
return m.ListTagsOfResourceOutput, nil
4754
}
4855

@@ -60,22 +67,24 @@ func TestGetDynamoDBTableWithClientContextE(t *testing.T) {
6067
client := &mockDynamoDBClient{
6168
DescribeTableOutput: &dynamodb.DescribeTableOutput{
6269
Table: &types.TableDescription{
63-
TableArn: aws.String(testTableArn),
64-
TableName: aws.String(testTableName),
70+
TableArn: awsSDK.String(testTableArn),
71+
TableName: awsSDK.String(testTableName),
6572
},
6673
},
6774
}
68-
got, err := GetDynamoDBTableWithClientContextE(t, context.Background(), client, testTableName)
75+
76+
got, err := aws.GetDynamoDBTableWithClientContextE(t, context.Background(), client, testTableName)
6977
require.NoError(t, err)
70-
require.Equal(t, testTableArn, aws.ToString(got.TableArn))
78+
require.Equal(t, testTableArn, awsSDK.ToString(got.TableArn))
7179
require.Equal(t, testTableName, client.lastDescribeTableName)
7280
})
7381

7482
t.Run("propagates api error", func(t *testing.T) {
7583
t.Parallel()
7684

7785
client := &mockDynamoDBClient{DescribeTableErr: errors.New("ResourceNotFoundException")}
78-
_, err := GetDynamoDBTableWithClientContextE(t, context.Background(), client, testTableName)
86+
87+
_, err := aws.GetDynamoDBTableWithClientContextE(t, context.Background(), client, testTableName)
7988
require.Error(t, err)
8089
})
8190
}
@@ -90,21 +99,23 @@ func TestGetDynamoDBTableTimeToLiveWithClientContextE(t *testing.T) {
9099
DescribeTimeToLiveOutput: &dynamodb.DescribeTimeToLiveOutput{
91100
TimeToLiveDescription: &types.TimeToLiveDescription{
92101
TimeToLiveStatus: types.TimeToLiveStatusEnabled,
93-
AttributeName: aws.String("expiresAt"),
102+
AttributeName: awsSDK.String("expiresAt"),
94103
},
95104
},
96105
}
97-
got, err := GetDynamoDBTableTimeToLiveWithClientContextE(t, context.Background(), client, testTableName)
106+
107+
got, err := aws.GetDynamoDBTableTimeToLiveWithClientContextE(t, context.Background(), client, testTableName)
98108
require.NoError(t, err)
99109
require.Equal(t, types.TimeToLiveStatusEnabled, got.TimeToLiveStatus)
100-
require.Equal(t, "expiresAt", aws.ToString(got.AttributeName))
110+
require.Equal(t, "expiresAt", awsSDK.ToString(got.AttributeName))
101111
})
102112

103113
t.Run("propagates api error", func(t *testing.T) {
104114
t.Parallel()
105115

106116
client := &mockDynamoDBClient{DescribeTimeToLiveErr: errors.New("InternalServerError")}
107-
_, err := GetDynamoDBTableTimeToLiveWithClientContextE(t, context.Background(), client, testTableName)
117+
118+
_, err := aws.GetDynamoDBTableTimeToLiveWithClientContextE(t, context.Background(), client, testTableName)
108119
require.Error(t, err)
109120
})
110121
}
@@ -114,8 +125,8 @@ func TestGetDynamoDBTableTagsWithClientContextE(t *testing.T) {
114125

115126
describeOK := &dynamodb.DescribeTableOutput{
116127
Table: &types.TableDescription{
117-
TableArn: aws.String(testTableArn),
118-
TableName: aws.String(testTableName),
128+
TableArn: awsSDK.String(testTableArn),
129+
TableName: awsSDK.String(testTableName),
119130
},
120131
}
121132

@@ -126,12 +137,13 @@ func TestGetDynamoDBTableTagsWithClientContextE(t *testing.T) {
126137
DescribeTableOutput: describeOK,
127138
ListTagsOfResourceOutput: &dynamodb.ListTagsOfResourceOutput{
128139
Tags: []types.Tag{
129-
{Key: aws.String("env"), Value: aws.String("prod")},
130-
{Key: aws.String("team"), Value: aws.String("platform")},
140+
{Key: awsSDK.String("env"), Value: awsSDK.String("prod")},
141+
{Key: awsSDK.String("team"), Value: awsSDK.String("platform")},
131142
},
132143
},
133144
}
134-
got, err := GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
145+
146+
got, err := aws.GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
135147
require.NoError(t, err)
136148
require.Len(t, got, 2)
137149
require.Equal(t, testTableArn, client.lastListTagsResourceArn)
@@ -144,7 +156,8 @@ func TestGetDynamoDBTableTagsWithClientContextE(t *testing.T) {
144156
DescribeTableOutput: describeOK,
145157
ListTagsOfResourceOutput: &dynamodb.ListTagsOfResourceOutput{},
146158
}
147-
got, err := GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
159+
160+
got, err := aws.GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
148161
require.NoError(t, err)
149162
require.Empty(t, got)
150163
})
@@ -153,7 +166,8 @@ func TestGetDynamoDBTableTagsWithClientContextE(t *testing.T) {
153166
t.Parallel()
154167

155168
client := &mockDynamoDBClient{DescribeTableErr: errors.New("ResourceNotFoundException")}
156-
_, err := GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
169+
170+
_, err := aws.GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
157171
require.Error(t, err)
158172
require.Empty(t, client.lastListTagsResourceArn, "ListTagsOfResource must not be called when describe fails")
159173
})
@@ -165,7 +179,8 @@ func TestGetDynamoDBTableTagsWithClientContextE(t *testing.T) {
165179
DescribeTableOutput: describeOK,
166180
ListTagsOfResourceErr: errors.New("AccessDeniedException"),
167181
}
168-
_, err := GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
182+
183+
_, err := aws.GetDynamoDBTableTagsWithClientContextE(t, context.Background(), client, testTableName)
169184
require.Error(t, err)
170185
})
171186
}

modules/aws/ebs_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
package aws
1+
package aws_test
22

33
import (
44
"context"
55
"errors"
66
"testing"
77

8-
"github.com/aws/aws-sdk-go-v2/aws"
8+
awsSDK "github.com/aws/aws-sdk-go-v2/aws"
99
"github.com/aws/aws-sdk-go-v2/service/ec2"
1010
"github.com/stretchr/testify/require"
11+
12+
aws "github.com/gruntwork-io/terratest/modules/aws"
1113
)
1214

13-
// mockEbsClient is a test double for EbsAPI that captures the snapshot ID passed to DeleteSnapshot
14-
// and returns a canned error.
15+
// mockEbsClient is a test double for aws.EbsAPI that captures the snapshot ID passed to
16+
// DeleteSnapshot and returns a canned error.
1517
type mockEbsClient struct {
1618
DeleteSnapshotErr error
1719
lastSnapshotID string
@@ -20,11 +22,12 @@ type mockEbsClient struct {
2022

2123
func (m *mockEbsClient) DeleteSnapshot(_ context.Context, params *ec2.DeleteSnapshotInput, _ ...func(*ec2.Options)) (*ec2.DeleteSnapshotOutput, error) {
2224
m.callCount++
23-
m.lastSnapshotID = aws.ToString(params.SnapshotId)
25+
m.lastSnapshotID = awsSDK.ToString(params.SnapshotId)
2426

2527
if m.DeleteSnapshotErr != nil {
2628
return nil, m.DeleteSnapshotErr
2729
}
30+
2831
return &ec2.DeleteSnapshotOutput{}, nil
2932
}
3033

@@ -35,7 +38,8 @@ func TestDeleteEbsSnapshotWithClientContextE(t *testing.T) {
3538
t.Parallel()
3639

3740
client := &mockEbsClient{}
38-
err := DeleteEbsSnapshotWithClientContextE(t, context.Background(), client, "snap-0123456789abcdef0")
41+
42+
err := aws.DeleteEbsSnapshotWithClientContextE(t, context.Background(), client, "snap-0123456789abcdef0")
3943
require.NoError(t, err)
4044
require.Equal(t, 1, client.callCount)
4145
require.Equal(t, "snap-0123456789abcdef0", client.lastSnapshotID)
@@ -45,7 +49,8 @@ func TestDeleteEbsSnapshotWithClientContextE(t *testing.T) {
4549
t.Parallel()
4650

4751
client := &mockEbsClient{DeleteSnapshotErr: errors.New("InvalidSnapshot.NotFound")}
48-
err := DeleteEbsSnapshotWithClientContextE(t, context.Background(), client, "snap-missing")
52+
53+
err := aws.DeleteEbsSnapshotWithClientContextE(t, context.Background(), client, "snap-missing")
4954
require.Error(t, err)
5055
})
5156
}

0 commit comments

Comments
 (0)