|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cloudposse/test-helpers/pkg/atmos" |
| 9 | + helper "github.com/cloudposse/test-helpers/pkg/atmos/component-helper" |
| 10 | + "github.com/gruntwork-io/terratest/modules/aws" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/gruntwork-io/terratest/modules/random" |
| 13 | +) |
| 14 | + |
| 15 | +type ComponentSuite struct { |
| 16 | + helper.TestSuite |
| 17 | +} |
| 18 | + |
| 19 | +func (s *ComponentSuite) TestBasic() { |
| 20 | + const component = "aurora-postgres/basic" |
| 21 | + const stack = "default-test" |
| 22 | + const awsRegion = "us-east-2" |
| 23 | + |
| 24 | + clusterName := strings.ToLower(random.UniqueId()) |
| 25 | + |
| 26 | + defer s.DestroyAtmosComponent(s.T(), component, stack, nil) |
| 27 | + inputs := map[string]interface{}{ |
| 28 | + "name": "db", |
| 29 | + "database_name": "postgres", |
| 30 | + "admin_user": "postgres", |
| 31 | + "database_port": 5432, |
| 32 | + "publicly_accessible": true, |
| 33 | + "allowed_cidr_blocks": []string{"0.0.0.0/0"}, |
| 34 | + "cluster_name": clusterName, |
| 35 | + } |
| 36 | + componentInstance, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs) |
| 37 | + assert.NotNil(s.T(), componentInstance) |
| 38 | + |
| 39 | + databaseName := atmos.Output(s.T(), componentInstance, "database_name") |
| 40 | + assert.Equal(s.T(), "postgres", databaseName) |
| 41 | + |
| 42 | + adminUsername := atmos.Output(s.T(), componentInstance, "admin_username") |
| 43 | + assert.Equal(s.T(), "postgres", adminUsername) |
| 44 | + |
| 45 | + delegatedDnsOptions := s.GetAtmosOptions("dns-delegated", stack, nil) |
| 46 | + delegatedDomainName := atmos.Output(s.T(), delegatedDnsOptions, "default_domain_name") |
| 47 | + delegatedDomainNZoneId := atmos.Output(s.T(), delegatedDnsOptions, "default_dns_zone_id") |
| 48 | + |
| 49 | + masterHostname := atmos.Output(s.T(), componentInstance, "master_hostname") |
| 50 | + expectedMasterHostname := fmt.Sprintf("%s-%s-writer.%s", inputs["name"], componentInstance.Vars["cluster_name"], delegatedDomainName) |
| 51 | + assert.Equal(s.T(), expectedMasterHostname, masterHostname) |
| 52 | + |
| 53 | + replicasHostname := atmos.Output(s.T(), componentInstance, "replicas_hostname") |
| 54 | + expectedReplicasHostname := fmt.Sprintf("%s-%s-reader.%s", inputs["name"], componentInstance.Vars["cluster_name"], delegatedDomainName) |
| 55 | + assert.Equal(s.T(), expectedReplicasHostname, replicasHostname) |
| 56 | + |
| 57 | + ssmKeyPaths := atmos.OutputList(s.T(), componentInstance, "ssm_key_paths") |
| 58 | + assert.Equal(s.T(), 7, len(ssmKeyPaths)) |
| 59 | + |
| 60 | + kmsKeyArn := atmos.Output(s.T(), componentInstance, "kms_key_arn") |
| 61 | + assert.NotEmpty(s.T(), kmsKeyArn) |
| 62 | + |
| 63 | + allowedSecurityGroups := atmos.OutputList(s.T(), componentInstance, "allowed_security_groups") |
| 64 | + assert.Equal(s.T(), 0, len(allowedSecurityGroups)) |
| 65 | + |
| 66 | + clusterIdentifier := atmos.Output(s.T(), componentInstance, "cluster_identifier") |
| 67 | + |
| 68 | + configMap := map[string]interface{}{} |
| 69 | + atmos.OutputStruct(s.T(), componentInstance, "config_map", &configMap) |
| 70 | + |
| 71 | + assert.Equal(s.T(), clusterIdentifier, configMap["cluster"]) |
| 72 | + assert.Equal(s.T(), databaseName, configMap["database"]) |
| 73 | + assert.Equal(s.T(), masterHostname, configMap["hostname"]) |
| 74 | + assert.EqualValues(s.T(), inputs["database_port"], configMap["port"]) |
| 75 | + assert.Equal(s.T(), adminUsername, configMap["username"]) |
| 76 | + |
| 77 | + masterHostnameDNSRecord := aws.GetRoute53Record(s.T(), delegatedDomainNZoneId, masterHostname, "CNAME", awsRegion) |
| 78 | + assert.Equal(s.T(), *masterHostnameDNSRecord.ResourceRecords[0].Value, configMap["endpoint"]) |
| 79 | + |
| 80 | + passwordSSMKey, ok := configMap["password_ssm_key"].(string) |
| 81 | + assert.True(s.T(), ok, "password_ssm_key should be a string") |
| 82 | + |
| 83 | + adminUserPassword := aws.GetParameter(s.T(), awsRegion, passwordSSMKey) |
| 84 | + |
| 85 | + dbUrl, ok := configMap["endpoint"].(string) |
| 86 | + assert.True(s.T(), ok, "endpoint should be a string") |
| 87 | + |
| 88 | + dbPort, ok := inputs["database_port"].(int) |
| 89 | + assert.True(s.T(), ok, "database_port should be an int") |
| 90 | + |
| 91 | + schemaExistsInRdsInstance := aws.GetWhetherSchemaExistsInRdsPostgresInstance(s.T(), dbUrl, int32(dbPort), adminUsername, adminUserPassword, databaseName) |
| 92 | + assert.True(s.T(), schemaExistsInRdsInstance) |
| 93 | + |
| 94 | + schemaExistsInRdsInstance = aws.GetWhetherSchemaExistsInRdsPostgresInstance(s.T(), masterHostname, int32(dbPort), adminUsername, adminUserPassword, databaseName) |
| 95 | + assert.True(s.T(), schemaExistsInRdsInstance) |
| 96 | + |
| 97 | + schemaExistsInRdsInstance = aws.GetWhetherSchemaExistsInRdsPostgresInstance(s.T(), replicasHostname, int32(dbPort), adminUsername, adminUserPassword, databaseName) |
| 98 | + assert.True(s.T(), schemaExistsInRdsInstance) |
| 99 | + |
| 100 | + s.DriftTest(component, stack, &inputs) |
| 101 | +} |
| 102 | + |
| 103 | +func (s *ComponentSuite) TestServerless() { |
| 104 | + const component = "aurora-postgres/serverless" |
| 105 | + const stack = "default-test" |
| 106 | + const awsRegion = "us-east-2" |
| 107 | + |
| 108 | + clusterName := strings.ToLower(random.UniqueId()) |
| 109 | + |
| 110 | + defer s.DestroyAtmosComponent(s.T(), component, stack, nil) |
| 111 | + inputs := map[string]interface{}{ |
| 112 | + "name": "db", |
| 113 | + "database_name": "postgres", |
| 114 | + "admin_user": "postgres", |
| 115 | + "database_port": 5432, |
| 116 | + "publicly_accessible": true, |
| 117 | + "allowed_cidr_blocks": []string{"0.0.0.0/0"}, |
| 118 | + "cluster_name": clusterName, |
| 119 | + } |
| 120 | + componentInstance, _ := s.DeployAtmosComponent(s.T(), component, stack, &inputs) |
| 121 | + assert.NotNil(s.T(), componentInstance) |
| 122 | + |
| 123 | + databaseName := atmos.Output(s.T(), componentInstance, "database_name") |
| 124 | + assert.Equal(s.T(), "postgres", databaseName) |
| 125 | + |
| 126 | + adminUsername := atmos.Output(s.T(), componentInstance, "admin_username") |
| 127 | + assert.Equal(s.T(), "postgres", adminUsername) |
| 128 | + |
| 129 | + delegatedDnsOptions := s.GetAtmosOptions("dns-delegated", stack, nil) |
| 130 | + delegatedDomainName := atmos.Output(s.T(), delegatedDnsOptions, "default_domain_name") |
| 131 | + delegatedDomainNZoneId := atmos.Output(s.T(), delegatedDnsOptions, "default_dns_zone_id") |
| 132 | + |
| 133 | + masterHostname := atmos.Output(s.T(), componentInstance, "master_hostname") |
| 134 | + expectedMasterHostname := fmt.Sprintf("%s-%s-writer.%s", inputs["name"], componentInstance.Vars["cluster_name"], delegatedDomainName) |
| 135 | + assert.Equal(s.T(), expectedMasterHostname, masterHostname) |
| 136 | + |
| 137 | + ssmKeyPaths := atmos.OutputList(s.T(), componentInstance, "ssm_key_paths") |
| 138 | + assert.Equal(s.T(), 7, len(ssmKeyPaths)) |
| 139 | + |
| 140 | + kmsKeyArn := atmos.Output(s.T(), componentInstance, "kms_key_arn") |
| 141 | + assert.NotEmpty(s.T(), kmsKeyArn) |
| 142 | + |
| 143 | + allowedSecurityGroups := atmos.OutputList(s.T(), componentInstance, "allowed_security_groups") |
| 144 | + assert.Equal(s.T(), 0, len(allowedSecurityGroups)) |
| 145 | + |
| 146 | + clusterIdentifier := atmos.Output(s.T(), componentInstance, "cluster_identifier") |
| 147 | + |
| 148 | + configMap := map[string]interface{}{} |
| 149 | + atmos.OutputStruct(s.T(), componentInstance, "config_map", &configMap) |
| 150 | + |
| 151 | + assert.Equal(s.T(), clusterIdentifier, configMap["cluster"]) |
| 152 | + assert.Equal(s.T(), databaseName, configMap["database"]) |
| 153 | + assert.Equal(s.T(), masterHostname, configMap["hostname"]) |
| 154 | + assert.EqualValues(s.T(), inputs["database_port"], configMap["port"]) |
| 155 | + assert.Equal(s.T(), adminUsername, configMap["username"]) |
| 156 | + |
| 157 | + masterHostnameDNSRecord := aws.GetRoute53Record(s.T(), delegatedDomainNZoneId, masterHostname, "CNAME", awsRegion) |
| 158 | + assert.Equal(s.T(), *masterHostnameDNSRecord.ResourceRecords[0].Value, configMap["endpoint"]) |
| 159 | + |
| 160 | + passwordSSMKey, ok := configMap["password_ssm_key"].(string) |
| 161 | + assert.True(s.T(), ok, "password_ssm_key should be a string") |
| 162 | + |
| 163 | + adminUserPassword := aws.GetParameter(s.T(), awsRegion, passwordSSMKey) |
| 164 | + |
| 165 | + dbUrl, ok := configMap["endpoint"].(string) |
| 166 | + assert.True(s.T(), ok, "endpoint should be a string") |
| 167 | + |
| 168 | + dbPort, ok := inputs["database_port"].(int) |
| 169 | + assert.True(s.T(), ok, "database_port should be an int") |
| 170 | + |
| 171 | + schemaExistsInRdsInstance := aws.GetWhetherSchemaExistsInRdsPostgresInstance(s.T(), dbUrl, int32(dbPort), adminUsername, adminUserPassword, databaseName) |
| 172 | + assert.True(s.T(), schemaExistsInRdsInstance) |
| 173 | + |
| 174 | + schemaExistsInRdsInstance = aws.GetWhetherSchemaExistsInRdsPostgresInstance(s.T(), masterHostname, int32(dbPort), adminUsername, adminUserPassword, databaseName) |
| 175 | + assert.True(s.T(), schemaExistsInRdsInstance) |
| 176 | + |
| 177 | + s.DriftTest(component, stack, &inputs) |
| 178 | +} |
| 179 | + |
| 180 | +func (s *ComponentSuite) TestDisabled() { |
| 181 | + const component = "aurora-postgres/disabled" |
| 182 | + const stack = "default-test" |
| 183 | + const awsRegion = "us-east-2" |
| 184 | + |
| 185 | + s.VerifyEnabledFlag(component, stack, nil) |
| 186 | +} |
| 187 | + |
| 188 | +func TestRunSuite(t *testing.T) { |
| 189 | + suite := new(ComponentSuite) |
| 190 | + |
| 191 | + suite.AddDependency(t, "vpc", "default-test", nil) |
| 192 | + |
| 193 | + subdomain := strings.ToLower(random.UniqueId()) |
| 194 | + inputs := map[string]interface{}{ |
| 195 | + "zone_config": []map[string]interface{}{ |
| 196 | + { |
| 197 | + "subdomain": subdomain, |
| 198 | + "zone_name": "components.cptest.test-automation.app", |
| 199 | + }, |
| 200 | + }, |
| 201 | + } |
| 202 | + suite.AddDependency(t, "dns-delegated", "default-test", &inputs) |
| 203 | + helper.Run(t, suite) |
| 204 | +} |
0 commit comments