Skip to content

Commit 0045c35

Browse files
authored
helper/resource: Additional Go documentation for TestCheck and TestMatch functions (#911)
Reference: hashicorp/terraform-plugin-sdk#885
1 parent 8c490f2 commit 0045c35

File tree

2 files changed

+477
-25
lines changed

2 files changed

+477
-25
lines changed

helper/resource/testing.go

Lines changed: 195 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ func AddTestSweepers(name string, s *Sweeper) {
9999
//
100100
// Sweeper flags added to the "go test" command:
101101
//
102-
// -sweep: Comma-separated list of locations/regions to run available sweepers.
103-
// -sweep-allow-failues: Enable to allow other sweepers to run after failures.
104-
// -sweep-run: Comma-separated list of resource type sweepers to run. Defaults
102+
// -sweep: Comma-separated list of locations/regions to run available sweepers.
103+
// -sweep-allow-failues: Enable to allow other sweepers to run after failures.
104+
// -sweep-run: Comma-separated list of resource type sweepers to run. Defaults
105105
// to all sweepers.
106106
//
107107
// Refer to the Env prefixed constants for environment variables that further
@@ -573,16 +573,16 @@ func ParallelTest(t testing.T, c TestCase) {
573573
// This function will automatically find or install Terraform CLI into a
574574
// temporary directory, based on the following behavior:
575575
//
576-
// - If the TF_ACC_TERRAFORM_PATH environment variable is set, that Terraform
577-
// CLI binary is used if found and executable. If not found or executable,
578-
// an error will be returned unless the TF_ACC_TERRAFORM_VERSION environment
579-
// variable is also set.
580-
// - If the TF_ACC_TERRAFORM_VERSION environment variable is set, install and
581-
// use that Terraform CLI version.
582-
// - If both the TF_ACC_TERRAFORM_PATH and TF_ACC_TERRAFORM_VERSION environment
583-
// variables are unset, perform a lookup for the Terraform CLI binary based
584-
// on the operating system PATH. If not found, the latest available Terraform
585-
// CLI binary is installed.
576+
// - If the TF_ACC_TERRAFORM_PATH environment variable is set, that
577+
// Terraform CLI binary is used if found and executable. If not found or
578+
// executable, an error will be returned unless the
579+
// TF_ACC_TERRAFORM_VERSION environment variable is also set.
580+
// - If the TF_ACC_TERRAFORM_VERSION environment variable is set, install
581+
// and use that Terraform CLI version.
582+
// - If both the TF_ACC_TERRAFORM_PATH and TF_ACC_TERRAFORM_VERSION
583+
// environment variables are unset, perform a lookup for the Terraform
584+
// CLI binary based on the operating system PATH. If not found, the
585+
// latest available Terraform CLI binary is installed.
586586
//
587587
// Refer to the Env prefixed constants for additional details about these
588588
// environment variables, and others, that control testing functionality.
@@ -718,6 +718,9 @@ func testResource(c TestStep, state *terraform.State) (*terraform.ResourceState,
718718
//
719719
// As a user testing their provider, this lets you decompose your checks
720720
// into smaller pieces more easily.
721+
//
722+
// ComposeTestCheckFunc returns immediately on the first TestCheckFunc error.
723+
// To aggregrate all errors, use ComposeAggregateTestCheckFunc instead.
721724
func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
722725
return func(s *terraform.State) error {
723726
for i, f := range fs {
@@ -752,10 +755,48 @@ func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
752755
}
753756
}
754757

755-
// TestCheckResourceAttrSet is a TestCheckFunc which ensures a value
756-
// exists in state for the given name/key combination. It is useful when
757-
// testing that computed values were set, when it is not possible to
758-
// know ahead of time what the values will be.
758+
// TestCheckResourceAttrSet ensures any value exists in the state for the
759+
// given name and key combination. The opposite of this TestCheckFunc is
760+
// TestCheckNoResourceAttr. State value checking is only recommended for
761+
// testing Computed attributes and attribute defaults.
762+
//
763+
// Use this as a last resort when a more specific TestCheckFunc cannot be
764+
// implemented, such as:
765+
//
766+
// - TestCheckResourceAttr: Equality checking of non-TypeSet state value.
767+
// - TestCheckResourceAttrPair: Equality checking of non-TypeSet state
768+
// value, based on another state value.
769+
// - TestCheckTypeSet*: Equality checking of TypeSet state values.
770+
// - TestMatchResourceAttr: Regular expression checking of non-TypeSet
771+
// state value.
772+
// - TestMatchTypeSet*: Regular expression checking on TypeSet state values.
773+
//
774+
// For managed resources, the name parameter is combination of the resource
775+
// type, a period (.), and the name label. The name for the below example
776+
// configuration would be "myprovider_thing.example".
777+
//
778+
// resource "myprovider_thing" "example" { ... }
779+
//
780+
// For data sources, the name parameter is a combination of the keyword "data",
781+
// a period (.), the data source type, a period (.), and the name label. The
782+
// name for the below example configuration would be
783+
// "data.myprovider_thing.example".
784+
//
785+
// data "myprovider_thing" "example" { ... }
786+
//
787+
// The key parameter is an attribute path in Terraform CLI 0.11 and earlier
788+
// "flatmap" syntax. Keys start with the attribute name of a top-level
789+
// attribute. Use the following special key syntax to inspect underlying
790+
// values of a list or map attribute:
791+
//
792+
// - .{NUMBER}: List value at index, e.g. .0 to inspect the first element
793+
// - .{KEY}: Map value at key, e.g. .example to inspect the example key
794+
// value
795+
//
796+
// While it is possible to check nested attributes under list and map
797+
// attributes using the special key syntax, checking a list, map, or set
798+
// attribute directly is not supported. Use TestCheckResourceAttr with
799+
// the special .# or .% key syntax for those situations instead.
759800
func TestCheckResourceAttrSet(name, key string) TestCheckFunc {
760801
return checkIfIndexesIntoTypeSet(key, func(s *terraform.State) error {
761802
is, err := primaryInstanceState(s, name)
@@ -789,8 +830,42 @@ func testCheckResourceAttrSet(is *terraform.InstanceState, name string, key stri
789830
return nil
790831
}
791832

792-
// TestCheckResourceAttr is a TestCheckFunc which validates
793-
// the value in state for the given name/key combination.
833+
// TestCheckResourceAttr ensures a specific value is stored in state for the
834+
// given name and key combination. State value checking is only recommended for
835+
// testing Computed attributes and attribute defaults.
836+
//
837+
// For managed resources, the name parameter is combination of the resource
838+
// type, a period (.), and the name label. The name for the below example
839+
// configuration would be "myprovider_thing.example".
840+
//
841+
// resource "myprovider_thing" "example" { ... }
842+
//
843+
// For data sources, the name parameter is a combination of the keyword "data",
844+
// a period (.), the data source type, a period (.), and the name label. The
845+
// name for the below example configuration would be
846+
// "data.myprovider_thing.example".
847+
//
848+
// data "myprovider_thing" "example" { ... }
849+
//
850+
// The key parameter is an attribute path in Terraform CLI 0.11 and earlier
851+
// "flatmap" syntax. Keys start with the attribute name of a top-level
852+
// attribute. Use the following special key syntax to inspect list, map, and
853+
// set attributes:
854+
//
855+
// - .{NUMBER}: List value at index, e.g. .0 to inspect the first element.
856+
// Use the TestCheckTypeSet* and TestMatchTypeSet* functions instead
857+
// for sets.
858+
// - .{KEY}: Map value at key, e.g. .example to inspect the example key
859+
// value.
860+
// - .#: Number of elements in list or set.
861+
// - .%: Number of elements in map.
862+
//
863+
// The value parameter is the stringified data to check at the given key. Use
864+
// the following attribute type rules to set the value:
865+
//
866+
// - Boolean: "false" or "true".
867+
// - Float/Integer: Stringified number, such as "1.2" or "123".
868+
// - String: No conversion necessary.
794869
func TestCheckResourceAttr(name, key, value string) TestCheckFunc {
795870
return checkIfIndexesIntoTypeSet(key, func(s *terraform.State) error {
796871
is, err := primaryInstanceState(s, name)
@@ -844,8 +919,37 @@ func testCheckResourceAttr(is *terraform.InstanceState, name string, key string,
844919
return nil
845920
}
846921

847-
// TestCheckNoResourceAttr is a TestCheckFunc which ensures that
848-
// NO value exists in state for the given name/key combination.
922+
// TestCheckNoResourceAttr ensures no value exists in the state for the
923+
// given name and key combination. The opposite of this TestCheckFunc is
924+
// TestCheckResourceAttrSet. State value checking is only recommended for
925+
// testing Computed attributes and attribute defaults.
926+
//
927+
// For managed resources, the name parameter is combination of the resource
928+
// type, a period (.), and the name label. The name for the below example
929+
// configuration would be "myprovider_thing.example".
930+
//
931+
// resource "myprovider_thing" "example" { ... }
932+
//
933+
// For data sources, the name parameter is a combination of the keyword "data",
934+
// a period (.), the data source type, a period (.), and the name label. The
935+
// name for the below example configuration would be
936+
// "data.myprovider_thing.example".
937+
//
938+
// data "myprovider_thing" "example" { ... }
939+
//
940+
// The key parameter is an attribute path in Terraform CLI 0.11 and earlier
941+
// "flatmap" syntax. Keys start with the attribute name of a top-level
942+
// attribute. Use the following special key syntax to inspect underlying
943+
// values of a list or map attribute:
944+
//
945+
// - .{NUMBER}: List value at index, e.g. .0 to inspect the first element.
946+
// - .{KEY}: Map value at key, e.g. .example to inspect the example key
947+
// value.
948+
//
949+
// While it is possible to check nested attributes under list and map
950+
// attributes using the special key syntax, checking a list, map, or set
951+
// attribute directly is not supported. Use TestCheckResourceAttr with
952+
// the special .# or .% key syntax for those situations instead.
849953
func TestCheckNoResourceAttr(name, key string) TestCheckFunc {
850954
return checkIfIndexesIntoTypeSet(key, func(s *terraform.State) error {
851955
is, err := primaryInstanceState(s, name)
@@ -892,8 +996,40 @@ func testCheckNoResourceAttr(is *terraform.InstanceState, name string, key strin
892996
return nil
893997
}
894998

895-
// TestMatchResourceAttr is a TestCheckFunc which checks that the value
896-
// in state for the given name/key combination matches the given regex.
999+
// TestMatchResourceAttr ensures a value matching a regular expression is
1000+
// stored in state for the given name and key combination. State value checking
1001+
// is only recommended for testing Computed attributes and attribute defaults.
1002+
//
1003+
// For managed resources, the name parameter is combination of the resource
1004+
// type, a period (.), and the name label. The name for the below example
1005+
// configuration would be "myprovider_thing.example".
1006+
//
1007+
// resource "myprovider_thing" "example" { ... }
1008+
//
1009+
// For data sources, the name parameter is a combination of the keyword "data",
1010+
// a period (.), the data source type, a period (.), and the name label. The
1011+
// name for the below example configuration would be
1012+
// "data.myprovider_thing.example".
1013+
//
1014+
// data "myprovider_thing" "example" { ... }
1015+
//
1016+
// The key parameter is an attribute path in Terraform CLI 0.11 and earlier
1017+
// "flatmap" syntax. Keys start with the attribute name of a top-level
1018+
// attribute. Use the following special key syntax to inspect list, map, and
1019+
// set attributes:
1020+
//
1021+
// - .{NUMBER}: List value at index, e.g. .0 to inspect the first element.
1022+
// Use the TestCheckTypeSet* and TestMatchTypeSet* functions instead
1023+
// for sets.
1024+
// - .{KEY}: Map value at key, e.g. .example to inspect the example key
1025+
// value.
1026+
// - .#: Number of elements in list or set.
1027+
// - .%: Number of elements in map.
1028+
//
1029+
// The value parameter is a compiled regular expression. A typical pattern is
1030+
// using the regexp.MustCompile() function, which will automatically ensure the
1031+
// regular expression is supported by the Go regular expression handlers during
1032+
// compilation.
8971033
func TestMatchResourceAttr(name, key string, r *regexp.Regexp) TestCheckFunc {
8981034
return checkIfIndexesIntoTypeSet(key, func(s *terraform.State) error {
8991035
is, err := primaryInstanceState(s, name)
@@ -935,6 +1071,9 @@ func testMatchResourceAttr(is *terraform.InstanceState, name string, key string,
9351071
// TestCheckResourceAttrPtr is like TestCheckResourceAttr except the
9361072
// value is a pointer so that it can be updated while the test is running.
9371073
// It will only be dereferenced at the point this step is run.
1074+
//
1075+
// Refer to the TestCheckResourceAttr documentation for more information about
1076+
// setting the name, key, and value parameters.
9381077
func TestCheckResourceAttrPtr(name string, key string, value *string) TestCheckFunc {
9391078
return func(s *terraform.State) error {
9401079
return TestCheckResourceAttr(name, key, *value)(s)
@@ -949,8 +1088,39 @@ func TestCheckModuleResourceAttrPtr(mp []string, name string, key string, value
9491088
}
9501089
}
9511090

952-
// TestCheckResourceAttrPair is a TestCheckFunc which validates that the values
953-
// in state for a pair of name/key combinations are equal.
1091+
// TestCheckResourceAttrPair ensures value equality in state between the first
1092+
// given name and key combination and the second name and key combination.
1093+
// State value checking is only recommended for testing Computed attributes
1094+
// and attribute defaults.
1095+
//
1096+
// For managed resources, the name parameter is combination of the resource
1097+
// type, a period (.), and the name label. The name for the below example
1098+
// configuration would be "myprovider_thing.example".
1099+
//
1100+
// resource "myprovider_thing" "example" { ... }
1101+
//
1102+
// For data sources, the name parameter is a combination of the keyword "data",
1103+
// a period (.), the data source type, a period (.), and the name label. The
1104+
// name for the below example configuration would be
1105+
// "data.myprovider_thing.example".
1106+
//
1107+
// data "myprovider_thing" "example" { ... }
1108+
//
1109+
// The first and second names may use any combination of managed resources
1110+
// and/or data sources.
1111+
//
1112+
// The key parameter is an attribute path in Terraform CLI 0.11 and earlier
1113+
// "flatmap" syntax. Keys start with the attribute name of a top-level
1114+
// attribute. Use the following special key syntax to inspect list, map, and
1115+
// set attributes:
1116+
//
1117+
// - .{NUMBER}: List value at index, e.g. .0 to inspect the first element.
1118+
// Use the TestCheckTypeSet* and TestMatchTypeSet* functions instead
1119+
// for sets.
1120+
// - .{KEY}: Map value at key, e.g. .example to inspect the example key
1121+
// value.
1122+
// - .#: Number of elements in list or set.
1123+
// - .%: Number of elements in map.
9541124
func TestCheckResourceAttrPair(nameFirst, keyFirst, nameSecond, keySecond string) TestCheckFunc {
9551125
return checkIfIndexesIntoTypeSetPair(keyFirst, keySecond, func(s *terraform.State) error {
9561126
isFirst, err := primaryInstanceState(s, nameFirst)

0 commit comments

Comments
 (0)