Skip to content

Commit 6e44de9

Browse files
committed
Merge remote-tracking branch 'origin/main' into james/terratest-v2-iac
2 parents d4b02e4 + 8d4556f commit 6e44de9

1 file changed

Lines changed: 116 additions & 40 deletions

File tree

docs/_docs/01_getting-started/quick-start.md

Lines changed: 116 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,93 @@ custom_js:
1414

1515
## Requirements
1616

17-
Terratest uses the Go testing framework. To use Terratest, you need to install:
17+
Terratest uses the Go testing framework, and the examples below drive [OpenTofu](https://opentofu.org/). To follow
18+
along, you need:
1819

1920
- [Go](https://golang.org/) (requires version >=1.26)
21+
- [OpenTofu](https://opentofu.org/docs/intro/install/) (or [Terraform](https://www.terraform.io/), if you prefer)
2022

21-
## Setting up your project
23+
Terratest's `terraform` module runs whichever binary you point it at. By default it uses `terraform` when that binary
24+
is on your `PATH` and otherwise falls back to `tofu`, so installing only OpenTofu is enough to get going. To pin a test
25+
to OpenTofu regardless of what else is installed, set `TerraformBinary: "tofu"` in the `terraform.Options` you pass to
26+
the helpers (more on this [below](#preferring-opentofu)).
2227

23-
The easiest way to get started with Terratest is to copy one of the examples and its corresponding tests from this
24-
repo. This quick start section uses a Terraform example, but check out the [Examples]({{site.baseurl}}/examples/) section for other
25-
types of infrastructure code you can test (e.g., Packer, Kubernetes, etc).
28+
The rest of this guide uses [mise](https://mise.jdx.dev/) to manage tool versions. It isn't required (install Go and
29+
OpenTofu however you like), but it keeps everyone on the same versions.
2630

27-
1. Create an `examples` and `test` folder.
31+
## Starting a new project
2832

29-
1. Copy the folder including all the files from the [basic terraform example](https://github.com/gruntwork-io/terratest/tree/{{ site.stable_ref }}/examples/terraform-basic-example/) into the `examples` folder.
33+
If you're adding tests to an existing module, skip to [Setting up your tests](#setting-up-your-tests). Otherwise, here
34+
is how to stand up a fresh Go project from scratch.
3035

31-
1. Copy the [basic terraform example test](https://github.com/gruntwork-io/terratest/blob/{{ site.stable_ref }}/test/terraform_basic_example_test.go) into the `test` folder.
36+
1. Create the project and initialize Git:
3237

33-
1. To configure dependencies, run:
38+
```bash
39+
mkdir my-infra && cd my-infra
40+
git init
41+
```
42+
43+
1. Pin your tools with mise. This writes a `mise.toml` that anyone cloning the repo can use to install the exact same
44+
versions:
45+
46+
```bash
47+
mise use go@1.26
48+
mise use opentofu@1.12.3
49+
```
50+
51+
Run `mise install` to fetch the tools, and prefix commands with `mise x -- <command>` (or run `mise activate` in
52+
your shell) so they resolve to the pinned versions.
53+
54+
1. Lay out the standard Terratest folders. Infrastructure code lives in `examples`, and the Go tests that exercise it
55+
live in `test`:
56+
57+
```bash
58+
mkdir examples test
59+
```
60+
61+
1. Initialize the Go module. Terratest tests live in their own module under `test`:
3462

3563
```bash
3664
cd test
3765
go mod init "<MODULE_NAME>"
38-
go mod tidy
3966
```
4067

4168
Where `<MODULE_NAME>` is the name of your module, typically in the format
4269
`github.com/<YOUR_USERNAME>/<YOUR_REPO_NAME>`.
4370

71+
1. Add a `.gitignore` so you don't commit local state, provider plugins, or build artifacts:
72+
73+
```bash
74+
cat > ../.gitignore <<'EOF'
75+
.terraform/
76+
*.tfstate
77+
*.tfstate.backup
78+
.terraform.lock.hcl
79+
EOF
80+
```
81+
82+
Now add some infrastructure and a test, as described next.
83+
84+
## Setting up your tests
85+
86+
The easiest way to get started with Terratest is to copy one of the examples and its corresponding tests from this
87+
repo. This quick start section uses an OpenTofu/Terraform example, but check out the
88+
[Examples]({{site.baseurl}}/examples/) section for other types of infrastructure code you can test (e.g., Packer,
89+
Kubernetes, etc).
90+
91+
1. Copy the folder including all the files from the [basic example](https://github.com/gruntwork-io/terratest/tree/{{ site.stable_ref }}/examples/terraform-basic-example/) into the `examples` folder.
92+
93+
1. Copy the [basic example test](https://github.com/gruntwork-io/terratest/blob/{{ site.stable_ref }}/test/terraform_basic_example_test.go) into the `test` folder.
94+
95+
1. To configure dependencies, run:
96+
97+
```bash
98+
cd test
99+
go mod tidy
100+
```
101+
102+
(If you skipped the previous section, run `go mod init "<MODULE_NAME>"` first.)
103+
44104
To lock your tests to a specific Terratest release, see [Pinning a Terratest version]({{ site.baseurl }}/docs/getting-started/version-pinning/).
45105
46106
1. To run the tests:
@@ -52,66 +112,82 @@ types of infrastructure code you can test (e.g., Packer, Kubernetes, etc).
52112
53113
*(See [Timeouts and logging]({{ site.baseurl }}/docs/testing-best-practices/timeouts-and-logging/) for why the `-timeout` parameter is used.)*
54114
115+
### Preferring OpenTofu
116+
117+
By default the `terraform` helpers use the `terraform` binary if it's installed and fall back to `tofu` otherwise. To
118+
run OpenTofu explicitly, even on a machine that also has Terraform, set `TerraformBinary` on the options you pass in:
119+
120+
```go
121+
terraformOptions := &terraform.Options{
122+
TerraformDir: "../examples/terraform-basic-example",
123+
TerraformBinary: "tofu",
124+
}
125+
```
126+
127+
The helper function names (`terraform.InitAndApply`, `terraform.Output`, and so on) don't change; only the underlying
128+
executable does.
129+
55130
56131
## Terratest intro
57132
58133
The basic usage pattern for writing automated tests with Terratest is to:
59134
60135
1. Write tests using Go’s built-in [package testing](https://golang.org/pkg/testing/): you create a file ending in `_test.go` and run tests with the `go test` command. E.g., `go test my_test.go`.
61-
1. Use Terratest to execute your _real_ IaC tools (e.g., Terraform, Packer, etc.) to deploy _real_ infrastructure (e.g., servers) in a _real_ environment (e.g., AWS).
136+
1. Use Terratest to execute your _real_ IaC tools (e.g., OpenTofu, Packer, etc.) to deploy _real_ infrastructure (e.g., servers) in a _real_ environment (e.g., AWS).
62137
1. Use the tools built into Terratest to validate that the infrastructure works correctly in that environment by making HTTP requests, API calls, SSH connections, etc.
63138
1. Undeploy everything at the end of the test.
64139
65-
To make this sort of testing easier, Terratest provides a variety of helper functions and patterns for common infrastructure testing tasks, such as testing Terraform code, testing Packer templates, testing Docker images, executing commands on servers over SSH, making HTTP requests, working with AWS APIs, and so on.
140+
To make this sort of testing easier, Terratest provides a variety of helper functions and patterns for common infrastructure testing tasks, such as testing OpenTofu and Terraform code, testing Packer templates, testing Docker images, executing commands on servers over SSH, making HTTP requests, working with AWS APIs, and so on.
66141
67142
68-
## Example #1: Terraform "Hello, World"
143+
## Example #1: OpenTofu "Hello, World"
144+
145+
Let's start with the simplest possible [OpenTofu](https://opentofu.org/) code, which just outputs the text,
146+
"Hello, World" (if you’re new to OpenTofu or Terraform, check out our [Comprehensive Guide to
147+
Terraform](https://blog.gruntwork.io/a-comprehensive-guide-to-terraform-b3d32832baca), which applies equally to
148+
OpenTofu):
69149

70-
Let's start with the simplest possible [Terraform](https://www.terraform.io/) code, which just outputs the text,
71-
"Hello, World" (if you’re new to Terraform, check out our [Comprehensive Guide to
72-
Terraform](https://blog.gruntwork.io/a-comprehensive-guide-to-terraform-b3d32832baca)):
73-
74150
{% include examples/explorer.html example_id='terraform-hello-world' file_id='terraform_code' class='wide quick-start-examples' skip_learn_more=true skip_view_on_github=true skip_tags=true %}
75151

76152
How can you test this code to be confident it works correctly? Well, let’s think about how you would test it manually:
77153

78-
1. Run `terraform init` and `terraform apply` to execute the code.
154+
1. Run `tofu init` and `tofu apply` to execute the code.
79155
1. When `apply` finishes, check that the output variable says, "Hello, World".
80-
1. When you're done testing, run `terraform destroy` to clean everything up.
156+
1. When you're done testing, run `tofu destroy` to clean everything up.
81157
82158
Using Terratest, you can write an automated test that performs the exact same steps! Here’s what the code looks like:
83159
84160
{% include examples/explorer.html example_id='terraform-hello-world' file_id='test_code' class='wide quick-start-examples' skip_learn_more=true skip_view_on_github=true skip_tags=true %}
85161
86-
This code does all the steps we mentioned above, including running `terraform init`, `terraform apply`, reading the
87-
output variable using `terraform output`, checking its value is what we expect, and running `terraform destroy`
162+
This code does all the steps we mentioned above, including running `tofu init`, `tofu apply`, reading the
163+
output variable using `tofu output`, checking its value is what we expect, and running `tofu destroy`
88164
(using [`defer`](https://blog.golang.org/defer-panic-and-recover) to run it at the end of the test, whether the test
89165
succeeds or fails). If you put this code in a file called `terraform_hello_world_example_test.go`, you can run it by
90166
executing `go test`, and you’ll see output that looks like this (truncated for readability):
91167
92-
```
168+
```console
93169
$ go test -v
94170
=== RUN TestTerraformHelloWorldExample
95-
Running command terraform with args [init]
171+
Running command tofu with args [init]
96172
Initializing provider plugins...
97173
[...]
98-
Terraform has been successfully initialized!
174+
OpenTofu has been successfully initialized!
99175
[...]
100176
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
101177
Outputs:
102178
hello_world = "Hello, World!"
103179
[...]
104-
Running command terraform with args [destroy -force -input=false]
180+
Running command tofu with args [destroy -auto-approve -input=false]
105181
[...]
106182
Destroy complete! Resources: 2 destroyed.
107183
--- PASS: TestTerraformHelloWorldExample (149.36s)
108184
```
109185
110186
Success!
111187
112-
## Example #2: Terraform and AWS
188+
## Example #2: OpenTofu and AWS
113189
114-
Let's now try out a more realistic Terraform example. Here is some Terraform code that deploys a simple web server in
190+
Let's now try out a more realistic example. Here is some OpenTofu code that deploys a simple web server in
115191
AWS:
116192

117193
{% include examples/explorer.html example_id='aws-hello-world' file_id='terraform_code' class='wide quick-start-examples' skip_learn_more=true skip_view_on_github=true skip_tags=true %}
@@ -124,31 +200,31 @@ script that, while the server is booting, fires up a dirt-simple web server that
124200
How can you test this code to be confident it works correctly? Well, let’s again think about how you would test it
125201
manually:
126202

127-
1. Run `terraform init` and `terraform apply` to deploy the web server into your AWS account.
203+
1. Run `tofu init` and `tofu apply` to deploy the web server into your AWS account.
128204
1. When `apply` finishes, get the IP of the web server by reading the `public_ip` output variable.
129205
1. Open the IP in your web browser with port 8080 and make sure it says “Hello, World”. Note that it can take 1–2
130206
minutes for the server to boot up, so you may have to retry a few times.
131-
1. When you’re done testing, run `terraform destroy` to clean everything up.
207+
1. When you’re done testing, run `tofu destroy` to clean everything up.
132208

133209
Here's how we can automate the steps above using Terratest:
134210
135211
{% include examples/explorer.html example_id='aws-hello-world' file_id='test_code' class='wide quick-start-examples' skip_learn_more=true skip_view_on_github=true skip_tags=true %}
136212
137-
This test code runs `terraform init` and `terraform apply`, reads the server IP using `terraform output`, makes HTTP
213+
This test code runs `tofu init` and `tofu apply`, reads the server IP using `tofu output`, makes HTTP
138214
requests to the web server (including plenty of retries to account for the server taking time to boot), checks the HTTP
139-
response is what we expect, and then runs `terraform destroy` at the end. If you put this code in a file called
215+
response is what we expect, and then runs `tofu destroy` at the end. If you put this code in a file called
140216
`terraform_aws_hello_world_example_test.go`, you can run just this test by passing the `-run` argument to `go test` as
141217
follows:
142218
143-
```
219+
```console
144220
$ go test -v -run TestTerraformAwsHelloWorldExample -timeout 30m
145221
=== RUN TestTerraformAwsHelloWorldExample
146-
Running command terraform with args [init]
222+
Running command tofu with args [init]
147223
Initializing provider plugins...
148224
[...]
149-
Terraform has been successfully initialized!
225+
OpenTofu has been successfully initialized!
150226
[...]
151-
Running command terraform with args [apply -auto-approve]
227+
Running command tofu with args [apply -auto-approve]
152228
aws_instance.example: Creating...
153229
associate_public_ip_address: "" => "<computed>"
154230
availability_zone: "" => "<computed>"
@@ -169,25 +245,25 @@ Sleeping for 5s and will try again.
169245
Making an HTTP GET call to URL http://52.67.41.31:8080
170246
Success!
171247
[...]
172-
Running command terraform with args [destroy -force -input=false]
248+
Running command tofu with args [destroy -auto-approve -input=false]
173249
[...]
174250
Destroy complete! Resources: 2 destroyed.
175251
--- PASS: TestTerraformAwsHelloWorldExample (149.36s)
176252
```
177253
178-
Success! Now, every time you make a change to this Terraform code, the test code can run and make sure your web server
254+
Success! Now, every time you make a change to this OpenTofu code, the test code can run and make sure your web server
179255
works as expected.
180256
181257
Note that in the `go test` command above, we set `-timeout 30m`. This is because Go sets a default test time out of 10
182258
minutes, and if your test take longer than that to run, Go will panic, and kill the test code part way through. This is
183-
not only annoying, but also prevents the clean up code from running (the `terraform destroy`), leaving you with lots of
259+
not only annoying, but also prevents the clean up code from running (the `tofu destroy`), leaving you with lots of
184260
resources hanging in your AWS account. To prevent this, we always recommend setting a high test timeout; the test above
185261
doesn't actually take anywhere near 30 minutes (typical runtime is ~3 minutes), but we give lots of extra buffer to be
186262
extra sure that the test always has a chance to finish cleanly.
187263

188264
## Example #3: Docker
189265

190-
You can use Terratest for testing a variety of infrastructure code, not just Terraform. For example, you can use it to
266+
You can use Terratest for testing a variety of infrastructure code, not just OpenTofu. For example, you can use it to
191267
test your [Docker](https://www.docker.com/) images:
192268

193269
{% include examples/explorer.html example_id='docker-hello-world' file_id='docker_code' class='wide quick-start-examples' skip_learn_more=true skip_view_on_github=true skip_tags=true %}
@@ -204,7 +280,7 @@ Here's how you can use Terratest to automate this process:
204280
205281
{% include examples/explorer.html example_id='docker-hello-world' file_id='test_code' class='wide quick-start-examples' skip_learn_more=true skip_view_on_github=true skip_tags=true %}
206282
207-
Instead of using Terraform helpers, this test code uses Terratest's Docker helpers to run `docker build`, `docker run`,
283+
Instead of using the OpenTofu/Terraform helpers, this test code uses Terratest's Docker helpers to run `docker build`, `docker run`,
208284
and check the contents of the text file. As before, you can run this test using `go test`!
209285

210286
## Example #4: Kubernetes

0 commit comments

Comments
 (0)