Skip to content

Commit

Permalink
test: refactor test to use GetClusterInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenAlbert committed Jul 15, 2024
1 parent 6983e27 commit b60cc3d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

const (
resourceName = "mongodbatlas_backup_compliance_policy.backup_policy_res"
dataSourceName = "data.mongodbatlas_backup_compliance_policy.backup_policy"
resourceName = "mongodbatlas_backup_compliance_policy.backup_policy_res"
dataSourceName = "data.mongodbatlas_backup_compliance_policy.backup_policy"
projectIDTerraform = "mongodbatlas_project.test.id"
)

func TestAccBackupCompliancePolicy_basic(t *testing.T) {
Expand Down Expand Up @@ -61,17 +62,31 @@ func TestAccBackupCompliancePolicy_overwriteBackupPolicies(t *testing.T) {
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acc.RandomProjectName() // No ProjectIDExecution to avoid conflicts with backup compliance policy
projectOwnerID = os.Getenv("MONGODB_ATLAS_PROJECT_OWNER_ID")
req = acc.ClusterRequest{
AdvancedConfiguration: map[string]any{
acc.ClusterAdvConfigOplogMinRetentionHours: 8,
},
ProjectID: projectIDTerraform,
MongoDBMajorVersion: "6.0",
CloudBackup: true,
DiskSizeGb: 12,
RetainBackupsEnabled: true,
ReplicationSpecs: []acc.ReplicationSpecRequest{
{EbsVolumeType: "STANDARD", AutoScalingDiskGbEnabled: true, NodeCount: 3},
},
}
clusterInfo = acc.GetClusterInfo(t, &req)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: configClusterWithBackupSchedule(projectName, orgID, projectOwnerID),
Config: configClusterWithBackupSchedule(projectName, orgID, projectOwnerID, &clusterInfo),
},
{
Config: configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectOwnerID),
Config: configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectOwnerID, &clusterInfo),
ExpectError: regexp.MustCompile(`BACKUP_POLICIES_NOT_MEETING_BACKUP_COMPLIANCE_POLICY_REQUIREMENTS`),
},
},
Expand Down Expand Up @@ -324,39 +339,11 @@ func configWithoutRestoreDays(projectName, orgID, projectOwnerID string) string
`
}

func configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectOwnerID string) string {
return acc.ConfigProjectWithSettings(projectName, orgID, projectOwnerID, false) + `
resource "mongodbatlas_cluster" "test" {
project_id = mongodbatlas_project.test.id
name = "test1"
provider_name = "AWS"
cluster_type = "REPLICASET"
mongo_db_major_version = "6.0"
provider_instance_size_name = "M10"
auto_scaling_compute_enabled = false
cloud_backup = true
auto_scaling_disk_gb_enabled = true
disk_size_gb = 12
provider_volume_type = "STANDARD"
retain_backups_enabled = true
advanced_configuration {
oplog_min_retention_hours = 8
}
replication_specs {
num_shards = 1
regions_config {
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
}
}
func configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectOwnerID string, info *acc.ClusterInfo) string {
return acc.ConfigProjectWithSettings(projectName, orgID, projectOwnerID, false) + fmt.Sprintf(`
%[1]s
resource "mongodbatlas_cloud_backup_schedule" "test" {
cluster_name = mongodbatlas_cluster.test.name
cluster_name = %[2]s.name
project_id = mongodbatlas_project.test.id
reference_hour_of_day = 3
Expand All @@ -367,7 +354,7 @@ func configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectO
cloud_provider = "AWS"
frequencies = ["DAILY"]
region_name = "US_WEST_1"
replication_spec_id = one(mongodbatlas_cluster.test.replication_specs).id
replication_spec_id = one(%[2]s.replication_specs).id
should_copy_oplogs = false
}
}
Expand All @@ -393,42 +380,14 @@ func configOverwriteIncompatibleBackupPoliciesError(projectName, orgID, projectO
retention_value = 1
}
}
`
`, info.ClusterTerraformStr, info.ClusterResourceName)
}

func configClusterWithBackupSchedule(projectName, orgID, projectOwnerID string) string {
return acc.ConfigProjectWithSettings(projectName, orgID, projectOwnerID, false) + `
resource "mongodbatlas_cluster" "test" {
project_id = mongodbatlas_project.test.id
name = "test1"
provider_name = "AWS"
cluster_type = "REPLICASET"
mongo_db_major_version = "6.0"
provider_instance_size_name = "M10"
auto_scaling_compute_enabled = false
cloud_backup = true
auto_scaling_disk_gb_enabled = true
disk_size_gb = 12
provider_volume_type = "STANDARD"
retain_backups_enabled = true
advanced_configuration {
oplog_min_retention_hours = 8
}
replication_specs {
num_shards = 1
regions_config {
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
}
}
func configClusterWithBackupSchedule(projectName, orgID, projectOwnerID string, info *acc.ClusterInfo) string {
return acc.ConfigProjectWithSettings(projectName, orgID, projectOwnerID, false) + fmt.Sprintf(`
%[1]s
resource "mongodbatlas_cloud_backup_schedule" "test" {
cluster_name = mongodbatlas_cluster.test.name
cluster_name = %[2]s.name
project_id = mongodbatlas_project.test.id
reference_hour_of_day = 3
Expand All @@ -439,11 +398,11 @@ func configClusterWithBackupSchedule(projectName, orgID, projectOwnerID string)
cloud_provider = "AWS"
frequencies = ["DAILY"]
region_name = "US_WEST_1"
replication_spec_id = one(mongodbatlas_cluster.test.replication_specs).id
replication_spec_id = one(%[2]s.replication_specs).id
should_copy_oplogs = false
}
}
`
`, info.ClusterTerraformStr, info.ClusterResourceName)
}

func basicChecks() []resource.TestCheckFunc {
Expand Down
12 changes: 6 additions & 6 deletions internal/testutil/acc/config_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ var (
)

func ClusterResourceHcl(req *ClusterRequest) (configStr, clusterName, resourceName string, err error) {
projectID := req.ProjectID
if req == nil || projectID == "" {
if req == nil || req.ProjectID == "" {
return "", "", "", errors.New("must specify a ClusterRequest with at least ProjectID set")
}
projectID := req.ProjectID
req.AddDefaults()
specRequests := req.ReplicationSpecs
specs := make([]admin.ReplicationSpec, len(specRequests))
Expand All @@ -106,10 +106,10 @@ func ClusterResourceHcl(req *ClusterRequest) (configStr, clusterName, resourceNa
resourceType := "mongodbatlas_advanced_cluster"
cluster := root.AppendNewBlock("resource", []string{resourceType, resourceSuffix}).Body()
clusterRootAttributes := map[string]any{
"cluster_type": clusterTypeStr,
"name": clusterName,
"backup_enabled": req.CloudBackup,
"pit_enabled": req.PitEnabled,
"cluster_type": clusterTypeStr,
"name": clusterName,
"backup_enabled": req.CloudBackup,
"pit_enabled": req.PitEnabled,
"mongo_db_major_version": req.MongoDBMajorVersion,
}
if strings.Contains(req.ProjectID, ".") {
Expand Down
7 changes: 4 additions & 3 deletions internal/testutil/acc/config_formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func Test_ClusterResourceHcl(t *testing.T) {
ReplicationSpecs: []acc.ReplicationSpecRequest{
{Region: "MY_REGION_1", ZoneName: "Zone X", InstanceSize: "M30", NodeCount: 30, ProviderName: constant.AZURE, EbsVolumeType: "STANDARD"},
},
PitEnabled: true,
AdvancedConfiguration: map[string]any{
acc.ClusterAdvConfigOplogMinRetentionHours: 8,
},
Expand Down Expand Up @@ -421,9 +422,9 @@ func Test_ClusterResourceHcl(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
req := tc.req
if req.ProjectID == "" {
req.ProjectID = "project"
}
if req.ProjectID == "" {
req.ProjectID = "project"
}
config, actualClusterName, actualResourceName, err := acc.ClusterResourceHcl(&req)
require.NoError(t, err)
assert.Equal(t, "mongodbatlas_advanced_cluster.cluster_info", actualResourceName)
Expand Down

0 comments on commit b60cc3d

Please sign in to comment.