@@ -4,82 +4,61 @@ import (
4
4
"fmt"
5
5
"testing"
6
6
7
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8
7
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9
8
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10
9
"github.com/xanzy/go-gitlab"
11
10
)
12
11
13
12
func TestAccGitlabGroupShareGroup_basic (t * testing.T ) {
14
- randName := acctest .RandomWithPrefix ("acctest" )
13
+ testAccCheck (t )
14
+
15
+ mainGroup := testAccCreateGroups (t , 1 )[0 ]
16
+ sharedGroup := testAccCreateGroups (t , 1 )[0 ]
15
17
16
- // lintignore: AT001 // TODO: Resolve this tfproviderlint issue
17
18
resource .Test (t , resource.TestCase {
18
19
PreCheck : func () { testAccPreCheck (t ) },
19
20
ProviderFactories : providerFactories ,
21
+ CheckDestroy : testAccCheckGitlabShareGroupDestroy ,
20
22
Steps : []resource.TestStep {
21
23
// Share a new group with another group
22
24
{
23
- Config : testAccGitlabGroupShareGroupConfig (
24
- randName ,
25
+ Config : testAccGitlabGroupShareGroupConfig (mainGroup .ID , sharedGroup .ID ,
25
26
`
26
27
group_access = "guest"
27
28
expires_at = "2099-01-01"
28
29
` ,
29
30
),
30
- Check : testAccCheckGitlabGroupSharedWithGroup (randName , "2099-01-01" , gitlab .GuestPermissions ),
31
+ Check : testAccCheckGitlabGroupSharedWithGroup (mainGroup . Name , sharedGroup . Name , "2099-01-01" , gitlab .GuestPermissions ),
31
32
},
32
- // Update the share group
33
- {
34
- Config : testAccGitlabGroupShareGroupConfig (randName , `group_access = "reporter"` ),
35
- Check : testAccCheckGitlabGroupSharedWithGroup (randName , "" , gitlab .ReporterPermissions ),
36
- },
37
- // Delete the gitlab_group_share_group resource
38
33
{
39
- Config : testAccGitlabGroupShareGroupConfigDelete (randName ),
40
- Check : testAccCheckGitlabGroupIsNotShared (randName ),
34
+ // Verify Import
35
+ ResourceName : "gitlab_group_share_group.test" ,
36
+ ImportState : true ,
37
+ ImportStateVerify : true ,
41
38
},
42
- },
43
- })
44
- }
45
-
46
- // lintignore: AT002 // TODO: Resolve this tfproviderlint issue
47
- func TestAccGitlabGroupShareGroup_import (t * testing.T ) {
48
- randName := acctest .RandomWithPrefix ("acctest" )
49
-
50
- resource .Test (t , resource.TestCase {
51
- ProviderFactories : providerFactories ,
52
- PreCheck : func () { testAccPreCheck (t ) },
53
- CheckDestroy : testAccCheckGitlabGroupDestroy ,
54
- Steps : []resource.TestStep {
39
+ // Update the share group
55
40
{
56
- // create shared groups
57
- Config : testAccGitlabGroupShareGroupConfig (
58
- randName ,
59
- `
60
- group_access = "guest"
61
- expires_at = "2099-03-03"
62
- ` ,
63
- ),
64
- Check : testAccCheckGitlabGroupSharedWithGroup (randName , "2099-03-03" , gitlab .GuestPermissions ),
41
+ Config : testAccGitlabGroupShareGroupConfig (mainGroup .ID , sharedGroup .ID , `group_access = "reporter"` ),
42
+ Check : testAccCheckGitlabGroupSharedWithGroup (mainGroup .Name , sharedGroup .Name , "" , gitlab .ReporterPermissions ),
65
43
},
66
44
{
67
45
// Verify Import
68
46
ResourceName : "gitlab_group_share_group.test" ,
69
47
ImportState : true ,
70
48
ImportStateVerify : true ,
71
49
},
50
+ // Delete the gitlab_group_share_group resource
51
+ {
52
+ Config : testAccGitlabGroupShareGroupConfigDelete (),
53
+ Check : testAccCheckGitlabGroupIsNotShared (mainGroup .Name ),
54
+ },
72
55
},
73
56
})
74
57
}
75
58
76
- func testAccCheckGitlabGroupSharedWithGroup (
77
- groupName string ,
78
- expireTime string ,
79
- accessLevel gitlab.AccessLevelValue ,
80
- ) resource.TestCheckFunc {
59
+ func testAccCheckGitlabGroupSharedWithGroup (mainGroupName string , sharedGroupName string , expireTime string , accessLevel gitlab.AccessLevelValue ) resource.TestCheckFunc {
81
60
return func (_ * terraform.State ) error {
82
- mainGroup , _ , err := testGitlabClient .Groups .GetGroup (fmt . Sprintf ( "%s_main" , groupName ) , nil )
61
+ mainGroup , _ , err := testGitlabClient .Groups .GetGroup (mainGroupName , nil )
83
62
if err != nil {
84
63
return err
85
64
}
@@ -91,8 +70,8 @@ func testAccCheckGitlabGroupSharedWithGroup(
91
70
92
71
sharedGroup := mainGroup .SharedWithGroups [0 ]
93
72
94
- if sharedGroup .GroupName != fmt . Sprintf ( "%s_share" , groupName ) {
95
- return fmt .Errorf ("group name was %s (wanted %s)" , sharedGroup .GroupName , fmt . Sprintf ( "%s_share" , groupName ) )
73
+ if sharedGroup .GroupName != sharedGroupName {
74
+ return fmt .Errorf ("group name was %s (wanted %s)" , sharedGroup .GroupName , sharedGroupName )
96
75
}
97
76
98
77
if gitlab .AccessLevelValue (sharedGroup .GroupAccessLevel ) != accessLevel {
@@ -109,9 +88,9 @@ func testAccCheckGitlabGroupSharedWithGroup(
109
88
}
110
89
}
111
90
112
- func testAccCheckGitlabGroupIsNotShared (groupName string ) resource.TestCheckFunc {
91
+ func testAccCheckGitlabGroupIsNotShared (mainGroupName string ) resource.TestCheckFunc {
113
92
return func (_ * terraform.State ) error {
114
- mainGroup , _ , err := testGitlabClient .Groups .GetGroup (fmt . Sprintf ( "%s_main" , groupName ) , nil )
93
+ mainGroup , _ , err := testGitlabClient .Groups .GetGroup (mainGroupName , nil )
115
94
if err != nil {
116
95
return err
117
96
}
@@ -125,46 +104,51 @@ func testAccCheckGitlabGroupIsNotShared(groupName string) resource.TestCheckFunc
125
104
}
126
105
}
127
106
128
- func testAccGitlabGroupShareGroupConfig (
129
- randName string ,
130
- shareGroupSettings string ,
131
- ) string {
132
- return fmt .Sprintf (
133
- `
134
- resource "gitlab_group" "test_main" {
135
- name = "%[1]s_main"
136
- path = "%[1]s_main"
107
+ func testAccCheckGitlabShareGroupDestroy (s * terraform.State ) error {
108
+ var groupId string
109
+ var sharedGroupId int
110
+ var err error
111
+
112
+ for _ , rs := range s .RootModule ().Resources {
113
+ if rs .Type == "gitlab_group_share_group" {
114
+ groupId , sharedGroupId , err = groupIdsFromId (rs .Primary .ID )
115
+ if err != nil {
116
+ return fmt .Errorf ("[ERROR] cannot get Group ID and ShareGroupId from input: %v" , rs .Primary .ID )
117
+ }
118
+
119
+ // Get Main Group
120
+ group , _ , err := testGitlabClient .Groups .GetGroup (groupId , nil )
121
+ if err != nil {
122
+ return err
123
+ }
124
+
125
+ // Make sure that SharedWithGroups attribute on the main group does not contain the shared group id at all
126
+ for _ , sharedGroup := range group .SharedWithGroups {
127
+ if sharedGroupId == sharedGroup .GroupID {
128
+ return fmt .Errorf ("GitLab Group Share %d still exists" , sharedGroupId )
129
+ }
130
+ }
137
131
}
132
+ }
138
133
139
- resource "gitlab_group" "test_share" {
140
- name = "%[1]s_share"
141
- path = "%[1]s_share"
142
- }
134
+ return nil
135
+ }
143
136
137
+ func testAccGitlabGroupShareGroupConfig (mainGroupId int , shareGroupId int , shareGroupSettings string ) string {
138
+ return fmt .Sprintf (
139
+ `
144
140
resource "gitlab_group_share_group" "test" {
145
- group_id = gitlab_group.test_main.id
146
- share_group_id = gitlab_group.test_share.id
147
- %[2 ]s
141
+ group_id = %[1]d
142
+ share_group_id = %[2]d
143
+ %[3 ]s
148
144
}
149
145
` ,
150
- randName ,
146
+ mainGroupId ,
147
+ shareGroupId ,
151
148
shareGroupSettings ,
152
149
)
153
150
}
154
151
155
- func testAccGitlabGroupShareGroupConfigDelete (randName string ) string {
156
- return fmt .Sprintf (
157
- `
158
- resource "gitlab_group" "test_main" {
159
- name = "%[1]s_main"
160
- path = "%[1]s_main"
161
- }
162
-
163
- resource "gitlab_group" "test_share" {
164
- name = "%[1]s_share"
165
- path = "%[1]s_share"
166
- }
167
- ` ,
168
- randName ,
169
- )
152
+ func testAccGitlabGroupShareGroupConfigDelete () string {
153
+ return ``
170
154
}
0 commit comments