forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_backend_example_test.go
58 lines (47 loc) · 1.84 KB
/
terraform_backend_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package test
import (
"fmt"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/require"
)
// An example of how to test the Terraform module in examples/terraform-backend-example using Terratest.
func TestTerraformBackendExample(t *testing.T) {
t.Parallel()
awsRegion := aws.GetRandomRegion(t, nil, nil)
uniqueId := random.UniqueId()
// Create an S3 bucket where we can store state
bucketName := fmt.Sprintf("test-terraform-backend-example-%s", strings.ToLower(uniqueId))
defer cleanupS3Bucket(t, awsRegion, bucketName)
aws.CreateS3Bucket(t, awsRegion, bucketName)
key := fmt.Sprintf("%s/terraform.tfstate", uniqueId)
data := fmt.Sprintf("data-for-test-%s", uniqueId)
// Deploy the module, configuring it to use the S3 bucket as an S3 backend
terraformOptions := &terraform.Options{
TerraformDir: "../examples/terraform-backend-example",
Vars: map[string]interface{}{
"foo": data,
},
BackendConfig: map[string]interface{}{
"bucket": bucketName,
"key": key,
"region": awsRegion,
},
}
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
// Check a state file actually got stored and contains our data in it somewhere (since that data is used in an
// output of the Terraform code)
contents := aws.GetS3ObjectContents(t, awsRegion, bucketName, key)
require.Contains(t, contents, data)
// The module doesn't really *do* anything, so we just check a dummy output here and move on
foo := terraform.OutputRequired(t, terraformOptions, "foo")
require.Equal(t, data, foo)
}
func cleanupS3Bucket(t *testing.T, awsRegion string, bucketName string) {
aws.EmptyS3Bucket(t, awsRegion, bucketName)
aws.DeleteS3Bucket(t, awsRegion, bucketName)
}