forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_aws_rds_example_test.go
86 lines (73 loc) · 3.72 KB
/
terraform_aws_rds_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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/assert"
)
// An example of how to test the Terraform module in examples/terraform-aws-example using Terratest.
func TestTerraformAwsRdsExample(t *testing.T) {
t.Parallel()
// Give this EC2 Instance a unique ID for a name tag so we can distinguish it from any other EC2 Instance running
// in your AWS account
expectedName := fmt.Sprintf("terratest-aws-example-%s", strings.ToLower(random.UniqueId()))
expectedPort := int64(3306)
expectedDatabaseName := "terratest"
username := "username"
password := "password"
// Pick a random AWS region to test in. This helps ensure your code works in all regions.
awsRegion := aws.GetRandomRegion(t, nil, nil)
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/terraform-aws-rds-example",
// Variables to pass to our Terraform code using -var options
// "username" and "password" should not be passed from here in a production scenario.
Vars: map[string]interface{}{
"name": expectedName,
"engine_name": "mysql",
"major_engine_version": "5.7",
"family": "mysql5.7",
"username": username,
"password": password,
"allocated_storage": 5,
"license_model": "general-public-license",
"engine_version": "5.7.21",
"port": expectedPort,
"database_name": expectedDatabaseName,
},
// Environment variables to set when running Terraform
EnvVars: map[string]string{
"AWS_DEFAULT_REGION": awsRegion,
},
}
// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
// Run `terraform output` to get the value of an output variable
dbInstanceID := terraform.Output(t, terraformOptions, "db_instance_id")
// Look up the endpoint address and port of the RDS instance
address := aws.GetAddressOfRdsInstance(t, dbInstanceID, awsRegion)
port := aws.GetPortOfRdsInstance(t, dbInstanceID, awsRegion)
schemaExistsInRdsInstance := aws.GetWhetherSchemaExistsInRdsMySqlInstance(t, address, port, username, password, expectedDatabaseName)
// Lookup parameter values. All defined values are strings in the API call response
generalLogParameterValue := aws.GetParameterValueForParameterOfRdsInstance(t, "general_log", dbInstanceID, awsRegion)
allowSuspiciousUdfsParameterValue := aws.GetParameterValueForParameterOfRdsInstance(t, "allow-suspicious-udfs", dbInstanceID, awsRegion)
// Lookup option values. All defined values are strings in the API call response
mariadbAuditPluginServerAuditEventsOptionValue := aws.GetOptionSettingForOfRdsInstance(t, "MARIADB_AUDIT_PLUGIN", "SERVER_AUDIT_EVENTS", dbInstanceID, awsRegion)
// Verify that the address is not null
assert.NotNil(t, address)
// Verify that the DB instance is listening on the port mentioned
assert.Equal(t, expectedPort, port)
// Verify that the table/schema requested for creation is actually present in the database
assert.True(t, schemaExistsInRdsInstance)
// Booleans are (string) "0", "1"
assert.Equal(t, "0", generalLogParameterValue)
// Values not set are "". This is custom behavior defined.
assert.Equal(t, "", allowSuspiciousUdfsParameterValue)
// assert.Equal(t, "", mariadbAuditPluginServerAuditEventsOptionValue)
assert.Equal(t, "CONNECT", mariadbAuditPluginServerAuditEventsOptionValue)
}