Skip to content

Commit 4910d52

Browse files
authored
docs: Update Check function examples with statecheck package implementations (#363)
1 parent f9ca4c3 commit 4910d52

File tree

5 files changed

+106
-144
lines changed

5 files changed

+106
-144
lines changed

website/docs/plugin/testing/acceptance-tests/index.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ or more configuration files, allowing multiple scenarios to be tested.
1717

1818
Terraform acceptance tests use real Terraform configurations to exercise the
1919
code in real plan, apply, refresh, and destroy life cycles. When run from the
20-
root of a Terraform Provider codebase, Terraforms testing framework compiles
20+
root of a Terraform Provider codebase, Terraform's testing framework compiles
2121
the current provider in-memory and executes the provided configuration in
2222
developer defined steps, creating infrastructure along the way. At the
2323
conclusion of all the steps, Terraform automatically destroys the
24-
infrastructure. Its important to note that during development, its possible
24+
infrastructure. It's important to note that during development, it's possible
2525
for Terraform to leave orphaned or “dangling” resources behind, depending on the
2626
correctness of the code in development. The testing framework provides means to
2727
validate all resources are destroyed, alerting developers if any fail to
@@ -42,7 +42,7 @@ While the test framework provides a reasonable simulation of real-world usage, t
4242

4343
Terraform follows many of the Go programming language conventions with regards
4444
to testing, with both acceptance tests and unit tests being placed in a file
45-
that matches the file under test, with an added `_test.go` suffix. Heres an
45+
that matches the file under test, with an added `_test.go` suffix. Here's an
4646
example file structure:
4747

4848
```
@@ -302,6 +302,6 @@ This error indicates that the provider server could not connect to Terraform Cor
302302
Terraform relies heavily on acceptance tests to ensure we keep our promise of
303303
helping users safely and predictably create, change, and improve
304304
infrastructure. In our next section we detail how to create “Test Cases”,
305-
individual acceptance tests using Terraforms testing framework, in order to
305+
individual acceptance tests using Terraform's testing framework, in order to
306306
build and verify real infrastructure. [Proceed to Test
307307
Cases](/terraform/plugin/testing/acceptance-tests/testcase)

website/docs/plugin/testing/acceptance-tests/testcase.mdx

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ description: |-
99

1010
Acceptance tests are expressed in terms of **Test Cases**, each using one or
1111
more Terraform configurations designed to create a set of resources under test,
12-
and then verify the actual infrastructure created. Terraforms `resource`
12+
and then verify the actual infrastructure created. Terraform's `resource`
1313
package offers a method `Test()`, accepting two parameters and acting as the
14-
entry point to Terraforms acceptance test framework. The first parameter is the
15-
standard [\*testing.T struct from Golangs Testing package][3], and the second is
14+
entry point to Terraform's acceptance test framework. The first parameter is the
15+
standard [\*testing.T struct from Golang's Testing package][3], and the second is
1616
[TestCase][1], a Go struct that developers use to setup the acceptance tests.
1717

18-
Heres an example acceptance test. Here the Provider is named `Example`, and the
18+
Here's an example acceptance test. Here the Provider is named `Example`, and the
1919
Resource under test is `Widget`. The parts of this test are explained below the
2020
example.
2121

@@ -34,15 +34,15 @@ func TestAccExampleWidget_basic(t *testing.T) {
3434
Steps: []resource.TestStep{
3535
{
3636
Config: testAccExampleResource(rName),
37-
Check: resource.ComposeTestCheckFunc(
38-
testAccCheckExampleResourceExists("example_widget.foo", &widgetBefore),
39-
),
37+
ConfigStateChecks: []statecheck.StateCheck{
38+
stateCheckExampleResourceExists("example_widget.foo", &widgetBefore),
39+
},
4040
},
4141
{
4242
Config: testAccExampleResource_removedPolicy(rName),
43-
Check: resource.ComposeTestCheckFunc(
44-
testAccCheckExampleResourceExists("example_widget.foo", &widgetAfter),
45-
),
43+
ConfigStateChecks: []statecheck.StateCheck{
44+
stateCheckExampleResourceExists("example_widget.foo", &widgetAfter),
45+
},
4646
},
4747
},
4848
})
@@ -81,7 +81,7 @@ func TestAccExampleWidget_basic(t *testing.T) {
8181
The majority of acceptance tests will only invoke `resource.Test()` and exit. If
8282
at any point this method encounters an error, either in executing the provided
8383
Terraform configurations or subsequent developer defined checks, `Test()` will
84-
invoke the `t.Error` method of Gos standard testing framework and the test will
84+
invoke the `t.Error` method of Go's standard testing framework and the test will
8585
fail. A failed test will not halt or otherwise interrupt any other tests
8686
currently running.
8787

@@ -190,7 +190,7 @@ a configuration file for testing must be represented in this map or the test
190190
will fail during initialization.
191191

192192
This map is most commonly constructed once in a common `init()` method of the
193-
Providers main test file, and includes an object of the current Provider type.
193+
Provider's main test file, and includes an object of the current Provider type.
194194

195195
**Example usage:** (note the different files `widget_test.go` and `provider_test.go`)
196196

@@ -326,15 +326,15 @@ func TestAccExampleWidget_basic(t *testing.T) {
326326
Steps: []resource.TestStep{
327327
{
328328
Config: testAccExampleResource(rName),
329-
Check: resource.ComposeTestCheckFunc(
330-
testAccCheckExampleResourceExists("example_widget.foo", &widgetBefore),
331-
),
329+
ConfigStateChecks: []statecheck.StateCheck{
330+
stateCheckExampleResourceExists("example_widget.foo", &widgetBefore),
331+
},
332332
},
333333
{
334334
Config: testAccExampleResource_removedPolicy(rName),
335-
Check: resource.ComposeTestCheckFunc(
336-
testAccCheckExampleResourceExists("example_widget.foo", &widgetAfter),
337-
),
335+
ConfigStateChecks: []statecheck.StateCheck{
336+
stateCheckExampleResourceExists("example_widget.foo", &widgetAfter),
337+
},
338338
},
339339
},
340340
})
@@ -346,7 +346,7 @@ func TestAccExampleWidget_basic(t *testing.T) {
346346
`TestCases` are used to verify the features of a given part of a plugin. Each
347347
case should represent a scenario of normal usage of the plugin, from simple
348348
creation to creating, adding, and removing specific properties. In the next
349-
Section [`TestSteps`][2], well detail `Steps` portion of `TestCase` and see how
349+
Section [`TestSteps`][2], we'll detail `Steps` portion of `TestCase` and see how
350350
to create these scenarios by iterating on Terraform configurations.
351351

352352
[1]: https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/helper/resource#TestCase

website/docs/plugin/testing/acceptance-tests/teststep.mdx

+36-22
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,59 @@ func TestAccExampleWidget_basic(t *testing.T) {
6464
Steps: []resource.TestStep{
6565
{
6666
Config: testAccExampleResource(rName),
67-
Check: resource.ComposeTestCheckFunc(
68-
testAccCheckExampleResourceExists("example_widget.foo", &widgetBefore),
69-
),
67+
ConfigStateChecks: []statecheck.StateCheck{
68+
stateCheckExampleResourceExists("example_widget.foo", &widgetBefore),
69+
},
7070
},
7171
{
7272
Config: testAccExampleResource_removedPolicy(rName),
73-
Check: resource.ComposeTestCheckFunc(
74-
testAccCheckExampleResourceExists("example_widget.foo", &widgetAfter),
75-
),
73+
ConfigStateChecks: []statecheck.StateCheck{
74+
stateCheckExampleResourceExists("example_widget.foo", &widgetAfter),
75+
},
7676
},
7777
},
7878
})
7979
}
8080
```
8181

82-
In the above example each `TestCase` invokes a function to retrieve its desired
82+
In the above example each `TestCase` invokes a function to retrieve it's desired
8383
configuration, based on a randomized name provided, however an in-line string or
8484
constant string would work as well, so long as they contain valid Terraform
8585
configuration for the plugin or resource under test. This pattern of first
8686
applying and checking a basic configuration, followed by applying a modified
8787
configuration with updated or additional checks is a common pattern used to test
8888
update functionality.
8989

90-
## State Check Functions
90+
## Plan Checks
91+
Before and after the configuration for a `TestStep` is applied, Terraform's testing framework provides developers an opportunity to make test assertions against `terraform plan` results via the plan file. This is provided via [Plan Checks](/terraform/plugin/testing/acceptance-tests/plan-checks), which provide both built-in plan checks and an interface to implement custom plan checks.
92+
93+
## State Checks
94+
95+
After the configuration for a `TestStep` is applied, Terraform's testing
96+
framework provides developers an opportunity to check the results by providing one
97+
or more [state check implementations](/terraform/plugin/testing/acceptance-tests/state-checks).
98+
While possible to only supply a single state check, it is recommended you use multiple state checks
99+
to validate specific information about the results of the `terraform apply` ran in each `TestStep`.
91100

92-
After the configuration for a `TestStep` is applied, Terraform’s testing
93-
framework provides developers an opportunity to check the results by providing a
94-
“Check” function. While possible to only supply a single function, it is
95-
recommended you use multiple functions to validate specific information about
96-
the results of the `terraform apply` ran in each `TestStep`. The `Check`
101+
See the [State Checks](/terraform/plugin/testing/acceptance-tests/state-checks) section for more information about the built-in state checks for resources, data sources,
102+
output values, and how to write custom state checks.
103+
104+
### Legacy Check function
105+
106+
<Note>
107+
108+
Use the new `ConfigStateChecks` attribute and [State Check implementations](/terraform/plugin/testing/acceptance-tests/state-checks)
109+
instead of the `Check` function.
110+
111+
</Note>
112+
113+
The `Check` function is used to check results of a Terraform operation. The `Check`
97114
attribute of `TestStep` is singular, so in order to include multiple checks
98115
developers should use either `ComposeTestCheckFunc` or
99116
`ComposeAggregateTestCheckFunc` (defined below) to group multiple check
100117
functions, defined below:
101118

102-
### ComposeTestCheckFunc
119+
#### ComposeTestCheckFunc
103120

104121
ComposeTestCheckFunc lets you compose multiple TestCheckFunc functions into a
105122
single check. As a user testing their provider, this lets you decompose your
@@ -124,10 +141,10 @@ Steps: []resource.TestStep{
124141
},
125142
```
126143

127-
### ComposeAggregateTestCheckFunc
144+
#### ComposeAggregateTestCheckFunc
128145

129146
ComposeAggregateTestCheckFunc lets you compose multiple TestCheckFunc functions
130-
into a single check. Its purpose and usage is identical to
147+
into a single check. It's purpose and usage is identical to
131148
ComposeTestCheckFunc, however each check is ran in order even if a previous
132149
check failed, collecting the errors returned from any checks and returning a
133150
single aggregate error. The entire `TestCase` is still stopped, and Terraform
@@ -149,7 +166,7 @@ Steps: []resource.TestStep{
149166
},
150167
```
151168

152-
## Builtin check functions
169+
#### Built-in check functions
153170

154171
Terraform has several TestCheckFunc functions built in for developers to use for
155172
common checks, such as verifying the status and value of a specific attribute in
@@ -204,7 +221,7 @@ All of these functions also accept the below syntax in attribute keys to enable
204221
| `.#` | Number of elements in list or set | `TestCheckResourceAttr("example_widget.foo", "some_list.#", "2")` |
205222
| `.%` | Number of keys in map | `TestCheckResourceAttr("example_widget.foo", "some_map.%", "2")` |
206223

207-
## Custom check functions
224+
### Custom check functions
208225

209226
The `Check` field of `TestStep` accepts any function of type
210227
[TestCheckFunc](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-testing/helper/resource#TestCheckFunc).
@@ -299,9 +316,6 @@ func testAccCheckExampleWidgetExists(resourceName string, widget *example.Widget
299316
}
300317
```
301318

302-
## Plan Checks
303-
Before and after the configuration for a `TestStep` is applied, Terraform's testing framework provides developers an opportunity to make test assertions against `terraform plan` results via the plan file. This is provided via [Plan Checks](/terraform/plugin/testing/acceptance-tests/plan-checks), which provide both built-in plan checks and an interface to implement custom plan checks.
304-
305319
## Sweepers
306320

307-
Acceptance Testing is an essential approach to validating the implementation of a Terraform Provider. Using actual APIs to provision resources for testing can leave behind real infrastructure that costs money between tests. The reasons for these leaks can vary, regardless Terraform provides a mechanism known as [Sweepers](/terraform/plugin/testing/acceptance-tests/sweepers) to help keep the testing account clean.
321+
Acceptance Testing is an essential approach to validating the implementation of a Terraform Provider. Using actual APIs to provision resources for testing can leave behind real infrastructure that costs money between tests. The reasons for these leaks can vary, regardless Terraform provides a mechanism known as [Sweepers](/terraform/plugin/testing/acceptance-tests/sweepers) to help keep the testing account clean.

website/docs/plugin/testing/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ verified. Terraform includes a framework for constructing acceptance tests that
2626
imitate the execution of one or more steps of applying one or more configuration
2727
files, allowing multiple scenarios to be tested.
2828

29-
Its important to reiterate that acceptance tests in resources _create actual
29+
It's important to reiterate that acceptance tests in resources _create actual
3030
cloud infrastructure_, with possible expenses incurred, and are the
3131
responsibility of the user running the tests. Creating real infrastructure in
3232
tests verifies the described behavior of Terraform Plugins in real world use

0 commit comments

Comments
 (0)