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

chore: Reuses project in tests - auditing #2082

Merged
merged 5 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
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
38 changes: 0 additions & 38 deletions internal/service/auditing/data_source_auditing_test.go

This file was deleted.

37 changes: 6 additions & 31 deletions internal/service/auditing/resource_auditing_migration_test.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,29 @@
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()
auditAuth = true
auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}"
enabled = true
projectID = mig.ProjectIDGlobal(t)
auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}"
config = configBasic(projectID, auditFilter, true, true)
)

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),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "audit_filter"),
resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"),
resource.TestCheckResourceAttrSet(resourceName, "enabled"),
resource.TestCheckResourceAttr(resourceName, "audit_filter", auditFilter),
resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
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(),
},
},
Check: resource.ComposeTestCheckFunc(checks(auditFilter, true, true)...),
},
mig.TestStepCheckEmptyPlan(config),
},
})
}
107 changes: 32 additions & 75 deletions internal/service/auditing/resource_auditing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package auditing_test
import (
"context"
"fmt"
"os"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
)

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)
auditFilter = "{ 'atype': 'authenticate', 'param': { 'user': 'auditAdmin', 'db': 'admin', 'mechanism': 'SCRAM-SHA-1' }}"
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -27,65 +28,12 @@ func TestAccGenericAuditing_basic(t *testing.T) {
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "audit_filter"),
resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"),
resource.TestCheckResourceAttrSet(resourceName, "enabled"),
resource.TestCheckResourceAttr(resourceName, "audit_filter", auditFilter),
resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"),
),
Config: configBasic(projectID, auditFilter, true, true),
Check: resource.ComposeTestCheckFunc(checks(auditFilter, true, true)...),
},
{
Config: configBasic(orgID, projectName, "{}", false, false),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "audit_filter"),
resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"),
resource.TestCheckResourceAttrSet(resourceName, "enabled"),
resource.TestCheckResourceAttr(resourceName, "audit_filter", "{}"),
resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "false"),
resource.TestCheckResourceAttr(resourceName, "enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"),
),
},
},
})
}

func TestAccGenericAuditing_importBasic(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
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configBasic(orgID, projectName, auditFilter, auditAuth, enabled),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "audit_filter"),
resource.TestCheckResourceAttrSet(resourceName, "audit_authorization_success"),
resource.TestCheckResourceAttrSet(resourceName, "enabled"),
resource.TestCheckResourceAttr(resourceName, "audit_filter", auditFilter),
resource.TestCheckResourceAttr(resourceName, "audit_authorization_success", "true"),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "configuration_type", "FILTER_JSON"),
),
Config: configBasic(projectID, "{}", false, false),
Check: resource.ComposeTestCheckFunc(checks("{}", false, false)...),
},
{
ResourceName: resourceName,
Expand Down Expand Up @@ -121,7 +69,7 @@ func checkDestroy(s *terraform.State) error {
continue
}
auditingRes, _, _ := acc.ConnV2().AuditingApi.GetAuditingConfiguration(context.Background(), rs.Primary.ID).Execute()
if auditingRes != nil {
if auditingRes.GetEnabled() {
return fmt.Errorf("auditing (%s) exists", rs.Primary.ID)
}
}
Expand All @@ -139,21 +87,30 @@ 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)
}

func checks(auditFilter string, auditAuth, enabled bool) []resource.TestCheckFunc {
commonChecks := map[string]string{
"audit_filter": auditFilter,
"audit_authorization_success": strconv.FormatBool(auditAuth),
"enabled": strconv.FormatBool(auditAuth),
"configuration_type": "FILTER_JSON",
}
checks := acc.AddAttrChecks(resourceName, nil, commonChecks)
checks = acc.AddAttrChecks(dataSourceName, checks, commonChecks)
checks = append(checks, checkExists(resourceName), checkExists(dataSourceName))
return checks
}