Skip to content

Commit 278927f

Browse files
csanxCristina Sánchez Sánchez
andauthored
test: Create migration test for the mongodbatlas_cloud_user_team_assignment resource migration path. (#3553)
* Added migration test * Fixed migration test * Empty commit * Changed usernames * Changed usernames * Fix * Fix * Enhanced test * Comments --------- Co-authored-by: Cristina Sánchez Sánchez <[email protected]>
1 parent 124afd5 commit 278927f

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

.github/workflows/acceptance-tests-runner.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ jobs:
616616
MONGODB_ATLAS_TEAMS_IDS: ${{ inputs.mongodb_atlas_teams_ids }}
617617
MONGODB_ATLAS_PROJECT_OWNER_ID: ${{ inputs.mongodb_atlas_project_owner_id }}
618618
MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_org_id }}
619+
MONGODB_ATLAS_USERNAME: ${{ vars.MONGODB_ATLAS_USERNAME }}
619620
ACCTEST_PACKAGES: |
620621
./internal/service/clouduserorgassignment
621622
./internal/service/clouduserteamassignment
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,95 @@
11
package clouduserteamassignment_test
22

33
import (
4+
"fmt"
5+
"os"
46
"testing"
57

8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
610
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
711
)
812

13+
const (
14+
resourceTeamName = "mongodbatlas_team.test"
15+
resourceTeamAssignmentName = "mongodbatlas_cloud_user_team_assignment.test"
16+
)
17+
918
func TestMigCloudUserTeamAssignmentRS_basic(t *testing.T) {
1019
mig.SkipIfVersionBelow(t, "2.0.0") // when resource 1st released
1120
mig.CreateAndRunTest(t, basicTestCase(t))
1221
}
22+
23+
func TestMigCloudUserTeamAssignmentRS_migrationJourney(t *testing.T) {
24+
var (
25+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
26+
teamName = fmt.Sprintf("team-test-%s", acc.RandomName())
27+
username = os.Getenv("MONGODB_ATLAS_USERNAME")
28+
)
29+
30+
resource.ParallelTest(t, resource.TestCase{
31+
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckAtlasUsername(t) },
32+
CheckDestroy: checkDestroy,
33+
Steps: []resource.TestStep{
34+
{
35+
// NOTE: 'usernames' attribute (available v1.39.0, deprecated in v2.0.0) is used in this test in team resource,
36+
// which may be removed in future versions. This could cause the test to break - keep for version tracking.
37+
ExternalProviders: mig.ExternalProviders(),
38+
Config: configTeamWithUsernamesFirst(orgID, teamName, username),
39+
},
40+
{
41+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
42+
Config: configWithTeamAssignmentsSecond(orgID, teamName, username), // expected to see 1 import in the plan
43+
Check: resource.ComposeTestCheckFunc(
44+
resource.TestCheckResourceAttr(resourceTeamName, "name", teamName),
45+
46+
resource.TestCheckResourceAttrSet(resourceTeamAssignmentName, "user_id"),
47+
resource.TestCheckResourceAttr(resourceTeamAssignmentName, "username", username),
48+
),
49+
},
50+
mig.TestStepCheckEmptyPlan(configWithTeamAssignmentsSecond(orgID, teamName, username)),
51+
},
52+
})
53+
}
54+
55+
// Step 1: Original configuration with usernames attribute
56+
func configTeamWithUsernamesFirst(orgID, teamName, username string) string {
57+
return fmt.Sprintf(`
58+
resource "mongodbatlas_team" "test" {
59+
org_id = %[2]q
60+
name = %[3]q
61+
usernames = [%[1]q]
62+
}
63+
`, username, orgID, teamName)
64+
}
65+
66+
// Step 2: Configuration with team assignments using import blocks
67+
68+
// NOTE: Using static resource assignment instead of for_each with multiple usernames
69+
// due to a known limitation in Terraform's acceptance testing framework with indexed resources.
70+
// The actual migration using for_each works correctly (verified locally).
71+
func configWithTeamAssignmentsSecond(orgID, teamName, username string) string {
72+
return fmt.Sprintf(`
73+
resource "mongodbatlas_team" "test" {
74+
org_id = %[1]q
75+
name = %[2]q
76+
}
77+
78+
data "mongodbatlas_team" "test" {
79+
org_id = %[1]q
80+
team_id = mongodbatlas_team.test.team_id
81+
}
82+
83+
resource "mongodbatlas_cloud_user_team_assignment" "test" {
84+
org_id = %[1]q
85+
team_id = mongodbatlas_team.test.team_id
86+
user_id = data.mongodbatlas_team.test.users[0].id
87+
}
88+
89+
import {
90+
to = mongodbatlas_cloud_user_team_assignment.test
91+
id = "%[1]s/${mongodbatlas_team.test.team_id}/${data.mongodbatlas_team.test.users[0].id}"
92+
}
93+
94+
`, orgID, teamName)
95+
}

0 commit comments

Comments
 (0)