From 448f5be7f4d8dbeddbc79af837967c70a6dec9e1 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 26 Mar 2024 11:28:03 +0100 Subject: [PATCH] mig tests --- .../resource_auditing_migration_test.go | 20 +++-------- .../auditing/resource_auditing_test.go | 35 +++++++++---------- .../resource_cloud_backup_schedule_test.go | 2 +- .../resource_cloud_backup_snapshot_test.go | 2 +- internal/testutil/acc/cluster.go | 5 +-- internal/testutil/acc/search_index.go | 2 +- 6 files changed, 26 insertions(+), 40 deletions(-) diff --git a/internal/service/auditing/resource_auditing_migration_test.go b/internal/service/auditing/resource_auditing_migration_test.go index 48a6a26343..4817aff031 100644 --- a/internal/service/auditing/resource_auditing_migration_test.go +++ b/internal/service/auditing/resource_auditing_migration_test.go @@ -1,23 +1,20 @@ package auditing_test import ( - "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/plancheck" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" ) func TestMigGenericAuditing_basic(t *testing.T) { var ( resourceName = "mongodbatlas_auditing.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() + projectID = mig.ProjectIDGlobal(t) auditAuth = true auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" enabled = true + config = configBasic(projectID, auditFilter, auditAuth, enabled) ) resource.ParallelTest(t, resource.TestCase{ @@ -25,7 +22,7 @@ func TestMigGenericAuditing_basic(t *testing.T) { CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), + Config: config, ExternalProviders: mig.ExternalProviders(), Check: resource.ComposeTestCheckFunc( checkExists(resourceName), @@ -39,16 +36,7 @@ func TestMigGenericAuditing_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"), ), }, - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), - ConfigPlanChecks: resource.ConfigPlanChecks{ - PreApply: []plancheck.PlanCheck{ - acc.DebugPlan(), - plancheck.ExpectEmptyPlan(), - }, - }, - }, + mig.TestStepCheckEmptyPlan(config), }, }) } diff --git a/internal/service/auditing/resource_auditing_test.go b/internal/service/auditing/resource_auditing_test.go index b0e4eb83c7..43e4903a3f 100644 --- a/internal/service/auditing/resource_auditing_test.go +++ b/internal/service/auditing/resource_auditing_test.go @@ -3,7 +3,6 @@ package auditing_test import ( "context" "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -12,17 +11,16 @@ import ( ) const ( + resourceName = "mongodbatlas_auditing.test" dataSourceName = "data.mongodbatlas_auditing.test" ) func TestAccGenericAuditing_basic(t *testing.T) { var ( - resourceName = "mongodbatlas_auditing.test" - orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") - projectName = acc.RandomProjectName() - auditAuth = true - auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" - enabled = true + projectID = acc.ProjectIDExecution(t) + auditAuth = true + auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}" + enabled = true ) resource.ParallelTest(t, resource.TestCase{ @@ -31,7 +29,7 @@ func TestAccGenericAuditing_basic(t *testing.T) { CheckDestroy: checkDestroy, Steps: []resource.TestStep{ { - Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled), + Config: configBasic(projectID, auditFilter, auditAuth, enabled), Check: resource.ComposeTestCheckFunc( checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -51,7 +49,7 @@ func TestAccGenericAuditing_basic(t *testing.T) { ), }, { - Config: configBasic(orgID, projectName, "{}", false, false), + Config: configBasic(projectID, "{}", false, false), Check: resource.ComposeTestCheckFunc( checkExists(resourceName), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -93,6 +91,9 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { + if acc.UsingLocalResources() { + return nil + } for _, rs := range s.RootModule().Resources { if rs.Type != "mongodbatlas_auditing" { continue @@ -116,21 +117,17 @@ func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { } } -func configBasic(orgID, projectName, auditFilter string, auditAuth, enabled bool) string { +func configBasic(projectID, auditFilter string, auditAuth, enabled bool) string { return fmt.Sprintf(` - resource "mongodbatlas_project" "test" { - name = %[2]q - org_id = %[1]q - } resource "mongodbatlas_auditing" "test" { - project_id = mongodbatlas_project.test.id - audit_filter = %[3]q - audit_authorization_success = %[4]t - enabled = %[5]t + project_id = %[1]q + audit_filter = %[2]q + audit_authorization_success = %[3]t + enabled = %[4]t } data "mongodbatlas_auditing" "test" { project_id = mongodbatlas_auditing.test.id } - `, orgID, projectName, auditFilter, auditAuth, enabled) + `, projectID, auditFilter, auditAuth, enabled) } diff --git a/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go b/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go index 085489a984..283cfb9b89 100644 --- a/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go +++ b/internal/service/cloudbackupschedule/resource_cloud_backup_schedule_test.go @@ -401,7 +401,7 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.ExistingClusterUsed() { + if acc.UsingLocalResources() { return nil } for _, rs := range s.RootModule().Resources { diff --git a/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go b/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go index a4282abda3..59efc234f2 100644 --- a/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go +++ b/internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot_test.go @@ -121,7 +121,7 @@ func checkExists(resourceName string) resource.TestCheckFunc { } func checkDestroy(s *terraform.State) error { - if acc.ExistingClusterUsed() { + if acc.UsingLocalResources() { return nil } for _, rs := range s.RootModule().Resources { diff --git a/internal/testutil/acc/cluster.go b/internal/testutil/acc/cluster.go index 7541605863..b0fa3079a5 100644 --- a/internal/testutil/acc/cluster.go +++ b/internal/testutil/acc/cluster.go @@ -80,8 +80,9 @@ func GetClusterInfo(tb testing.TB, req *ClusterRequest) ClusterInfo { } } -func ExistingClusterUsed() bool { +// UsingLocalResources is normally used in CheckDestroy when an existing project or cluster is used in local. +func UsingLocalResources() bool { clusterName := os.Getenv("MONGODB_ATLAS_CLUSTER_NAME") projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID") - return clusterName != "" && projectID != "" + return clusterName != "" || projectID != "" } diff --git a/internal/testutil/acc/search_index.go b/internal/testutil/acc/search_index.go index 04023b5928..108cbd144f 100644 --- a/internal/testutil/acc/search_index.go +++ b/internal/testutil/acc/search_index.go @@ -9,7 +9,7 @@ import ( ) func CheckDestroySearchIndex(state *terraform.State) error { - if ExistingClusterUsed() { + if UsingLocalResources() { return nil } for _, rs := range state.RootModule().Resources {