Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Including scenario for adding/removing replication specs in the middle for sharded and geosharded cluster #2479

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 144 additions & 24 deletions internal/service/advancedcluster/resource_advanced_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedOldSchemaDiskSizeGBAtEl
})
}

func TestAccClusterAdvancedClusterConfig_symmetricShardedNewSchema(t *testing.T) {
func TestAccClusterAdvancedClusterConfig_symmetricShardedNewSchemaToAsymmetricAddingRemovingShard(t *testing.T) {
var (
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acc.RandomProjectName()
Expand All @@ -586,8 +586,12 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedNewSchema(t *testing.T)
Check: checkShardedNewSchema(50, "M30", "M30", "2000", "2000", false, false),
},
{
Config: configShardedNewSchema(orgID, projectName, clusterName, 55, "M30", "M40", 2000, 2000, true),
Check: checkShardedNewSchema(55, "M30", "M40", "2000", "2000", true, true),
Config: configShardedNewSchema(orgID, projectName, clusterName, 55, "M30", "M40", 2000, 2500, true),
Check: checkShardedNewSchema(55, "M30", "M40", "2000", "2500", true, true),
},
{
Config: configShardedNewSchema(orgID, projectName, clusterName, 55, "M30", "M40", 2000, 2500, false), // removes middle replication spec
Check: checkShardedNewSchema(55, "M30", "M40", "2000", "2500", true, false),
},
},
})
Expand All @@ -613,6 +617,34 @@ func TestAccClusterAdvancedClusterConfig_asymmetricShardedNewSchema(t *testing.T
})
}

func TestAccClusterAdvancedClusterConfig_asymmetricGeoShardedNewSchemaAddingRemovingShard(t *testing.T) {
var (
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acc.RandomProjectName()
clusterName = acc.RandomClusterName()
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configGeoShardedNewSchema(orgID, projectName, clusterName, false),
Check: checkGeoShardedNewSchema(false),
},
{
Config: configGeoShardedNewSchema(orgID, projectName, clusterName, true),
Check: checkGeoShardedNewSchema(true),
},
{
Config: configGeoShardedNewSchema(orgID, projectName, clusterName, false),
Check: checkGeoShardedNewSchema(false),
},
},
})
}

func TestAccClusterAdvancedClusterConfig_shardedTransitionFromOldToNewSchema(t *testing.T) {
var (
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
Expand Down Expand Up @@ -1426,28 +1458,28 @@ func checkShardedOldSchemaDiskSizeGBElectableLevel(diskSizeGB int) resource.Test
})
}

func configShardedNewSchema(orgID, projectName, name string, diskSizeGB int, instanceSizeSpec1, instanceSizeSpec2 string, diskIopsSpec1, diskIopsSpec2 int, includeThirdSpec bool) string {
func configShardedNewSchema(orgID, projectName, name string, diskSizeGB int, firstInstanceSize, lastInstanceSize string, firstDiskIops, lastDiskIops int, includeMiddleSpec bool) string {
var thirdReplicationSpec string
if includeThirdSpec {
if includeMiddleSpec {
thirdReplicationSpec = fmt.Sprintf(`
replication_specs {
region_configs {
electable_specs {
instance_size = %[1]q
node_count = 3
disk_size_gb = %[3]d
disk_size_gb = %[2]d
}
analytics_specs {
instance_size = %[1]q
node_count = 1
disk_size_gb = %[3]d
disk_size_gb = %[2]d
}
provider_name = "AWS"
priority = 7
region_name = "EU_WEST_1"
}
}
`, instanceSizeSpec1, diskIopsSpec1, diskSizeGB)
`, firstInstanceSize, diskSizeGB)
}
return fmt.Sprintf(`
resource "mongodbatlas_project" "cluster_project" {
Expand Down Expand Up @@ -1481,6 +1513,8 @@ func configShardedNewSchema(orgID, projectName, name string, diskSizeGB int, ins
}
}

%[8]s

replication_specs {
region_configs {
electable_specs {
Expand All @@ -1500,8 +1534,6 @@ func configShardedNewSchema(orgID, projectName, name string, diskSizeGB int, ins
region_name = "EU_WEST_1"
}
}

%[8]s
}

data "mongodbatlas_advanced_cluster" "test" {
Expand All @@ -1514,27 +1546,31 @@ func configShardedNewSchema(orgID, projectName, name string, diskSizeGB int, ins
project_id = mongodbatlas_advanced_cluster.test.project_id
use_replication_spec_per_shard = true
}
`, orgID, projectName, name, instanceSizeSpec1, instanceSizeSpec2, diskIopsSpec1, diskIopsSpec2, thirdReplicationSpec, diskSizeGB)
`, orgID, projectName, name, firstInstanceSize, lastInstanceSize, firstDiskIops, lastDiskIops, thirdReplicationSpec, diskSizeGB)
}

func checkShardedNewSchema(diskSizeGB int, instanceSizeSpec1, instanceSizeSpec2, diskIopsSpec1, diskIopsSpec2 string, isAsymmetricCluster, includesThirdSpec bool) resource.TestCheckFunc {
var amtOfReplicationSpecs int
if includesThirdSpec {
func checkShardedNewSchema(diskSizeGB int, firstInstanceSize, lastInstanceSize, firstDiskIops, lastDiskIops string, isAsymmetricCluster, includeMiddleSpec bool) resource.TestCheckFunc {
amtOfReplicationSpecs := 2
if includeMiddleSpec {
amtOfReplicationSpecs = 3
} else {
amtOfReplicationSpecs = 2
}

lastSpecIndex := 1
if includeMiddleSpec {
lastSpecIndex = 2
}

clusterChecks := map[string]string{
"disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
"replication_specs.#": fmt.Sprintf("%d", amtOfReplicationSpecs),
"replication_specs.0.region_configs.0.electable_specs.0.instance_size": instanceSizeSpec1,
"replication_specs.1.region_configs.0.electable_specs.0.instance_size": instanceSizeSpec2,
"replication_specs.0.region_configs.0.electable_specs.0.disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
"replication_specs.1.region_configs.0.electable_specs.0.disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
"replication_specs.0.region_configs.0.analytics_specs.0.disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
"replication_specs.1.region_configs.0.analytics_specs.0.disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
"replication_specs.0.region_configs.0.electable_specs.0.disk_iops": diskIopsSpec1,
"replication_specs.1.region_configs.0.electable_specs.0.disk_iops": diskIopsSpec2,
"replication_specs.0.region_configs.0.electable_specs.0.instance_size": firstInstanceSize,
fmt.Sprintf("replication_specs.%d.region_configs.0.electable_specs.0.instance_size", lastSpecIndex): lastInstanceSize,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

"replication_specs.0.region_configs.0.electable_specs.0.disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
fmt.Sprintf("replication_specs.%d.region_configs.0.electable_specs.0.disk_size_gb", lastSpecIndex): fmt.Sprintf("%d", diskSizeGB),
"replication_specs.0.region_configs.0.analytics_specs.0.disk_size_gb": fmt.Sprintf("%d", diskSizeGB),
fmt.Sprintf("replication_specs.%d.region_configs.0.analytics_specs.0.disk_size_gb", lastSpecIndex): fmt.Sprintf("%d", diskSizeGB),
"replication_specs.0.region_configs.0.electable_specs.0.disk_iops": firstDiskIops,
fmt.Sprintf("replication_specs.%d.region_configs.0.electable_specs.0.disk_iops", lastSpecIndex): lastDiskIops,
}

// plural data source checks
Expand Down Expand Up @@ -1564,6 +1600,90 @@ func checkShardedNewSchema(diskSizeGB int, instanceSizeSpec1, instanceSizeSpec2,
)
}

func configGeoShardedNewSchema(orgID, projectName, name string, includeThirdShardInFirstZone bool) string {
var thirdReplicationSpec string
if includeThirdShardInFirstZone {
thirdReplicationSpec = `
replication_specs {
zone_name = "zone n1"
region_configs {
electable_specs {
instance_size = "M10"
node_count = 3
}
provider_name = "AWS"
priority = 7
region_name = "US_EAST_1"
}
}
`
}
return fmt.Sprintf(`
resource "mongodbatlas_project" "cluster_project" {
org_id = %[1]q
name = %[2]q
}
resource "mongodbatlas_advanced_cluster" "test" {
project_id = mongodbatlas_project.cluster_project.id
name = %[3]q
backup_enabled = false
mongo_db_major_version = "7.0"
cluster_type = "GEOSHARDED"
replication_specs {
zone_name = "zone n1"
region_configs {
electable_specs {
instance_size = "M10"
node_count = 3
}
provider_name = "AWS"
priority = 7
region_name = "US_EAST_1"
}
}
%[4]s
replication_specs {
zone_name = "zone n2"
region_configs {
electable_specs {
instance_size = "M20"
node_count = 3
}
provider_name = "AWS"
priority = 7
region_name = "EU_WEST_1"
}
}
}
data "mongodbatlas_advanced_cluster" "test" {
project_id = mongodbatlas_advanced_cluster.test.project_id
name = mongodbatlas_advanced_cluster.test.name
use_replication_spec_per_shard = true
}
data "mongodbatlas_advanced_clusters" "test" {
project_id = mongodbatlas_advanced_cluster.test.project_id
use_replication_spec_per_shard = true
}
`, orgID, projectName, name, thirdReplicationSpec)
}

func checkGeoShardedNewSchema(includeThirdShardInFirstZone bool) resource.TestCheckFunc {
var amtOfReplicationSpecs int
if includeThirdShardInFirstZone {
amtOfReplicationSpecs = 3
} else {
amtOfReplicationSpecs = 2
}
clusterChecks := map[string]string{
"replication_specs.#": fmt.Sprintf("%d", amtOfReplicationSpecs),
}

return checkAggr(
[]string{},
clusterChecks,
)
}

func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useNewSchema bool) string {
var numShardsStr string
if !useNewSchema {
Expand Down