From 81e21d3476180728596bd88fa2f133b60793b28f Mon Sep 17 00:00:00 2001 From: James Kwon <96548424+hongil0316@users.noreply.github.com> Date: Thu, 7 Nov 2024 19:47:29 -0500 Subject: [PATCH] add GetWhetherSchemaExistsInRdsPostgresInstance --- examples/terraform-aws-rds-example/main.tf | 25 +-- go.mod | 14 +- go.sum | 32 ++-- modules/aws/rds.go | 31 ++++ test/terraform_aws_rds_example_test.go | 173 +++++++++++++-------- 5 files changed, 187 insertions(+), 88 deletions(-) diff --git a/examples/terraform-aws-rds-example/main.tf b/examples/terraform-aws-rds-example/main.tf index 5e9e20dfe..5eca4e4cf 100644 --- a/examples/terraform-aws-rds-example/main.tf +++ b/examples/terraform-aws-rds-example/main.tf @@ -65,12 +65,15 @@ resource "aws_db_option_group" "example" { Name = var.name } - option { - option_name = "MARIADB_AUDIT_PLUGIN" - - option_settings { - name = "SERVER_AUDIT_EVENTS" - value = "CONNECT" + dynamic "option" { + for_each = var.engine_name == "mysql" ? [1] : [] + content { + option_name = "MARIADB_AUDIT_PLUGIN" + + option_settings { + name = "SERVER_AUDIT_EVENTS" + value = "CONNECT" + } } } } @@ -83,9 +86,13 @@ resource "aws_db_parameter_group" "example" { Name = var.name } - parameter { - name = "general_log" - value = "0" + dynamic "parameter" { + for_each = var.engine_name == "mysql" ? [1] : [] + content { + name = "general_log" + value = "0" + + } } } diff --git a/go.mod b/go.mod index 202a8abc4..426b8e971 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/tmccombs/hcl2json v0.3.3 github.com/urfave/cli v1.22.2 github.com/zclconf/go-cty v1.9.1 - golang.org/x/crypto v0.21.0 + golang.org/x/crypto v0.27.0 golang.org/x/net v0.23.0 golang.org/x/oauth2 v0.8.0 google.golang.org/api v0.114.0 @@ -51,6 +51,7 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/gonvenience/ytbx v1.4.4 github.com/homeport/dyff v1.6.0 + github.com/jackc/pgx/v5 v5.7.1 github.com/slack-go/slack v0.10.3 gotest.tools/v3 v3.0.3 ) @@ -101,6 +102,9 @@ require ( github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-safetemp v1.0.0 // indirect github.com/imdario/mergo v0.3.11 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect @@ -129,10 +133,10 @@ require ( github.com/ulikunitz/xz v0.5.10 // indirect github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 // indirect go.opencensus.io v0.24.0 // indirect - golang.org/x/sync v0.4.0 // indirect - golang.org/x/sys v0.18.0 // indirect - golang.org/x/term v0.18.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.25.0 // indirect + golang.org/x/term v0.24.0 // indirect + golang.org/x/text v0.18.0 // indirect golang.org/x/time v0.3.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect google.golang.org/appengine v1.6.7 // indirect diff --git a/go.sum b/go.sum index feef308f2..acddd8e64 100644 --- a/go.sum +++ b/go.sum @@ -737,6 +737,14 @@ github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= +github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a h1:zPPuIq2jAWWPTrGt70eK/BSch+gFAGrNzecsoENgu2o= github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a/go.mod h1:yL958EeXv8Ylng6IfnvG4oflryUi3vgA3xPs9hmII1s= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= @@ -1129,8 +1137,8 @@ golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= -golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= +golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -1275,8 +1283,8 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= -golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1384,13 +1392,13 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= -golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= +golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM= +golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -1401,8 +1409,8 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -1471,8 +1479,8 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= -golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/modules/aws/rds.go b/modules/aws/rds.go index 5ec6bbf50..ab0967de4 100644 --- a/modules/aws/rds.go +++ b/modules/aws/rds.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/service/rds" _ "github.com/go-sql-driver/mysql" "github.com/gruntwork-io/terratest/modules/testing" + _ "github.com/jackc/pgx/v5/stdlib" "github.com/stretchr/testify/require" ) @@ -78,6 +79,36 @@ func GetWhetherSchemaExistsInRdsMySqlInstanceE(t testing.TestingT, dbUrl string, return true, nil } +// GetWhetherSchemaExistsInRdsPostgresInstance checks whether the specified schema/table name exists in the RDS instance +func GetWhetherSchemaExistsInRdsPostgresInstance(t testing.TestingT, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) bool { + output, err := GetWhetherSchemaExistsInRdsPostgresInstanceE(t, dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName) + if err != nil { + t.Fatal(err) + } + return output +} + +// GetWhetherSchemaExistsInRdsPostgresInstanceE checks whether the specified schema/table name exists in the RDS instance +func GetWhetherSchemaExistsInRdsPostgresInstanceE(t testing.TestingT, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) (bool, error) { + connectionString := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName) + + db, connErr := sql.Open("pgx", connectionString) + if connErr != nil { + return false, connErr + } + defer db.Close() + var ( + schemaName string + ) + sqlStatement := `SELECT "catalog_name" FROM "information_schema"."schemata" where catalog_name=$1` + row := db.QueryRow(sqlStatement, expectedSchemaName) + scanErr := row.Scan(&schemaName) + if scanErr != nil { + return false, scanErr + } + return true, nil +} + // GetParameterValueForParameterOfRdsInstance gets the value of the parameter name specified for the RDS instance in the given region. func GetParameterValueForParameterOfRdsInstance(t testing.TestingT, parameterName string, dbInstanceID string, awsRegion string) string { parameterValue, err := GetParameterValueForParameterOfRdsInstanceE(t, parameterName, dbInstanceID, awsRegion) diff --git a/test/terraform_aws_rds_example_test.go b/test/terraform_aws_rds_example_test.go index 354dab131..c69e9f92f 100644 --- a/test/terraform_aws_rds_example_test.go +++ b/test/terraform_aws_rds_example_test.go @@ -13,75 +13,124 @@ import ( // An example of how to test the Terraform module in examples/terraform-aws-rds-example using Terratest. func TestTerraformAwsRdsExample(t *testing.T) { - t.Parallel() + ttable := []struct { + name string - // Give this RDS Instance a unique ID for a name tag so we can distinguish it from any other RDS Instance running - // in your AWS account - expectedName := fmt.Sprintf("terratest-aws-rds-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.GetRandomStableRegion(t, nil, nil) - engineVersion := aws.GetValidEngineVersion(t, awsRegion, "mysql", "5.7") - instanceType := aws.GetRecommendedRdsInstanceType(t, awsRegion, "mysql", engineVersion, []string{"db.t2.micro", "db.t3.micro", "db.t3.small"}) + engineName string + majorEngineVersion string + engineFamily string + licenseModel string + schemaCheck func(t *testing.T, dbUrl string, dbPort int64, dbUsername string, dbPassword string, expectedSchemaName string) bool + expectedOptins map[struct { + opName string + setName string + }]string + expectedParameter map[string]string + }{ + { + name: "mysql", + engineName: "mysql", + majorEngineVersion: "5.7", + engineFamily: "mysql5.7", + licenseModel: "general-public-license", + schemaCheck: func(t *testing.T, dbUrl string, dbPort int64, dbUsername, dbPassword, expectedSchemaName string) bool { + return aws.GetWhetherSchemaExistsInRdsMySqlInstance(t, dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName) + }, + expectedOptins: map[struct { + opName string + setName string + }]string{ + {opName: "MARIADB_AUDIT_PLUGIN", setName: "SERVER_AUDIT_EVENTS"}: "CONNECT", + }, + expectedParameter: map[string]string{ + "general_log": "0", + "allow-suspicious-udfs": "", + }, + }, + { + name: "postgres", + engineName: "postgres", + majorEngineVersion: "13", + engineFamily: "postgres13", + licenseModel: "postgresql-license", + schemaCheck: func(t *testing.T, dbUrl string, dbPort int64, dbUsername, dbPassword, expectedSchemaName string) bool { + return aws.GetWhetherSchemaExistsInRdsPostgresInstance(t, dbUrl, dbPort, dbUsername, dbPassword, expectedSchemaName) + }, + }, + } - // Construct the terraform options with default retryable errors to handle the most common retryable errors in - // terraform testing. - terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ - // The path to where our Terraform code is located - TerraformDir: "../examples/terraform-aws-rds-example", + for _, tt := range ttable { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() - // 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", - "instance_class": instanceType, - "username": username, - "password": password, - "allocated_storage": 5, - "license_model": "general-public-license", - "engine_version": engineVersion, - "port": expectedPort, - "database_name": expectedDatabaseName, - "region": awsRegion, - }, - }) + // Give this RDS Instance a unique ID for a name tag so we can distinguish it from any other RDS Instance running + // in your AWS account + expectedName := fmt.Sprintf("terratest-aws-rds-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.GetRandomStableRegion(t, nil, nil) + engineVersion := aws.GetValidEngineVersion(t, awsRegion, tt.engineName, tt.majorEngineVersion) + instanceType := aws.GetRecommendedRdsInstanceType(t, awsRegion, tt.engineName, engineVersion, []string{"db.t2.micro", "db.t3.micro", "db.t3.small"}) + + // Construct the terraform options with default retryable errors to handle the most common retryable errors in + // terraform testing. + terraformOptions := terraform.WithDefaultRetryableErrors(t, &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": tt.engineName, + "major_engine_version": tt.majorEngineVersion, + "family": tt.engineFamily, + "instance_class": instanceType, + "username": username, + "password": password, + "allocated_storage": 5, + "license_model": tt.licenseModel, + "engine_version": engineVersion, + "port": expectedPort, + "database_name": expectedDatabaseName, + "region": awsRegion, + }, + }) + + // At the end of the test, run `terraform destroy` to clean up any resources that were created + defer terraform.Destroy(t, terraformOptions) - // 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) - // 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") - // 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 := tt.schemaCheck(t, address, port, username, password, expectedDatabaseName) + // Lookup parameter values. All defined values are strings in the API call response - // 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) + // 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) - // 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) + for k, v := range tt.expectedParameter { + assert.Equal(t, v, aws.GetParameterValueForParameterOfRdsInstance(t, k, 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) + for k, v := range tt.expectedOptins { + // Lookup option values. All defined values are strings in the API call response + assert.Equal(t, v, aws.GetOptionSettingForOfRdsInstance(t, k.opName, k.setName, dbInstanceID, awsRegion)) + } + }) + } }