Skip to content

Commit

Permalink
add migration test for check correct adoption of external_id (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinBettati authored Jul 16, 2024
1 parent 95a18d5 commit 19595be
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMigAdvancedCluster_replicaSetAWSProvider(t *testing.T) {
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
config = configReplicaSetAWSProvider(projectID, clusterName)
config = configReplicaSetAWSProvider(projectID, clusterName, 3)
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -25,9 +25,14 @@ func TestMigAdvancedCluster_replicaSetAWSProvider(t *testing.T) {
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: checkReplicaSetAWSProvider(projectID, clusterName, false),
Check: checkReplicaSetAWSProvider(projectID, clusterName, 3, false, false),
},
mig.TestStepCheckEmptyPlan(config),
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: config,
Check: checkReplicaSetAWSProvider(projectID, clusterName, 3, true, true), // external_id will be present in latest version
},
},
})
}
Expand All @@ -47,7 +52,89 @@ func TestMigAdvancedCluster_replicaSetMultiCloud(t *testing.T) {
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: checkReplicaSetMultiCloud(clusterName, 3),
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
},
},
})
}

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 = configSingleShardedMultiCloud(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: checkSingleShardedMultiCloud(clusterName, false),
},
mig.TestStepCheckEmptyPlan(config),
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: config,
Check: checkSingleShardedMultiCloud(clusterName, true), // external_id will be present in latest version
},
},
})
}

func TestMigAdvancedCluster_singleShardPerZoneGeoSharded(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, 1, 1, false)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: checkGeoShardedOldSchema(clusterName, 1, 1, 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
},
},
})
}

func TestMigAdvancedCluster_symmetricGeoShardedOldSchema(t *testing.T) {
acc.SkipTestForCI(t) // TODO: CLOUDP-260154 for ensuring old schema is supported
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{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: checkGeoShardedOldSchema(clusterName, 2, 2, false, false),
},
mig.TestStepCheckEmptyPlan(config),
},
Expand Down
96 changes: 67 additions & 29 deletions internal/service/advancedcluster/resource_advanced_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ func TestAccClusterAdvancedCluster_replicaSetAWSProvider(t *testing.T) {
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configReplicaSetAWSProvider(projectID, clusterName),
Check: checkReplicaSetAWSProvider(projectID, clusterName, true),
Config: configReplicaSetAWSProvider(projectID, clusterName, 3),
Check: checkReplicaSetAWSProvider(projectID, clusterName, 3, true, true),
},
{
Config: configReplicaSetAWSProvider(projectID, clusterName, 5),
Check: checkReplicaSetAWSProvider(projectID, clusterName, 5, true, true),
},
{
ResourceName: resourceName,
Expand Down Expand Up @@ -95,11 +99,11 @@ func TestAccClusterAdvancedCluster_replicaSetMultiCloud(t *testing.T) {
Steps: []resource.TestStep{
{
Config: configReplicaSetMultiCloud(orgID, projectName, clusterName),
Check: checkReplicaSetMultiCloud(clusterName, 3),
Check: checkReplicaSetMultiCloud(clusterName, 3, true),
},
{
Config: configReplicaSetMultiCloud(orgID, projectName, clusterNameUpdated),
Check: checkReplicaSetMultiCloud(clusterNameUpdated, 3),
Check: checkReplicaSetMultiCloud(clusterNameUpdated, 3, true),
},
{
ResourceName: resourceName,
Expand Down Expand Up @@ -127,11 +131,11 @@ func TestAccClusterAdvancedCluster_singleShardedMultiCloud(t *testing.T) {
Steps: []resource.TestStep{
{
Config: configSingleShardedMultiCloud(orgID, projectName, clusterName),
Check: checkSingleShardedMultiCloud(clusterName),
Check: checkSingleShardedMultiCloud(clusterName, true),
},
{
Config: configSingleShardedMultiCloud(orgID, projectName, clusterNameUpdated),
Check: checkSingleShardedMultiCloud(clusterNameUpdated),
Check: checkSingleShardedMultiCloud(clusterNameUpdated, true),
},
{
ResourceName: resourceName,
Expand Down Expand Up @@ -409,7 +413,7 @@ func TestAccClusterAdvancedClusterConfig_singleShardedTransitionToOldSchemaExpec
Steps: []resource.TestStep{
{
Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 1, 1, false),
Check: checkGeoShardedOldSchema(clusterName, 1, 1),
Check: checkGeoShardedOldSchema(clusterName, 1, 1, true, true),
},
{
Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 1, 2, false),
Expand Down Expand Up @@ -509,11 +513,11 @@ func TestAccClusterAdvancedClusterConfig_symmetricGeoShardedOldSchema(t *testing
Steps: []resource.TestStep{
{
Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 2, 2, false),
Check: checkGeoShardedOldSchema(clusterName, 2, 2),
Check: checkGeoShardedOldSchema(clusterName, 2, 2, true, false),
},
{
Config: configGeoShardedOldSchema(orgID, projectName, clusterName, 3, 3, false),
Check: checkGeoShardedOldSchema(clusterName, 3, 3),
Check: checkGeoShardedOldSchema(clusterName, 3, 3, true, false),
},
},
})
Expand Down Expand Up @@ -725,7 +729,7 @@ func checkTags(name string, tags ...map[string]string) resource.TestCheckFunc {
tagChecks...)
}

func configReplicaSetAWSProvider(projectID, name string) string {
func configReplicaSetAWSProvider(projectID, name string, nodeCountElectable int) string {
return fmt.Sprintf(`
resource "mongodbatlas_advanced_cluster" "test" {
project_id = %[1]q
Expand All @@ -738,7 +742,7 @@ func configReplicaSetAWSProvider(projectID, name string) string {
region_configs {
electable_specs {
instance_size = "M10"
node_count = 3
node_count = %[3]d
}
analytics_specs {
instance_size = "M10"
Expand All @@ -755,10 +759,10 @@ func configReplicaSetAWSProvider(projectID, name string) string {
project_id = mongodbatlas_advanced_cluster.test.project_id
name = mongodbatlas_advanced_cluster.test.name
}
`, projectID, name)
`, projectID, name, nodeCountElectable)
}

func checkReplicaSetAWSProvider(projectID, name string, checkDiskSizeGBInnerLevel bool) resource.TestCheckFunc {
func checkReplicaSetAWSProvider(projectID, name string, nodeCountElectable int, checkDiskSizeGBInnerLevel, checkExternalID bool) resource.TestCheckFunc {
additionalChecks := []resource.TestCheckFunc{
resource.TestCheckResourceAttr(resourceName, "retain_backups_enabled", "true"),
resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.0.electable_specs.0.disk_iops", acc.IntGreatThan(0)),
Expand All @@ -772,12 +776,18 @@ func checkReplicaSetAWSProvider(projectID, name string, checkDiskSizeGBInnerLeve
}),
)
}

if checkExternalID {
additionalChecks = append(additionalChecks, resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.external_id"))
}

return checkAggr(
[]string{"replication_specs.#", "replication_specs.0.region_configs.#"},
map[string]string{
"project_id": projectID,
"disk_size_gb": "60",
"name": name},
"replication_specs.0.region_configs.0.electable_specs.0.node_count": fmt.Sprintf("%d", nodeCountElectable),
"name": name},
additionalChecks...,
)
}
Expand Down Expand Up @@ -869,18 +879,24 @@ func configReplicaSetMultiCloud(orgID, projectName, name string) string {
`, orgID, projectName, name)
}

func checkReplicaSetMultiCloud(name string, regionConfigs int) resource.TestCheckFunc {
return checkAggr(
[]string{"project_id", "replication_specs.#"},
map[string]string{
"name": name},
func checkReplicaSetMultiCloud(name string, regionConfigs int, verifyExternalID bool) resource.TestCheckFunc {
additionalChecks := []resource.TestCheckFunc{
resource.TestCheckResourceAttr(resourceName, "retain_backups_enabled", "false"),
resource.TestCheckResourceAttrWith(resourceName, "replication_specs.0.region_configs.#", acc.JSONEquals(strconv.Itoa(regionConfigs))),
resource.TestCheckResourceAttrWith(dataSourceName, "replication_specs.0.region_configs.#", acc.JSONEquals(strconv.Itoa(regionConfigs))),
resource.TestCheckResourceAttrWith(dataSourcePluralName, "results.0.replication_specs.0.region_configs.#", acc.JSONEquals(strconv.Itoa(regionConfigs))),
resource.TestCheckResourceAttrSet(dataSourcePluralName, "results.#"),
resource.TestCheckResourceAttrSet(dataSourcePluralName, "results.0.replication_specs.#"),
resource.TestCheckResourceAttrSet(dataSourcePluralName, "results.0.name"),
}
if verifyExternalID {
additionalChecks = append(additionalChecks, resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.external_id"))
}
return checkAggr(
[]string{"project_id", "replication_specs.#"},
map[string]string{
"name": name},
additionalChecks...,
)
}

Expand Down Expand Up @@ -930,11 +946,18 @@ func configSingleShardedMultiCloud(orgID, projectName, name string) string {
`, orgID, projectName, name)
}

func checkSingleShardedMultiCloud(name string) resource.TestCheckFunc {
func checkSingleShardedMultiCloud(name string, verifyExternalID bool) resource.TestCheckFunc {
additionalChecks := []resource.TestCheckFunc{}

if verifyExternalID {
additionalChecks = append(additionalChecks, resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.external_id"))
}

return checkAggr(
[]string{"project_id", "replication_specs.#", "replication_specs.0.region_configs.#"},
map[string]string{
"name": name})
"name": name},
additionalChecks...)
}

func configSingleProviderPaused(projectID, clusterName string, paused bool, instanceSize string) string {
Expand Down Expand Up @@ -1233,17 +1256,32 @@ func configGeoShardedOldSchema(orgID, projectName, name string, numShardsFirstZo
`, orgID, projectName, name, numShardsFirstZone, numShardsSecondZone, selfManagedSharding)
}

func checkGeoShardedOldSchema(name string, numShardsFirstZone, numShardsSecondZone int) resource.TestCheckFunc {
func checkGeoShardedOldSchema(name string, numShardsFirstZone, numShardsSecondZone int, verifyDiskSizeGBInnerLevel, verifyExternalID bool) resource.TestCheckFunc {
additionalChecks := []resource.TestCheckFunc{}

if verifyExternalID {
additionalChecks = append(additionalChecks, resource.TestCheckResourceAttrSet(resourceName, "replication_specs.0.external_id"))
}

if verifyDiskSizeGBInnerLevel {
additionalChecks = append(additionalChecks, checkAggr(
[]string{},
map[string]string{
"replication_specs.0.region_configs.0.electable_specs.0.disk_size_gb": "60",
"replication_specs.0.region_configs.0.analytics_specs.0.disk_size_gb": "60",
}))
}

return checkAggr(
[]string{"project_id"},
map[string]string{
"name": name,
"disk_size_gb": "60",
"replication_specs.0.region_configs.0.electable_specs.0.disk_size_gb": "60",
"replication_specs.0.region_configs.0.analytics_specs.0.disk_size_gb": "60",
"replication_specs.0.num_shards": strconv.Itoa(numShardsFirstZone),
"replication_specs.1.num_shards": strconv.Itoa(numShardsSecondZone),
})
"name": name,
"disk_size_gb": "60",
"replication_specs.0.num_shards": strconv.Itoa(numShardsFirstZone),
"replication_specs.1.num_shards": strconv.Itoa(numShardsSecondZone),
},
additionalChecks...,
)
}

func configShardedOldSchemaDiskSizeGBElectableLevel(orgID, projectName, name string) string {
Expand Down

0 comments on commit 19595be

Please sign in to comment.