Documentation ¶
Index ¶
- Constants
- func CleanupTestData(t testing.TestingT, path string)
- func CleanupTestDataFolder(t testing.TestingT, path string)
- func CleanupTestDataFolderE(t testing.TestingT, path string) error
- func CopyTerraformFolderToDest(t testing.TestingT, rootFolder string, terraformModuleFolder string, ...) string
- func CopyTerraformFolderToTemp(t testing.TestingT, rootFolder string, terraformModuleFolder string) string
- func FindTerraformModulePathsInRootE(opts *ValidationOptions) ([]string, error)
- func FormatTestDataPath(testFolder string, filename string) string
- func IsTestDataPresent(t testing.TestingT, path string) bool
- func LoadAmiId(t testing.TestingT, testFolder string) stringdeprecated
- func LoadArtifactID(t testing.TestingT, testFolder string) string
- func LoadEc2KeyPair(t testing.TestingT, testFolder string) *aws.Ec2Keypair
- func LoadInt(t testing.TestingT, testFolder string, name string) int
- func LoadKubectlOptions(t testing.TestingT, testFolder string) *k8s.KubectlOptions
- func LoadPackerOptions(t testing.TestingT, testFolder string) *packer.Options
- func LoadSshKeyPair(t testing.TestingT, testFolder string) *ssh.KeyPair
- func LoadString(t testing.TestingT, testFolder string, name string) string
- func LoadTerraformOptions(t testing.TestingT, testFolder string) *terraform.Options
- func LoadTestData(t testing.TestingT, path string, value interface{})
- func OPAEvalAllTerraformModules(t *go_test.T, opts *ValidationOptions, opaEvalOpts *opa.EvalOptions, ...)
- func RunTestStage(t testing.TestingT, stageName string, stage func())
- func SaveAmiId(t testing.TestingT, testFolder string, amiId string)deprecated
- func SaveArtifactID(t testing.TestingT, testFolder string, artifactID string)
- func SaveEc2KeyPair(t testing.TestingT, testFolder string, keyPair *aws.Ec2Keypair)
- func SaveInt(t testing.TestingT, testFolder string, name string, val int)
- func SaveKubectlOptions(t testing.TestingT, testFolder string, kubectlOptions *k8s.KubectlOptions)
- func SavePackerOptions(t testing.TestingT, testFolder string, packerOptions *packer.Options)
- func SaveSshKeyPair(t testing.TestingT, testFolder string, keyPair *ssh.KeyPair)
- func SaveString(t testing.TestingT, testFolder string, name string, val string)
- func SaveTerraformOptions(t testing.TestingT, testFolder string, terraformOptions *terraform.Options)
- func SaveTestData(t testing.TestingT, path string, value interface{})
- func SkipStageEnvVarSet() bool
- func ValidateAllTerraformModules(t *go_test.T, opts *ValidationOptions)
- type ValidateFileType
- type ValidationAbsolutePathErr
- type ValidationOptions
- type ValidationUndefinedRootDirErr
Constants ¶
const ( // TF represents repositories that contain Terraform code TF = "*.tf" // TG represents repositories that contain Terragrunt code TG = "terragrunt.hcl" )
const SKIP_STAGE_ENV_VAR_PREFIX = "SKIP_"
SKIP_STAGE_ENV_VAR_PREFIX is the prefix used for skipping stage environment variables.
Variables ¶
This section is empty.
Functions ¶
func CleanupTestData ¶
CleanupTestData cleans up the test data at the given path.
func CleanupTestDataFolder ¶
CleanupTestDataFolder cleans up the .test-data folder inside the given folder. If there are any errors, fail the test.
func CleanupTestDataFolderE ¶
CleanupTestDataFolderE cleans up the .test-data folder inside the given folder.
func CopyTerraformFolderToDest ¶
func CopyTerraformFolderToDest(t testing.TestingT, rootFolder string, terraformModuleFolder string, destRootFolder string) string
CopyTerraformFolderToDest copies the given root folder to a randomly-named temp folder and return the path to the given terraform modules folder within the new temp root folder. This is useful when running multiple tests in parallel against the same set of Terraform files to ensure the tests don't overwrite each other's .terraform working directory and terraform.tfstate files. To ensure relative paths work, we copy over the entire root folder to a temp folder, and then return the path within that temp folder to the given terraform module dir, which is where the actual test will be running. For example, suppose you had the target terraform folder you want to test in "/examples/terraform-aws-example" relative to the repo root. If your tests reside in the "/test" relative to the root, then you will use this as follows:
// Destination for the copy of the files. In this example we are using the Azure Dev Ops variable // for the folder that is cleaned after each pipeline job. destRootFolder := os.Getenv("AGENT_TEMPDIRECTORY") // Root folder where terraform files should be (relative to the test folder) rootFolder := ".." // Relative path to terraform module being tested from the root folder terraformFolderRelativeToRoot := "examples/terraform-aws-example" // Copy the terraform folder to a temp folder tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot, destRootFolder) // Make sure to use the temp test folder in the terraform options terraformOptions := &terraform.Options{ TerraformDir: tempTestFolder, }
Note that if any of the SKIP_<stage> environment variables is set, we assume this is a test in the local dev where there are no other concurrent tests running and we want to be able to cache test data between test stages, so in that case, we do NOT copy anything to a temp folder, and return the path to the original terraform module folder instead.
func CopyTerraformFolderToTemp ¶
func CopyTerraformFolderToTemp(t testing.TestingT, rootFolder string, terraformModuleFolder string) string
CopyTerraformFolderToTemp copies the given root folder to a randomly-named temp folder and return the path to the given terraform modules folder within the new temp root folder. This is useful when running multiple tests in parallel against the same set of Terraform files to ensure the tests don't overwrite each other's .terraform working directory and terraform.tfstate files. To ensure relative paths work, we copy over the entire root folder to a temp folder, and then return the path within that temp folder to the given terraform module dir, which is where the actual test will be running. For example, suppose you had the target terraform folder you want to test in "/examples/terraform-aws-example" relative to the repo root. If your tests reside in the "/test" relative to the root, then you will use this as follows:
// Root folder where terraform files should be (relative to the test folder) rootFolder := ".." // Relative path to terraform module being tested from the root folder terraformFolderRelativeToRoot := "examples/terraform-aws-example" // Copy the terraform folder to a temp folder tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, rootFolder, terraformFolderRelativeToRoot) // Make sure to use the temp test folder in the terraform options terraformOptions := &terraform.Options{ TerraformDir: tempTestFolder, }
Note that if any of the SKIP_<stage> environment variables is set, we assume this is a test in the local dev where there are no other concurrent tests running and we want to be able to cache test data between test stages, so in that case, we do NOT copy anything to a temp folder, and return the path to the original terraform module folder instead.
func FindTerraformModulePathsInRootE ¶
func FindTerraformModulePathsInRootE(opts *ValidationOptions) ([]string, error)
FindTerraformModulePathsInRootE returns a slice strings representing the filepaths for all valid Terraform / Terragrunt modules in the given RootDir, subject to the include / exclude filters.
func FormatTestDataPath ¶
FormatTestDataPath formats a path to save test data.
func IsTestDataPresent ¶
IsTestDataPresent returns true if a file exists at $path and the test data there is non-empty.
func LoadAmiId
deprecated
func LoadArtifactID ¶
LoadArtifactID loads and unserializes an Artifact ID from the given folder. This allows you to reuse an Artifact that was created during an earlier setup step in later validation and teardown steps.
func LoadEc2KeyPair ¶
func LoadEc2KeyPair(t testing.TestingT, testFolder string) *aws.Ec2Keypair
LoadEc2KeyPair loads and unserializes an Ec2KeyPair from the given folder. This allows you to reuse an Ec2KeyPair that was created during an earlier setup step in later validation and teardown steps.
func LoadInt ¶
LoadInt loads a uniquely named int value from the given folder. This allows you to reuse one or more int values that were created during an earlier setup step in later steps.
func LoadKubectlOptions ¶
func LoadKubectlOptions(t testing.TestingT, testFolder string) *k8s.KubectlOptions
LoadKubectlOptions loads and unserializes a KubectlOptions from the given folder. This allows you to reuse a KubectlOptions that was created during an earlier setup step in later validation and teardown steps.
func LoadPackerOptions ¶
LoadPackerOptions loads and unserializes PackerOptions from the given folder. This allows you to reuse a PackerOptions that was created during an earlier setup step in later validation and teardown steps.
func LoadSshKeyPair ¶
LoadSshKeyPair loads and unserializes an SshKeyPair from the given folder. This allows you to reuse an SshKeyPair that was created during an earlier setup step in later validation and teardown steps.
func LoadString ¶
LoadString loads and unserializes a uniquely named string value from the given folder. This allows you to reuse one or more string values that were created during an earlier setup step in later steps.
func LoadTerraformOptions ¶
LoadTerraformOptions loads and unserializes TerraformOptions from the given folder. This allows you to reuse a TerraformOptions that was created during an earlier setup step in later validation and teardown steps.
func LoadTestData ¶
LoadTestData loads and unserializes a value stored at the given path. The value should be a pointer to a struct into which the value will be deserialized. This allows you to reuse some sort of test data (e.g., TerraformOptions) from earlier setup steps in later validation and teardown steps.
func OPAEvalAllTerraformModules ¶
func OPAEvalAllTerraformModules( t *go_test.T, opts *ValidationOptions, opaEvalOpts *opa.EvalOptions, resultQuery string, )
OPAEvalAllTerraformModules automatically finds all folders specified in RootDir that contain .tf files and runs OPAEval in all of them. The behavior of this function is similar to ValidateAllTerraformModules. Refer to the docs of that function for more details.
func RunTestStage ¶
RunTestStage executes the given test stage (e.g., setup, teardown, validation) if an environment variable of the name `SKIP_<stageName>` (e.g., SKIP_teardown) is not set.
func SaveAmiId
deprecated
func SaveArtifactID ¶
SaveArtifactID serializes and saves an Artifact ID into the given folder. This allows you to build an Artifact during setup and to reuse that Artifact later during validation and teardown.
func SaveEc2KeyPair ¶
func SaveEc2KeyPair(t testing.TestingT, testFolder string, keyPair *aws.Ec2Keypair)
SaveEc2KeyPair serializes and saves an Ec2KeyPair into the given folder. This allows you to create an Ec2KeyPair during setup and to reuse that Ec2KeyPair later during validation and teardown.
func SaveInt ¶
SaveInt saves a uniquely named int value into the given folder. This allows you to create one or more int values during one stage -- each with a unique name -- and to reuse those values during later stages.
func SaveKubectlOptions ¶
func SaveKubectlOptions(t testing.TestingT, testFolder string, kubectlOptions *k8s.KubectlOptions)
SaveKubectlOptions serializes and saves KubectlOptions into the given folder. This allows you to create a KubectlOptions during setup and reuse that KubectlOptions later during validation and teardown.
func SavePackerOptions ¶
SavePackerOptions serializes and saves PackerOptions into the given folder. This allows you to create PackerOptions during setup and to reuse that PackerOptions later during validation and teardown.
func SaveSshKeyPair ¶
SaveSshKeyPair serializes and saves an SshKeyPair into the given folder. This allows you to create an SshKeyPair during setup and to reuse that SshKeyPair later during validation and teardown.
func SaveString ¶
SaveString serializes and saves a uniquely named string value into the given folder. This allows you to create one or more string values during one stage -- each with a unique name -- and to reuse those values during later stages.
func SaveTerraformOptions ¶
func SaveTerraformOptions(t testing.TestingT, testFolder string, terraformOptions *terraform.Options)
SaveTerraformOptions serializes and saves TerraformOptions into the given folder. This allows you to create TerraformOptions during setup and to reuse that TerraformOptions later during validation and teardown.
func SaveTestData ¶
SaveTestData serializes and saves a value used at test time to the given path. This allows you to create some sort of test data (e.g., TerraformOptions) during setup and to reuse this data later during validation and teardown.
func SkipStageEnvVarSet ¶
func SkipStageEnvVarSet() bool
SkipStageEnvVarSet returns true if an environment variable is set instructing Terratest to skip a test stage. This can be an easy way to tell if the tests are running in a local dev environment vs a CI server.
func ValidateAllTerraformModules ¶
func ValidateAllTerraformModules(t *go_test.T, opts *ValidationOptions)
ValidateAllTerraformModules automatically finds all folders specified in RootDir that contain .tf files and runs InitAndValidate in all of them. Filters down to only those paths passed in ValidationOptions.IncludeDirs, if passed. Excludes any folders specified in the ValidationOptions.ExcludeDirs. IncludeDirs will take precedence over ExcludeDirs Use the NewValidationOptions method to pass relative paths for either of these options to have the full paths built Note that go_test is an alias to Golang's native testing package created to avoid naming conflicts with Terratest's own testing package. We are using the native testing.T here because Terratest's testing.T struct does not implement Run Note that we have opted to place the ValidateAllTerraformModules function here instead of in the terraform package to avoid import cycling
Types ¶
type ValidateFileType ¶
type ValidateFileType string
ValidateFileType is the underlying module type to search for when performing validation. Either Terraform or Terragrunt files are targeted during a given validation sweep
type ValidationAbsolutePathErr ¶
type ValidationAbsolutePathErr struct {
// contains filtered or unexported fields
}
ValidationAbsolutePathErr is returned when NewValidationOptions was unable to convert a non-absolute RootDir to an absolute path
func (ValidationAbsolutePathErr) Error ¶
func (e ValidationAbsolutePathErr) Error() string
type ValidationOptions ¶
type ValidationOptions struct { // The target directory to recursively search for all Terraform directories (those that contain .tf files) // If you provide RootDir and do not pass entries in either IncludeDirs or ExcludeDirs, then all Terraform directories // From the RootDir, recursively, will be validated RootDir string FileType ValidateFileType // If you only want to include certain sub directories of RootDir, add the absolute paths here. For example, if the // RootDir is /home/project and you want to only include /home/project/examples, add /home/project/examples here // Note that while the struct requires full paths, you can pass relative paths to the NewValidationOptions function // which will build the full paths based on the supplied RootDir IncludeDirs []string // If you want to explicitly exclude certain sub directories of RootDir, add the absolute paths here. For example, if the // RootDir is /home/project and you want to include everything EXCEPT /home/project/modules, add // /home/project/modules to this slice. Note that ExcludeDirs is only considered when IncludeDirs is not passed // Note that while the struct requires full paths, you can pass relative paths to the NewValidationOptions function // which will build the full paths based on the supplied RootDir ExcludeDirs []string }
ValidationOptions represent the configuration for a given validation sweep of a target repo
func NewTerragruntValidationOptions ¶
func NewTerragruntValidationOptions(rootDir string, includeDirs, excludeDirs []string) (*ValidationOptions, error)
NewTerragruntValidationOptions returns a ValidationOptions struct, with override-able sane defaults, configured to find and process all directories containing .hcl files.
func NewValidationOptions ¶
func NewValidationOptions(rootDir string, includeDirs, excludeDirs []string) (*ValidationOptions, error)
NewValidationOptions returns a ValidationOptions struct, with override-able sane defaults, configured to find and process all directories containing .tf files
type ValidationUndefinedRootDirErr ¶
type ValidationUndefinedRootDirErr struct{}
ValidationUndefinedRootDirErr is returned when NewValidationOptions is called without a RootDir argument
func (ValidationUndefinedRootDirErr) Error ¶
func (e ValidationUndefinedRootDirErr) Error() string