From 038ca2611c21268ac994b1f8a3db7c6dcd300b54 Mon Sep 17 00:00:00 2001 From: Agustin Bettati Date: Wed, 7 Aug 2024 15:23:41 +0200 Subject: [PATCH] test: Refactor migration tests to use utility function and cover migration scenarios coming from 1.17.5 (#2481) * refactor migration test and define scenarios for updating and transitioning after upgrade * fix check of external id in migration tests --- ...esource_advanced_cluster_migration_test.go | 128 ++++++------------ .../resource_advanced_cluster_test.go | 86 +++++++----- 2 files changed, 94 insertions(+), 120 deletions(-) diff --git a/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go b/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go index e586f7ebaa..9a64289e8c 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go +++ b/internal/service/advancedcluster/resource_advanced_cluster_migration_test.go @@ -12,91 +12,39 @@ import ( ) func TestMigAdvancedCluster_replicaSetAWSProvider(t *testing.T) { - var ( - projectID = acc.ProjectIDExecution(t) - clusterName = acc.RandomClusterName() - config = configReplicaSetAWSProvider(projectID, clusterName, 60, 3) - ) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { mig.PreCheckBasic(t) }, - CheckDestroy: acc.CheckDestroyCluster, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: config, - Check: checkReplicaSetAWSProvider(projectID, clusterName, 60, 3, false, false), - }, - mig.TestStepCheckEmptyPlan(config), - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: config, - Check: checkReplicaSetAWSProvider(projectID, clusterName, 60, 3, true, true), // external_id will be present in latest version - }, - }, - }) + // once 1.18.0 is released we can adjust this to always check new attributes - CLOUDP-266096 + testCase := replicaSetAWSProviderTestCase(t, false) + mig.CreateAndRunTest(t, &testCase) } func TestMigAdvancedCluster_replicaSetMultiCloud(t *testing.T) { - var ( - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region - clusterName = acc.RandomClusterName() - config = configReplicaSetMultiCloud(orgID, projectName, clusterName) - ) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { mig.PreCheckBasic(t) }, - CheckDestroy: acc.CheckDestroyCluster, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: config, - Check: checkReplicaSetMultiCloud(clusterName, 3, false), - }, - mig.TestStepCheckEmptyPlan(config), - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: config, - Check: checkReplicaSetMultiCloud(clusterName, 3, true), // external_id will be present in latest version - }, - }, - }) + // once 1.18.0 is released we can adjust this to always check new attributes - CLOUDP-266096 + testCase := replicaSetMultiCloudTestCase(t, false) + mig.CreateAndRunTest(t, &testCase) } func TestMigAdvancedCluster_singleShardedMultiCloud(t *testing.T) { - var ( - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region - clusterName = acc.RandomClusterName() - config = configShardedMultiCloud(orgID, projectName, clusterName, 1, "M30") - ) + // once 1.18.0 is released we can adjust this to always check new attributes - CLOUDP-266096 + testCase := singleShardedMultiCloudTestCase(t, false) + mig.CreateAndRunTest(t, &testCase) +} - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { mig.PreCheckBasic(t) }, - CheckDestroy: acc.CheckDestroyCluster, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: config, - Check: checkShardedMultiCloud(clusterName, 1, "M30", false), - }, - mig.TestStepCheckEmptyPlan(config), - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: config, - Check: checkShardedMultiCloud(clusterName, 1, "M30", true), // external_id will be present in latest version - }, - }, - }) +func TestMigAdvancedCluster_symmetricGeoShardedOldSchema(t *testing.T) { + // once 1.18.0 is released we can adjust this to always check new attributes - CLOUDP-266096 + testCase := symmetricGeoShardedOldSchemaTestCase(t, false) + mig.CreateAndRunTest(t, &testCase) +} + +func TestMigAdvancedCluster_asymmetricShardedNewSchema(t *testing.T) { + acc.SkipTestForCI(t) // latest release does not support ISS, to be adjusted in CLOUDP-266096 + testCase := asymmetricShardedNewSchemaTestCase(t) + mig.CreateAndRunTest(t, &testCase) } -func TestMigAdvancedCluster_singleShardPerZoneGeoSharded(t *testing.T) { +func TestMigAdvancedCluster_replicaSetAWSProviderUpdateAfterVerisonUpgrade(t *testing.T) { var ( - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region + projectID = acc.ProjectIDExecution(t) clusterName = acc.RandomClusterName() - config = configGeoShardedOldSchema(orgID, projectName, clusterName, 1, 1, false) ) resource.ParallelTest(t, resource.TestCase{ @@ -104,26 +52,24 @@ func TestMigAdvancedCluster_singleShardPerZoneGeoSharded(t *testing.T) { CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - ExternalProviders: mig.ExternalProviders(), - Config: config, - Check: checkGeoShardedOldSchema(clusterName, 1, 1, false, false), + ExternalProviders: acc.ExternalProviders("1.17.5"), // last version that did not support new sharding schema or attributes + Config: configReplicaSetAWSProvider(projectID, clusterName, 60, 3), + Check: checkReplicaSetAWSProvider(projectID, clusterName, 60, 3, false, false), }, - mig.TestStepCheckEmptyPlan(config), { ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: config, - Check: checkGeoShardedOldSchema(clusterName, 1, 1, true, true), // external_id will be present in latest version + Config: configReplicaSetAWSProvider(projectID, clusterName, 60, 5), + Check: checkReplicaSetAWSProvider(projectID, clusterName, 60, 5, true, true), }, }, }) } -func TestMigAdvancedCluster_symmetricGeoShardedOldSchema(t *testing.T) { +func TestMigAdvancedCluster_geoShardedOldSchemaUpdateAfterVerisonUpgrade(t *testing.T) { var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region clusterName = acc.RandomClusterName() - config = configGeoShardedOldSchema(orgID, projectName, clusterName, 2, 2, false) ) resource.ParallelTest(t, resource.TestCase{ @@ -131,16 +77,20 @@ func TestMigAdvancedCluster_symmetricGeoShardedOldSchema(t *testing.T) { CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - ExternalProviders: mig.ExternalProviders(), - Config: config, + ExternalProviders: acc.ExternalProviders("1.17.5"), // last version that did not support new sharding schema or attributes + Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 2, 2, false), Check: checkGeoShardedOldSchema(clusterName, 2, 2, false, false), }, - mig.TestStepCheckEmptyPlan(config), + { + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 2, 1, false), + Check: checkGeoShardedOldSchema(clusterName, 2, 1, true, false), + }, }, }) } -func TestMigAdvancedCluster_shardedTransitionFromOldToNewSchema(t *testing.T) { +func TestMigAdvancedCluster_shardedMigrationFromOldToNewSchema(t *testing.T) { var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() @@ -152,7 +102,7 @@ func TestMigAdvancedCluster_shardedTransitionFromOldToNewSchema(t *testing.T) { CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - ExternalProviders: mig.ExternalProviders(), + ExternalProviders: acc.ExternalProviders("1.17.5"), // last version that did not support new sharding schema or attributes Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false), Check: checkShardedTransitionOldToNewSchema(false), }, @@ -165,7 +115,7 @@ func TestMigAdvancedCluster_shardedTransitionFromOldToNewSchema(t *testing.T) { }) } -func TestMigAdvancedCluster_geoShardedTransitionFromOldToNewSchema(t *testing.T) { +func TestMigAdvancedCluster_geoShardedMigrationFromOldToNewSchema(t *testing.T) { var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() @@ -177,7 +127,7 @@ func TestMigAdvancedCluster_geoShardedTransitionFromOldToNewSchema(t *testing.T) CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - ExternalProviders: mig.ExternalProviders(), + ExternalProviders: acc.ExternalProviders("1.17.5"), // last version that did not support new sharding schema or attributes Config: configGeoShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false), Check: checkGeoShardedTransitionOldToNewSchema(false), }, diff --git a/internal/service/advancedcluster/resource_advanced_cluster_test.go b/internal/service/advancedcluster/resource_advanced_cluster_test.go index 15817a8aa0..37b84bd00e 100644 --- a/internal/service/advancedcluster/resource_advanced_cluster_test.go +++ b/internal/service/advancedcluster/resource_advanced_cluster_test.go @@ -54,23 +54,27 @@ func TestAccClusterAdvancedCluster_basicTenant(t *testing.T) { } func TestAccClusterAdvancedCluster_replicaSetAWSProvider(t *testing.T) { + resource.ParallelTest(t, replicaSetAWSProviderTestCase(t, true)) +} +func replicaSetAWSProviderTestCase(t *testing.T, checkNewAttributes bool) resource.TestCase { + t.Helper() var ( projectID = acc.ProjectIDExecution(t) clusterName = acc.RandomClusterName() ) - resource.ParallelTest(t, resource.TestCase{ + return resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { Config: configReplicaSetAWSProvider(projectID, clusterName, 60, 3), - Check: checkReplicaSetAWSProvider(projectID, clusterName, 60, 3, true, true), + Check: checkReplicaSetAWSProvider(projectID, clusterName, 60, 3, checkNewAttributes, checkNewAttributes), }, { Config: configReplicaSetAWSProvider(projectID, clusterName, 50, 5), - Check: checkReplicaSetAWSProvider(projectID, clusterName, 50, 5, true, true), + Check: checkReplicaSetAWSProvider(projectID, clusterName, 50, 5, checkNewAttributes, checkNewAttributes), }, { ResourceName: resourceName, @@ -80,10 +84,14 @@ func TestAccClusterAdvancedCluster_replicaSetAWSProvider(t *testing.T) { ImportStateVerifyIgnore: []string{"replication_specs", "retain_backups_enabled"}, }, }, - }) + } } func TestAccClusterAdvancedCluster_replicaSetMultiCloud(t *testing.T) { + resource.ParallelTest(t, replicaSetMultiCloudTestCase(t, true)) +} +func replicaSetMultiCloudTestCase(t *testing.T, verifyExternalID bool) resource.TestCase { + t.Helper() var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region @@ -91,18 +99,18 @@ func TestAccClusterAdvancedCluster_replicaSetMultiCloud(t *testing.T) { clusterNameUpdated = acc.RandomClusterName() ) - resource.ParallelTest(t, resource.TestCase{ + return resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { Config: configReplicaSetMultiCloud(orgID, projectName, clusterName), - Check: checkReplicaSetMultiCloud(clusterName, 3, true), + Check: checkReplicaSetMultiCloud(clusterName, 3, verifyExternalID), }, { Config: configReplicaSetMultiCloud(orgID, projectName, clusterNameUpdated), - Check: checkReplicaSetMultiCloud(clusterNameUpdated, 3, true), + Check: checkReplicaSetMultiCloud(clusterNameUpdated, 3, verifyExternalID), }, { ResourceName: resourceName, @@ -112,10 +120,15 @@ func TestAccClusterAdvancedCluster_replicaSetMultiCloud(t *testing.T) { ImportStateVerifyIgnore: []string{"replication_specs", "retain_backups_enabled"}, }, }, - }) + } } func TestAccClusterAdvancedCluster_singleShardedMultiCloud(t *testing.T) { + resource.ParallelTest(t, singleShardedMultiCloudTestCase(t, true)) +} + +func singleShardedMultiCloudTestCase(t *testing.T, verifyExternalID bool) resource.TestCase { + t.Helper() var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region @@ -123,18 +136,18 @@ func TestAccClusterAdvancedCluster_singleShardedMultiCloud(t *testing.T) { clusterNameUpdated = acc.RandomClusterName() ) - resource.ParallelTest(t, resource.TestCase{ + return resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - Config: configShardedMultiCloud(orgID, projectName, clusterName, 1, "M30"), - Check: checkShardedMultiCloud(clusterName, 1, "M30", true), + Config: configShardedOldSchemaMultiCloud(orgID, projectName, clusterName, 1, "M30"), + Check: checkShardedMultiCloud(clusterName, 1, "M30", verifyExternalID), }, { - Config: configShardedMultiCloud(orgID, projectName, clusterNameUpdated, 1, "M30"), - Check: checkShardedMultiCloud(clusterNameUpdated, 1, "M30", true), + Config: configShardedOldSchemaMultiCloud(orgID, projectName, clusterNameUpdated, 1, "M30"), + Check: checkShardedMultiCloud(clusterNameUpdated, 1, "M30", verifyExternalID), }, { ResourceName: resourceName, @@ -144,7 +157,7 @@ func TestAccClusterAdvancedCluster_singleShardedMultiCloud(t *testing.T) { ImportStateVerifyIgnore: []string{"replication_specs"}, }, }, - }) + } } func TestAccClusterAdvancedCluster_unpausedToPaused(t *testing.T) { @@ -510,11 +523,11 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedOldSchema(t *testing.T) CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { - Config: configShardedMultiCloud(orgID, projectName, clusterName, 2, "M30"), + Config: configShardedOldSchemaMultiCloud(orgID, projectName, clusterName, 2, "M30"), Check: checkShardedMultiCloud(clusterName, 2, "M30", false), }, { - Config: configShardedMultiCloud(orgID, projectName, clusterName, 2, "M40"), + Config: configShardedOldSchemaMultiCloud(orgID, projectName, clusterName, 2, "M40"), Check: checkShardedMultiCloud(clusterName, 2, "M40", false), }, }, @@ -522,27 +535,32 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedOldSchema(t *testing.T) } func TestAccClusterAdvancedClusterConfig_symmetricGeoShardedOldSchema(t *testing.T) { + resource.ParallelTest(t, symmetricGeoShardedOldSchemaTestCase(t, true)) +} + +func symmetricGeoShardedOldSchemaTestCase(t *testing.T, checkNewAttributes bool) resource.TestCase { + t.Helper() var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid cross-region limits because multi-region clusterName = acc.RandomClusterName() ) - resource.ParallelTest(t, resource.TestCase{ + return resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyCluster, Steps: []resource.TestStep{ { Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 2, 2, false), - Check: checkGeoShardedOldSchema(clusterName, 2, 2, true, false), + Check: checkGeoShardedOldSchema(clusterName, 2, 2, checkNewAttributes, false), }, { Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 3, 3, false), - Check: checkGeoShardedOldSchema(clusterName, 3, 3, true, false), + Check: checkGeoShardedOldSchema(clusterName, 3, 3, checkNewAttributes, false), }, }, - }) + } } func TestAccClusterAdvancedClusterConfig_symmetricShardedOldSchemaDiskSizeGBAtElectableLevel(t *testing.T) { @@ -598,13 +616,18 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedNewSchemaToAsymmetricAd } func TestAccClusterAdvancedClusterConfig_asymmetricShardedNewSchema(t *testing.T) { + resource.ParallelTest(t, asymmetricShardedNewSchemaTestCase(t)) +} + +func asymmetricShardedNewSchemaTestCase(t *testing.T) resource.TestCase { + t.Helper() var ( orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") projectName = acc.RandomProjectName() clusterName = acc.RandomClusterName() ) - resource.ParallelTest(t, resource.TestCase{ + return resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyCluster, @@ -614,7 +637,7 @@ func TestAccClusterAdvancedClusterConfig_asymmetricShardedNewSchema(t *testing.T Check: checkShardedNewSchema(50, "M30", "M40", "2000", "2500", true, false), }, }, - }) + } } func TestAccClusterAdvancedClusterConfig_asymmetricGeoShardedNewSchemaAddingRemovingShard(t *testing.T) { @@ -1010,7 +1033,7 @@ func checkReplicaSetMultiCloud(name string, regionConfigs int, verifyExternalID ) } -func configShardedMultiCloud(orgID, projectName, name string, numShards int, analyticsSize string) string { +func configShardedOldSchemaMultiCloud(orgID, projectName, name string, numShards int, analyticsSize string) string { return fmt.Sprintf(` resource "mongodbatlas_project" "cluster_project" { org_id = %[1]q @@ -1057,18 +1080,19 @@ func configShardedMultiCloud(orgID, projectName, name string, numShards int, ana } func checkShardedMultiCloud(name string, numShards int, analyticsSize string, verifyExternalID bool) resource.TestCheckFunc { - additionalChecks := []resource.TestCheckFunc{} + additionalChecks := []resource.TestCheckFunc{ + resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.0.electable_specs.0.disk_iops", acc.IntGreatThan(0)), + resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.0.analytics_specs.0.disk_iops", acc.IntGreatThan(0)), + resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.1.electable_specs.0.disk_iops", acc.IntGreatThan(0)), + resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.0.electable_specs.0.disk_iops", acc.IntGreatThan(0)), + resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.0.analytics_specs.0.disk_iops", acc.IntGreatThan(0)), + resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.1.electable_specs.0.disk_iops", acc.IntGreatThan(0)), + } if verifyExternalID { additionalChecks = append( additionalChecks, - resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.external_id"), - resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.0.electable_specs.0.disk_iops", acc.IntGreatThan(0)), - resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.0.analytics_specs.0.disk_iops", acc.IntGreatThan(0)), - resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.1.electable_specs.0.disk_iops", acc.IntGreatThan(0)), - resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.0.electable_specs.0.disk_iops", acc.IntGreatThan(0)), - resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.0.analytics_specs.0.disk_iops", acc.IntGreatThan(0)), - resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.1.electable_specs.0.disk_iops", acc.IntGreatThan(0))) + resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.external_id")) } return checkAggr(