github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/terraform_aws_s3_example_test.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/gruntwork-io/terratest/modules/aws" 9 "github.com/gruntwork-io/terratest/modules/random" 10 "github.com/gruntwork-io/terratest/modules/terraform" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 // An example of how to test the Terraform module in examples/terraform-aws-s3-example using Terratest. 15 func TestTerraformAwsS3Example(t *testing.T) { 16 t.Parallel() 17 18 // Give this S3 Bucket a unique ID for a name tag so we can distinguish it from any other Buckets provisioned 19 // in your AWS account 20 expectedName := fmt.Sprintf("terratest-aws-s3-example-%s", strings.ToLower(random.UniqueId())) 21 22 // Give this S3 Bucket an environment to operate as a part of for the purposes of resource tagging 23 expectedEnvironment := "Automated Testing" 24 25 // Pick a random AWS region to test in. This helps ensure your code works in all regions. 26 awsRegion := aws.GetRandomStableRegion(t, nil, nil) 27 28 // Construct the terraform options with default retryable errors to handle the most common retryable errors in 29 // terraform testing. 30 terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ 31 // The path to where our Terraform code is located 32 TerraformDir: "../examples/terraform-aws-s3-example", 33 34 // Variables to pass to our Terraform code using -var options 35 Vars: map[string]interface{}{ 36 "tag_bucket_name": expectedName, 37 "tag_bucket_environment": expectedEnvironment, 38 "with_policy": "true", 39 }, 40 41 // Environment variables to set when running Terraform 42 EnvVars: map[string]string{ 43 "AWS_DEFAULT_REGION": awsRegion, 44 }, 45 }) 46 47 // At the end of the test, run `terraform destroy` to clean up any resources that were created 48 defer terraform.Destroy(t, terraformOptions) 49 50 // This will run `terraform init` and `terraform apply` and fail the test if there are any errors 51 terraform.InitAndApply(t, terraformOptions) 52 53 // Run `terraform output` to get the value of an output variable 54 bucketID := terraform.Output(t, terraformOptions, "bucket_id") 55 56 // Verify that our Bucket has versioning enabled 57 actualStatus := aws.GetS3BucketVersioning(t, awsRegion, bucketID) 58 expectedStatus := "Enabled" 59 assert.Equal(t, expectedStatus, actualStatus) 60 61 // Verify that our Bucket has a policy attached 62 aws.AssertS3BucketPolicyExists(t, awsRegion, bucketID) 63 64 // Verify that our bucket has server access logging TargetBucket set to what's expected 65 loggingTargetBucket := aws.GetS3BucketLoggingTarget(t, awsRegion, bucketID) 66 expectedLogsTargetBucket := fmt.Sprintf("%s-logs", bucketID) 67 loggingObjectTargetPrefix := aws.GetS3BucketLoggingTargetPrefix(t, awsRegion, bucketID) 68 expectedLogsTargetPrefix := "TFStateLogs/" 69 70 assert.Equal(t, expectedLogsTargetBucket, loggingTargetBucket) 71 assert.Equal(t, expectedLogsTargetPrefix, loggingObjectTargetPrefix) 72 }