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 cloud_backup_snapshot_restore_job #2413

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
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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),
Copy link
Member

Choose a reason for hiding this comment

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

same as other PRs, pass clusterInfo?

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"),
},
},
Expand All @@ -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"
)
Expand All @@ -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"),
Expand Down Expand Up @@ -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 = %[2]s.project_id
cluster_name = %[2]s.name
description = %[3]q
retention_in_days = %[4]q
depends_on = [mongodbatlas_cluster.my_cluster]
depends_on = [%[2]s]
}

resource "mongodbatlas_cloud_backup_snapshot_restore_job" "test" {
Expand Down Expand Up @@ -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
}
Expand All @@ -227,5 +217,5 @@ func configDownload(projectID, clusterName, description, retentionInDays string,
download = true
}
}
`, projectID, clusterName, description, retentionInDays, snapshotIDField)
`, terraformStr, clusterResourceName, description, retentionInDays, snapshotIDField)
}