Skip to content

chore: CLOUDP-214958: Run acc tests faster in local reusing cluster database_user #1775

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

Merged
merged 5 commits into from
Dec 20, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,15 @@ func TestAccMigrationConfigRSDatabaseUser_WithScopes(t *testing.T) {
username = acctest.RandomWithPrefix("test-acc-user-")
password = acctest.RandomWithPrefix("test-acc-pass-")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
clusterName = acctest.RandomWithPrefix("test-acc-cluster")
clusterInfo = acc.GetClusterInfo(orgID)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{
{
Name: "test-acc-nurk4llu2z",
Expand All @@ -316,7 +315,7 @@ func TestAccMigrationConfigRSDatabaseUser_WithScopes(t *testing.T) {
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{
{
Name: "test-acc-nurk4llu2z",
Expand All @@ -341,16 +340,15 @@ func TestAccMigrationConfigRSDatabaseUser_WithScopesAndEmpty(t *testing.T) {
username = acctest.RandomWithPrefix("test-acc-user-")
password = acctest.RandomWithPrefix("test-acc-pass-")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
clusterName = acctest.RandomWithPrefix("test-acc-cluster")
clusterInfo = acc.GetClusterInfo(orgID)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { mig.PreCheckBasic(t) },
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{},
),
Check: resource.ComposeTestCheckFunc(
Expand All @@ -363,7 +361,7 @@ func TestAccMigrationConfigRSDatabaseUser_WithScopesAndEmpty(t *testing.T) {
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{},
),
ConfigPlanChecks: resource.ConfigPlanChecks{
Expand Down
14 changes: 6 additions & 8 deletions internal/service/databaseuser/resource_database_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ func TestAccConfigRSDatabaseUser_withScopes(t *testing.T) {
username = acctest.RandomWithPrefix("test-acc-user-")
password = acctest.RandomWithPrefix("test-acc-pass-")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
clusterName = acctest.RandomWithPrefix("test-acc-cluster")
clusterInfo = acc.GetClusterInfo(orgID)
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -352,7 +351,7 @@ func TestAccConfigRSDatabaseUser_withScopes(t *testing.T) {
CheckDestroy: acc.CheckDestroyDatabaseUser,
Steps: []resource.TestStep{
{
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{
{
Name: "test-acc-nurk4llu2z",
Expand All @@ -375,7 +374,7 @@ func TestAccConfigRSDatabaseUser_withScopes(t *testing.T) {
),
},
{
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{
{
Name: "test-acc-nurk4llu2z",
Expand Down Expand Up @@ -404,8 +403,7 @@ func TestAccConfigRSDatabaseUser_withScopesAndEmpty(t *testing.T) {
username = acctest.RandomWithPrefix("test-acc-user-")
password = acctest.RandomWithPrefix("test-acc-pass-")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
clusterName = acctest.RandomWithPrefix("test-acc-cluster")
clusterInfo = acc.GetClusterInfo(orgID)
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -414,7 +412,7 @@ func TestAccConfigRSDatabaseUser_withScopesAndEmpty(t *testing.T) {
CheckDestroy: acc.CheckDestroyDatabaseUser,
Steps: []resource.TestStep{
{
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{
{
Name: "test-acc-nurk4llu2z",
Expand All @@ -437,7 +435,7 @@ func TestAccConfigRSDatabaseUser_withScopesAndEmpty(t *testing.T) {
),
},
{
Config: acc.ConfigDatabaseUserWithScopes(username, password, projectName, orgID, "atlasAdmin", clusterName,
Config: acc.ConfigDatabaseUserWithScopes(username, password, "atlasAdmin", clusterInfo.ProjectIDStr, clusterInfo.ClusterName, clusterInfo.ClusterTerraformStr,
[]*admin.UserScope{},
),
Check: resource.ComposeTestCheckFunc(
Expand Down
28 changes: 6 additions & 22 deletions internal/testutil/acc/database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func ConfigDatabaseUserWithAWSIAMType(projectName, orgID, roleName, username, ke
`, projectName, orgID, roleName, username, keyLabel, valueLabel)
}

func ConfigDatabaseUserWithScopes(username, password, projectName, orgID, roleName, clusterName string, scopesArr []*admin.UserScope) string {
func ConfigDatabaseUserWithScopes(username, password, roleName, projectID, clusterName, clusterTerraformStr string, scopesArr []*admin.UserScope) string {
var scopes string

for _, scope := range scopesArr {
Expand All @@ -249,33 +249,17 @@ func ConfigDatabaseUserWithScopes(username, password, projectName, orgID, roleNa

scopes += fmt.Sprintf(`
scopes {
name = "${mongodbatlas_cluster.my_cluster.name}"
name = "%s"
%s
}
`, scopeType)
`, clusterName, scopeType)
}

return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = "%s"
org_id = "%s"
}

resource "mongodbatlas_cluster" "my_cluster" {
project_id = "${mongodbatlas_project.test.id}"
name = "%s"

// Provider Settings "block"
provider_name = "AWS"
provider_region_name = "US_EAST_2"
provider_instance_size_name = "M10"
cloud_backup = true //enable cloud provider snapshots
}

return clusterTerraformStr + fmt.Sprintf(`
resource "mongodbatlas_database_user" "test" {
username = "%s"
password = "%s"
project_id = "${mongodbatlas_project.test.id}"
project_id = %s
auth_database_name = "admin"

roles {
Expand All @@ -286,7 +270,7 @@ func ConfigDatabaseUserWithScopes(username, password, projectName, orgID, roleNa
%s

}
`, projectName, orgID, clusterName, username, password, roleName, scopes)
`, username, password, projectID, roleName, scopes)
}

func ConfigDatabaseUserWithLDAPAuthType(projectName, orgID, roleName, username, keyLabel, valueLabel string) string {
Expand Down