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: Refactors resource tests to use GetClusterInfo cluster_outage_simulation #2423

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,13 @@ package clusteroutagesimulation_test
import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
)

func TestMigOutageSimulationCluster_SingleRegion_basic(t *testing.T) {
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
config = configSingleRegion(projectID, clusterName)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "outage_filters.#"),
resource.TestCheckResourceAttrSet(resourceName, "start_request_date"),
resource.TestCheckResourceAttrSet(resourceName, "simulation_id"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
),
},
mig.TestStepCheckEmptyPlan(config),
},
})
mig.CreateAndRunTest(t, singleRegionTestCase(t))
}

func TestMigOutageSimulationCluster_MultiRegion_basic(t *testing.T) {
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
config = configMultiRegion(projectID, clusterName)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: config,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "outage_filters.#"),
resource.TestCheckResourceAttrSet(resourceName, "start_request_date"),
resource.TestCheckResourceAttrSet(resourceName, "simulation_id"),
resource.TestCheckResourceAttrSet(resourceName, "state"),
),
},
mig.TestStepCheckEmptyPlan(config),
},
})
mig.CreateAndRunTest(t, multiRegionTestCase(t))
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,27 @@ const (
)

func TestAccOutageSimulationCluster_SingleRegion_basic(t *testing.T) {
resource.ParallelTest(t, *singleRegionTestCase(t))
}

func singleRegionTestCase(t *testing.T) *resource.TestCase {
t.Helper()
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
singleRegionRequest = acc.ClusterRequest{
ReplicationSpecs: []acc.ReplicationSpecRequest{
{Region: "US_WEST_2", InstanceSize: "M10"},
},
}
clusterInfo = acc.GetClusterInfo(t, &singleRegionRequest)
clusterName = clusterInfo.ClusterName
)

resource.ParallelTest(t, resource.TestCase{
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configSingleRegion(projectID, clusterName),
Config: configSingleRegion(&clusterInfo),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
Expand All @@ -46,22 +55,37 @@ func TestAccOutageSimulationCluster_SingleRegion_basic(t *testing.T) {
),
},
},
})
}
}

func TestAccOutageSimulationCluster_MultiRegion_basic(t *testing.T) {
resource.ParallelTest(t, *multiRegionTestCase(t))
}

func multiRegionTestCase(t *testing.T) *resource.TestCase {
t.Helper()
var (
projectID = acc.ProjectIDExecution(t)
clusterName = acc.RandomClusterName()
multiRegionRequest = acc.ClusterRequest{ReplicationSpecs: []acc.ReplicationSpecRequest{
{
Region: "US_EAST_1",
NodeCount: 3,
ExtraRegionConfigs: []acc.ReplicationSpecRequest{
{Region: "US_EAST_2", NodeCount: 2, Priority: 6},
{Region: "US_WEST_2", NodeCount: 2, Priority: 5, NodeCountReadOnly: 2},
},
},
}}
clusterInfo = acc.GetClusterInfo(t, &multiRegionRequest)
clusterName = clusterInfo.ClusterName
)

resource.ParallelTest(t, resource.TestCase{
return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configMultiRegion(projectID, clusterName),
Config: configMultiRegion(&clusterInfo),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "cluster_name", clusterName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
Expand All @@ -79,73 +103,36 @@ func TestAccOutageSimulationCluster_MultiRegion_basic(t *testing.T) {
),
},
},
})
}
}

func configSingleRegion(projectID, clusterName string) string {
func configSingleRegion(info *acc.ClusterInfo) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "test" {
project_id = %[1]q
name = %[2]q
provider_name = "AWS"
provider_region_name = "US_WEST_2"
provider_instance_size_name = "M10"
}

%[1]s
resource "mongodbatlas_cluster_outage_simulation" "test_outage" {
project_id = %[1]q
cluster_name = %[2]q
project_id = %[2]q
cluster_name = %[3]q
outage_filters {
cloud_provider = "AWS"
region_name = "US_WEST_2"
}
depends_on = ["mongodbatlas_cluster.test"]
depends_on = [%[4]s]
}

data "mongodbatlas_cluster_outage_simulation" "test" {
project_id = %[1]q
cluster_name = %[2]q
project_id = %[2]q
cluster_name = %[3]q
depends_on = [mongodbatlas_cluster_outage_simulation.test_outage]
}
`, projectID, clusterName)
`, info.ClusterTerraformStr, info.ProjectID, info.ClusterName, info.ClusterResourceName)
}

func configMultiRegion(projectID, clusterName string) string {
func configMultiRegion(info *acc.ClusterInfo) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "test" {
project_id = %[1]q
name = %[2]q
cluster_type = "REPLICASET"

provider_name = "AWS"
provider_instance_size_name = "M10"

replication_specs {
num_shards = 1
regions_config {
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
regions_config {
region_name = "US_EAST_2"
electable_nodes = 2
priority = 6
read_only_nodes = 0
}
regions_config {
region_name = "US_WEST_2"
electable_nodes = 2
priority = 5
read_only_nodes = 2
}
}
}

%[1]s
resource "mongodbatlas_cluster_outage_simulation" "test_outage" {
project_id = %[1]q
cluster_name = %[2]q
project_id = %[2]q
cluster_name = %[3]q

outage_filters {
cloud_provider = "AWS"
Expand All @@ -155,15 +142,15 @@ func configMultiRegion(projectID, clusterName string) string {
cloud_provider = "AWS"
region_name = "US_EAST_2"
}
depends_on = ["mongodbatlas_cluster.test"]
depends_on = [%[4]s]
}

data "mongodbatlas_cluster_outage_simulation" "test" {
project_id = %[1]q
cluster_name = %[2]q
project_id = %[2]q
cluster_name = %[3]q
depends_on = [mongodbatlas_cluster_outage_simulation.test_outage]
}
`, projectID, clusterName)
`, info.ClusterTerraformStr, info.ProjectID, info.ClusterName, info.ClusterResourceName)
}

func checkDestroy(s *terraform.State) error {
Expand Down
15 changes: 15 additions & 0 deletions internal/testutil/acc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ type ReplicationSpecRequest struct {
ProviderName string
ExtraRegionConfigs []ReplicationSpecRequest
NodeCount int
NodeCountReadOnly int
Priority int
AutoScalingDiskGbEnabled bool
}

func (r *ReplicationSpecRequest) AddDefaults() {
if r.Priority == 0 {
r.Priority = 7
}
if r.NodeCount == 0 {
r.NodeCount = 3
}
Expand Down Expand Up @@ -141,13 +146,23 @@ func ReplicationSpec(req *ReplicationSpecRequest) admin.ReplicationSpec {
}

func CloudRegionConfig(req ReplicationSpecRequest) admin.CloudRegionConfig {
req.AddDefaults()
var readOnly admin.DedicatedHardwareSpec
if req.NodeCountReadOnly != 0 {
readOnly = admin.DedicatedHardwareSpec{
NodeCount: &req.NodeCountReadOnly,
InstanceSize: &req.InstanceSize,
}
}
return admin.CloudRegionConfig{
RegionName: &req.Region,
Priority: &req.Priority,
ProviderName: &req.ProviderName,
ElectableSpecs: &admin.HardwareSpec{
InstanceSize: &req.InstanceSize,
NodeCount: &req.NodeCount,
},
ReadOnlySpecs: &readOnly,
AutoScaling: &admin.AdvancedAutoScalingSettings{
DiskGB: &admin.DiskGBAutoScaling{Enabled: &req.AutoScalingDiskGbEnabled},
},
Expand Down
6 changes: 6 additions & 0 deletions internal/testutil/acc/config_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ func writeReplicationSpec(cluster *hclwrite.Body, spec admin.ReplicationSpec) er
nodeSpec := rc.GetElectableSpecs()
nodeSpecBlock := rcBlock.AppendNewBlock("electable_specs", nil).Body()
err = addPrimitiveAttributesViaJSON(nodeSpecBlock, nodeSpec)

readOnlySpecs := rc.GetReadOnlySpecs()
if readOnlySpecs.GetNodeCount() != 0 {
readOnlyBlock := rcBlock.AppendNewBlock("read_only_specs", nil).Body()
err = addPrimitiveAttributesViaJSON(readOnlyBlock, readOnlySpecs)
}
}
return err
}
Expand Down
40 changes: 40 additions & 0 deletions internal/testutil/acc/config_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,38 @@ resource "mongodbatlas_advanced_cluster" "cluster_info" {
value = "test"
}

}
`
var readOnlyAndPriority = `
resource "mongodbatlas_advanced_cluster" "cluster_info" {
backup_enabled = false
cluster_type = "REPLICASET"
name = "my-name"
pit_enabled = false
project_id = "project"

replication_specs {
num_shards = 1
zone_name = "Zone 1"

region_configs {
priority = 5
provider_name = "AWS"
region_name = "US_EAST_1"
auto_scaling {
disk_gb_enabled = false
}
electable_specs {
instance_size = "M10"
node_count = 5
}
read_only_specs {
instance_size = "M10"
node_count = 1
}
}
}

}
`

Expand Down Expand Up @@ -398,6 +430,14 @@ func Test_ClusterResourceHcl(t *testing.T) {
{AutoScalingDiskGbEnabled: true},
}},
},
"readOnlyAndPriority": {
readOnlyAndPriority,
acc.ClusterRequest{
ClusterName: clusterName,
ReplicationSpecs: []acc.ReplicationSpecRequest{
{Priority: 5, NodeCount: 5, Region: "US_EAST_1", NodeCountReadOnly: 1},
}},
},
}
)
for name, tc := range testCases {
Expand Down