tftest

package
v1.10.13 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	//EnvVPCID is a premade test vpc
	EnvVPCID = "VPC_ID"
	// EnvDatabaseSubnetGroup is a collection of database subnets
	EnvDatabaseSubnetGroup = "DATABASE_SUBNET_GROUP"
	// EnvPrivateSubnets is a comma separated list of private subnets
	EnvPrivateSubnets = "PRIVATE_SUBNETS"
	// EnvVPCCIDRBlock is the premate test vpc CIDR block
	EnvVPCCIDRBlock = "VPC_CIDR_BLOCK"
	// EnvRoute53ZoneID is the premade test route53 zone
	EnvRoute53ZoneID = "ROUTE53_ZONE_ID"
	// EnvRoute53ZoneName is the premate test route53 zone
	EnvRoute53ZoneName = "ROUTE53_ZONE_NAME"
	// EnvWildcardCertARN is a premade test ACM cert
	EnvWildcardCertARN = "WILDCARD_CERT_ARN"
	// EnvWildcardCloudfrontCertARN is a premate test ACM cert in us-east-1
	EnvWildcardCloudfrontCertARN = "CLOUDFRONT_WILDCARD_CERT_ARN"
	// EnvAccountID is the aws account id
	EnvAccountID = "ACCOUNT_ID"
	// EnvAWSProfile is the aws profile
	EnvAWSProfile = "AWS_PROFILE"
)
View Source
const (
	// IAMRegion IAM is allegedly hosted in us-east-1, so use this region for IAM related things
	IAMRegion     = "us-east-1"
	DefaultRegion = "us-west-2"
)
View Source
const Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
View Source
const AlphaNum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

Variables

View Source
var UserArn = "arn:aws:iam::119435350371:user/ci/cztack-ci"

Functions

func AWSCurrentAccountID

func AWSCurrentAccountID(t *testing.T) string

func Boolptr

func Boolptr(in bool) *bool

func Clean

func Clean(dir string)

func Cleanup

func Cleanup(t *testing.T, options *terraform.Options)

func CreateRole

func CreateRole(t *testing.T) string

CreateRole will create a new role with a random name and path and return the name.

func CreateSecurityGroup

func CreateSecurityGroup(t *testing.T, region, vpc string) string

func DeleteRole

func DeleteRole(t *testing.T, name string)

func DeleteSecurityGroup

func DeleteSecurityGroup(t *testing.T, region, id string)

func Destroy

func Destroy(t *testing.T, options *terraform.Options, retries ...int)

Destroy with retries

func EnvVar

func EnvVar(name string) string

func Int64ptr

func Int64ptr(in int64) *int64

func ListEnvVar

func ListEnvVar(name string) []string

func Options

func Options(region string, vars map[string]interface{}) *terraform.Options

func RandomString

func RandomString(chars string, length int) string

func Run

func Run(t *testing.T, options *terraform.Options)

func S3SimulateRequest

func S3SimulateRequest(t *testing.T, region, action, bucketArn, bucketPolicy string, secureTransport bool) *iam.EvaluationResult

S3S3SimulateRequest uses the IAM policy simulator to run end-to-end tests on permissions

func Strptr

func Strptr(in string) *string

func UniqueID

func UniqueID() string

UniqueId returns a unique (ish) id we can attach to resources and tfstate files so they don't conflict with each other Uses base 62 to generate a 6 character string that's unlikely to collide with the handful of tests we run in parallel. Based on code here: http://stackoverflow.com/a/9543797/483528

Types

type AWSStrings

type AWSStrings []string

func (*AWSStrings) UnmarshalJSON

func (a *AWSStrings) UnmarshalJSON(data []byte) error

General Unmarshal function for values that could be a string or []string, unmarshal as []string

type S3BucketPolicy

type S3BucketPolicy struct {
	Version    string      `json:"Version"`
	Statements []Statement `json:"Statement"`
}

func UnmarshalS3BucketPolicy

func UnmarshalS3BucketPolicy(in string) (*S3BucketPolicy, error)

UnmarshalS3BucketPolicy will parse an s3 bucket policy and return as a go struct. Only parts that have been used are supported so far

type Statement

type Statement struct {
	Sid       string                       `json:"Sid"`
	Effect    string                       `json:"Effect"`
	Principal string                       `json:"Principal"`
	Action    AWSStrings                   `json:"Action"`
	Resource  AWSStrings                   `json:"Resource"`
	Condition map[string]map[string]string `json:"Condition"`
}

type Test

type Test struct {
	Setup    func(*testing.T) *terraform.Options
	Validate func(*testing.T, *terraform.Options)
	Cleanup  func(*testing.T, *terraform.Options)

	Mode        TestMode
	SkipDestroy bool
	// contains filtered or unexported fields
}

Test encapsulates and provides structure to a terratest-driven terraform test.

Tests are run in 4 stages– Setup, Apply, Validate and Cleanup. Each stage will persist relevant data so that subsequent test runs can be isolated to a subset of stages.

Setup Stage

The setup Stage is used to create all the preconditions for running the terraform code under test. The user supplied Setup function must return a set of Options for running the code. In addition, it can create any additional resources that need to exist before running.

Apply Stage

The above options are used to initialize and apply the Terraform code under test. Note that Mode can be set to something other than Apply. The terraform state file is saved locally for use in Validate and Cleanup.

Validate Stage

If code was successfully applied, the user-supplied Validate function is run to make assertions about the resulting infrastructure. Note that if Mode is set to something other than Apply, the Validate function is not currently very useful.

Cleanup Stage

In addition to running a `terraform destroy`, and deleting any cached data (saved options, terraform state) the user-supplied Cleanup function is run to do arbitrary clean up work.

Env Variables

Each stage persists relevant data and can be skipped on subsequent runs. There are two environment variables which control which stages are run – SKIP and ONLY, each of which take a comma-separated list of stage names. Setting both is not allowed and will generate a test failure.

Example–

SKIP=cleanup go test . -run TestFoo

This will run the first three stages. If after that–

ONLY=validate go test . -run TestFoo

...the saved options from Setup and saved terraform state from Apply will be reused (and the infrastructure is presumably still up). This enables one to iterate quickly on testing terraform modules.

Hopefully many useful workflows can be derived from these building blocks.

func (*Test) Run

func (tt *Test) Run(t *testing.T)

func (*Test) Stage

func (tt *Test) Stage(t *testing.T, stage string, f func())

type TestMode

type TestMode int
const (
	Apply TestMode = 0
	Plan  TestMode = 1
	Init  TestMode = 2
)

Jump to

Keyboard shortcuts

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