1
0
mirror of synced 2025-12-22 03:12:25 -05:00

Updates based on PR feedback

This commit is contained in:
Petri Autero
2019-02-05 23:51:46 +02:00
parent ba6ec1296d
commit 39770cbbaa
5 changed files with 86 additions and 62 deletions

View File

@@ -6,11 +6,10 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/gruntwork-io/terratest/modules/gcp"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
"log"
"os"
"path/filepath"
"strings"
"testing"
@@ -19,30 +18,36 @@ import (
const DB_NAME = "testdb"
const DB_USER = "testuser"
const DB_PASS = "testpassword"
const NAME_PREFIX = "mysql-test"
const MYSQL_VERSION = "MYSQL_5_7"
const EXAMPLE_NAME = "cloud-sql-mysql"
const KEY_REGION = "region"
const KEY_PROJECT = "project"
const OUTPUT_INSTANCE_NAME = "instance_name"
const OUTPUT_PROXY_CONNECTION = "proxy_connection"
const OUTPUT_DB_NAME = "db_name"
const OUTPUT_PUBLIC_IP = "public_ip"
// TODO: try to actually connect to the RDS DBs and check they are working
func TestCloudSQLMySql(t *testing.T) {
t.Parallel()
//os.Setenv("SKIP_bootstrap", "true")
os.Setenv("SKIP_bootstrap", "true")
//os.Setenv("SKIP_deploy", "true")
//os.Setenv("SKIP_validate_outputs", "true")
//os.Setenv("SKIP_sql_tests", "true")
//os.Setenv("SKIP_teardown", "true")
os.Setenv("SKIP_validate_outputs", "true")
os.Setenv("SKIP_sql_tests", "true")
os.Setenv("SKIP_teardown", "true")
_examplesDir := test_structure.CopyTerraformFolderToTemp(t, "../", "examples")
exampleDir := filepath.Join(_examplesDir, "cloud-sql-mysql")
rootDir := filepath.Join(_examplesDir, "../")
exampleDir := filepath.Join(_examplesDir, EXAMPLE_NAME)
test_structure.RunTestStage(t, "bootstrap", func() {
uniqueId := strings.ToLower(random.UniqueId())
instanceName := fmt.Sprintf("mysql-test-%s", uniqueId)
projectId := gcp.GetGoogleProjectIDFromEnvVar(t)
region := getRandomRegion(t, projectId)
test_structure.SaveString(t, exampleDir, "instance-name", instanceName)
test_structure.SaveString(t, exampleDir, "region", region)
test_structure.SaveString(t, exampleDir, "project-id", projectId)
test_structure.SaveString(t, exampleDir, KEY_REGION, region)
test_structure.SaveString(t, exampleDir, KEY_PROJECT, projectId)
})
// At the end of the test, run `terraform destroy` to clean up any resources that were created
@@ -52,10 +57,9 @@ func TestCloudSQLMySql(t *testing.T) {
})
test_structure.RunTestStage(t, "deploy", func() {
region := test_structure.LoadString(t, exampleDir, "region")
projectId := test_structure.LoadString(t, exampleDir, "project-id")
instanceName := test_structure.LoadString(t, exampleDir, "instance-name")
terraformOptions := createTerratestOptionsForMySql(projectId, region, instanceName, rootDir)
region := test_structure.LoadString(t, exampleDir, KEY_REGION)
projectId := test_structure.LoadString(t, exampleDir, KEY_PROJECT)
terraformOptions := createTerratestOptionsForMySql(projectId, region, exampleDir)
test_structure.SaveTerraformOptions(t, exampleDir, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
@@ -63,22 +67,25 @@ func TestCloudSQLMySql(t *testing.T) {
test_structure.RunTestStage(t, "validate_outputs", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, exampleDir)
instanceName := test_structure.LoadString(t, exampleDir, "instance-name")
region := test_structure.LoadString(t, exampleDir, "region")
projectId := test_structure.LoadString(t, exampleDir, "project-id")
region := test_structure.LoadString(t, exampleDir, KEY_REGION)
projectId := test_structure.LoadString(t, exampleDir, KEY_PROJECT)
expectedDBConn := fmt.Sprintf("%s:%s:%s", projectId, region, instanceName)
instanceNameFromOutput := terraform.Output(t, terraformOptions, OUTPUT_INSTANCE_NAME)
dbNameFromOutput := terraform.Output(t, terraformOptions, OUTPUT_DB_NAME)
proxyConnectionFromOutput := terraform.Output(t, terraformOptions, OUTPUT_PROXY_CONNECTION)
assert.Equal(t, instanceName, terraform.Output(t, terraformOptions, "instance_name"))
assert.Equal(t, "testdb", terraform.Output(t, terraformOptions, "db_name"))
assert.Equal(t, expectedDBConn, terraform.Output(t, terraformOptions, "proxy_connection"))
expectedDBConn := fmt.Sprintf("%s:%s:%s", projectId, region, instanceNameFromOutput)
assert.True(t, strings.HasPrefix(instanceNameFromOutput, NAME_PREFIX))
assert.Equal(t, DB_NAME, dbNameFromOutput)
assert.Equal(t, expectedDBConn, proxyConnectionFromOutput)
})
test_structure.RunTestStage(t, "sql_tests", func() {
terraformOptions := test_structure.LoadTerraformOptions(t, exampleDir)
publicIp := terraform.Output(t, terraformOptions, "public_ip")
publicIp := terraform.Output(t, terraformOptions, OUTPUT_PUBLIC_IP)
connectionString := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", DB_USER, DB_PASS, publicIp, DB_NAME)
@@ -128,26 +135,26 @@ func TestCloudSQLMySql(t *testing.T) {
// Get the last insert id
lastId, err := res.LastInsertId()
if err != nil {
log.Fatal(err)
t.Fatalf("Failed to get last insert id: %v", err)
}
// Since we set the auto increment to 5, modulus should always be 0
assert.Equal(t, int64(0), int64(lastId%5))
})
}
func createTerratestOptionsForMySql(projectId string, region string, instanceName string, repoPath string) *terraform.Options {
func createTerratestOptionsForMySql(projectId string, region string, exampleDir string) *terraform.Options {
terratestOptions := &terraform.Options{
// The path to where your Terraform code is located
TerraformDir: filepath.Join(repoPath, "examples", "cloud-sql-mysql"),
TerraformDir: exampleDir,
Vars: map[string]interface{}{
"region": region,
"project": projectId,
"name": instanceName,
"mysql_version": "MYSQL_5_7",
"db_name": DB_NAME,
"master_username": DB_USER,
"master_password": DB_PASS,
"region": region,
"project": projectId,
"name_prefix": NAME_PREFIX,
"mysql_version": MYSQL_VERSION,
"db_name": DB_NAME,
"master_user_name": DB_USER,
"master_user_password": DB_PASS,
},
}