Terraform Configuration
The configuration used during the execution of an acceptance test can be specified at the TestStep level by populating one of the following mutually exclusive fields:
Terraform configuration can be used in conjunction with Terraform variables defined via TestStep.ConfigVariables.
TestStep Config
The TestStep.Config
field accepts a string containing valid Terraform configuration.
In the following example, the Config
field specifies a resource which is used in combination with ExternalProviders
to specify the version and source for the provider:
func TestAccResourcePassword_UpgradeFromVersion3_2_0(t *testing.T) { resource.Test(t, resource.TestCase{ Steps: []resource.TestStep{ { ExternalProviders: map[string]resource.ExternalProvider{ "random": { VersionConstraint: "3.2.0", Source: "hashicorp/random", }, }, Config: `resource "random_password" "min" { length = 12 override_special = "!#@" min_lower = 2 min_upper = 3 min_special = 1 min_numeric = 4 }`,
TestStep ConfigDirectory
The TestStep.ConfigDirectory
field accepts a TestStepConfigFunc which is a function that accepts a TestStepConfigRequest and returns a string containing a path to a directory containing Terraform configuration files. The path can be a relative or absolute path.
There are helper methods available for generating a TestStepConfigFunc including:
Note: TestStep.ExternalProviders
cannot be specified when using ConfigDirectory. It is expected that required_providers are defined within the configuration files.
Custom functions can be written and used in the TestStep.ConfigDirectory
field as long as the function is a TestStepConfigFunc type.
StaticDirectory
The StaticDirectory(directory string) function accepts a string specifying a path to a directory containing Terraform configuration.
For example:
func Test_ConfigDirectory_StaticDirectory(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigDirectory: config.StaticDirectory(`testdata/directory_containing_config`), /* ... */ }, }, })}
In this instance, the testing configuration is expected to be in the testdata/directory_containing_config
directory relative to the file containing the Test_ConfigDirectory_StaticDirectory
test.
TestNameDirectory
The TestNameDirectory() function will use the name of the executing test to specify a path to a directory containing Terraform configuration.
For example:
func Test_ConfigDirectory_TestNameDirectory(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigDirectory: config.TestNameDirectory(), /* ... */ }, }, })}
In this instance, the testing configuration is expected to be in the testdata/Test_ConfigDirectory_TestNameDirectory
directory relative to the file containing the Test_ConfigDirectory_TestNameDirectory
test.
TestStepDirectory
The TestStepDirectory() function will use the name of the executing test and the current test step number to specify a path to a directory containing Terraform configuration.
For example:
func Test_ConfigDirectory_TestStepDirectory(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigDirectory: config.TestStepDirectory(), /* ... */ }, }, })}
In this instance, because this is the first test step in the test, the testing configuration is expected to be in the testdata/Test_ConfigDirectory_TestStepDirectory/1
directory relative to the file containing the Test_ConfigDirectory_TestStepDirectory
test.
TestStep ConfigFile
The TestStep.ConfigFile
field accepts a TestStepConfigFunc which is a function that accepts a TestStepConfigRequest and returns a string containing a path to a file containing Terraform configuration. The path can be a relative or absolute path.
There are helper methods available for generating a TestStepConfigFunc including:
Note: TestStep.ExternalProviders
cannot be specified when using ConfigFile
. It is expected that required_providers are defined within the configuration file.
Custom functions can be written and used in the TestStep.ConfigFile
field as long as the function is a TestStepConfigFunc type.
StaticFile
The StaticFile(file string) function accepts a string specifying a path to a file containing Terraform configuration.
For example:
func Test_ConfigFile_StaticFile(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigFile: config.StaticFile(`testdata/directory_containing_config/main.tf`), /* ... */ }, }, })}
In this instance, the testing configuration is expected to be in the testdata/directory_containing_config/main.tf
file relative to the file containing the Test_ConfigFile_StaticFile
test.
TestNameFile
The TestNameFile(file string) function will use the name of the executing test to specify a path to a file containing Terraform configuration.
For example:
func Test_ConfigFile_TestNameFile(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigFile: config.TestNameFile("main.tf"), /* ... */ }, }, })}
In this instance, the testing configuration is expected to be in the testdata/Test_ConfigFile_TestNameFile
directory relative to the file containing the Test_ConfigFile_TestNameFile
test.
TestStepFile
The TestStepFile(file string) function will use the name of the executing test and the current test step number to specify a path to a file containing Terraform configuration.
For example:
func Test_ConfigFile_TestStepFile(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigFile: config.TestStepFile("main.tf"), /* ... */ }, }, })}
In this instance, because this is the first test step in the test, the testing configuration is expected to be in the testdata/Test_ConfigFile_TestStepFile/1/main.tf
file relative to the file containing the Test_ConfigDirectory_TestNameDirectory
test.
TestStep ConfigVariables
Terraform input variables allow customization of a Terraform configuration without altering the configuration itself.
The TestStep.ConfigVariables
field accepts a Variables type which is a key-value map of string to Variable.
The following functions return types implementing Variable that correlate with the Terraform type constraints:
- BoolVariable(value bool)
- FloatVariable[T constraints.Float](value T)
- IntegerVariable[T constraints.Integer](value T)
- func ListVariable(value ...Variable)
- MapVariable(value map[string]Variable)
- ObjectVariable(value map[string]Variable)
- SetVariable(value ...Variable)
- StringVariable(value string)
- TupleVariable(value ...Variable)
The following example shows the usage of TestStep.ConfigVariables
in conjunction with TestStep.ConfigFile
:
func Test_ConfigFile_TestNameFile(t *testing.T) { t.Parallel() Test(t, TestCase{ Steps: []TestStep{ { ConfigFile: config.TestNameFile("random.tf"), ConfigVariables: config.Variables{ "length": config.IntegerVariable(8), "numeric": config.BoolVariable(false), }, /* ... */ }, }, })}
The configuration would be expected to be in the testdata/Test_ConfigFile_TestNameFile/random.tf
file, for example:
terraform { required_providers { random = { source = "registry.terraform.io/hashicorp/random" version = "3.5.1" } }}provider "random" {}resource "random_password" "test" { length = var.length numeric = var.numeric}variable "length" { type = number}variable "numeric" { type = bool}