Skip to content

Commit 2e26cb9

Browse files
Adding automated tests for gcbdr managed instances (#15977)
1 parent e706e96 commit 2e26cb9

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go.tmpl

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4152,6 +4152,86 @@ func TestAccSqlDatabaseInstance_updateInstanceTierForEnhancedBackupTierInstance(
41524152
})
41534153
}
41544154

4155+
func TestAccSqlDatabaseInstance_majorVersionUpgradeForEnhancedBackupTierInstance(t *testing.T) {
4156+
t.Parallel()
4157+
4158+
backupVault := acctest.BootstrapBackupDRVault(t, "bv-test", "us-central1")
4159+
4160+
context := map[string]interface{}{
4161+
"random_suffix": acctest.RandString(t, 10),
4162+
"project": envvar.GetTestProjectFromEnv(),
4163+
"backup_vault": backupVault,
4164+
"db_version": "MYSQL_8_0_41",
4165+
}
4166+
4167+
acctest.VcrTest(t, resource.TestCase{
4168+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4169+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4170+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
4171+
Steps: []resource.TestStep{
4172+
{
4173+
// Create backup plan and associate with instance
4174+
Config: testGoogleSqlDatabaseInstance_attachGCBDR(context),
4175+
Check: resource.ComposeAggregateTestCheckFunc(
4176+
resource.TestCheckResourceAttrSet("google_backup_dr_backup_plan_association.backup_association", "id"),
4177+
),
4178+
},
4179+
{
4180+
// Update instance database version to a new major version, which should ignore backup_configuration settings
4181+
Config: testGoogleSqlDatabaseInstance_majorVersionUpgradeGcbdrManagedInstance(context),
4182+
Check: resource.ComposeAggregateTestCheckFunc(
4183+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "database_version", "MYSQL_8_0_42"),
4184+
),
4185+
},
4186+
},
4187+
})
4188+
}
4189+
4190+
func TestAccSqlDatabaseInstance_editionUpdateForEnhancedBackupTierInstance(t *testing.T) {
4191+
t.Parallel()
4192+
4193+
backupVault := acctest.BootstrapBackupDRVault(t, "bv-test", "us-central1")
4194+
4195+
context := map[string]interface{}{
4196+
"random_suffix": acctest.RandString(t, 10),
4197+
"project": envvar.GetTestProjectFromEnv(),
4198+
"backup_vault": backupVault,
4199+
"db_version": "MYSQL_8_0_41",
4200+
"edition": "ENTERPRISE",
4201+
"tier": "db-f1-micro",
4202+
}
4203+
4204+
acctest.VcrTest(t, resource.TestCase{
4205+
PreCheck: func() { acctest.AccTestPreCheck(t) },
4206+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
4207+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
4208+
Steps: []resource.TestStep{
4209+
{
4210+
// Create backup plan and associate with instance
4211+
Config: testGoogleSqlDatabaseInstance_attachGCBDR(context),
4212+
Check: resource.ComposeAggregateTestCheckFunc(
4213+
resource.TestCheckResourceAttrSet("google_backup_dr_backup_plan_association.backup_association", "id"),
4214+
),
4215+
},
4216+
{
4217+
// Edition upgrade, which should ignore backup_configuration settings
4218+
Config: testGoogleSqlDatabaseInstance_editionUpdateForGcbdrManagedInstance(context, "ENTERPRISE_PLUS", "db-perf-optimized-N-4"),
4219+
Check: resource.ComposeAggregateTestCheckFunc(
4220+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE_PLUS"),
4221+
),
4222+
},
4223+
{
4224+
// Edition downgrade, which should ignore backup_configuration settings
4225+
Config: testGoogleSqlDatabaseInstance_editionUpdateForGcbdrManagedInstance(context, "ENTERPRISE", "db-f1-micro"),
4226+
Check: resource.ComposeAggregateTestCheckFunc(
4227+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "settings.0.edition", "ENTERPRISE"),
4228+
),
4229+
},
4230+
4231+
},
4232+
})
4233+
}
4234+
41554235
func testGoogleSqlDatabaseInstance_attachGCBDR(context map[string]interface{}) string {
41564236
return acctest.Nprintf(`
41574237
data "google_project" "project" {}
@@ -4259,6 +4339,131 @@ resource "google_backup_dr_backup_plan_association" "backup_association" {
42594339
`, context)
42604340
}
42614341

4342+
func testGoogleSqlDatabaseInstance_majorVersionUpgradeGcbdrManagedInstance(context map[string]interface{}) string {
4343+
return acctest.Nprintf(`
4344+
data "google_project" "project" {}
4345+
4346+
resource "google_sql_database_instance" "instance" {
4347+
name = "tf-test-instance-%{random_suffix}"
4348+
database_version = "MYSQL_8_0_42"
4349+
region = "us-central1"
4350+
4351+
settings {
4352+
tier = "db-f1-micro"
4353+
4354+
backup_configuration {
4355+
enabled = false
4356+
binary_log_enabled = false
4357+
start_time = "05:00"
4358+
4359+
backup_retention_settings {
4360+
retained_backups = 8
4361+
retention_unit = "COUNT"
4362+
}
4363+
}
4364+
}
4365+
deletion_protection = false
4366+
}
4367+
4368+
resource "google_backup_dr_backup_plan" "plan" {
4369+
location = "us-central1"
4370+
backup_plan_id = "tf-test-bp-test-%{random_suffix}"
4371+
resource_type = "sqladmin.googleapis.com/Instance"
4372+
backup_vault = "%{backup_vault}"
4373+
4374+
backup_rules {
4375+
rule_id = "rule-1"
4376+
backup_retention_days = 7
4377+
4378+
standard_schedule {
4379+
recurrence_type = "DAILY"
4380+
hourly_frequency = 6
4381+
time_zone = "UTC"
4382+
4383+
backup_window {
4384+
start_hour_of_day = 0
4385+
end_hour_of_day = 23
4386+
}
4387+
}
4388+
}
4389+
}
4390+
4391+
resource "google_backup_dr_backup_plan_association" "backup_association" {
4392+
location = "us-central1"
4393+
backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}"
4394+
resource = "projects/${data.google_project.project.project_id}/instances/${google_sql_database_instance.instance.name}"
4395+
resource_type = "sqladmin.googleapis.com/Instance"
4396+
backup_plan = google_backup_dr_backup_plan.plan.name
4397+
}
4398+
`, context)
4399+
}
4400+
4401+
func testGoogleSqlDatabaseInstance_editionUpdateForGcbdrManagedInstance(context map[string]interface{}, edition string, tier string) string {
4402+
// Create a copy of the context map to avoid modifying the map from the caller
4403+
localContext := make(map[string]interface{})
4404+
for k, v := range context {
4405+
localContext[k] = v
4406+
}
4407+
localContext["edition"] = edition
4408+
localContext["tier"] = tier
4409+
return acctest.Nprintf(`
4410+
data "google_project" "project" {}
4411+
4412+
resource "google_sql_database_instance" "instance" {
4413+
name = "tf-test-instance-%{random_suffix}"
4414+
database_version = "%{db_version}"
4415+
region = "us-central1"
4416+
4417+
settings {
4418+
tier = "%{tier}"
4419+
edition = "%{edition}"
4420+
4421+
backup_configuration {
4422+
enabled = false
4423+
binary_log_enabled = false
4424+
start_time = "05:00"
4425+
4426+
backup_retention_settings {
4427+
retained_backups = 8
4428+
retention_unit = "COUNT"
4429+
}
4430+
}
4431+
}
4432+
deletion_protection = false
4433+
}
4434+
4435+
resource "google_backup_dr_backup_plan" "plan" {
4436+
location = "us-central1"
4437+
backup_plan_id = "tf-test-bp-test-%{random_suffix}"
4438+
resource_type = "sqladmin.googleapis.com/Instance"
4439+
backup_vault = "%{backup_vault}"
4440+
4441+
backup_rules {
4442+
rule_id = "rule-1"
4443+
backup_retention_days = 7
4444+
4445+
standard_schedule {
4446+
recurrence_type = "DAILY"
4447+
hourly_frequency = 6
4448+
time_zone = "UTC"
4449+
4450+
backup_window {
4451+
start_hour_of_day = 0
4452+
end_hour_of_day = 23
4453+
}
4454+
}
4455+
}
4456+
}
4457+
4458+
resource "google_backup_dr_backup_plan_association" "backup_association" {
4459+
location = "us-central1"
4460+
backup_plan_association_id = "tf-test-bpa-test-%{random_suffix}"
4461+
resource = "projects/${data.google_project.project.project_id}/instances/${google_sql_database_instance.instance.name}"
4462+
resource_type = "sqladmin.googleapis.com/Instance"
4463+
backup_plan = google_backup_dr_backup_plan.plan.name
4464+
}
4465+
`, localContext)
4466+
}
42624467

42634468
func testGoogleSqlDatabaseInstance_setCustomSubjectAlternateName(context map[string]interface{}) string {
42644469
return acctest.Nprintf(`

0 commit comments

Comments
 (0)