From 96ae641c4f611e029acb83254cdcdfb7b8e95348 Mon Sep 17 00:00:00 2001 From: "jsakyle@gmail.com" Date: Mon, 1 Mar 2021 21:33:52 -0500 Subject: [PATCH] finalizing implementation and tests --- modules/aws/rds_test.go | 124 +++++++++++++++++++++++++ test/terraform_aws_rds_example_test.go | 2 + 2 files changed, 126 insertions(+) create mode 100644 modules/aws/rds_test.go diff --git a/modules/aws/rds_test.go b/modules/aws/rds_test.go new file mode 100644 index 000000000..642b01621 --- /dev/null +++ b/modules/aws/rds_test.go @@ -0,0 +1,124 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetRecommendedRdsInstanceType(t *testing.T) { + type TestingScenerios struct { + name string + region string + databaseEngine string + instanceTypes []string + expected string + } +} + +func TestGetRecommendedRdsInstanceTypeHappyPath(t *testing.T) { + type TestingScenerios struct { + name string + region string + databaseEngine string + instanceTypes []string + expected string + } + + testingScenerios := []TestingScenerios{ + { + name: "US region, mysql, first offering available", + region: "us-east-2", + databaseEngine: "mysql", + instanceTypes: []string{"db.t2.micro", "db.t3.micro"}, + expected: "db.t2.micro", + }, + { + name: "EU region, postgres, 2nd offering available based on region", + region: "eu-north-1", + databaseEngine: "postgres", + instanceTypes: []string{"db.t2.micro", "db.m5.large"}, + expected: "db.m5.large", + }, + { + name: "US region, oracle-ee, 2nd offering available based on db type", + region: "us-west-2", + databaseEngine: "oracle-ee", + instanceTypes: []string{"db.m5d.xlarge", "db.m5.large"}, + expected: "db.m5.large", + }, + } + + for _, scenerio := range testingScenerios { + scenerio := scenerio + + t.Run(scenerio.name, func(t *testing.T) { + t.Parallel() + + actual, err := GetRecommendedRdsInstanceTypeE(t, scenerio.region, scenerio.databaseEngine, scenerio.instanceTypes) + assert.NoError(t, err) + assert.Equal(t, scenerio.expected, actual) + }) + } +} + +func TestGetRecommendedRdsInstanceTypeErrors(t *testing.T) { + type TestingScenerios struct { + name string + region string + databaseEngine string + instanceTypes []string + } + + testingScenerios := []TestingScenerios{ + { + name: "All empty", + region: "", + databaseEngine: "", + instanceTypes: nil, + }, + { + name: "No engine or instance type", + region: "us-east-2", + databaseEngine: "", + instanceTypes: nil, + }, + { + name: "No instance types", + region: "us-east-2", + databaseEngine: "mysql", + instanceTypes: nil, + }, + { + name: "Invalid instance types", + region: "us-east-2", + databaseEngine: "mysql", + instanceTypes: []string{"garbage"}, + }, + { + name: "Region has no instance type available", + region: "eu-north-1", + databaseEngine: "mysql", + instanceTypes: []string{"db.t2.micro"}, + }, + { + name: "No instance type available for engine", + region: "us-east-1", + databaseEngine: "oracle-ee", + instanceTypes: []string{"db.r5d.large"}, + }, + } + + for _, scenerio := range testingScenerios { + scenerio := scenerio + + t.Run(scenerio.name, func(t *testing.T) { + t.Parallel() + + _, err := GetRecommendedRdsInstanceTypeE(t, scenerio.region, scenerio.databaseEngine, scenerio.instanceTypes) + fmt.Println(err) + assert.EqualError(t, err, NoRdsInstanceTypeError{InstanceTypeOptions: scenerio.instanceTypes, DatabaseEngine: scenerio.databaseEngine}.Error()) + }) + } +} diff --git a/test/terraform_aws_rds_example_test.go b/test/terraform_aws_rds_example_test.go index bd6f1a10a..542c39d3a 100644 --- a/test/terraform_aws_rds_example_test.go +++ b/test/terraform_aws_rds_example_test.go @@ -24,6 +24,7 @@ func TestTerraformAwsRdsExample(t *testing.T) { password := "password" // Pick a random AWS region to test in. This helps ensure your code works in all regions. awsRegion := aws.GetRandomStableRegion(t, nil, nil) + instanceType := aws.GetRecommendedRdsInstanceType(t, awsRegion, "mysql", []string{"db.t2.micro", "db.t3.micro"}) // Construct the terraform options with default retryable errors to handle the most common retryable errors in // terraform testing. @@ -38,6 +39,7 @@ func TestTerraformAwsRdsExample(t *testing.T) { "engine_name": "mysql", "major_engine_version": "5.7", "family": "mysql5.7", + "instance_class": instanceType, "username": username, "password": password, "allocated_storage": 5,