From 894f4303daa024e5fe9cc8cd40a630bced80b955 Mon Sep 17 00:00:00 2001
From: EspenAlbert <espen.albert@mongodb.com>
Date: Fri, 12 Jul 2024 08:59:20 +0100
Subject: [PATCH 1/2] test: Refactors resource tests to use GetClusterInfo
 `cloud_backup_snapshot_restore_job`

---
 ..._cloud_backup_snapshot_restore_job_test.go | 70 ++++++++-----------
 1 file changed, 30 insertions(+), 40 deletions(-)

diff --git a/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go b/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go
index 188b026b33..b8c105a0c0 100644
--- a/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go
+++ b/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go
@@ -18,17 +18,26 @@ const (
 	dataSourceName = "data.mongodbatlas_cloud_backup_snapshot_restore_job.test"
 )
 
+var clusterRequest = acc.ClusterRequest{
+	CloudBackup: true,
+	ReplicationSpecs: []acc.ReplicationSpecRequest{
+		{Region: "US_WEST_2"},
+	},
+}
+
 func TestAccCloudBackupSnapshotRestoreJob_basic(t *testing.T) {
 	resource.ParallelTest(t, *basicTestCase(t))
 }
 
 func TestAccCloudBackupSnapshotRestoreJob_basicDownload(t *testing.T) {
 	var (
-		projectID       = acc.ProjectIDExecution(t)
-		clusterName     = acc.RandomClusterName()
-		description     = fmt.Sprintf("My description in %s", clusterName)
-		retentionInDays = "1"
-		useSnapshotID   = true
+		clusterInfo         = acc.GetClusterInfo(t, &clusterRequest)
+		clusterName         = clusterInfo.ClusterName
+		description         = fmt.Sprintf("My description in %s", clusterName)
+		retentionInDays     = "1"
+		useSnapshotID       = true
+		clusterTerraformStr = clusterInfo.ClusterTerraformStr
+		clusterResourceName = clusterInfo.ClusterResourceName
 	)
 
 	resource.ParallelTest(t, resource.TestCase{
@@ -37,14 +46,14 @@ func TestAccCloudBackupSnapshotRestoreJob_basicDownload(t *testing.T) {
 		CheckDestroy:             checkDestroy,
 		Steps: []resource.TestStep{
 			{
-				Config: configDownload(projectID, clusterName, description, retentionInDays, useSnapshotID),
+				Config: configDownload(clusterTerraformStr, clusterResourceName, description, retentionInDays, useSnapshotID),
 				Check: resource.ComposeAggregateTestCheckFunc(
 					checkExists(resourceName),
 					resource.TestCheckResourceAttr(resourceName, "delivery_type_config.0.download", "true"),
 				),
 			},
 			{
-				Config:      configDownload(projectID, clusterName, description, retentionInDays, !useSnapshotID),
+				Config:      configDownload(clusterTerraformStr, clusterResourceName, description, retentionInDays, !useSnapshotID),
 				ExpectError: regexp.MustCompile("SNAPSHOT_NOT_FOUND"),
 			},
 		},
@@ -57,8 +66,8 @@ func basicTestCase(tb testing.TB) *resource.TestCase {
 	var (
 		snapshotsDataSourceName           = "data.mongodbatlas_cloud_backup_snapshot_restore_jobs.test"
 		snapshotsDataSourcePaginationName = "data.mongodbatlas_cloud_backup_snapshot_restore_jobs.pagination"
-		projectID                         = acc.ProjectIDExecution(tb)
-		clusterName                       = acc.RandomClusterName()
+		clusterInfo                       = acc.GetClusterInfo(tb, &clusterRequest)
+		clusterName                       = clusterInfo.ClusterName
 		description                       = fmt.Sprintf("My description in %s", clusterName)
 		retentionInDays                   = "1"
 	)
@@ -69,7 +78,7 @@ func basicTestCase(tb testing.TB) *resource.TestCase {
 		CheckDestroy:             checkDestroy,
 		Steps: []resource.TestStep{
 			{
-				Config: configBasic(projectID, clusterName, description, retentionInDays),
+				Config: configBasic(clusterInfo.ClusterTerraformStr, clusterInfo.ClusterResourceName, description, retentionInDays),
 				Check: resource.ComposeAggregateTestCheckFunc(
 					checkExists(resourceName),
 					resource.TestCheckResourceAttr(resourceName, "delivery_type_config.0.automated", "true"),
@@ -139,25 +148,15 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
 	}
 }
 
-func configBasic(projectID, clusterName, description, retentionInDays string) string {
+func configBasic(terraformStr, clusterResourceName, description, retentionInDays string) string {
 	return fmt.Sprintf(`
-		resource "mongodbatlas_cluster" "my_cluster" {
-			project_id   = %[1]q
-			name         = %[2]q
-			
-			// Provider Settings "block"
-			provider_name               = "AWS"
-			provider_region_name        = "US_WEST_2"
-			provider_instance_size_name = "M10"
-			cloud_backup                = true
-		}
-
+		%[1]s
 		resource "mongodbatlas_cloud_backup_snapshot" "test" {
-			project_id        = mongodbatlas_cluster.my_cluster.project_id
-			cluster_name      = mongodbatlas_cluster.my_cluster.name
+			project_id        = %[1]s.project_id
+			cluster_name      = %[1]s.name
 			description       = %[3]q
 			retention_in_days = %[4]q
-			depends_on = [mongodbatlas_cluster.my_cluster]
+			depends_on = [%[1]s]
 		}
 
 		resource "mongodbatlas_cloud_backup_snapshot_restore_job" "test" {
@@ -191,29 +190,20 @@ func configBasic(projectID, clusterName, description, retentionInDays string) st
 			page_num = 1
 			items_per_page = 5
 		}
-	`, projectID, clusterName, description, retentionInDays)
+	`, terraformStr, clusterResourceName, description, retentionInDays)
 }
 
-func configDownload(projectID, clusterName, description, retentionInDays string, useSnapshotID bool) string {
+func configDownload(terraformStr, clusterResourceName, description, retentionInDays string, useSnapshotID bool) string {
 	var snapshotIDField string
 	if useSnapshotID {
 		snapshotIDField = `snapshot_id  = mongodbatlas_cloud_backup_snapshot.test.id`
 	}
 
 	return fmt.Sprintf(`
-		resource "mongodbatlas_cluster" "my_cluster" {
-			project_id   = %[1]q
-			name         = %[2]q
-			
-			provider_name               = "AWS"
-			provider_region_name        = "US_WEST_2"
-			provider_instance_size_name = "M10"
-			cloud_backup                = true   // enable cloud provider snapshots
-		}
-
+		%[1]s
 		resource "mongodbatlas_cloud_backup_snapshot" "test" {
-			project_id        = mongodbatlas_cluster.my_cluster.project_id
-			cluster_name      = mongodbatlas_cluster.my_cluster.name
+			project_id        = %[2]s.project_id
+			cluster_name      = %[2]s.name
 			description       = %[3]q
 			retention_in_days = %[4]q
 		}
@@ -227,5 +217,5 @@ func configDownload(projectID, clusterName, description, retentionInDays string,
 				download = true
 			}
 		}
-	`, projectID, clusterName, description, retentionInDays, snapshotIDField)
+	`, terraformStr, clusterResourceName, description, retentionInDays, snapshotIDField)
 }

From d767f2aac9a93c3ec834e88de97139822f9eab0a Mon Sep 17 00:00:00 2001
From: EspenAlbert <espen.albert@mongodb.com>
Date: Fri, 12 Jul 2024 10:29:44 +0100
Subject: [PATCH 2/2] test: fix reference to clusterResourceName

---
 .../resource_cloud_backup_snapshot_restore_job_test.go      | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go b/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go
index b8c105a0c0..25b1dea41f 100644
--- a/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go
+++ b/internal/service/cloudbackupsnapshotrestorejob/resource_cloud_backup_snapshot_restore_job_test.go
@@ -152,11 +152,11 @@ func configBasic(terraformStr, clusterResourceName, description, retentionInDays
 	return fmt.Sprintf(`
 		%[1]s
 		resource "mongodbatlas_cloud_backup_snapshot" "test" {
-			project_id        = %[1]s.project_id
-			cluster_name      = %[1]s.name
+			project_id        = %[2]s.project_id
+			cluster_name      = %[2]s.name
 			description       = %[3]q
 			retention_in_days = %[4]q
-			depends_on = [%[1]s]
+			depends_on = [%[2]s]
 		}
 
 		resource "mongodbatlas_cloud_backup_snapshot_restore_job" "test" {