Skip to content

Commit 4f21eb3

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. Deprecated wrappers are left in place so this is behavior-preserving and builds green on its own. An AST codemod derives each transform from the wrapper body (single-statement, Helper-prefixed multi-statement, and variadic forms) and applies it as a text edit to the original call source, so existing arguments, inline comments, and formatting are preserved. Signature-changing wrappers (ssh CheckSSH* handlers, value-to-pointer, logger.Default) were migrated by hand.
1 parent d4b02e4 commit 4f21eb3

100 files changed

Lines changed: 831 additions & 760 deletions

File tree

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func GetAccountIDE(t testing.TestingT) (string, error) {
6565
//
6666
//nolint:staticcheck,revive // preserving deprecated function name
6767
func GetAccountId(t testing.TestingT) string {
68-
return GetAccountID(t)
68+
return GetAccountIDContext(t, context.Background())
6969
}
7070

7171
// GetAccountIdE gets the Account ID for the currently logged in IAM User.
@@ -74,7 +74,7 @@ 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())
7878
}
7979

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

modules/aws/account_test.go

Lines changed: 2 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"
@@ -10,7 +11,7 @@ import (
1011
func TestGetAccountId(t *testing.T) {
1112
t.Parallel()
1213

13-
accountID := aws.GetAccountID(t)
14+
accountID := aws.GetAccountIDContext(t, context.Background())
1415
assert.Regexp(t, "^[0-9]{12}$", accountID)
1516
}
1617

modules/aws/ami.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func GetMostRecentAmiIDE(t testing.TestingT, region string, ownerID string, filt
216216
//
217217
//nolint:staticcheck,revive // preserving deprecated function name
218218
func GetMostRecentAmiId(t testing.TestingT, region string, ownerId string, filters map[string][]string) string {
219-
return GetMostRecentAmiID(t, region, ownerId, filters)
219+
return GetMostRecentAmiIDContext(t, context.Background(), region, ownerId, filters)
220220
}
221221

222222
// GetMostRecentAmiIdE gets the ID of the most recent AMI in the given region that has the given owner and matches
@@ -226,7 +226,7 @@ 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(), region, ownerId, filters)
230230
}
231231

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

modules/aws/ami_test.go

Lines changed: 8 additions & 7 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"
@@ -10,48 +11,48 @@ import (
1011
func TestGetUbuntu1404AmiReturnsSomeAmi(t *testing.T) {
1112
t.Parallel()
1213

13-
amiID := aws.GetUbuntu1404Ami(t, "us-east-1")
14+
amiID := aws.GetUbuntu1404AmiContext(t, context.Background(), "us-east-1")
1415
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
1516
}
1617

1718
func TestGetUbuntu1604AmiReturnsSomeAmi(t *testing.T) {
1819
t.Parallel()
1920

20-
amiID := aws.GetUbuntu1604Ami(t, "us-west-1")
21+
amiID := aws.GetUbuntu1604AmiContext(t, context.Background(), "us-west-1")
2122
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
2223
}
2324

2425
func TestGetUbuntu2004AmiReturnsSomeAmi(t *testing.T) {
2526
t.Parallel()
2627

27-
amiID := aws.GetUbuntu2004Ami(t, "us-west-1")
28+
amiID := aws.GetUbuntu2004AmiContext(t, context.Background(), "us-west-1")
2829
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
2930
}
3031

3132
func TestGetUbuntu2204AmiReturnsSomeAmi(t *testing.T) {
3233
t.Parallel()
3334

34-
amiID := aws.GetUbuntu2204Ami(t, "us-west-1")
35+
amiID := aws.GetUbuntu2204AmiContext(t, context.Background(), "us-west-1")
3536
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
3637
}
3738

3839
func TestGetCentos7AmiReturnsSomeAmi(t *testing.T) {
3940
t.Parallel()
4041

41-
amiID := aws.GetCentos7Ami(t, "eu-west-1")
42+
amiID := aws.GetCentos7AmiContext(t, context.Background(), "eu-west-1")
4243
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
4344
}
4445

4546
func TestGetAmazonLinuxAmiReturnsSomeAmi(t *testing.T) {
4647
t.Parallel()
4748

48-
amiID := aws.GetAmazonLinuxAmi(t, "ap-southeast-1")
49+
amiID := aws.GetAmazonLinuxAmiContext(t, context.Background(), "ap-southeast-1")
4950
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
5051
}
5152

5253
func TestGetEcsOptimizedAmazonLinuxAmiEReturnsSomeAmi(t *testing.T) {
5354
t.Parallel()
5455

55-
amiID := aws.GetEcsOptimizedAmazonLinuxAmi(t, "us-east-2")
56+
amiID := aws.GetEcsOptimizedAmazonLinuxAmiContext(t, context.Background(), "us-east-2")
5657
assert.Regexp(t, "^ami-[[:alnum:]]+$", amiID)
5758
}

modules/aws/asg_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ func TestGetCapacityInfoForAsg(t *testing.T) {
2222

2323
uniqueID := random.UniqueID()
2424
asgName := t.Name() + "-" + uniqueID
25-
region := aws.GetRandomStableRegion(t, []string{}, []string{})
25+
region := aws.GetRandomStableRegionContext(t, context.Background(), []string{}, []string{})
2626

2727
defer deleteAutoScalingGroup(t, asgName, region)
2828

2929
createTestAutoScalingGroup(t, asgName, region, 2)
30-
aws.WaitForCapacity(t, asgName, region, 40, 15*time.Second)
30+
aws.WaitForCapacityContext(t, context.Background(), asgName, region, 40, 15*time.Second)
3131

32-
capacityInfo := aws.GetCapacityInfoForAsg(t, asgName, region)
32+
capacityInfo := aws.GetCapacityInfoForAsgContext(t, context.Background(), asgName, region)
3333
assert.Equal(t, int64(2), capacityInfo.DesiredCapacity)
3434
assert.Equal(t, int64(2), capacityInfo.CurrentCapacity)
3535
assert.Equal(t, int64(1), capacityInfo.MinCapacity)
@@ -41,33 +41,33 @@ func TestGetInstanceIdsForAsg(t *testing.T) {
4141

4242
uniqueID := random.UniqueID()
4343
asgName := t.Name() + "-" + uniqueID
44-
region := aws.GetRandomStableRegion(t, []string{}, []string{})
44+
region := aws.GetRandomStableRegionContext(t, context.Background(), []string{}, []string{})
4545

4646
defer deleteAutoScalingGroup(t, asgName, region)
4747

4848
createTestAutoScalingGroup(t, asgName, region, 1)
49-
aws.WaitForCapacity(t, asgName, region, 40, 15*time.Second)
49+
aws.WaitForCapacityContext(t, context.Background(), asgName, region, 40, 15*time.Second)
5050

51-
instanceIDs := aws.GetInstanceIdsForAsg(t, asgName, region)
51+
instanceIDs := aws.GetInstanceIdsForAsgContext(t, context.Background(), asgName, region)
5252
assert.Len(t, instanceIDs, 1)
5353
}
5454

5555
func createTestAutoScalingGroup(t *testing.T, name string, region string, desiredCount int32) {
5656
t.Helper()
5757

58-
azs := aws.GetAvailabilityZones(t, region)
59-
ec2Client := aws.NewEc2Client(t, region)
60-
imageID := aws.GetAmazonLinuxAmi(t, region)
58+
azs := aws.GetAvailabilityZonesContext(t, context.Background(), region)
59+
ec2Client := aws.NewEc2ClientContext(t, context.Background(), region)
60+
imageID := aws.GetAmazonLinuxAmiContext(t, context.Background(), region)
6161
template, err := ec2Client.CreateLaunchTemplate(context.Background(), &ec2.CreateLaunchTemplateInput{
6262
LaunchTemplateData: &types.RequestLaunchTemplateData{
6363
ImageId: awsSDK.String(imageID),
64-
InstanceType: types.InstanceType(aws.GetRecommendedInstanceType(t, region, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})),
64+
InstanceType: types.InstanceType(aws.GetRecommendedInstanceTypeContext(t, context.Background(), region, []string{"t2.micro, t3.micro", "t2.small", "t3.small"})),
6565
},
6666
LaunchTemplateName: awsSDK.String(name),
6767
})
6868
require.NoError(t, err)
6969

70-
asgClient := aws.NewAsgClient(t, region)
70+
asgClient := aws.NewAsgClientContext(t, context.Background(), region)
7171
param := &autoscaling.CreateAutoScalingGroupInput{
7272
AutoScalingGroupName: &name,
7373
LaunchTemplate: &autoscalingTypes.LaunchTemplateSpecification{
@@ -95,7 +95,7 @@ func deleteAutoScalingGroup(t *testing.T, name string, region string) {
9595
// We have to scale ASG down to 0 before we can delete it
9696
scaleAsgToZero(t, name, region)
9797

98-
asgClient := aws.NewAsgClient(t, region)
98+
asgClient := aws.NewAsgClientContext(t, context.Background(), region)
9999
input := &autoscaling.DeleteAutoScalingGroupInput{AutoScalingGroupName: awsSDK.String(name)}
100100
_, err := asgClient.DeleteAutoScalingGroup(context.Background(), input)
101101
require.NoError(t, err)
@@ -106,7 +106,7 @@ func deleteAutoScalingGroup(t *testing.T, name string, region string) {
106106
}, 40*time.Minute)
107107
require.NoError(t, err)
108108

109-
ec2Client := aws.NewEc2Client(t, region)
109+
ec2Client := aws.NewEc2ClientContext(t, context.Background(), region)
110110
_, err = ec2Client.DeleteLaunchTemplate(context.Background(), &ec2.DeleteLaunchTemplateInput{
111111
LaunchTemplateName: awsSDK.String(name),
112112
})
@@ -116,7 +116,7 @@ func deleteAutoScalingGroup(t *testing.T, name string, region string) {
116116
func scaleAsgToZero(t *testing.T, name string, region string) {
117117
t.Helper()
118118

119-
asgClient := aws.NewAsgClient(t, region)
119+
asgClient := aws.NewAsgClientContext(t, context.Background(), region)
120120
input := &autoscaling.UpdateAutoScalingGroupInput{
121121
AutoScalingGroupName: awsSDK.String(name),
122122
DesiredCapacity: awsSDK.Int32(0),
@@ -125,7 +125,7 @@ func scaleAsgToZero(t *testing.T, name string, region string) {
125125
}
126126
_, err := asgClient.UpdateAutoScalingGroup(context.Background(), input)
127127
require.NoError(t, err)
128-
aws.WaitForCapacity(t, name, region, 40, 15*time.Second)
128+
aws.WaitForCapacityContext(t, context.Background(), name, region, 40, 15*time.Second)
129129

130130
// There is an eventual consistency bug where even though the ASG is scaled down, AWS sometimes still views a
131131
// scaling activity so we add a 5-second pause here to work around it.

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func GetPrivateIPOfEc2InstanceE(t testing.TestingT, instanceID string, awsRegion
6262
//
6363
//nolint:staticcheck,revive // preserving deprecated function name
6464
func GetPrivateIpOfEc2Instance(t testing.TestingT, instanceID string, awsRegion string) string {
65-
return GetPrivateIPOfEc2Instance(t, instanceID, awsRegion)
65+
return GetPrivateIPOfEc2InstanceContext(t, context.Background(), instanceID, awsRegion)
6666
}
6767

6868
// GetPrivateIpOfEc2InstanceE gets the private IP address of the given EC2 Instance in the given region.
@@ -71,7 +71,7 @@ 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(), instanceID, awsRegion)
7575
}
7676

7777
// GetPrivateIpsOfEc2InstancesContextE gets the private IP address of the given EC2 Instance in the given region. Returns a map of instance ID to IP address.
@@ -236,7 +236,7 @@ func GetPublicIPOfEc2InstanceE(t testing.TestingT, instanceID string, awsRegion
236236
//
237237
//nolint:staticcheck,revive // preserving deprecated function name
238238
func GetPublicIpOfEc2Instance(t testing.TestingT, instanceID string, awsRegion string) string {
239-
return GetPublicIPOfEc2Instance(t, instanceID, awsRegion)
239+
return GetPublicIPOfEc2InstanceContext(t, context.Background(), instanceID, awsRegion)
240240
}
241241

242242
// GetPublicIpOfEc2InstanceE gets the public IP address of the given EC2 Instance in the given region.
@@ -245,7 +245,7 @@ func GetPublicIpOfEc2Instance(t testing.TestingT, instanceID string, awsRegion s
245245
//
246246
//nolint:staticcheck,revive // preserving deprecated function name
247247
func GetPublicIpOfEc2InstanceE(t testing.TestingT, instanceID string, awsRegion string) (string, error) {
248-
return GetPublicIPOfEc2InstanceE(t, instanceID, awsRegion)
248+
return GetPublicIPOfEc2InstanceContextE(t, context.Background(), instanceID, awsRegion)
249249
}
250250

251251
// 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: 6 additions & 5 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

@@ -16,22 +17,22 @@ import (
1617
func TestGetEc2InstanceIdsByTag(t *testing.T) {
1718
t.Parallel()
1819

19-
region := aws.GetRandomStableRegion(t, nil, nil)
20-
ids, err := aws.GetEc2InstanceIdsByTagE(t, region, "Name", "nonexistent-"+random.UniqueID())
20+
region := aws.GetRandomStableRegionContext(t, context.Background(), nil, nil)
21+
ids, err := aws.GetEc2InstanceIdsByTagContextE(t, context.Background(), region, "Name", "nonexistent-"+random.UniqueID())
2122
require.NoError(t, err)
2223
assert.Empty(t, ids)
2324
}
2425

2526
func TestGetEc2InstanceIdsByFilters(t *testing.T) {
2627
t.Parallel()
2728

28-
region := aws.GetRandomStableRegion(t, nil, nil)
29+
region := aws.GetRandomStableRegionContext(t, context.Background(), nil, nil)
2930
filters := map[string][]string{
3031
"instance-state-name": {"running", "shutting-down"},
3132
"tag:Name": {"nonexistent-" + random.UniqueID()},
3233
}
3334

34-
ids, err := aws.GetEc2InstanceIdsByFiltersE(t, region, filters)
35+
ids, err := aws.GetEc2InstanceIdsByFiltersContextE(t, context.Background(), region, filters)
3536
require.NoError(t, err)
3637
assert.Empty(t, ids)
3738
}
@@ -55,7 +56,7 @@ func TestGetRecommendedInstanceType(t *testing.T) {
5556

5657
t.Run(testCase.region+"-"+strings.Join(testCase.instanceTypeOptions, "-"), func(t *testing.T) {
5758
t.Parallel()
58-
instanceType := aws.GetRecommendedInstanceType(t, testCase.region, testCase.instanceTypeOptions)
59+
instanceType := aws.GetRecommendedInstanceTypeContext(t, context.Background(), testCase.region, testCase.instanceTypeOptions)
5960
// We could hard-code the expected result (e.g., as of July 2020, we expect eu-west-1 to return t2.micro
6061
// and ap-northeast-2 to return t3.micro), but the result will likely change over time, so to avoid a
6162
// brittle test, we simply check that we get _one_ result. Combined with the unit test below, this hopefully

0 commit comments

Comments
 (0)