terraform

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 33 Imported by: 285

Documentation

Overview

Package terraform allows to interact with Terraform.

Index

Constants

View Source
const (
	// TofuDefaultPath command to run tofu
	TofuDefaultPath = "tofu"

	// TerraformDefaultPath to run terraform
	TerraformDefaultPath = "terraform"
)
View Source
const DefaultErrorExitCode = 1

DefaultErrorExitCode is the exit code returned when terraform command fails

View Source
const DefaultSuccessExitCode = 0

DefaultSuccessExitCode is the exit code returned when terraform command succeeds

View Source
const GetResourceCountErrMessage = "can't parse Terraform output"

GetResourceCountErrMessage is the error message returned when terraform output cannot be parsed.

View Source
const TerraformPlanChangesPresentExitCode = 2

TerraformPlanChangesPresentExitCode is the exit code returned by terraform plan detailed exitcode when changes are present

Variables

View Source
var DefaultExecutable = defaultTerraformExecutable()

DefaultExecutable is the default terraform executable to use. It is set to "terraform" if the terraform binary is available, otherwise it falls back to "tofu".

View Source
var (
	DefaultRetryableTerraformErrors = map[string]string{

		".*read: connection reset by peer.*": "Failed to reach helm charts repository.",
		".*transport is closing.*":           "Failed to reach Kubernetes API.",

		".*unable to verify signature.*":                  "Failed to retrieve plugin due to transient network error.",
		".*unable to verify checksum.*":                   "Failed to retrieve plugin due to transient network error.",
		".*no provider exists with the given name.*":      "Failed to retrieve plugin due to transient network error.",
		".*registry service is unreachable.*":             "Failed to retrieve plugin due to transient network error.",
		".*Error installing provider.*":                   "Failed to retrieve plugin due to transient network error.",
		".*Failed to query available provider packages.*": "Failed to retrieve plugin due to transient network error.",
		".*timeout while waiting for plugin to start.*":   "Failed to retrieve plugin due to transient network error.",
		".*timed out waiting for server handshake.*":      "Failed to retrieve plugin due to transient network error.",
		"could not query provider registry for":           "Failed to retrieve plugin due to transient network error.",

		".*Provider produced inconsistent result after apply.*": "Provider eventual consistency error.",
	}
)

DefaultRetryableTerraformErrors is the default set of error patterns that [Options.RetryableTerraformErrors] will be initialized with. The keys are regular expressions matched against Terraform output, and the values are human-friendly messages displayed when the matching error triggers a retry.

View Source
var (
	// PlanFilePathRequired is returned when PlanFilePath is not set on Options but is required by the called function.
	PlanFilePathRequired = errors.New("you must set PlanFilePath on options struct to use this function") //nolint:staticcheck // preserving existing public variable name
)
View Source
var TerraformCommandsWithLockSupport = []string{
	"plan",
	"apply",
	"destroy",
	"init",
	"refresh",
	"taint",
	"untaint",
	"import",
}

TerraformCommandsWithLockSupport is a list of all the Terraform commands that can obtain locks on Terraform state

View Source
var TerraformCommandsWithPlanFileSupport = []string{
	"plan",
	"apply",
	"show",
	"graph",
}

TerraformCommandsWithPlanFileSupport is a list of all the Terraform commands that support interacting with plan files.

Functions

func Apply deprecated

func Apply(t testing.TestingT, options *Options) string

Apply runs terraform apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use ApplyContext instead.

func ApplyAndIdempotent deprecated added in v0.28.5

func ApplyAndIdempotent(t testing.TestingT, options *Options) string

ApplyAndIdempotent runs terraform apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use ApplyAndIdempotentContext instead.

func ApplyAndIdempotentContext added in v1.0.0

func ApplyAndIdempotentContext(t testing.TestingT, ctx context.Context, options *Options) string

ApplyAndIdempotentContext runs terraform apply with the given options and returns stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func ApplyAndIdempotentContextE added in v1.0.0

func ApplyAndIdempotentContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

ApplyAndIdempotentContextE runs terraform apply with the given options and returns stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func ApplyAndIdempotentE deprecated added in v0.28.5

func ApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error)

ApplyAndIdempotentE runs terraform apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use ApplyAndIdempotentContextE instead.

func ApplyContext added in v1.0.0

func ApplyContext(t testing.TestingT, ctx context.Context, options *Options) string

ApplyContext runs terraform apply with the given options and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func ApplyContextE added in v1.0.0

func ApplyContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

ApplyContextE runs terraform apply with the given options and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func ApplyE deprecated

func ApplyE(t testing.TestingT, options *Options) (string, error)

ApplyE runs terraform apply with the given options and return stdout/stderr. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use ApplyContextE instead.

func AssertPlannedValuesMapKeyExists added in v0.32.19

func AssertPlannedValuesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

AssertPlannedValuesMapKeyExists checks if the given key exists in the map, failing the test if it does not.

func AssertResourceChangesMapKeyExists added in v0.32.19

func AssertResourceChangesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

AssertResourceChangesMapKeyExists checks if the given key exists in the map, failing the test if it does not.

func Destroy deprecated

func Destroy(t testing.TestingT, options *Options) string

Destroy runs terraform destroy with the given options and return stdout/stderr.

Deprecated: Use DestroyContext instead.

func DestroyContext added in v1.0.0

func DestroyContext(t testing.TestingT, ctx context.Context, options *Options) string

DestroyContext runs terraform destroy with the given options and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func DestroyContextE added in v1.0.0

func DestroyContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

DestroyContextE runs terraform destroy with the given options and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func DestroyE deprecated

func DestroyE(t testing.TestingT, options *Options) (string, error)

DestroyE runs terraform destroy with the given options and return stdout/stderr.

Deprecated: Use DestroyContextE instead.

func FormatArgs

func FormatArgs(options *Options, args ...string) []string

FormatArgs converts the inputs to a format palatable to terraform. This includes converting the given vars to the format the Terraform CLI expects (-var key=value).

func FormatTerraformArgs added in v0.13.27

func FormatTerraformArgs(argName string, args []string) []string

FormatTerraformArgs will format multiple args with the arg name (e.g. "-var-file", []string{"foo.tfvars", "bar.tfvars", "baz.tfvars.json"}) returns "-var-file foo.tfvars -var-file bar.tfvars -var-file baz.tfvars.json"

func FormatTerraformBackendConfigAsArgs added in v0.9.13

func FormatTerraformBackendConfigAsArgs(vars map[string]any) []string

FormatTerraformBackendConfigAsArgs formats the given variables as backend config args for Terraform (e.g. of the format -backend-config=key=value).

func FormatTerraformLockAsArgs added in v0.23.1

func FormatTerraformLockAsArgs(lockCheck bool, lockTimeout string) []string

FormatTerraformLockAsArgs formats the lock and lock-timeout variables -lock, -lock-timeout

func FormatTerraformPlanFileAsArg added in v0.30.16

func FormatTerraformPlanFileAsArg(commandType string, outPath string) []string

FormatTerraformPlanFileAsArg formats the out variable as a command-line arg for Terraform (e.g. of the format -out=/some/path/to/plan.out or /some/path/to/plan.out). Only plan supports passing in the plan file as -out; the other commands expect it as the first positional argument. This returns an empty string if outPath is empty string.

func FormatTerraformPluginDirAsArgs added in v0.32.13

func FormatTerraformPluginDirAsArgs(pluginDir string) []string

FormatTerraformPluginDirAsArgs formats the plugin-dir variable -plugin-dir

func FormatTerraformVarsAsArgs

func FormatTerraformVarsAsArgs(vars map[string]any) []string

FormatTerraformVarsAsArgs formats the given variables as command-line args for Terraform (e.g. of the format -var key=value).

func Get deprecated

func Get(t testing.TestingT, options *Options) string

Get calls terraform get and return stdout/stderr.

Deprecated: Use GetContext instead.

func GetAllVariablesFromVarFile added in v0.32.6

func GetAllVariablesFromVarFile(t testing.TestingT, fileName string, out any)

GetAllVariablesFromVarFile Parses all data from a provided input file found ind in VarFile and stores the result in the value pointed to by out.

func GetAllVariablesFromVarFileE added in v0.32.6

func GetAllVariablesFromVarFileE(t testing.TestingT, fileName string, out any) error

GetAllVariablesFromVarFileE Parses all data from a provided input file found ind in VarFile and stores the result in the value pointed to by out. Returns an error if the specified file does not exist, the specified file is not readable, or the specified file cannot be decoded from HCL.

func GetContext added in v1.0.0

func GetContext(t testing.TestingT, ctx context.Context, options *Options) string

GetContext calls terraform get and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func GetContextE added in v1.0.0

func GetContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

GetContextE calls terraform get and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func GetE deprecated

func GetE(t testing.TestingT, options *Options) (string, error)

GetE calls terraform get and return stdout/stderr.

Deprecated: Use GetContextE instead.

func GetExitCodeForTerraformCommand deprecated added in v0.10.4

func GetExitCodeForTerraformCommand(t testing.TestingT, additionalOptions *Options, args ...string) int

GetExitCodeForTerraformCommand runs terraform with the given arguments and options and returns exit code.

Deprecated: Use GetExitCodeForTerraformCommandContext instead.

func GetExitCodeForTerraformCommandContext added in v1.0.0

func GetExitCodeForTerraformCommandContext(t testing.TestingT, ctx context.Context, additionalOptions *Options, args ...string) int

GetExitCodeForTerraformCommandContext runs terraform with the given arguments and options and returns exit code.

func GetExitCodeForTerraformCommandContextE added in v1.0.0

func GetExitCodeForTerraformCommandContextE(t testing.TestingT, ctx context.Context, additionalOptions *Options, additionalArgs ...string) (int, error)

GetExitCodeForTerraformCommandContextE runs terraform with the given arguments and options and returns exit code.

func GetExitCodeForTerraformCommandE deprecated added in v0.10.4

func GetExitCodeForTerraformCommandE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (int, error)

GetExitCodeForTerraformCommandE runs terraform with the given arguments and options and returns exit code.

Deprecated: Use GetExitCodeForTerraformCommandContextE instead.

func GetVariableAsListFromVarFile added in v0.32.6

func GetVariableAsListFromVarFile(t testing.TestingT, fileName string, key string) []string

GetVariableAsListFromVarFile Gets the string list representation of a variable from a provided input file found in VarFile Note that this returns a list of strings. For lists containing complex types, use GetAllVariablesFromVarFile.

func GetVariableAsListFromVarFileE added in v0.32.6

func GetVariableAsListFromVarFileE(t testing.TestingT, fileName string, key string) ([]string, error)

GetVariableAsListFromVarFileE Gets the string list representation of a variable from a provided input file found in VarFile Note that this returns a list of strings. For lists containing complex types, use GetAllVariablesFromVarFile. Will return error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a list

func GetVariableAsMapFromVarFile added in v0.32.6

func GetVariableAsMapFromVarFile(t testing.TestingT, fileName string, key string) map[string]string

GetVariableAsMapFromVarFile Gets the map representation of a variable from a provided input file found in VarFile Note that this returns a map of strings. For maps containing complex types, use GetAllVariablesFromVarFile.

func GetVariableAsMapFromVarFileE added in v0.32.6

func GetVariableAsMapFromVarFileE(t testing.TestingT, fileName string, key string) (map[string]string, error)

GetVariableAsMapFromVarFileE Gets the map representation of a variable from a provided input file found in VarFile. Note that this returns a map of strings. For maps containing complex types, use GetAllVariablesFromVarFile Returns an error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a map

func GetVariableAsStringFromVarFile added in v0.32.6

func GetVariableAsStringFromVarFile(t testing.TestingT, fileName string, key string) string

GetVariableAsStringFromVarFile Gets the string representation of a variable from a provided input file found in VarFile For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.

func GetVariableAsStringFromVarFileE added in v0.32.6

func GetVariableAsStringFromVarFileE(t testing.TestingT, fileName string, key string) (string, error)

GetVariableAsStringFromVarFileE Gets the string representation of a variable from a provided input file found in VarFile Will return an error if GetAllVariablesFromVarFileE returns an error or the key provided does not exist in the file. For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.

func HCLFileToJSONFile added in v0.38.1

func HCLFileToJSONFile(hclPath, jsonOutPath string) error

HCLFileToJSONFile is a function that takes a path containing HCL code, and converts it to JSON representation and writes out the contents to the given path.

func Init deprecated

func Init(t testing.TestingT, options *Options) string

Init calls terraform init and return stdout/stderr.

Deprecated: Use InitContext instead.

func InitAndApply deprecated

func InitAndApply(t testing.TestingT, options *Options) string

InitAndApply runs terraform init and apply with the given options and return stdout/stderr from the apply command. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use InitAndApplyContext instead.

func InitAndApplyAndIdempotent deprecated added in v0.28.5

func InitAndApplyAndIdempotent(t testing.TestingT, options *Options) string

InitAndApplyAndIdempotent runs terraform init and apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use InitAndApplyAndIdempotentContext instead.

func InitAndApplyAndIdempotentContext added in v1.0.0

func InitAndApplyAndIdempotentContext(t testing.TestingT, ctx context.Context, options *Options) string

InitAndApplyAndIdempotentContext runs terraform init, apply, and then plan with the given options and returns stdout/stderr from the apply command. It will fail the test if plan requires additional changes after the apply. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func InitAndApplyAndIdempotentContextE added in v1.0.0

func InitAndApplyAndIdempotentContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

InitAndApplyAndIdempotentContextE runs terraform init, apply, and then plan with the given options and returns stdout/stderr from the apply command. It will fail the test if plan requires additional changes after the apply. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func InitAndApplyAndIdempotentE deprecated added in v0.28.5

func InitAndApplyAndIdempotentE(t testing.TestingT, options *Options) (string, error)

InitAndApplyAndIdempotentE runs terraform init and apply with the given options and return stdout/stderr from the apply command. It then runs plan again and will fail the test if plan requires additional changes. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use InitAndApplyAndIdempotentContextE instead.

func InitAndApplyContext added in v1.0.0

func InitAndApplyContext(t testing.TestingT, ctx context.Context, options *Options) string

InitAndApplyContext runs terraform init and apply with the given options and returns stdout/stderr from the apply command. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func InitAndApplyContextE added in v1.0.0

func InitAndApplyContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

InitAndApplyContextE runs terraform init and apply with the given options and returns stdout/stderr from the apply command. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

func InitAndApplyE deprecated

func InitAndApplyE(t testing.TestingT, options *Options) (string, error)

InitAndApplyE runs terraform init and apply with the given options and return stdout/stderr from the apply command. Note that this method does NOT call destroy and assumes the caller is responsible for cleaning up any resources created by running apply.

Deprecated: Use InitAndApplyContextE instead.

func InitAndPlan deprecated added in v0.10.4

func InitAndPlan(t testing.TestingT, options *Options) string

InitAndPlan runs terraform init and plan with the given options and returns stdout/stderr from the plan command. This will fail the test if there is an error in the command.

Deprecated: Use InitAndPlanContext instead.

func InitAndPlanAndShow deprecated added in v0.30.16

func InitAndPlanAndShow(t testing.TestingT, options *Options) string

InitAndPlanAndShow runs terraform init, then terraform plan, and then terraform show with the given options, and returns the json output of the plan file. This will fail the test if there is an error in the command.

Deprecated: Use InitAndPlanAndShowContext instead.

func InitAndPlanAndShowContext added in v1.0.0

func InitAndPlanAndShowContext(t testing.TestingT, ctx context.Context, options *Options) string

InitAndPlanAndShowContext runs terraform init, then terraform plan, and then terraform show with the given options, and returns the json output of the plan file. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func InitAndPlanAndShowContextE added in v1.0.0

func InitAndPlanAndShowContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

InitAndPlanAndShowContextE runs terraform init, then terraform plan, and then terraform show with the given options, and returns the json output of the plan file. The context argument can be used for cancellation or timeout control.

func InitAndPlanAndShowE deprecated added in v0.30.16

func InitAndPlanAndShowE(t testing.TestingT, options *Options) (string, error)

InitAndPlanAndShowE runs terraform init, then terraform plan, and then terraform show with the given options, and returns the json output of the plan file.

Deprecated: Use InitAndPlanAndShowContextE instead.

func InitAndPlanContext added in v1.0.0

func InitAndPlanContext(t testing.TestingT, ctx context.Context, options *Options) string

InitAndPlanContext runs terraform init and plan with the given options and returns stdout/stderr from the plan command. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func InitAndPlanContextE added in v1.0.0

func InitAndPlanContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

InitAndPlanContextE runs terraform init and plan with the given options and returns stdout/stderr from the plan command. The context argument can be used for cancellation or timeout control.

func InitAndPlanE deprecated added in v0.10.4

func InitAndPlanE(t testing.TestingT, options *Options) (string, error)

InitAndPlanE runs terraform init and plan with the given options and returns stdout/stderr from the plan command.

Deprecated: Use InitAndPlanContextE instead.

func InitAndPlanWithExitCode deprecated added in v0.17.0

func InitAndPlanWithExitCode(t testing.TestingT, options *Options) int

InitAndPlanWithExitCode runs terraform init and plan with the given options and returns exitcode for the plan command. This will fail the test if there is an error in the command.

Deprecated: Use InitAndPlanWithExitCodeContext instead.

func InitAndPlanWithExitCodeContext added in v1.0.0

func InitAndPlanWithExitCodeContext(t testing.TestingT, ctx context.Context, options *Options) int

InitAndPlanWithExitCodeContext runs terraform init and plan with the given options and returns the exitcode for the plan command. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func InitAndPlanWithExitCodeContextE added in v1.0.0

func InitAndPlanWithExitCodeContextE(t testing.TestingT, ctx context.Context, options *Options) (int, error)

InitAndPlanWithExitCodeContextE runs terraform init and plan with the given options and returns the exitcode for the plan command. The context argument can be used for cancellation or timeout control.

func InitAndPlanWithExitCodeE deprecated added in v0.17.0

func InitAndPlanWithExitCodeE(t testing.TestingT, options *Options) (int, error)

InitAndPlanWithExitCodeE runs terraform init and plan with the given options and returns exitcode for the plan command.

Deprecated: Use InitAndPlanWithExitCodeContextE instead.

func InitAndValidate deprecated added in v0.32.23

func InitAndValidate(t testing.TestingT, options *Options) string

InitAndValidate runs terraform init and validate with the given options and returns stdout/stderr from the validate command. This will fail the test if there is an error in the command.

Deprecated: Use InitAndValidateContext instead.

func InitAndValidateContext added in v1.0.0

func InitAndValidateContext(t testing.TestingT, ctx context.Context, options *Options) string

InitAndValidateContext runs terraform init and validate with the given options and returns stdout/stderr from the validate command. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control. This will fail the test if there is an error in the command.

func InitAndValidateContextE added in v1.0.0

func InitAndValidateContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

InitAndValidateContextE runs terraform init and validate with the given options and returns stdout/stderr from the validate command. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func InitAndValidateE deprecated added in v0.32.23

func InitAndValidateE(t testing.TestingT, options *Options) (string, error)

InitAndValidateE runs terraform init and validate with the given options and returns stdout/stderr from the validate command.

Deprecated: Use InitAndValidateContextE instead.

func InitContext added in v1.0.0

func InitContext(t testing.TestingT, ctx context.Context, options *Options) string

InitContext calls terraform init with the given options and returns stdout/stderr. The context argument can be used for cancellation or timeout control.

func InitContextE added in v1.0.0

func InitContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

InitContextE calls terraform init with the given options and returns stdout/stderr. The context argument can be used for cancellation or timeout control.

func InitE deprecated

func InitE(t testing.TestingT, options *Options) (string, error)

InitE calls terraform init and return stdout/stderr.

Deprecated: Use InitContextE instead.

func IsExistingWorkspace added in v1.0.0

func IsExistingWorkspace(out string, name string) bool

IsExistingWorkspace checks if a workspace with the given name exists in the terraform workspace list output.

func OPAEval deprecated added in v0.38.1

func OPAEval(
	t testing.TestingT,
	tfOptions *Options,
	opaEvalOptions *opa.EvalOptions,
	resultQuery string,
)

OPAEval runs `opa eval` with the given option on the terraform files identified in the TerraformDir directory of the Options struct. Note that since OPA does not natively support parsing HCL code, we first convert all the files to JSON prior to passing it through OPA. This function fails the test if there is an error.

Deprecated: Use OPAEvalContext instead.

func OPAEvalContext added in v1.0.0

func OPAEvalContext(
	t testing.TestingT,
	ctx context.Context,
	tfOptions *Options,
	opaEvalOptions *opa.EvalOptions,
	resultQuery string,
)

OPAEvalContext runs `opa eval` with the given options on the terraform files identified in the TerraformDir directory of the Options struct. Note that since OPA does not natively support parsing HCL code, we first convert all the files to JSON prior to passing it through OPA. The context argument can be used for cancellation or timeout control. This function fails the test if there is an error.

func OPAEvalContextE added in v1.0.0

func OPAEvalContextE(
	t testing.TestingT,
	ctx context.Context,
	tfOptions *Options,
	opaEvalOptions *opa.EvalOptions,
	resultQuery string,
) error

OPAEvalContextE runs `opa eval` with the given options on the terraform files identified in the TerraformDir directory of the Options struct. Note that since OPA does not natively support parsing HCL code, we first convert all the files to JSON prior to passing it through OPA. The context argument can be used for cancellation or timeout control.

func OPAEvalE deprecated added in v0.38.1

func OPAEvalE(
	t testing.TestingT,
	tfOptions *Options,
	opaEvalOptions *opa.EvalOptions,
	resultQuery string,
) error

OPAEvalE runs `opa eval` with the given option on the terraform files identified in the TerraformDir directory of the Options struct. Note that since OPA does not natively support parsing HCL code, we first convert all the files to JSON prior to passing it through OPA.

Deprecated: Use OPAEvalContextE instead.

func Output deprecated

func Output(t testing.TestingT, options *Options, key string) string

Output calls terraform output for the given variable and return its string value representation. It only designed to work with primitive terraform types: string, number and bool. Please use OutputStruct for anything else.

Deprecated: Use OutputContext instead.

func OutputAll deprecated added in v0.13.19

func OutputAll(t testing.TestingT, options *Options) map[string]any

OutputAll calls terraform output returns all values as a map. If there is error fetching the output, fails the test

Deprecated: Use OutputAllContext instead.

func OutputAllContext added in v1.0.0

func OutputAllContext(t testing.TestingT, ctx context.Context, options *Options) map[string]any

OutputAllContext calls terraform output and returns all values as a map. If there is an error fetching the output, it fails the test. The context argument can be used for cancellation or timeout control.

func OutputAllContextE added in v1.0.0

func OutputAllContextE(t testing.TestingT, ctx context.Context, options *Options) (map[string]any, error)

OutputAllContextE calls terraform output and returns all the outputs as a map. The context argument can be used for cancellation or timeout control.

func OutputAllE deprecated added in v0.13.19

func OutputAllE(t testing.TestingT, options *Options) (map[string]any, error)

OutputAllE calls terraform and returns all the outputs as a map

Deprecated: Use OutputAllContextE instead.

func OutputContext added in v1.0.0

func OutputContext(t testing.TestingT, ctx context.Context, options *Options, key string) string

OutputContext calls terraform output for the given variable and returns its string value representation. It is only designed to work with primitive terraform types: string, number and bool. Please use OutputStructContext for anything else. The context argument can be used for cancellation or timeout control.

func OutputContextE added in v1.0.0

func OutputContextE(t testing.TestingT, ctx context.Context, options *Options, key string) (string, error)

OutputContextE calls terraform output for the given variable and returns its string value representation. It is only designed to work with primitive terraform types: string, number and bool. Please use OutputStructContextE for anything else. The context argument can be used for cancellation or timeout control.

func OutputE deprecated

func OutputE(t testing.TestingT, options *Options, key string) (string, error)

OutputE calls terraform output for the given variable and return its string value representation. It only designed to work with primitive terraform types: string, number and bool. Please use OutputStructE for anything else.

Deprecated: Use OutputContextE instead.

func OutputForKeys deprecated added in v0.13.19

func OutputForKeys(t testing.TestingT, options *Options, keys []string) map[string]any

OutputForKeys calls terraform output for the given key list and returns values as a map. If keys not found in the output, fails the test

Deprecated: Use OutputForKeysContext instead.

func OutputForKeysContext added in v1.0.0

func OutputForKeysContext(t testing.TestingT, ctx context.Context, options *Options, keys []string) map[string]any

OutputForKeysContext calls terraform output for the given key list and returns values as a map. If keys are not found in the output, it fails the test. The context argument can be used for cancellation or timeout control.

func OutputForKeysContextE added in v1.0.0

func OutputForKeysContextE(t testing.TestingT, ctx context.Context, options *Options, keys []string) (map[string]any, error)

OutputForKeysContextE calls terraform output for the given key list and returns values as a map. The returned values are of type any and need to be type casted as necessary. The context argument can be used for cancellation or timeout control.

func OutputForKeysE deprecated added in v0.13.19

func OutputForKeysE(t testing.TestingT, options *Options, keys []string) (map[string]any, error)

OutputForKeysE calls terraform output for the given key list and returns values as a map. The returned values are of type any and need to be type casted as necessary. Refer to output_test.go

Deprecated: Use OutputForKeysContextE instead.

func OutputJSON deprecated added in v1.0.0

func OutputJSON(t testing.TestingT, options *Options, key string) string

OutputJSON calls terraform output for the given variable and returns the result as the JSON string. If key is an empty string, it will return all the output variables.

Deprecated: Use OutputJSONContext instead.

func OutputJSONContext added in v1.0.0

func OutputJSONContext(t testing.TestingT, ctx context.Context, options *Options, key string) string

OutputJSONContext calls terraform output for the given variable and returns the result as a JSON string. If key is an empty string, it will return all the output variables. The context argument can be used for cancellation or timeout control.

func OutputJSONContextE added in v1.0.0

func OutputJSONContextE(t testing.TestingT, ctx context.Context, options *Options, key string) (string, error)

OutputJSONContextE calls terraform output for the given variable and returns the result as a JSON string. If key is an empty string, it will return all the output variables. The context argument can be used for cancellation or timeout control.

func OutputJSONE deprecated added in v1.0.0

func OutputJSONE(t testing.TestingT, options *Options, key string) (string, error)

OutputJSONE calls terraform output for the given variable and returns the result as the JSON string. If key is an empty string, it will return all the output variables.

Deprecated: Use OutputJSONContextE instead.

func OutputJson deprecated added in v0.31.1

func OutputJson(t testing.TestingT, options *Options, key string) string

OutputJson calls terraform output for the given variable and returns the result as the JSON string. If key is an empty string, it will return all the output variables.

Deprecated: Use OutputJSONContext instead.

func OutputJsonE deprecated added in v0.31.1

func OutputJsonE(t testing.TestingT, options *Options, key string) (string, error)

OutputJsonE calls terraform output for the given variable and returns the result as the JSON string. If key is an empty string, it will return all the output variables.

Deprecated: Use OutputJSONContextE instead.

func OutputList deprecated added in v0.13.1

func OutputList(t testing.TestingT, options *Options, key string) []string

OutputList calls terraform output for the given variable and returns its value as a list. If the output value is not a list type, then it fails the test.

Deprecated: Use OutputListContext instead.

func OutputListContext added in v1.0.0

func OutputListContext(t testing.TestingT, ctx context.Context, options *Options, key string) []string

OutputListContext calls terraform output for the given variable and returns its value as a list. If the output value is not a list type, then it fails the test. The context argument can be used for cancellation or timeout control.

func OutputListContextE added in v1.0.0

func OutputListContextE(t testing.TestingT, ctx context.Context, options *Options, key string) ([]string, error)

OutputListContextE calls terraform output for the given variable and returns its value as a list. If the output value is not a list type, then it returns an error. The context argument can be used for cancellation or timeout control.

func OutputListE deprecated added in v0.13.1

func OutputListE(t testing.TestingT, options *Options, key string) ([]string, error)

OutputListE calls terraform output for the given variable and returns its value as a list. If the output value is not a list type, then it returns an error.

Deprecated: Use OutputListContextE instead.

func OutputListOfObjects deprecated added in v0.28.2

func OutputListOfObjects(t testing.TestingT, options *Options, key string) []map[string]any

OutputListOfObjects calls terraform output for the given variable and returns its value as a list of maps/lists. If the output value is not a list of maps/lists, then it fails the test.

Deprecated: Use OutputListOfObjectsContext instead.

func OutputListOfObjectsContext added in v1.0.0

func OutputListOfObjectsContext(t testing.TestingT, ctx context.Context, options *Options, key string) []map[string]any

OutputListOfObjectsContext calls terraform output for the given variable and returns its value as a list of maps/lists. If the output value is not a list of maps/lists, then it fails the test. The context argument can be used for cancellation or timeout control.

func OutputListOfObjectsContextE added in v1.0.0

func OutputListOfObjectsContextE(t testing.TestingT, ctx context.Context, options *Options, key string) ([]map[string]any, error)

OutputListOfObjectsContextE calls terraform output for the given variable and returns its value as a list of maps/lists. Also returns an error object if an error was generated. If the output value is not a list of maps/lists, then it returns an error. The context argument can be used for cancellation or timeout control.

func OutputListOfObjectsE deprecated added in v0.28.2

func OutputListOfObjectsE(t testing.TestingT, options *Options, key string) ([]map[string]any, error)

OutputListOfObjectsE calls terraform output for the given variable and returns its value as a list of maps/lists. Also returns an error object if an error was generated. If the output value is not a list of maps/lists, then it fails the test.

Deprecated: Use OutputListOfObjectsContextE instead.

func OutputMap deprecated added in v0.13.6

func OutputMap(t testing.TestingT, options *Options, key string) map[string]string

OutputMap calls terraform output for the given variable and returns its value as a map. If the output value is not a map type, then it fails the test.

Deprecated: Use OutputMapContext instead.

func OutputMapContext added in v1.0.0

func OutputMapContext(t testing.TestingT, ctx context.Context, options *Options, key string) map[string]string

OutputMapContext calls terraform output for the given variable and returns its value as a map. If the output value is not a map type, then it fails the test. The context argument can be used for cancellation or timeout control.

func OutputMapContextE added in v1.0.0

func OutputMapContextE(t testing.TestingT, ctx context.Context, options *Options, key string) (map[string]string, error)

OutputMapContextE calls terraform output for the given variable and returns its value as a map. If the output value is not a map type, then it returns an error. The context argument can be used for cancellation or timeout control.

func OutputMapE deprecated added in v0.13.6

func OutputMapE(t testing.TestingT, options *Options, key string) (map[string]string, error)

OutputMapE calls terraform output for the given variable and returns its value as a map. If the output value is not a map type, then it returns an error.

Deprecated: Use OutputMapContextE instead.

func OutputMapOfObjects deprecated added in v0.28.2

func OutputMapOfObjects(t testing.TestingT, options *Options, key string) map[string]any

OutputMapOfObjects calls terraform output for the given variable and returns its value as a map of lists/maps. If the output value is not a map of lists/maps, then it fails the test.

Deprecated: Use OutputMapOfObjectsContext instead.

func OutputMapOfObjectsContext added in v1.0.0

func OutputMapOfObjectsContext(t testing.TestingT, ctx context.Context, options *Options, key string) map[string]any

OutputMapOfObjectsContext calls terraform output for the given variable and returns its value as a map of lists/maps. If the output value is not a map of lists/maps, then it fails the test. The context argument can be used for cancellation or timeout control.

func OutputMapOfObjectsContextE added in v1.0.0

func OutputMapOfObjectsContextE(t testing.TestingT, ctx context.Context, options *Options, key string) (map[string]any, error)

OutputMapOfObjectsContextE calls terraform output for the given variable and returns its value as a map of lists/maps. Also returns an error object if an error was generated. If the output value is not a map of lists/maps, then it returns an error. The context argument can be used for cancellation or timeout control.

func OutputMapOfObjectsE deprecated added in v0.28.2

func OutputMapOfObjectsE(t testing.TestingT, options *Options, key string) (map[string]any, error)

OutputMapOfObjectsE calls terraform output for the given variable and returns its value as a map of lists/maps. Also returns an error object if an error was generated. If the output value is not a map of lists/maps, then it fails the test.

Deprecated: Use OutputMapOfObjectsContextE instead.

func OutputRequired deprecated

func OutputRequired(t testing.TestingT, options *Options, key string) string

OutputRequired calls terraform output for the given variable and return its value. If the value is empty, fail the test.

Deprecated: Use OutputRequiredContext instead.

func OutputRequiredContext added in v1.0.0

func OutputRequiredContext(t testing.TestingT, ctx context.Context, options *Options, key string) string

OutputRequiredContext calls terraform output for the given variable and returns its value. If the value is empty, the test is failed. The context argument can be used for cancellation or timeout control.

func OutputRequiredContextE added in v1.0.0

func OutputRequiredContextE(t testing.TestingT, ctx context.Context, options *Options, key string) (string, error)

OutputRequiredContextE calls terraform output for the given variable and returns its value. If the value is empty, an error is returned. The context argument can be used for cancellation or timeout control.

func OutputRequiredE deprecated

func OutputRequiredE(t testing.TestingT, options *Options, key string) (string, error)

OutputRequiredE calls terraform output for the given variable and return its value. If the value is empty, return an error.

Deprecated: Use OutputRequiredContextE instead.

func OutputStruct deprecated added in v0.28.8

func OutputStruct(t testing.TestingT, options *Options, key string, v any)

OutputStruct calls terraform output for the given variable and stores the result in the value pointed to by v. If v is nil or not a pointer, or if the value returned by Terraform is not appropriate for a given target type, it fails the test.

Deprecated: Use OutputStructContext instead.

func OutputStructContext added in v1.0.0

func OutputStructContext(t testing.TestingT, ctx context.Context, options *Options, key string, v any)

OutputStructContext calls terraform output for the given variable and stores the result in the value pointed to by v. If v is nil or not a pointer, or if the value returned by Terraform is not appropriate for a given target type, it fails the test. The context argument can be used for cancellation or timeout control.

func OutputStructContextE added in v1.0.0

func OutputStructContextE(t testing.TestingT, ctx context.Context, options *Options, key string, v any) error

OutputStructContextE calls terraform output for the given variable and stores the result in the value pointed to by v. If v is nil or not a pointer, or if the value returned by Terraform is not appropriate for a given target type, it returns an error. The context argument can be used for cancellation or timeout control.

func OutputStructE deprecated added in v0.28.8

func OutputStructE(t testing.TestingT, options *Options, key string, v any) error

OutputStructE calls terraform output for the given variable and stores the result in the value pointed to by v. If v is nil or not a pointer, or if the value returned by Terraform is not appropriate for a given target type, it returns an error.

Deprecated: Use OutputStructContextE instead.

func Plan deprecated added in v0.17.0

func Plan(t testing.TestingT, options *Options) string

Plan runs terraform plan with the given options and returns stdout/stderr. This will fail the test if there is an error in the command.

Deprecated: Use PlanContext instead.

func PlanContext added in v1.0.0

func PlanContext(t testing.TestingT, ctx context.Context, options *Options) string

PlanContext runs terraform plan with the given options and returns stdout/stderr. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func PlanContextE added in v1.0.0

func PlanContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

PlanContextE runs terraform plan with the given options and returns stdout/stderr. The context argument can be used for cancellation or timeout control.

func PlanE deprecated added in v0.17.0

func PlanE(t testing.TestingT, options *Options) (string, error)

PlanE runs terraform plan with the given options and returns stdout/stderr.

Deprecated: Use PlanContextE instead.

func PlanExitCode deprecated added in v0.10.4

func PlanExitCode(t testing.TestingT, options *Options) int

PlanExitCode runs terraform plan with the given options and returns the detailed exitcode. This will fail the test if there is an error in the command.

Deprecated: Use PlanExitCodeContext instead.

func PlanExitCodeContext added in v1.0.0

func PlanExitCodeContext(t testing.TestingT, ctx context.Context, options *Options) int

PlanExitCodeContext runs terraform plan with the given options and returns the detailed exitcode. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func PlanExitCodeContextE added in v1.0.0

func PlanExitCodeContextE(t testing.TestingT, ctx context.Context, options *Options) (int, error)

PlanExitCodeContextE runs terraform plan with the given options and returns the detailed exitcode. The context argument can be used for cancellation or timeout control.

func PlanExitCodeE deprecated added in v0.10.4

func PlanExitCodeE(t testing.TestingT, options *Options) (int, error)

PlanExitCodeE runs terraform plan with the given options and returns the detailed exitcode.

Deprecated: Use PlanExitCodeContextE instead.

func RequirePlannedValuesMapKeyExists added in v0.32.19

func RequirePlannedValuesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

RequirePlannedValuesMapKeyExists checks if the given key exists in the map, failing and halting the test if it does not.

func RequireResourceChangesMapKeyExists added in v0.32.19

func RequireResourceChangesMapKeyExists(t testing.TestingT, plan *PlanStruct, keyQuery string)

RequireResourceChangesMapKeyExists checks if the given key exists in the map, failing the test if it does not.

func RunTerraformCommand deprecated added in v0.9.2

func RunTerraformCommand(t testing.TestingT, additionalOptions *Options, args ...string) string

RunTerraformCommand runs terraform with the given arguments and options and return stdout/stderr.

Deprecated: Use RunTerraformCommandContext instead.

func RunTerraformCommandAndGetStdOutErrCode deprecated added in v0.49.0

func RunTerraformCommandAndGetStdOutErrCode(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (stdout string, stderr string, exit int)

RunTerraformCommandAndGetStdOutErrCode runs terraform with the given arguments and options and returns its stdout, stderr, and exitcode.

Deprecated: Use RunTerraformCommandAndGetStdOutErrCodeContext instead.

func RunTerraformCommandAndGetStdOutErrCodeContext added in v1.0.0

func RunTerraformCommandAndGetStdOutErrCodeContext(t testing.TestingT, ctx context.Context, additionalOptions *Options, additionalArgs ...string) (stdout string, stderr string, exit int)

RunTerraformCommandAndGetStdOutErrCodeContext runs terraform with the given arguments and options and returns its stdout, stderr, and exitcode.

func RunTerraformCommandAndGetStdOutErrCodeContextE added in v1.0.0

func RunTerraformCommandAndGetStdOutErrCodeContextE(t testing.TestingT, ctx context.Context, additionalOptions *Options, additionalArgs ...string) (stdout string, stderr string, exit int, err error)

RunTerraformCommandAndGetStdOutErrCodeContextE runs terraform with the given arguments and options and returns its stdout, stderr, and exitcode.

func RunTerraformCommandAndGetStdOutErrCodeE deprecated added in v0.49.0

func RunTerraformCommandAndGetStdOutErrCodeE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (stdout string, stderr string, exit int, err error)

RunTerraformCommandAndGetStdOutErrCodeE runs terraform with the given arguments and options and returns its stdout, stderr, and exitcode.

Deprecated: Use RunTerraformCommandAndGetStdOutErrCodeContextE instead.

func RunTerraformCommandAndGetStdout deprecated added in v0.49.0

func RunTerraformCommandAndGetStdout(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) string

RunTerraformCommandAndGetStdout runs terraform with the given arguments and options and returns solely its stdout (but not stderr).

Deprecated: Use RunTerraformCommandAndGetStdoutContext instead.

func RunTerraformCommandAndGetStdoutContext added in v1.0.0

func RunTerraformCommandAndGetStdoutContext(t testing.TestingT, ctx context.Context, additionalOptions *Options, additionalArgs ...string) string

RunTerraformCommandAndGetStdoutContext runs terraform with the given arguments and options and returns solely its stdout (but not stderr).

func RunTerraformCommandAndGetStdoutContextE added in v1.0.0

func RunTerraformCommandAndGetStdoutContextE(t testing.TestingT, ctx context.Context, additionalOptions *Options, additionalArgs ...string) (string, error)

RunTerraformCommandAndGetStdoutContextE runs terraform with the given arguments and options and returns solely its stdout (but not stderr).

func RunTerraformCommandAndGetStdoutE deprecated added in v0.17.2

func RunTerraformCommandAndGetStdoutE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (string, error)

RunTerraformCommandAndGetStdoutE runs terraform with the given arguments and options and returns solely its stdout (but not stderr).

Deprecated: Use RunTerraformCommandAndGetStdoutContextE instead.

func RunTerraformCommandContext added in v1.0.0

func RunTerraformCommandContext(t testing.TestingT, ctx context.Context, additionalOptions *Options, args ...string) string

RunTerraformCommandContext runs terraform with the given arguments and options and returns stdout/stderr.

func RunTerraformCommandContextE added in v1.0.0

func RunTerraformCommandContextE(t testing.TestingT, ctx context.Context, additionalOptions *Options, additionalArgs ...string) (string, error)

RunTerraformCommandContextE runs terraform with the given arguments and options and returns stdout/stderr.

func RunTerraformCommandE deprecated added in v0.9.2

func RunTerraformCommandE(t testing.TestingT, additionalOptions *Options, additionalArgs ...string) (string, error)

RunTerraformCommandE runs terraform with the given arguments and options and return stdout/stderr.

Deprecated: Use RunTerraformCommandContextE instead.

func Show deprecated added in v0.30.16

func Show(t testing.TestingT, options *Options) string

Show calls terraform show in json mode with the given options and returns stdout from the command. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir. This will fail the test if there is an error in the command.

Deprecated: Use ShowContext instead.

func ShowContext added in v1.0.0

func ShowContext(t testing.TestingT, ctx context.Context, options *Options) string

ShowContext calls terraform show in json mode with the given options and returns stdout from the command. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func ShowContextE added in v1.0.0

func ShowContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

ShowContextE calls terraform show in json mode with the given options and returns stdout from the command. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir. The context argument can be used for cancellation or timeout control.

func ShowE deprecated added in v0.30.16

func ShowE(t testing.TestingT, options *Options) (string, error)

ShowE calls terraform show in json mode with the given options and returns stdout from the command. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir.

Deprecated: Use ShowContextE instead.

func Validate deprecated added in v0.32.23

func Validate(t testing.TestingT, options *Options) string

Validate calls terraform validate and returns stdout/stderr.

Deprecated: Use ValidateContext instead.

func ValidateContext added in v1.0.0

func ValidateContext(t testing.TestingT, ctx context.Context, options *Options) string

ValidateContext calls terraform validate and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func ValidateContextE added in v1.0.0

func ValidateContextE(t testing.TestingT, ctx context.Context, options *Options) (string, error)

ValidateContextE calls terraform validate and returns stdout/stderr. The provided context is passed through to the underlying command execution, allowing for timeout and cancellation control.

func ValidateE deprecated added in v0.32.23

func ValidateE(t testing.TestingT, options *Options) (string, error)

ValidateE calls terraform validate and returns stdout/stderr.

Deprecated: Use ValidateContextE instead.

func WorkspaceDelete deprecated added in v0.32.17

func WorkspaceDelete(t testing.TestingT, options *Options, name string) string

WorkspaceDelete removes the specified terraform workspace with the given options. It returns the name of the current workspace AFTER deletion. If the workspace to delete is the current one, then it tries to switch to the "default" workspace. Deleting the workspace "default" is not supported and only return an empty string (to avoid a fatal error).

Deprecated: Use WorkspaceDeleteContext instead.

func WorkspaceDeleteContext added in v1.0.0

func WorkspaceDeleteContext(t testing.TestingT, ctx context.Context, options *Options, name string) string

WorkspaceDeleteContext removes the specified terraform workspace with the given options. It returns the name of the current workspace AFTER deletion. If the workspace to delete is the current one, then it tries to switch to the "default" workspace. Deleting the workspace "default" is not supported and only returns an empty string (to avoid a fatal error). The context argument can be used for cancellation or timeout control.

func WorkspaceDeleteContextE added in v1.0.0

func WorkspaceDeleteContextE(t testing.TestingT, ctx context.Context, options *Options, name string) (string, error)

WorkspaceDeleteContextE removes the specified terraform workspace with the given options. It returns the name of the current workspace AFTER deletion, and the returned error (that can be nil). If the workspace to delete is the current one, then it tries to switch to the "default" workspace. Deleting the workspace "default" is not supported. The context argument can be used for cancellation or timeout control.

func WorkspaceDeleteE deprecated added in v0.32.17

func WorkspaceDeleteE(t testing.TestingT, options *Options, name string) (string, error)

WorkspaceDeleteE removes the specified terraform workspace with the given options. It returns the name of the current workspace AFTER deletion, and the returned error (that can be nil). If the workspace to delete is the current one, then it tries to switch to the "default" workspace. Deleting the workspace "default" is not supported.

Deprecated: Use WorkspaceDeleteContextE instead.

func WorkspaceSelectOrNew deprecated added in v0.12.2

func WorkspaceSelectOrNew(t testing.TestingT, options *Options, name string) string

WorkspaceSelectOrNew runs terraform workspace with the given options and the workspace name and returns a name of the current workspace. It tries to select a workspace with the given name, or it creates a new one if it doesn't exist.

Deprecated: Use WorkspaceSelectOrNewContext instead.

func WorkspaceSelectOrNewContext added in v1.0.0

func WorkspaceSelectOrNewContext(t testing.TestingT, ctx context.Context, options *Options, name string) string

WorkspaceSelectOrNewContext runs terraform workspace with the given options and the workspace name and returns the name of the current workspace. It tries to select a workspace with the given name, or it creates a new one if it doesn't exist. The context argument can be used for cancellation or timeout control.

func WorkspaceSelectOrNewContextE added in v1.0.0

func WorkspaceSelectOrNewContextE(t testing.TestingT, ctx context.Context, options *Options, name string) (string, error)

WorkspaceSelectOrNewContextE runs terraform workspace with the given options and the workspace name and returns the name of the current workspace. It tries to select a workspace with the given name, or it creates a new one if it doesn't exist. The context argument can be used for cancellation or timeout control.

func WorkspaceSelectOrNewE deprecated added in v0.12.2

func WorkspaceSelectOrNewE(t testing.TestingT, options *Options, name string) (string, error)

WorkspaceSelectOrNewE runs terraform workspace with the given options and the workspace name and returns a name of the current workspace. It tries to select a workspace with the given name, or it creates a new one if it doesn't exist.

Deprecated: Use WorkspaceSelectOrNewContextE instead.

Types

type CtyJSONOutput added in v1.0.0

type CtyJSONOutput struct {
	Value map[string]any `json:"value"`
	Type  any            `json:"type"`
}

CtyJSONOutput captures the output of marshalling a cty value to JSON. When you convert a cty value to JSON, if any of the types are not yet known (i.e., are labeled as DynamicPseudoType), cty's Marshal method will write the type information to a type field and the actual value to a value field. This struct is used to capture that information so when we parse the JSON back into a Go struct, we can pull out just the Value field we need.

type CtyJsonOutput deprecated added in v0.32.8

type CtyJsonOutput = CtyJSONOutput //nolint:revive,staticcheck // preserving deprecated type name

CtyJsonOutput is a backwards-compatible alias for CtyJSONOutput.

Deprecated: Use CtyJSONOutput instead.

type EmptyOutput

type EmptyOutput string

EmptyOutput is an error that occurs when an output is empty.

func (EmptyOutput) Error

func (outputName EmptyOutput) Error() string

type ExtraArgs added in v0.49.0

type ExtraArgs struct {
	Apply           []string
	Destroy         []string
	Get             []string
	Init            []string
	Plan            []string
	Validate        []string
	WorkspaceDelete []string
	WorkspaceSelect []string
	WorkspaceNew    []string
	Output          []string
	Show            []string
}

ExtraArgs holds additional command-line arguments to pass to specific Terraform subcommands. Each field is appended to the corresponding subcommand invocation (e.g. Apply args are appended to `terraform apply`).

type InputFileKeyNotFound added in v0.32.6

type InputFileKeyNotFound struct {
	FilePath string
	Key      string
}

InputFileKeyNotFound occurs when tfvar file does not contain a value for the key specified in the function call

func (InputFileKeyNotFound) Error added in v0.32.6

func (err InputFileKeyNotFound) Error() string

type Options

type Options struct {
	Stdin            io.Reader         // Optional stdin to pass to Terraform commands
	WarningsAsErrors map[string]string // Terraform warning messages that should be treated as errors. The keys are a regexp to match against the warning and the value is what to display to a user if that warning is matched.
	Logger           *logger.Logger    // Set a non-default logger that should be used. See the logger package for more info.
	BackendConfig    map[string]any    // The vars to pass to the terraform init command for extra configuration for the backend. If a var is nil, it will be formated as `--backend-config=var` instead of `--backend-config=var=null`
	EnvVars          map[string]string // Environment variables to set when running Terraform

	// The vars to pass to Terraform commands using the -var option. Note that terraform does not support passing `null`
	// as a variable value through the command line. That is, if you use `map[string]any{"foo": nil}` as `Vars`,
	// this will translate to the string literal `"null"` being assigned to the variable `foo`. However, nulls in
	// lists and maps/objects are supported. E.g., the following var will be set as expected (`{ bar = null }`:
	// map[string]any{
	//     "foo": map[string]any{"bar": nil},
	// }
	Vars map[string]any

	RetryableTerraformErrors map[string]string // If Terraform apply fails with one of these (transient) errors, retry. The keys are a regexp to match against the error and the message is what to display to a user if that error is matched.
	SshAgent                 *ssh.SSHAgent     //nolint:revive,staticcheck // preserving deprecated field name. Overrides local SSH agent with the given in-process agent
	TerraformBinary          string            // Name of the binary that will be used
	TerraformDir             string            // The path to the folder where the Terraform code is defined.
	PlanFilePath             string            // The path to output a plan file to (for the plan command) or read one from (for the apply command)
	PluginDir                string            // The path of downloaded plugins to pass to the terraform init command (-plugin-dir)
	LockTimeout              string            // The lock timeout option to pass to the terraform command with -lock-timeout
	ExtraArgs                ExtraArgs         // Extra arguments passed to Terraform commands
	Targets                  []string          // The target resources to pass to the terraform command with -target
	MixedVars                []Var             // Mix of `-var` and `-var-file` in arbritrary order, use `VarInline()` `VarFile()` to set the value.
	VarFiles                 []string          // The var file paths to pass to Terraform commands using -var-file option.
	TimeBetweenRetries       time.Duration     // The amount of time to wait between retries
	Parallelism              int               // Set the parallelism setting for Terraform
	OutputMaxLineSize        int               // The max size of one line in stdout and stderr (in bytes)
	MaxRetries               int               // Maximum number of times to retry errors matching RetryableTerraformErrors
	NoStderr                 bool              // Disable stderr redirection
	NoColor                  bool              // Whether the -no-color flag will be set for any Terraform command or not
	MigrateState             bool              // Set the -migrate-state and -force-copy (suppress 'yes' answer prompt) flag to the terraform init command
	Reconfigure              bool              // Set the -reconfigure flag to the terraform init command
	Upgrade                  bool              // Whether the -upgrade flag of the terraform init command should be set to true or not
	SetVarsAfterVarFiles     bool              // Pass -var options after -var-file options to Terraform commands
	Lock                     bool              // The lock option to pass to the terraform command with -lock
}

Options for running Terraform commands

func GetCommonOptions added in v0.10.4

func GetCommonOptions(options *Options, args ...string) (*Options, []string)

GetCommonOptions extracts commons terraform options

func WithDefaultRetryableErrors added in v0.30.15

func WithDefaultRetryableErrors(t testing.TestingT, originalOptions *Options) *Options

WithDefaultRetryableErrors makes a copy of the Options object and returns an updated object with sensible defaults for retryable errors. The included retryable errors are typical errors that most terraform modules encounter during testing, and are known to self resolve upon retrying. This will fail the test if there are any errors in the cloning process.

func (*Options) Clone added in v0.30.15

func (options *Options) Clone() (*Options, error)

Clone makes a deep copy of most fields on the Options object and returns it.

NOTE: options.SshAgent and options.Logger CANNOT be deep copied (e.g., the SshAgent struct contains channels and listeners that can't be meaningfully copied), so the original values are retained.

type OutputKeyNotFound added in v0.15.12

type OutputKeyNotFound string

OutputKeyNotFound occurs when terraform output does not contain a value for the key specified in the function call

func (OutputKeyNotFound) Error added in v0.15.12

func (err OutputKeyNotFound) Error() string

type OutputValueNotList added in v0.15.12

type OutputValueNotList struct {
	Value any
}

OutputValueNotList occurs when casting a found output value to a list of interfaces fails

func (OutputValueNotList) Error added in v0.15.12

func (err OutputValueNotList) Error() string

type OutputValueNotMap added in v0.15.12

type OutputValueNotMap struct {
	Value any
}

OutputValueNotMap occurs when casting a found output value to a map fails

func (OutputValueNotMap) Error added in v0.15.12

func (err OutputValueNotMap) Error() string

type PanicWhileParsingVarFile added in v0.32.8

type PanicWhileParsingVarFile struct {
	RecoveredValue any
	ConfigFile     string
}

PanicWhileParsingVarFile is returned when the HCL parsing routine panics due to errors.

func (PanicWhileParsingVarFile) Error added in v0.32.8

func (err PanicWhileParsingVarFile) Error() string

type PlanStruct added in v0.32.19

type PlanStruct struct {
	// A map that maps full resource addresses (e.g., module.foo.null_resource.test) to the planned values of that
	// resource.
	ResourcePlannedValuesMap map[string]*tfjson.StateResource

	// A map that maps full resource addresses (e.g., module.foo.null_resource.test) to the planned actions terraform
	// will take on that resource.
	ResourceChangesMap map[string]*tfjson.ResourceChange

	// The raw representation of the plan. See
	// https://www.terraform.io/docs/internals/json-format.html#plan-representation for details on the structure of the
	// plan output.
	RawPlan tfjson.Plan
}

PlanStruct is a Go Struct representation of the plan object returned from Terraform (after running `terraform show`). Unlike the raw plan representation returned by terraform-json, this struct provides a map that maps the resource addresses to the changes and planned values to make it easier to navigate the raw plan struct.

func InitAndPlanAndShowWithStruct deprecated added in v0.32.19

func InitAndPlanAndShowWithStruct(t testing.TestingT, options *Options) *PlanStruct

InitAndPlanAndShowWithStruct runs terraform init, then terraform plan, and then terraform show with the given options, and parses the json result into a go struct. This will fail the test if there is an error in the command.

Deprecated: Use InitAndPlanAndShowWithStructContext instead.

func InitAndPlanAndShowWithStructContext added in v1.0.0

func InitAndPlanAndShowWithStructContext(t testing.TestingT, ctx context.Context, options *Options) *PlanStruct

InitAndPlanAndShowWithStructContext runs terraform init, then terraform plan, and then terraform show with the given options, and parses the json result into a go struct. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func InitAndPlanAndShowWithStructContextE added in v1.0.0

func InitAndPlanAndShowWithStructContextE(t testing.TestingT, ctx context.Context, options *Options) (*PlanStruct, error)

InitAndPlanAndShowWithStructContextE runs terraform init, then terraform plan, and then terraform show with the given options, and parses the json result into a go struct. The context argument can be used for cancellation or timeout control.

func InitAndPlanAndShowWithStructE deprecated added in v0.32.19

func InitAndPlanAndShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error)

InitAndPlanAndShowWithStructE runs terraform init, then terraform plan, and then terraform show with the given options, and parses the json result into a go struct.

Deprecated: Use InitAndPlanAndShowWithStructContextE instead.

func InitAndPlanAndShowWithStructNoLogTempPlanFile deprecated added in v0.37.1

func InitAndPlanAndShowWithStructNoLogTempPlanFile(t testing.TestingT, options *Options) *PlanStruct

InitAndPlanAndShowWithStructNoLogTempPlanFile runs InitAndPlanAndShowWithStruct without logging and also by allocating a temporary plan file destination that is discarded before returning the struct.

Deprecated: Use InitAndPlanAndShowWithStructNoLogTempPlanFileContext instead.

func InitAndPlanAndShowWithStructNoLogTempPlanFileContext added in v1.0.0

func InitAndPlanAndShowWithStructNoLogTempPlanFileContext(t testing.TestingT, ctx context.Context, options *Options) *PlanStruct

InitAndPlanAndShowWithStructNoLogTempPlanFileContext runs InitAndPlanAndShowWithStructContext without logging and also by allocating a temporary plan file destination that is discarded before returning the struct. The context argument can be used for cancellation or timeout control.

func ParsePlanJSON added in v0.43.8

func ParsePlanJSON(jsonStr string) (*PlanStruct, error)

ParsePlanJSON takes in the json string representation of the terraform plan and returns a go struct representation for easy introspection.

func ShowWithStruct deprecated added in v0.40.16

func ShowWithStruct(t testing.TestingT, options *Options) *PlanStruct

ShowWithStruct calls terraform show in json mode with the given options, parses the json result into a PlanStruct, and returns it. This will fail the test if there is an error in the command.

Deprecated: Use ShowWithStructContext instead.

func ShowWithStructContext added in v1.0.0

func ShowWithStructContext(t testing.TestingT, ctx context.Context, options *Options) *PlanStruct

ShowWithStructContext calls terraform show in json mode with the given options, parses the json result into a PlanStruct, and returns it. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir. The context argument can be used for cancellation or timeout control. This will fail the test if there is an error in the command.

func ShowWithStructContextE added in v1.0.0

func ShowWithStructContextE(t testing.TestingT, ctx context.Context, options *Options) (*PlanStruct, error)

ShowWithStructContextE calls terraform show in json mode with the given options, parses the json result into a PlanStruct, and returns it. If PlanFilePath is set on the options, this will show the plan file. Otherwise, this will show the current state of the terraform module at options.TerraformDir. The context argument can be used for cancellation or timeout control.

func ShowWithStructE deprecated added in v0.40.16

func ShowWithStructE(t testing.TestingT, options *Options) (*PlanStruct, error)

ShowWithStructE calls terraform show in json mode with the given options, parses the json result into a PlanStruct, and returns it.

Deprecated: Use ShowWithStructContextE instead.

type ResourceCount added in v0.17.1

type ResourceCount struct {
	Add     int
	Change  int
	Destroy int
}

ResourceCount represents counts of resources affected by terraform apply/plan/destroy command.

func GetResourceCount added in v0.17.1

func GetResourceCount(t testing.TestingT, cmdout string) *ResourceCount

GetResourceCount parses stdout/stderr of apply/plan/destroy commands and returns number of affected resources. This will fail the test if given stdout/stderr isn't a valid output of apply/plan/destroy.

func GetResourceCountE added in v0.17.1

func GetResourceCountE(t testing.TestingT, cmdout string) (*ResourceCount, error)

GetResourceCountE parses stdout/stderr of apply/plan/destroy commands and returns number of affected resources.

type UnexpectedOutputType added in v0.15.13

type UnexpectedOutputType struct {
	Key          string
	ExpectedType string
	ActualType   string
}

UnexpectedOutputType is an error that occurs when the output is not of the type we expect

func (UnexpectedOutputType) Error added in v0.15.13

func (err UnexpectedOutputType) Error() string

type UnsupportedDefaultWorkspaceDeletion added in v0.32.17

type UnsupportedDefaultWorkspaceDeletion struct{}

UnsupportedDefaultWorkspaceDeletion is returned when user tries to delete the workspace "default"

func (*UnsupportedDefaultWorkspaceDeletion) Error added in v0.32.17

type Var added in v0.49.0

type Var interface {
	// Args returns the command-line arguments that pass this variable to Terraform.
	Args() []string
	// contains filtered or unexported methods
}

Var represents a Terraform variable assignment that can be rendered as command-line arguments. Use VarInline to pass an inline value with -var, or VarFile to reference a -var-file on disk.

func VarFile added in v0.49.0

func VarFile(path string) Var

VarFile returns a Var that passes the file at the given path to Terraform via a -var-file flag.

func VarInline added in v0.49.0

func VarInline(name string, value any) Var

VarInline returns a Var that passes the given name/value pair to Terraform via a -var flag.

type VarFileNotFound added in v0.32.6

type VarFileNotFound struct {
	Path string
}

VarFileNotFound is an error that occurs when a var file cannot be found in an option's VarFile list

func (VarFileNotFound) Error added in v0.32.6

func (err VarFileNotFound) Error() string

type WorkspaceDoesNotExist added in v0.32.17

type WorkspaceDoesNotExist string

WorkspaceDoesNotExist is returned when user tries to delete a workspace which does not exist

func (WorkspaceDoesNotExist) Error added in v0.32.17

func (err WorkspaceDoesNotExist) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL