Skip to content

Commit

Permalink
test: Add tests for mongodb_advanced_cluster covering transitions t…
Browse files Browse the repository at this point in the history
…o auto-scaling mode (#2851)

* implement GetIndependentShardScalingMode

* add tests

* add comment on fixed version

* use base url from env variable to make tests work in QA
  • Loading branch information
oarbusi authored Dec 4, 2024
1 parent 249c425 commit cfc513d
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ func TestMigAdvancedCluster_shardedMigrationFromOldToNewSchema(t *testing.T) {
Steps: []resource.TestStep{
{
ExternalProviders: acc.ExternalProviders(versionBeforeISSRelease),
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false),
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false),
Check: checkShardedTransitionOldToNewSchema(false),
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true),
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false),
Check: checkShardedTransitionOldToNewSchema(true),
},
},
Expand Down Expand Up @@ -228,6 +228,32 @@ func TestMigAdvancedCluster_partialAdvancedConf(t *testing.T) {
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName),
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
ExternalProviders: acc.ExternalProviders("1.22.0"), // last version before cluster tier auto-scaling per shard was introduced
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false),
Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"),
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, true),
Check: checkIndependentShardScalingMode(clusterName, "SHARD"),
},
},
})
}

func configPartialAdvancedConfig(projectID, clusterName, extraArgs, autoScaling string) string {
return fmt.Sprintf(`
resource "mongodbatlas_advanced_cluster" "test" {
Expand Down
139 changes: 135 additions & 4 deletions internal/service/advancedcluster/resource_advanced_cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package advancedcluster_test

import (
"context"
"fmt"
"os"
"regexp"
Expand Down Expand Up @@ -708,11 +709,11 @@ func TestAccClusterAdvancedClusterConfig_shardedTransitionFromOldToNewSchema(t *
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false),
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false),
Check: checkShardedTransitionOldToNewSchema(false),
},
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true),
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false),
Check: checkShardedTransitionOldToNewSchema(true),
},
},
Expand Down Expand Up @@ -866,6 +867,127 @@ func TestAccClusterAdvancedCluster_priorityNewSchema(t *testing.T) {
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false),
Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"),
},
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false),
Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, true),
Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"),
},
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, true),
Check: checkIndependentShardScalingMode(clusterName, "SHARD"),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, false, false),
Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"),
},
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false),
Check: checkIndependentShardScalingMode(clusterName, "CLUSTER"),
},
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, true),
Check: checkIndependentShardScalingMode(clusterName, "SHARD"),
},
},
})
}

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: acc.PreCheckBasicSleep(t, nil, orgID, projectName),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyCluster,
Steps: []resource.TestStep{
{
Config: configShardedTransitionOldToNewSchema(orgID, projectName, clusterName, true, false),
Check: checkIndependentShardScalingMode(clusterName, "SHARD"),
},
},
})
}

func checkIndependentShardScalingMode(clusterName, expectedMode string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}
ids := conversion.DecodeStateID(rs.Primary.ID)
issMode, _, err := acc.GetIndependentShardScalingMode(context.Background(), ids["project_id"], clusterName)
if err != nil {
return fmt.Errorf("error getting independent shard scaling mode: %w", err)
}
if *issMode != expectedMode {
return fmt.Errorf("expected independent shard scaling mode to be %s, got %s", expectedMode, *issMode)
}
return nil
}
}

func checkAggr(attrsSet []string, attrsMap map[string]string, extra ...resource.TestCheckFunc) resource.TestCheckFunc {
checks := []resource.TestCheckFunc{checkExists(resourceName)}
checks = acc.AddAttrChecks(resourceName, checks, attrsMap)
Expand Down Expand Up @@ -1908,11 +2030,19 @@ func checkGeoShardedNewSchema(includeThirdShardInFirstZone bool) resource.TestCh
)
}

func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useNewSchema bool) string {
func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useNewSchema, autoscaling bool) string {
var numShardsStr string
if !useNewSchema {
numShardsStr = `num_shards = 2`
}
var autoscalingStr string
if autoscaling {
autoscalingStr = `auto_scaling {
compute_enabled = true
disk_gb_enabled = true
compute_max_instance_size = "M20"
}`
}
replicationSpec := fmt.Sprintf(`
replication_specs {
%[1]s
Expand All @@ -1928,9 +2058,10 @@ func configShardedTransitionOldToNewSchema(orgID, projectName, name string, useN
provider_name = "AWS"
priority = 7
region_name = "EU_WEST_1"
%[2]s
}
}
`, numShardsStr)
`, numShardsStr, autoscalingStr)

var replicationSpecs string
if useNewSchema {
Expand Down
49 changes: 49 additions & 0 deletions internal/testutil/acc/independent_shard_scaling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package acc

import (
"context"
"io"
"net/http"
"os"

"github.com/mongodb-forks/digest"
)

func GetIndependentShardScalingMode(ctx context.Context, projectID, clusterName string) (*string, *http.Response, error) {
baseURL := os.Getenv("MONGODB_ATLAS_BASE_URL")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"test/utils/auth/groups/"+projectID+"/clusters/"+clusterName+"/independentShardScalingMode", http.NoBody)
if err != nil {
return nil, nil, err
}

req.Header.Add("Accept", "*/*")

transport := digest.NewTransport(os.Getenv("MONGODB_ATLAS_PUBLIC_KEY"), os.Getenv("MONGODB_ATLAS_PRIVATE_KEY"))
httpClient, err := transport.Client()
if err != nil {
return nil, nil, err
}

resp, err := httpClient.Do(req)
if err != nil || resp == nil {
return nil, resp, err
}

var result *string
result, err = decode(resp.Body)
if err != nil {
return nil, resp, err
}

return result, resp, nil
}

func decode(body io.ReadCloser) (*string, error) {
buf, err := io.ReadAll(body)
_ = body.Close()
if err != nil {
return nil, err
}
result := string(buf)
return &result, nil
}

0 comments on commit cfc513d

Please sign in to comment.