Skip to content

Commit b2a5179

Browse files
authored
Merge branch 'main' into james/oss-3364-bump-gcp-sdk
2 parents b51d76d + c6062de commit b2a5179

13 files changed

Lines changed: 163 additions & 134 deletions

File tree

docs/_docs/01_getting-started/testing-terragrunt.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ category: getting-started
55
excerpt: >-
66
Learn how to test Terragrunt configurations with Terratest.
77
tags: ["terragrunt", "testing", "quick-start"]
8-
order: 104
8+
order: 105
99
nav_title: Documentation
1010
nav_title_link: /docs/
1111
---
@@ -27,15 +27,17 @@ For testing a single Terragrunt unit, use the `terraform` package with `Terrafor
2727
func TestTerragruntModule(t *testing.T) {
2828
t.Parallel()
2929

30+
ctx := t.Context()
31+
3032
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
3133
TerraformDir: "../examples/my-module",
3234
TerraformBinary: "terragrunt",
3335
})
3436

35-
defer terraform.Destroy(t, terraformOptions)
36-
terraform.Apply(t, terraformOptions)
37+
defer terraform.DestroyContext(t, ctx, terraformOptions)
38+
terraform.ApplyContext(t, ctx, terraformOptions)
3739

38-
output := terraform.Output(t, terraformOptions, "my_output")
40+
output := terraform.OutputContext(t, ctx, terraformOptions, "my_output")
3941
assert.Equal(t, "expected_value", output)
4042
}
4143
```
@@ -48,17 +50,19 @@ For testing a stack of units with dependencies, use the dedicated `terragrunt` p
4850
func TestStack(t *testing.T) {
4951
t.Parallel()
5052

53+
ctx := t.Context()
54+
5155
testFolder, err := files.CopyTerragruntFolderToTemp("../live/prod", t.Name())
5256
require.NoError(t, err)
5357

5458
options := &terragrunt.Options{
5559
TerragruntDir: testFolder,
5660
}
5761

58-
defer terragrunt.DestroyAll(t, options)
59-
terragrunt.ApplyAll(t, options)
62+
defer terragrunt.DestroyAllContext(t, ctx, options)
63+
terragrunt.ApplyAllContext(t, ctx, options)
6064

61-
exitCode := terragrunt.PlanAllExitCode(t, options)
65+
exitCode := terragrunt.PlanAllExitCodeContext(t, ctx, options)
6266
require.Equal(t, 0, exitCode)
6367
}
6468
```

docs/_docs/02_testing-best-practices/cleanup.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ always cleanup after themselves so you don't leave a bunch of resources lying ar
1818
For example, if your test runs `terraform apply`, you should run `terraform destroy` at the end to clean up:
1919

2020
```go
21+
ctx := t.Context()
22+
2123
// Ensure cleanup always runs
22-
defer terraform.Destroy(t, options)
24+
defer terraform.DestroyContext(t, ctx, options)
2325

2426
// Deploy
25-
terraform.Apply(t, options)
27+
terraform.ApplyContext(t, ctx, options)
2628

2729
// Validate
2830
checkServerWorks(t, options)

docs/_docs/02_testing-best-practices/debugging-interleaved-test-output.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ You can install any binary using one of the following methods:
6969

7070
### Manual installation
7171

72-
To install the binary manually, download the version that matches your platform and place it somewhere on your `PATH`.
73-
For example to install version 0.13.13 of `terratest_log_parser`:
72+
To install the binary manually, download the version that matches your platform and place it somewhere on your `PATH`:
7473

7574
```bash
76-
# This example assumes a linux 64bit machine
75+
# This example assumes a linux 64bit machine and installs the latest release.
7776
# Use curl to download the binary
78-
curl --location --silent --fail --show-error -o terratest_log_parser https://github.com/gruntwork-io/terratest/releases/download/v0.13.13/terratest_log_parser_linux_amd64
77+
curl --location --silent --fail --show-error -o terratest_log_parser https://github.com/gruntwork-io/terratest/releases/latest/download/terratest_log_parser_linux_amd64
7978
# Make the downloaded binary executable
8079
chmod +x terratest_log_parser
8180
# Finally, we place the downloaded binary to a place in the PATH
@@ -89,13 +88,13 @@ install](https://pkg.go.dev/cmd/go#hdr-Compile_and_install_packages_and_dependen
8988
with `go install`, point `go install` to the repo and path where the main code for each relevant command lives. For
9089
example, you can install the terratest log parser binary with:
9190

92-
```
91+
```bash
9392
go install github.com/gruntwork-io/terratest/cmd/terratest_log_parser@latest
9493
```
9594

9695
Similarly, to install `pick-instance-type`, you can run:
9796

98-
```
97+
```bash
9998
go install github.com/gruntwork-io/terratest/cmd/pick-instance-type@latest
10099
```
101100

@@ -105,5 +104,5 @@ You can also use [the gruntwork-installer utility](https://github.com/gruntwork-
105104
binaries, which will do the above steps and automatically select the right binary for your platform:
106105

107106
```bash
108-
gruntwork-install --binary-name 'terratest_log_parser' --repo 'https://github.com/gruntwork-io/terratest' --tag 'v0.13.13'
107+
gruntwork-install --binary-name 'terratest_log_parser' --repo 'https://github.com/gruntwork-io/terratest' --tag 'v1.0.0'
109108
```

docs/_docs/02_testing-best-practices/error-handling.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ nav_title: Documentation
1010
nav_title_link: /docs/
1111
---
1212

13-
Just about every method `foo` in Terratest comes in two versions: `foo` and `fooE` (e.g., `terraform.Apply` and
14-
`terraform.ApplyE`).
13+
Just about every method `foo` in Terratest comes in two versions: `foo` and `fooE` (e.g., `terraform.ApplyContext` and
14+
`terraform.ApplyContextE`).
1515

16-
- `foo`: The base method takes a `t *testing.T` as an argument. If the method hits any errors, it calls `t.Fatal` to
17-
fail the test.
16+
- `foo`: The base method takes a `t *testing.T` and a `context.Context` as arguments. If the method hits any errors,
17+
it calls `t.Fatal` to fail the test.
1818

1919
- `fooE`: Methods that end with the capital letter `E` always return an `error` as the last argument and never call
2020
`t.Fatal` themselves. This allows you to decide how to handle errors.
@@ -23,25 +23,28 @@ You will use the base method name most of the time, as it allows you to keep you
2323
`if err != nil` checks all over the place:
2424

2525
```go
26-
terraform.Init(t, terraformOptions)
27-
terraform.Apply(t, terraformOptions)
28-
url := terraform.Output(t, terraformOptions, "url")
26+
ctx := t.Context()
27+
terraform.InitContext(t, ctx, terraformOptions)
28+
terraform.ApplyContext(t, ctx, terraformOptions)
29+
url := terraform.OutputContext(t, ctx, terraformOptions, "url")
2930
```
3031

31-
In the code above, if `Init`, `Apply`, or `Output` hits an error, the method will call `t.Fatal` and fail the test
32-
immediately, which is typically the behavior you want. However, if you are _expecting_ an error and don't want it to
33-
cause a test failure, use the method name that ends with a capital `E`:
32+
In the code above, if `InitContext`, `ApplyContext`, or `OutputContext` hits an error, the method will call `t.Fatal`
33+
and fail the test immediately, which is typically the behavior you want. However, if you are _expecting_ an error and
34+
don't want it to cause a test failure, use the method name that ends with a capital `E`:
3435

3536
```go
36-
if _, err := terraform.InitE(t, terraformOptions); err != nil {
37+
ctx := t.Context()
38+
39+
if _, err := terraform.InitContextE(t, ctx, terraformOptions); err != nil {
3740
// Do something with err
3841
}
3942

40-
if _, err := terraform.ApplyE(t, terraformOptions); err != nil {
43+
if _, err := terraform.ApplyContextE(t, ctx, terraformOptions); err != nil {
4144
// Do something with err
4245
}
4346

44-
url, err := terraform.OutputE(t, terraformOptions, "url")
47+
url, err := terraform.OutputContextE(t, ctx, terraformOptions, "url")
4548
if err != nil {
4649
// Do something with err
4750
}

docs/_docs/02_testing-best-practices/idempotent.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ A Terraform configuration is idempotent when a second apply results in 0 changes
1515
1. What you define in Terraform is exactly what is being deployed.
1616
1. Detection of bugs in Terraform resources and providers that might affect your configuration.
1717

18-
You can use Terratest's `terraform.ApplyAndIdempotent()` function to both apply your Terraform configuration and test its
19-
idempotency.
18+
You can use Terratest's `terraform.ApplyAndIdempotentContext()` function to both apply your Terraform configuration and
19+
test its idempotency.
2020

2121
```go
22-
terraform.ApplyAndIdempotent(t, terraformOptions)
22+
ctx := t.Context()
23+
24+
defer terraform.DestroyContext(t, ctx, terraformOptions)
25+
terraform.ApplyAndIdempotentContext(t, ctx, terraformOptions)
2326
```
2427

2528
If a second apply of your Terraform configuration results in changes then your test will fail.

docs/_docs/02_testing-best-practices/namespacing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ a unique name to ensure that:
2020
For example, when deploying AWS infrastructure with Terraform, that typically means exposing variables that allow you
2121
to configure auto scaling group names, security group names, IAM role names, and any other names that must be unique.
2222

23-
You can use Terratest's `random.UniqueId()` function to generate identifiers that are short enough to use in resource
23+
You can use Terratest's `random.UniqueID()` function to generate identifiers that are short enough to use in resource
2424
names (just 6 characters) but random enough to make it unlikely that you'll have a conflict.
2525

2626
```go
27-
uniqueId := random.UniqueId()
28-
instanceName := fmt.Sprintf("terratest-http-example-%s", uniqueId)
27+
uniqueID := random.UniqueID()
28+
instanceName := fmt.Sprintf("terratest-http-example-%s", uniqueID)
2929

3030
terraformOptions := &terraform.Options {
3131
TerraformDir: "../examples/terraform-http-example",
@@ -34,5 +34,5 @@ terraformOptions := &terraform.Options {
3434
},
3535
}
3636

37-
terraform.Apply(t, terraformOptions)
37+
terraform.ApplyContext(t, t.Context(), terraformOptions)
3838
```

docs/_docs/02_testing-best-practices/picking-instance-types.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,53 @@ nav_title: Documentation
1010
nav_title_link: /docs/
1111
---
1212

13-
It's common to want to test infrastructure code that deploys [EC2 instances](https://aws.amazon.com/ec2/) into AWS.
13+
It's common to want to test infrastructure code that deploys [EC2 instances](https://aws.amazon.com/ec2/) into AWS.
1414
There are many different [instance types](https://aws.amazon.com/ec2/instance-types/), but not all instance types
15-
are available in all [regions or availability zones
16-
(AZs)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html). For example,
15+
are available in all [regions or availability zones
16+
(AZs)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html). For example,
1717
`t3.micro` is sometimes available only in newer AZs, while `t2.micro` is sometimes only available in older AZs. If you
1818
are testing code that needs to deploy a "small" instance across many regions, this can make it tricky to know which
1919
region to pick.
2020

2121
To help work around this problem, Terratest includes:
2222

23-
1. [`GetRecommendedInstanceType`](#getrecommendedinstancetype): A Go function that helps you pick a recommended instance type.
23+
1. [`GetRecommendedInstanceTypeContext`](#getrecommendedinstancetypecontext): A Go function that helps you pick a recommended instance type.
2424
1. [`pick-instance-type`](#pick-instance-type): A CLI tool that helps you pick a recommended instance type.
2525

2626

2727

2828

29-
## `GetRecommendedInstanceType`
29+
## `GetRecommendedInstanceTypeContext`
3030

31-
`GetRecommendedInstanceType` takes in an AWS region and a list of EC2 instance types and returns the first instance
32-
type in the list that is available in all Availability Zones (AZs) in the given region. If there's no
33-
instance available in all AZs, this function exits with an error.
31+
`GetRecommendedInstanceTypeContext` takes in an AWS region and a list of EC2 instance types and returns the first
32+
instance type in the list that is available in all Availability Zones (AZs) in the given region. If there's no
33+
instance available in all AZs, this function exits with an error.
3434

3535
Example usage:
3636

3737
```go
38-
aws.GetRecommendedInstanceType(t, "eu-west-1", []string{"t2.micro", "t3.micro"})
39-
// As of July, 2020, returns "t2.micro"
38+
ctx := t.Context()
4039

41-
aws.GetRecommendedInstanceType(t, "ap-northeast-2", []string{"t2.micro", "t3.micro"})
42-
// As of July, 2020, returns "t3.micro"
43-
```
40+
aws.GetRecommendedInstanceTypeContext(t, ctx, "eu-west-1", []string{"t2.micro", "t3.micro"})
41+
aws.GetRecommendedInstanceTypeContext(t, ctx, "ap-northeast-2", []string{"t2.micro", "t3.micro"})
42+
```
4443

4544

4645

4746
## `pick-instance-type`
4847

49-
`pick-instance-type` is a CLI tool that you can download from the [Terratest releases
50-
page](https://github.com/gruntwork-io/terratest/releases) (click "Assets" under any release). It takes in an AWS
51-
region and a list of EC2 instance types and prints to `stdout` the first instance type in the list that is available in
48+
`pick-instance-type` is a CLI tool that you can download from the [Terratest releases
49+
page](https://github.com/gruntwork-io/terratest/releases) (click "Assets" under any release). It takes in an AWS
50+
region and a list of EC2 instance types and prints to `stdout` the first instance type in the list that is available in
5251
all Availability Zones (AZs) in the given region. If there's no instance available in all AZs, `pick-instance-type`
5352
exits with an error.
5453

5554
Example usage:
5655

5756
```bash
58-
# Data below is from July, 2020
59-
6057
$ pick-instance-type eu-west-1 t2.micro t3.micro
6158
t2.micro
6259

6360
$ pick-instance-type ap-northeast-2 t2.micro t3.micro
6461
t3.micro
65-
```
62+
```

docs/_docs/04_community/contributing.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ We accept different types of contributions for each of these two types of helper
4747
These are helper functions that integrate with various DevOps tools—e.g., Terraform, Docker, Packer, and
4848
Kubernetes—that you can use to deploy infrastructure in your automated tests. Examples:
4949

50-
* `terraform.InitAndApply`: run `terraform init` and `terraform apply`.
51-
* `packer.BuildArtifacts`: run `packer build`.
52-
* `shell.RunCommandAndGetOutput`: run an arbitrary shell command and return `stdout` and `stderr` as a string.
50+
* `terraform.InitAndApplyContext`: run `terraform init` and `terraform apply`.
51+
* `packer.BuildArtifactsContext`: run `packer build`.
52+
* `shell.RunCommandContextAndGetOutput`: run an arbitrary shell command and return `stdout` and `stderr` as a string.
5353

5454
Here are the guidelines for contributions with external tools:
5555

@@ -66,10 +66,10 @@ Here are the guidelines for contributions with external tools:
6666
These are helper functions for creating, destroying, and validating infrastructure directly via API calls or SDKs.
6767
Examples:
6868

69-
* `http_helper.HttpGetWithRetry`: make an HTTP request, retrying until you get a certain expected response.
70-
* `ssh.CheckSshCommand`: SSH to a server and execute a command.
71-
* `aws.CreateS3Bucket`: create an S3 bucket.
72-
* `aws.GetPrivateIpsOfEc2Instances`: use the AWS APIs to fetch IPs of some EC2 instances.
69+
* `http_helper.HTTPGetWithRetryContext`: make an HTTP request, retrying until you get a certain expected response.
70+
* `ssh.CheckSSHCommandContext`: SSH to a server and execute a command.
71+
* `aws.CreateS3BucketContext`: create an S3 bucket.
72+
* `aws.GetPrivateIpsOfEc2InstancesContext`: use the AWS APIs to fetch IPs of some EC2 instances.
7373

7474
The number of possible such helpers is nearly infinite, so to avoid Terratest becoming a gigantic, sprawling library
7575
we ask that contributions for new infrastructure helpers are limited to:
@@ -81,8 +81,8 @@ we ask that contributions for new infrastructure helpers are limited to:
8181
1. **Complexity**: we ask that you only contribute infrastructure and validation helpers for code that is relatively
8282
complex to do from scratch. For example, a helper that merely wraps an existing function in the AWS or GCP SDK is
8383
not a great choice, as the wrapper isn't contributing much value, but is bloating the Terratest API. On the other
84-
hand, helpers that expose simple APIs for complex logic are great contributions: `ssh.CheckSshCommand` is a great
85-
example of this, as it provides a simple one-line interface for dozens of lines of complicated SSH logic.
84+
hand, helpers that expose simple APIs for complex logic are great contributions: `ssh.CheckSSHCommandContext` is a
85+
great example of this, as it provides a simple one-line interface for dozens of lines of complicated SSH logic.
8686

8787
1. **Popularity**: Terratest should only contain helpers for common use cases that come up again and again in the
8888
course of testing. We don't want to bloat the library with lots of esoteric helpers for rarely used tools, so
@@ -262,7 +262,7 @@ MINOR, and PATCH versions on each release to indicate any incompatibilities.
262262

263263
### Developing For Azure
264264

265-
Azure supports multliple cloud environments. In order to properly register the correct environment for you test code, you need to use the Azure SDK Client Factory.
265+
Azure supports multiple cloud environments. In order to properly register the correct environment for you test code, you need to use the Azure SDK Client Factory.
266266

267267
#### Azure SDK Client Factory
268268

examples/terraform-asg-scp-example/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ When a test fails, it is often important to be able to quickly get to logs and c
2222
You can use terratest to help with this task by specifying `RemoteFileSpecification` structs that describe which files you want to copy from your instances:
2323

2424
```go
25+
ctx := t.Context()
26+
2527
logstashSpec := aws.RemoteFileSpecification{
2628
SshUser:sshUserName,
2729
UseSudo:true,
2830
KeyPair:keyPair,
2931
LocalDestinationDir:filepath.Join("/tmp", "logs", t.Name(), "logstash"),
30-
AsgNames: strings.Split(strings.Replace(terraform.OutputRequired(t, terraformOptions, "logstash_server_asg_names"), "\n", "", -1), ","),
32+
AsgNames: strings.Split(strings.Replace(terraform.OutputRequiredContext(t, ctx, terraformOptions, "logstash_server_asg_names"), "\n", "", -1), ","),
3133
RemotePathToFileFilter: map[string][]string {
3234
"/var/log/logstash":{"*"},
3335
"/etc/logstash/conf.d" : {"*"},
@@ -37,12 +39,12 @@ logstashSpec := aws.RemoteFileSpecification{
3739

3840
Once you've described what files you want, grabbing them from ASGs is simple with:
3941
```go
40-
aws.FetchFilesFromAllAsgsE(t, awsRegion, logstashSpec)
42+
aws.FetchFilesFromAsgsPContextE(t, ctx, awsRegion, &logstashSpec)
4143
```
4244

4345
or directly from EC2 instances with:
4446
```go
45-
aws.FetchFilesFromInstance(t, awsRegion, sshUserName, keyPair, appServerInstanceId, true, appServerConfig, filepath.Join("/tmp", "logs", t.Name(), "app_server"), []string{"*.yml", "caFile", "*.key", "*.pem"})
47+
aws.FetchFilesFromInstanceContext(t, ctx, awsRegion, sshUserName, keyPair, appServerInstanceId, true, appServerConfig, filepath.Join("/tmp", "logs", t.Name(), "app_server"), []string{"*.yml", "caFile", "*.key", "*.pem"})
4648
```
4749

4850
Finally, to put all of this together, in your go test you could do something like:

0 commit comments

Comments
 (0)