From cab29376212b20af0f590a0519392299358145c4 Mon Sep 17 00:00:00 2001 From: shemau Date: Tue, 14 Jan 2025 12:36:55 +0000 Subject: [PATCH] test: refactor basic test --- examples/basic/README.md | 2 +- examples/basic/main.tf | 42 ++++--------------------------------- examples/basic/outputs.tf | 16 +++++++------- examples/basic/replica.tf | 34 ++++++++++++++++++++++++++++++ examples/basic/variables.tf | 21 ++++--------------- tests/other_test.go | 40 ----------------------------------- tests/pr_test.go | 41 ++++++++++++++++++------------------ 7 files changed, 72 insertions(+), 124 deletions(-) create mode 100644 examples/basic/replica.tf diff --git a/examples/basic/README.md b/examples/basic/README.md index 52c49f4b..9a95ba7f 100644 --- a/examples/basic/README.md +++ b/examples/basic/README.md @@ -4,4 +4,4 @@ An end-to-end example that uses the module's default variable values. This examp - Create a new resource group if one is not passed in. - Create a new ICD Postgresql database instance. -- Create a read-only replica of the leader Postgresql database instance. For more info on Read-only Replicas, see [Configuring Read-only Replicas](https://cloud.ibm.com/docs/databases-for-postgresql?topic=databases-for-postgresql-read-only-replicas) +- Create a read-only replica of the leader database instance. For more info on Read-only Replicas, see [Configuring Read-only Replicas](https://cloud.ibm.com/docs/databases-for-postgresql?topic=databases-for-postgresql-read-only-replicas) diff --git a/examples/basic/main.tf b/examples/basic/main.tf index 4605ce5b..675feac8 100644 --- a/examples/basic/main.tf +++ b/examples/basic/main.tf @@ -11,50 +11,16 @@ module "resource_group" { } ############################################################################## -# ICD postgresql database +# ICD database ############################################################################## -module "postgresql_db" { +module "database" { source = "../.." resource_group_id = module.resource_group.resource_group_id - name = "${var.prefix}-postgres" - pg_version = var.pg_version + name = "${var.prefix}-data-store" + pg_version = var.db_version region = var.region resource_tags = var.resource_tags access_tags = var.access_tags member_host_flavor = var.member_host_flavor } - -# On destroy, we are seeing that even though the replica has been returned as -# destroyed by terraform, the leader instance destroy can fail with: "You -# must delete all replicas before disabling the leader. Try again with valid -# values or contact support if the issue persists." -# The ICD team have recommended to wait for a period of time after the replica -# destroy completes before attempting to destroy the leader instance, so hence -# adding a time sleep here. - -resource "time_sleep" "wait_time" { - depends_on = [module.postgresql_db] - - destroy_duration = "5m" -} - -############################################################################## -# ICD postgresql read-only-replica -############################################################################## - -module "read_only_replica_postgresql_db" { - count = var.read_only_replicas_count - source = "../.." - resource_group_id = module.resource_group.resource_group_id - name = "${var.prefix}-read-only-replica-${count.index}" - region = var.region - resource_tags = var.resource_tags - access_tags = var.access_tags - pg_version = var.pg_version - remote_leader_crn = module.postgresql_db.crn - member_host_flavor = "multitenant" - member_memory_mb = 4096 # Must be an increment of 384 megabytes. The minimum size of a read-only replica is 2 GB RAM, new hosting model minimum is 4 GB RAM. - member_disk_mb = 5120 # Must be an increment of 512 megabytes. The minimum size of a read-only replica is 5 GB of disk - depends_on = [time_sleep.wait_time] -} diff --git a/examples/basic/outputs.tf b/examples/basic/outputs.tf index 9e43df1a..20c8778e 100644 --- a/examples/basic/outputs.tf +++ b/examples/basic/outputs.tf @@ -2,32 +2,32 @@ # Outputs ############################################################################## output "id" { - description = "Postgresql instance id" - value = module.postgresql_db.id + description = "Database instance id" + value = module.database.id } output "version" { - description = "Postgresql instance version" - value = module.postgresql_db.version + description = "Database instance version" + value = module.database.version } output "adminuser" { description = "Database admin user name" - value = module.postgresql_db.adminuser + value = module.database.adminuser } output "hostname" { description = "Database connection hostname" - value = module.postgresql_db.hostname + value = module.database.hostname } output "port" { description = "Database connection port" - value = module.postgresql_db.port + value = module.database.port } output "certificate_base64" { description = "Database connection certificate" - value = module.postgresql_db.certificate_base64 + value = module.database.certificate_base64 sensitive = true } diff --git a/examples/basic/replica.tf b/examples/basic/replica.tf new file mode 100644 index 00000000..01028411 --- /dev/null +++ b/examples/basic/replica.tf @@ -0,0 +1,34 @@ +############################################################################## +# ICD read-only-replica extension for edb, mysql and postgresql +############################################################################## + +# On destroy, after the replica has been returned as destroyed by terraform, +# the leader instance destroy can fail with: "You must delete all replicas +# before disabling the leader. Try again with valid values or contact support +# if the issue persists." +# The ICD team recommend to wait for a period of time after the replica +# destroy completes before attempting to destroy the leader instance, so +# hence adding a time sleep here. + +resource "time_sleep" "wait_time" { + depends_on = [module.database] + + destroy_duration = "5m" +} + +############################################################################## +# ICD read-only-replica +############################################################################## + +module "replica" { + source = "../.." + resource_group_id = module.resource_group.resource_group_id + name = "${var.prefix}-replica" + pg_version = var.db_version + region = var.region + resource_tags = var.resource_tags + access_tags = var.access_tags + member_host_flavor = var.member_host_flavor + remote_leader_crn = module.database.crn + depends_on = [time_sleep.wait_time] +} diff --git a/examples/basic/variables.tf b/examples/basic/variables.tf index 5b215690..185ac83f 100644 --- a/examples/basic/variables.tf +++ b/examples/basic/variables.tf @@ -14,8 +14,8 @@ variable "prefix" { description = "Prefix to append to all resources created by this example" } -variable "pg_version" { - description = "Version of the postgresql instance. If no value passed, the current ICD preferred version is used." +variable "db_version" { + description = "Version of the ICD instance. If no value passed, the current ICD preferred version is used." type = string default = null } @@ -34,25 +34,12 @@ variable "resource_tags" { variable "access_tags" { type = list(string) - description = "A list of access tags to apply to the PostgreSQL instance created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details" + description = "A list of access tags to apply to the ICD instance created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details" default = [] } -variable "read_only_replicas_count" { - type = number - description = "Number of read-only replicas per leader" - default = 1 - validation { - condition = alltrue([ - var.read_only_replicas_count >= 1, - var.read_only_replicas_count <= 5 - ]) - error_message = "There is a limit of five read-only replicas per leader" - } -} - variable "member_host_flavor" { type = string description = "Allocated host flavor per member. For more information, see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/database#host_flavor" - default = null + default = "multitenant" } diff --git a/tests/other_test.go b/tests/other_test.go index a323bbd0..fbcd6966 100644 --- a/tests/other_test.go +++ b/tests/other_test.go @@ -52,26 +52,6 @@ func TestRunPointInTimeRecoveryDBExample(t *testing.T) { assert.NotNil(t, output, "Expected some output") } -func TestRunBasicExample(t *testing.T) { - t.Parallel() - - options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{ - Testing: t, - TerraformDir: "examples/basic", - Prefix: "postgres", - BestRegionYAMLPath: regionSelectionPath, - ResourceGroup: resourceGroup, - TerraformVars: map[string]interface{}{ - "pg_version": "13", - }, - CloudInfoService: sharedInfoSvc, - }) - - output, err := options.RunTestConsistency() - assert.Nil(t, err, "This should not have errored") - assert.NotNil(t, output, "Expected some output") -} - func testPlanICDVersions(t *testing.T, version string) { t.Parallel() @@ -117,23 +97,3 @@ func TestRunCompleteExample(t *testing.T) { assert.Nil(t, err, "This should not have errored") assert.NotNil(t, output, "Expected some output") } - -func TestRunBasicExampleWithFlavorMultitenant(t *testing.T) { - t.Parallel() - - options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{ - Testing: t, - TerraformDir: "examples/basic", - Prefix: "pg-flvr-multitenant", - BestRegionYAMLPath: regionSelectionPath, - ResourceGroup: resourceGroup, - TerraformVars: map[string]interface{}{ - "member_host_flavor": "multitenant", - }, - CloudInfoService: sharedInfoSvc, - }) - - output, err := options.RunTestConsistency() - assert.Nil(t, err, "This should not have errored") - assert.NotNil(t, output, "Expected some output") -} diff --git a/tests/pr_test.go b/tests/pr_test.go index 12640483..e15cd09a 100644 --- a/tests/pr_test.go +++ b/tests/pr_test.go @@ -41,6 +41,27 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestRunBasicExample(t *testing.T) { + t.Parallel() + + options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{ + Testing: t, + TerraformDir: "examples/basic", + Prefix: "pg", + BestRegionYAMLPath: regionSelectionPath, + ResourceGroup: resourceGroup, + TerraformVars: map[string]interface{}{ + "db_version": "13", + "member_host_flavor": "multitenant", + }, + CloudInfoService: sharedInfoSvc, + }) + + output, err := options.RunTestConsistency() + assert.Nil(t, err, "This should not have errored") + assert.NotNil(t, output, "Expected some output") +} + func TestRunFSCloudExample(t *testing.T) { t.Parallel() @@ -76,26 +97,6 @@ func TestRunFSCloudExample(t *testing.T) { options.TestTearDown() } -func TestRunBasicExampleWithFlavor(t *testing.T) { - t.Parallel() - - options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{ - Testing: t, - TerraformDir: "examples/basic", - Prefix: "postgres-flvr", - BestRegionYAMLPath: regionSelectionPath, - ResourceGroup: resourceGroup, - TerraformVars: map[string]interface{}{ - "member_host_flavor": "b3c.4x16.encrypted", - }, - CloudInfoService: sharedInfoSvc, - }) - - output, err := options.RunTestConsistency() - assert.Nil(t, err, "This should not have errored") - assert.NotNil(t, output, "Expected some output") -} - func TestRunStandardSolution(t *testing.T) { t.Parallel()