Skip to content

Commit

Permalink
mig tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Mar 26, 2024
1 parent 6c33853 commit 448f5be
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 40 deletions.
20 changes: 4 additions & 16 deletions internal/service/auditing/resource_auditing_migration_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
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{
PreCheck: func() { mig.PreCheckBasic(t) },
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled),
Config: config,
ExternalProviders: mig.ExternalProviders(),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
Expand All @@ -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),
},
})
}
35 changes: 16 additions & 19 deletions internal/service/auditing/resource_auditing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auditing_test
import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -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{
Expand All @@ -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"),
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions internal/testutil/acc/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != ""
}
2 changes: 1 addition & 1 deletion internal/testutil/acc/search_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func CheckDestroySearchIndex(state *terraform.State) error {
if ExistingClusterUsed() {
if UsingLocalResources() {
return nil
}
for _, rs := range state.RootModule().Resources {
Expand Down

0 comments on commit 448f5be

Please sign in to comment.