Skip to content

Commit b8164dd

Browse files
committed
Migrate internal callers off deprecated function wrappers
Rewrite every in-repo call site (production, tests, examples) from the deprecated non-context wrappers to their Context variants, inserting context.Background() to preserve behavior. The deprecated wrappers are left in place so this change is behavior-preserving and builds green on its own; the wrappers themselves are removed in a follow-up. Done with an AST codemod that derives each transform from the wrapper body, so argument mapping (including pointer and variadic forms) is exact.
1 parent d4b02e4 commit b8164dd

89 files changed

Lines changed: 2169 additions & 968 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/aws/account.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func GetAccountId(t testing.TestingT) string {
7474
//
7575
//nolint:staticcheck,revive // preserving deprecated function name
7676
func GetAccountIdE(t testing.TestingT) (string, error) {
77-
return GetAccountIDE(t)
77+
return GetAccountIDContextE(t, context.Background())
78+
7879
}
7980

8081
// ExtractAccountIDFromARN extracts the AWS account ID from an IAM ARN.

modules/aws/ami.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ func GetMostRecentAmiId(t testing.TestingT, region string, ownerId string, filte
226226
//
227227
//nolint:staticcheck,revive // preserving deprecated function name
228228
func GetMostRecentAmiIdE(t testing.TestingT, region string, ownerId string, filters map[string][]string) (string, error) {
229-
return GetMostRecentAmiIDE(t, region, ownerId, filters)
229+
return GetMostRecentAmiIDContextE(t, context.Background(),
230+
231+
region, ownerId, filters)
230232
}
231233

232234
// Image sorting code borrowed from: https://github.com/hashicorp/packer/blob/7f4112ba229309cfc0ebaa10ded2abdfaf1b22c8/builder/amazon/common/step_source_ami_info.go

modules/aws/ec2-files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func FetchFilesFromInstanceContextE(t testing.TestingT, ctx context.Context, aws
286286
}
287287

288288
//nolint:staticcheck,contextcheck // ScpDirFromE has no Context variant yet
289-
return ssh.ScpDirFromE(t, scpOptions, useSudo)
289+
return ssh.SCPDirFromContextE(t, context.Background(), &scpOptions, useSudo)
290290
}
291291

292292
// FetchFilesFromInstanceContext looks up the EC2 Instances in the given ASG, looks up the public IPs of those EC2

modules/aws/ec2.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func GetPrivateIPOfEc2InstanceContextE(t testing.TestingT, ctx context.Context,
2222
ip, containsIP := ips[instanceID]
2323

2424
if !containsIP {
25-
return "", IpForEc2InstanceNotFound{InstanceId: instanceID, AwsRegion: awsRegion, Type: "private"}
25+
return "", IPForEc2InstanceNotFound{InstanceId: instanceID, AwsRegion: awsRegion, Type: "private"}
2626
}
2727

2828
return ip, nil
@@ -71,7 +71,9 @@ func GetPrivateIpOfEc2Instance(t testing.TestingT, instanceID string, awsRegion
7171
//
7272
//nolint:staticcheck,revive // preserving deprecated function name
7373
func GetPrivateIpOfEc2InstanceE(t testing.TestingT, instanceID string, awsRegion string) (string, error) {
74-
return GetPrivateIPOfEc2InstanceE(t, instanceID, awsRegion)
74+
return GetPrivateIPOfEc2InstanceContextE(t, context.Background(),
75+
76+
instanceID, awsRegion)
7577
}
7678

7779
// GetPrivateIpsOfEc2InstancesContextE gets the private IP address of the given EC2 Instance in the given region. Returns a map of instance ID to IP address.
@@ -196,7 +198,7 @@ func GetPublicIPOfEc2InstanceContextE(t testing.TestingT, ctx context.Context, i
196198
ip, containsIP := ips[instanceID]
197199

198200
if !containsIP {
199-
return "", IpForEc2InstanceNotFound{InstanceId: instanceID, AwsRegion: awsRegion, Type: "public"}
201+
return "", IPForEc2InstanceNotFound{InstanceId: instanceID, AwsRegion: awsRegion, Type: "public"}
200202
}
201203

202204
return ip, nil
@@ -245,7 +247,9 @@ func GetPublicIpOfEc2Instance(t testing.TestingT, instanceID string, awsRegion s
245247
//
246248
//nolint:staticcheck,revive // preserving deprecated function name
247249
func GetPublicIpOfEc2InstanceE(t testing.TestingT, instanceID string, awsRegion string) (string, error) {
248-
return GetPublicIPOfEc2InstanceE(t, instanceID, awsRegion)
250+
return GetPublicIPOfEc2InstanceContextE(t, context.Background(),
251+
252+
instanceID, awsRegion)
249253
}
250254

251255
// GetPublicIpsOfEc2InstancesContextE gets the public IP address of the given EC2 Instance in the given region. Returns a map of instance ID to IP address.

modules/aws/ec2_test.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aws_test
22

33
import (
4+
"context"
45
"strings"
56
"testing"
67

@@ -17,7 +18,23 @@ func TestGetEc2InstanceIdsByTag(t *testing.T) {
1718
t.Parallel()
1819

1920
region := aws.GetRandomStableRegion(t, nil, nil)
20-
ids, err := aws.GetEc2InstanceIdsByTagE(t, region, "Name", "nonexistent-"+random.UniqueID())
21+
ids, err := aws.GetEc2InstanceIdsByTagContextE(t,
22+
23+
// The following is necessary to make sure testCase's values don't get updated due to concurrency within the
24+
// scope of t.Run(..) below. https://golang.org/doc/faq#closures_and_goroutines
25+
26+
// We could hard-code the expected result (e.g., as of July 2020, we expect eu-west-1 to return t2.micro
27+
// and ap-northeast-2 to return t3.micro), but the result will likely change over time, so to avoid a
28+
// brittle test, we simply check that we get _one_ result. Combined with the unit test below, this hopefully
29+
// is enough to be confident this function works correctly.
30+
31+
// The following is necessary to make sure testCase's values don't get updated due to concurrency within the
32+
// scope of t.Run(..) below. https://golang.org/doc/faq#closures_and_goroutines
33+
34+
// The following is necessary to make sure testCase's values don't
35+
// get updated due to concurrency within the scope of t.Run(..) below
36+
37+
context.Background(), region, "Name", "nonexistent-"+random.UniqueID())
2138
require.NoError(t, err)
2239
assert.Empty(t, ids)
2340
}
@@ -31,7 +48,9 @@ func TestGetEc2InstanceIdsByFilters(t *testing.T) {
3148
"tag:Name": {"nonexistent-" + random.UniqueID()},
3249
}
3350

34-
ids, err := aws.GetEc2InstanceIdsByFiltersE(t, region, filters)
51+
ids, err := aws.GetEc2InstanceIdsByFiltersContextE(t,
52+
53+
context.Background(), region, filters)
3554
require.NoError(t, err)
3655
assert.Empty(t, ids)
3756
}
@@ -49,17 +68,13 @@ func TestGetRecommendedInstanceType(t *testing.T) {
4968
}
5069

5170
for _, testCase := range testCases {
52-
// The following is necessary to make sure testCase's values don't get updated due to concurrency within the
53-
// scope of t.Run(..) below. https://golang.org/doc/faq#closures_and_goroutines
71+
5472
testCase := testCase
5573

5674
t.Run(testCase.region+"-"+strings.Join(testCase.instanceTypeOptions, "-"), func(t *testing.T) {
5775
t.Parallel()
5876
instanceType := aws.GetRecommendedInstanceType(t, testCase.region, testCase.instanceTypeOptions)
59-
// We could hard-code the expected result (e.g., as of July 2020, we expect eu-west-1 to return t2.micro
60-
// and ap-northeast-2 to return t3.micro), but the result will likely change over time, so to avoid a
61-
// brittle test, we simply check that we get _one_ result. Combined with the unit test below, this hopefully
62-
// is enough to be confident this function works correctly.
77+
6378
assert.Contains(t, testCase.instanceTypeOptions, instanceType)
6479
})
6580
}
@@ -120,8 +135,7 @@ func TestPickRecommendedInstanceTypeHappyPath(t *testing.T) {
120135
}
121136

122137
for _, testCase := range testCases {
123-
// The following is necessary to make sure testCase's values don't get updated due to concurrency within the
124-
// scope of t.Run(..) below. https://golang.org/doc/faq#closures_and_goroutines
138+
125139
testCase := testCase
126140

127141
t.Run(testCase.name, func(t *testing.T) {
@@ -176,8 +190,7 @@ func TestPickRecommendedInstanceTypeErrors(t *testing.T) {
176190
}
177191

178192
for _, testCase := range testCases {
179-
// The following is necessary to make sure testCase's values don't
180-
// get updated due to concurrency within the scope of t.Run(..) below
193+
181194
testCase := testCase
182195
t.Run(testCase.name, func(t *testing.T) {
183196
t.Parallel()

modules/aws/ecr_test.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aws_test
22

33
import (
4+
"context"
45
"strings"
56
"testing"
67

@@ -18,14 +19,18 @@ func TestEcrRepo(t *testing.T) {
1819
region := aws.GetRandomStableRegion(t, nil, nil)
1920
ecrRepoName := "terratest" + strings.ToLower(random.UniqueID())
2021

21-
repo1, err := aws.CreateECRRepoE(t, region, ecrRepoName)
22+
repo1, err := aws.CreateECRRepoContextE(t,
23+
24+
context.Background(), region, ecrRepoName)
2225
defer aws.DeleteECRRepo(t, region, repo1)
2326

2427
require.NoError(t, err)
2528

2629
assert.Equal(t, ecrRepoName, awsSDK.ToString(repo1.RepositoryName))
2730

28-
repo2, err := aws.GetECRRepoE(t, region, ecrRepoName)
31+
repo2, err := aws.GetECRRepoContextE(t,
32+
33+
context.Background(), region, ecrRepoName)
2934
require.NoError(t, err)
3035
assert.Equal(t, ecrRepoName, awsSDK.ToString(repo2.RepositoryName))
3136
}
@@ -36,14 +41,18 @@ func TestGetEcrRepoLifecyclePolicyError(t *testing.T) {
3641
region := aws.GetRandomStableRegion(t, nil, nil)
3742
ecrRepoName := "terratest" + strings.ToLower(random.UniqueID())
3843

39-
repo1, err := aws.CreateECRRepoE(t, region, ecrRepoName)
44+
repo1, err := aws.CreateECRRepoContextE(t,
45+
46+
context.Background(), region, ecrRepoName)
4047
defer aws.DeleteECRRepo(t, region, repo1)
4148

4249
require.NoError(t, err)
4350

4451
assert.Equal(t, ecrRepoName, awsSDK.ToString(repo1.RepositoryName))
4552

46-
_, err = aws.GetECRRepoLifecyclePolicyE(t, region, repo1)
53+
_, err = aws.GetECRRepoLifecyclePolicyContextE(t,
54+
55+
context.Background(), region, repo1)
4756
require.Error(t, err)
4857
}
4958

@@ -53,7 +62,8 @@ func TestCanSetECRRepoLifecyclePolicyWithSingleRule(t *testing.T) {
5362
region := aws.GetRandomStableRegion(t, nil, nil)
5463
ecrRepoName := "terratest" + strings.ToLower(random.UniqueID())
5564

56-
repo1, err := aws.CreateECRRepoE(t, region, ecrRepoName)
65+
repo1, err := aws.CreateECRRepoContextE(t,
66+
context.Background(), region, ecrRepoName)
5767
defer aws.DeleteECRRepo(t, region, repo1)
5868

5969
require.NoError(t, err)
@@ -76,7 +86,9 @@ func TestCanSetECRRepoLifecyclePolicyWithSingleRule(t *testing.T) {
7686
]
7787
}`
7888

79-
err = aws.PutECRRepoLifecyclePolicyE(t, region, repo1, lifecyclePolicy)
89+
err = aws.PutECRRepoLifecyclePolicyContextE(t,
90+
91+
context.Background(), region, repo1, lifecyclePolicy)
8092
require.NoError(t, err)
8193

8294
policy := aws.GetECRRepoLifecyclePolicy(t, region, repo1)
@@ -89,7 +101,9 @@ func TestCanSetRepositoryPolicyWithSimplePolicy(t *testing.T) {
89101
region := aws.GetRandomStableRegion(t, nil, nil)
90102
ecrRepoName := "terratest" + strings.ToLower(random.UniqueID())
91103

92-
repo, err := aws.CreateECRRepoE(t, region, ecrRepoName)
104+
repo, err := aws.CreateECRRepoContextE(t, context.Background(),
105+
106+
region, ecrRepoName)
93107
defer aws.DeleteECRRepo(t, region, repo)
94108

95109
require.NoError(t, err)
@@ -109,7 +123,9 @@ func TestCanSetRepositoryPolicyWithSimplePolicy(t *testing.T) {
109123
]
110124
}`
111125

112-
err = aws.PutECRRepoPolicyE(t, region, repo, repositoryPolicy)
126+
err = aws.PutECRRepoPolicyContextE(t,
127+
128+
context.Background(), region, repo, repositoryPolicy)
113129
require.NoError(t, err)
114130

115131
policy := aws.GetECRRepoPolicy(t, region, repo)

modules/aws/ecs_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ func TestEcsCluster(t *testing.T) {
1919

2020
region := aws.GetRandomStableRegion(t, nil, nil)
2121

22-
c1, err := aws.CreateEcsClusterE(t, region, "terratest")
22+
c1, err := aws.CreateEcsClusterContextE(t,
23+
24+
context.Background(), region, "terratest")
2325
defer aws.DeleteEcsCluster(t, region, c1)
2426

2527
require.NoError(t, err)
2628
assert.Equal(t, "terratest", *c1.ClusterName)
2729

28-
c2, err := aws.GetEcsClusterE(t, region, *c1.ClusterName)
30+
c2, err := aws.GetEcsClusterContextE(t,
31+
32+
context.Background(), region, *c1.ClusterName)
2933

3034
require.NoError(t, err)
3135
assert.Equal(t, "terratest", *c2.ClusterName)
@@ -52,14 +56,18 @@ func TestEcsClusterWithInclude(t *testing.T) {
5256

5357
assert.Equal(t, clusterName, awsSDK.ToString(c1.Cluster.ClusterName))
5458

55-
c2, err := aws.GetEcsClusterWithIncludeE(t, region, clusterName, []types.ClusterField{types.ClusterFieldTags})
59+
c2, err := aws.GetEcsClusterWithIncludeContextE(t,
60+
61+
context.Background(), region, clusterName, []types.ClusterField{types.ClusterFieldTags})
5662
require.NoError(t, err)
5763

5864
assert.Equal(t, clusterName, awsSDK.ToString(c2.ClusterName))
5965
assert.Equal(t, tags, c2.Tags)
6066
assert.Empty(t, c2.Statistics)
6167

62-
c3, err := aws.GetEcsClusterWithIncludeE(t, region, clusterName, []types.ClusterField{types.ClusterFieldStatistics})
68+
c3, err := aws.GetEcsClusterWithIncludeContextE(t,
69+
70+
context.Background(), region, clusterName, []types.ClusterField{types.ClusterFieldStatistics})
6371
require.NoError(t, err)
6472

6573
assert.Equal(t, clusterName, awsSDK.ToString(c3.ClusterName))

modules/aws/iam_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func TestGetIAMPolicyDocument(t *testing.T) {
3535
t.Run("Exists", func(t *testing.T) {
3636
t.Parallel()
3737

38-
iamClient, err := aws.NewIamClientE(t, region)
38+
iamClient, err := aws.NewIamClientContextE(t,
39+
40+
context.Background(), region)
3941
require.NoError(t, err)
4042

4143
policyDocument := `{
@@ -74,7 +76,9 @@ func TestGetIAMPolicyDocument(t *testing.T) {
7476
t.Run("DoesNotExist", func(t *testing.T) {
7577
t.Parallel()
7678

77-
_, err := aws.GetIamPolicyDocumentE(t, region, "arn:aws:iam::1234567890:policy/does-not-exist")
79+
_, err := aws.GetIamPolicyDocumentContextE(t,
80+
81+
context.Background(), region, "arn:aws:iam::1234567890:policy/does-not-exist")
7882
require.Error(t, err)
7983
})
8084
}

modules/aws/rds_test.go

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

33
import (
4+
"context"
45
"testing"
56

67
aws "github.com/gruntwork-io/terratest/modules/aws/v2"
@@ -61,7 +62,9 @@ func TestGetRecommendedRdsInstanceTypeHappyPath(t *testing.T) {
6162
t.Run(scenerio.name, func(t *testing.T) {
6263
t.Parallel()
6364
engineVersion := aws.GetValidEngineVersion(t, scenerio.region, scenerio.databaseEngine, scenerio.engineMajorVersion)
64-
actual, err := aws.GetRecommendedRdsInstanceTypeE(t, scenerio.region, scenerio.databaseEngine, engineVersion, scenerio.instanceTypes)
65+
actual, err := aws.GetRecommendedRdsInstanceTypeContextE(t,
66+
67+
context.Background(), scenerio.region, scenerio.databaseEngine, engineVersion, scenerio.instanceTypes)
6568
require.NoError(t, err)
6669
assert.Equal(t, scenerio.expected, actual)
6770
})
@@ -144,7 +147,9 @@ func TestGetRecommendedRdsInstanceTypeErrors(t *testing.T) {
144147
t.Run(scenerio.name, func(t *testing.T) {
145148
t.Parallel()
146149

147-
_, err := aws.GetRecommendedRdsInstanceTypeE(t, scenerio.region, scenerio.databaseEngine, scenerio.databaseEngineVersion, scenerio.instanceTypes)
150+
_, err := aws.GetRecommendedRdsInstanceTypeContextE(t,
151+
152+
context.Background(), scenerio.region, scenerio.databaseEngine, scenerio.databaseEngineVersion, scenerio.instanceTypes)
148153
assert.EqualError(t, err, aws.NoRdsInstanceTypeError{InstanceTypeOptions: scenerio.instanceTypes, DatabaseEngine: scenerio.databaseEngine, DatabaseEngineVersion: scenerio.databaseEngineVersion}.Error())
149154
})
150155
}

modules/aws/region_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aws_test
22

33
import (
4+
"context"
45
"testing"
56

67
aws "github.com/gruntwork-io/terratest/modules/aws/v2"
@@ -64,7 +65,9 @@ func TestGetRandomRegionForService(t *testing.T) {
6465

6566
serviceName := "apigatewayv2"
6667

67-
regionsForService, _ := aws.GetRegionsForServiceE(t, serviceName)
68+
regionsForService, _ := aws.GetRegionsForServiceContextE(t,
69+
70+
context.Background(), serviceName)
6871
randomRegionForService := aws.GetRandomRegionForService(t, serviceName)
6972

7073
assert.Contains(t, regionsForService, randomRegionForService)

0 commit comments

Comments
 (0)