@@ -2,101 +2,77 @@ package gitlab
2
2
3
3
import (
4
4
"fmt"
5
- "reflect"
6
- "sort"
5
+ . "github.com/onsi/gomega"
7
6
"strconv"
8
- "strings"
9
7
"testing"
10
8
11
- "github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
12
9
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
13
10
"github.com/hashicorp/terraform-plugin-sdk/terraform"
14
11
gitlab "github.com/xanzy/go-gitlab"
15
12
)
16
13
17
14
func TestAccGitLabProjectApprovalRule_basic (t * testing.T ) {
15
+ // Set up project, groups, users, and branches to use in the test.
16
+
17
+ testAccCheck (t )
18
+
19
+ client := testAccNewClient (t )
20
+
21
+ testAccCheckEE (t , client )
22
+
23
+ // Need to get the current user (usually the admin) because they are automatically added as group members, and we
24
+ // will need the user ID for our assertions later.
25
+ currentUser := testAccCurrentUser (t , client )
26
+
27
+ project := testAccCreateProject (t , client )
28
+ projectUsers := testAccCreateUsers (t , client , 2 )
29
+ branches := testAccCreateProtectedBranches (t , client , project , 2 )
30
+ groups := testAccCreateGroups (t , client , 2 )
31
+ group0Users := testAccCreateUsers (t , client , 1 )
32
+ group1Users := testAccCreateUsers (t , client , 1 )
33
+
34
+ testAccAddProjectMembers (t , client , project .ID , projectUsers ) // Users must belong to the project for rules to work.
35
+ testAccAddGroupMembers (t , client , groups [0 ].ID , group0Users )
36
+ testAccAddGroupMembers (t , client , groups [1 ].ID , group1Users )
37
+
38
+ // Terraform test starts here.
39
+
18
40
var projectApprovalRule gitlab.ProjectApprovalRule
19
- randomInt := acctest .RandInt ()
20
41
21
42
resource .Test (t , resource.TestCase {
22
43
Providers : testAccProviders ,
23
- PreCheck : func () { testAccPreCheck (t ) },
24
- CheckDestroy : testAccCheckGitlabProjectApprovalRuleDestroy ,
44
+ CheckDestroy : testAccCheckGitlabProjectApprovalRuleDestroy (client , project .ID ),
25
45
Steps : []resource.TestStep {
26
- { // Create Rule
27
- SkipFunc : isRunningInCE ,
28
- Config : testAccGitLabProjectApprovalRuleConfig (randomInt , 3 , "" , "gitlab_group.foo.id" ),
29
- Check : resource .ComposeTestCheckFunc (
30
- testAccCheckGitlabProjectApprovalRuleExists ("gitlab_project_approval_rule.foo" , & projectApprovalRule ),
31
- testAccCheckGitlabProjectApprovalRuleAttributes (& projectApprovalRule , & testAccGitlabProjectApprovalRuleExpectedAttributes {
32
- ApproverUsernames : []string {fmt .Sprintf ("foo-user-%d" , randomInt )},
33
- ApprovalsRequired : 3 ,
34
- GroupPaths : []string {fmt .Sprintf ("foo-group-%d" , randomInt )},
35
- ProtectedBranchNames : []string {"master" },
36
- Name : fmt .Sprintf ("foo rule %d" , randomInt ),
37
- RandomInt : randomInt ,
38
- }),
39
- ),
40
- },
41
- { // Add group and user
42
- SkipFunc : isRunningInCE ,
43
- Config : testAccGitLabProjectApprovalRuleConfig (randomInt , 2 , "gitlab_user.baz.id" , "gitlab_group.bar.id, gitlab_group.foo.id" ),
46
+ // Create rule
47
+ {
48
+ Config : testAccGitlabProjectApprovalRuleConfig (project .ID , 3 , projectUsers [0 ].ID , groups [0 ].ID , branches [0 ].ID ),
44
49
Check : resource .ComposeTestCheckFunc (
45
50
testAccCheckGitlabProjectApprovalRuleExists ("gitlab_project_approval_rule.foo" , & projectApprovalRule ),
46
51
testAccCheckGitlabProjectApprovalRuleAttributes (& projectApprovalRule , & testAccGitlabProjectApprovalRuleExpectedAttributes {
47
- ApproverUsernames : []string {
48
- fmt .Sprintf ("bar-user-%d" , randomInt ),
49
- fmt .Sprintf ("baz-user-%d" , randomInt ),
50
- fmt .Sprintf ("foo-user-%d" , randomInt ),
51
- },
52
- ApprovalsRequired : 2 ,
53
- GroupPaths : []string {
54
- fmt .Sprintf ("bar-group-%d" , randomInt ),
55
- fmt .Sprintf ("foo-group-%d" , randomInt ),
56
- },
57
- ProtectedBranchNames : []string {"master" },
58
- Name : fmt .Sprintf ("foo rule %d" , randomInt ),
59
- RandomInt : randomInt ,
52
+ Name : "foo" ,
53
+ ApprovalsRequired : 3 ,
54
+ EligibleApproverIDs : []int {currentUser .ID , projectUsers [0 ].ID , group0Users [0 ].ID },
55
+ GroupIDs : []int {groups [0 ].ID },
56
+ ProtectedBranchIDs : []int {branches [0 ].ID },
60
57
}),
61
58
),
62
59
},
63
- { // Remove group and user
64
- SkipFunc : isRunningInCE ,
65
- Config : testAccGitLabProjectApprovalRuleConfig ( randomInt , 1 , "gitlab_user.qux.id" , "gitlab_group.bar.id" ),
60
+ // Update rule
61
+ {
62
+ Config : testAccGitlabProjectApprovalRuleConfig ( project . ID , 2 , projectUsers [ 1 ]. ID , groups [ 1 ]. ID , branches [ 1 ]. ID ),
66
63
Check : resource .ComposeTestCheckFunc (
67
64
testAccCheckGitlabProjectApprovalRuleExists ("gitlab_project_approval_rule.foo" , & projectApprovalRule ),
68
65
testAccCheckGitlabProjectApprovalRuleAttributes (& projectApprovalRule , & testAccGitlabProjectApprovalRuleExpectedAttributes {
69
- ApproverUsernames : []string {
70
- fmt .Sprintf ("bar-user-%d" , randomInt ),
71
- fmt .Sprintf ("qux-user-%d" , randomInt ),
72
- },
73
- ApprovalsRequired : 1 ,
74
- GroupPaths : []string {fmt .Sprintf ("bar-group-%d" , randomInt )},
75
- ProtectedBranchNames : []string {"master" },
76
- Name : fmt .Sprintf ("foo rule %d" , randomInt ),
77
- RandomInt : randomInt ,
66
+ Name : "foo" ,
67
+ ApprovalsRequired : 2 ,
68
+ EligibleApproverIDs : []int {currentUser .ID , projectUsers [1 ].ID , group1Users [0 ].ID },
69
+ GroupIDs : []int {groups [1 ].ID },
70
+ ProtectedBranchIDs : []int {branches [1 ].ID },
78
71
}),
79
72
),
80
73
},
81
- },
82
- })
83
- }
84
-
85
- // lintignore: AT002 // TODO: Resolve this tfproviderlint issue
86
- func TestAccGitLabProjectApprovalRule_import (t * testing.T ) {
87
- randomInt := acctest .RandInt ()
88
-
89
- resource .Test (t , resource.TestCase {
90
- Providers : testAccProviders ,
91
- PreCheck : func () { testAccPreCheck (t ) },
92
- CheckDestroy : testAccCheckGitlabProjectApprovalRuleDestroy ,
93
- Steps : []resource.TestStep {
94
- { // Create Rule
95
- SkipFunc : isRunningInCE ,
96
- Config : testAccGitLabProjectApprovalRuleConfig (randomInt , 1 , "" , "gitlab_group.foo.id" ),
97
- },
98
- { // Verify Import
99
- SkipFunc : isRunningInCE ,
74
+ // Verify import
75
+ {
100
76
ResourceName : "gitlab_project_approval_rule.foo" ,
101
77
ImportState : true ,
102
78
ImportStateVerify : true ,
@@ -106,181 +82,50 @@ func TestAccGitLabProjectApprovalRule_import(t *testing.T) {
106
82
}
107
83
108
84
type testAccGitlabProjectApprovalRuleExpectedAttributes struct {
109
- ApprovalsRequired int
110
- ApproverUsernames []string
111
- GroupPaths []string
112
- ProtectedBranchNames []string
113
- Name string
114
- RandomInt int
85
+ Name string
86
+ ApprovalsRequired int
87
+ EligibleApproverIDs []int
88
+ GroupIDs []int
89
+ ProtectedBranchIDs []int
115
90
}
116
91
117
- func testAccCheckGitlabProjectApprovalRuleAttributes (projectApprovalRule * gitlab.ProjectApprovalRule , want * testAccGitlabProjectApprovalRuleExpectedAttributes ) resource.TestCheckFunc {
92
+ func testAccCheckGitlabProjectApprovalRuleAttributes (got * gitlab.ProjectApprovalRule , want * testAccGitlabProjectApprovalRuleExpectedAttributes ) resource.TestCheckFunc {
118
93
return func (s * terraform.State ) error {
119
- if projectApprovalRule .Name != want .Name {
120
- return fmt .Errorf ("got name %s; want %s" , projectApprovalRule .Name , want .Name )
121
- }
122
-
123
- if projectApprovalRule .ApprovalsRequired != want .ApprovalsRequired {
124
- return fmt .Errorf ("got approvals_required %d; want %d" , projectApprovalRule .ApprovalsRequired , want .ApprovalsRequired )
125
- }
94
+ return InterceptGomegaFailure (func () {
95
+ Expect (got .Name ).To (Equal (want .Name ), "name" )
96
+ Expect (got .ApprovalsRequired ).To (Equal (want .ApprovalsRequired ), "approvals_required" )
126
97
127
- // Compare unique usernames
128
- var userNames []string
129
- for _ , approver := range projectApprovalRule .EligibleApprovers {
130
- // Approvers will include the group creator, which will come from the GITLAB_TOKEN user.
131
- // Filter for users with RandomInt in the username
132
- if strings .HasSuffix (approver .Username , strconv .Itoa (want .RandomInt )) {
133
- userNames = append (userNames , approver .Username )
98
+ var approverIDs []int
99
+ for _ , approver := range got .EligibleApprovers {
100
+ approverIDs = append (approverIDs , approver .ID )
134
101
}
135
- }
136
- sort .Strings (userNames )
137
-
138
- if ! reflect .DeepEqual (userNames , want .ApproverUsernames ) {
139
- return fmt .Errorf ("got approvers %s; want %s" , userNames , want .ApproverUsernames )
140
- }
141
-
142
- // Compare unique group paths
143
- var groupPaths []string
144
- for _ , group := range projectApprovalRule .Groups {
145
- groupPaths = append (groupPaths , group .Path )
146
- }
147
- sort .Strings (groupPaths )
148
-
149
- if ! reflect .DeepEqual (groupPaths , want .GroupPaths ) {
150
- return fmt .Errorf ("got groups %s; want %s" , groupPaths , want .GroupPaths )
151
- }
152
-
153
- var protectedBranchNames []string
154
- for _ , protectedBranch := range projectApprovalRule .ProtectedBranches {
155
- protectedBranchNames = append (protectedBranchNames , protectedBranch .Name )
156
- }
157
- sort .Strings (protectedBranchNames )
102
+ Expect (approverIDs ).To (ConsistOf (want .EligibleApproverIDs ), "eligible_approvers" )
158
103
159
- if ! reflect .DeepEqual (protectedBranchNames , want .ProtectedBranchNames ) {
160
- return fmt .Errorf ("got protected branches %v; want %v" , protectedBranchNames , want .ProtectedBranchNames )
161
- }
104
+ var groupIDs []int
105
+ for _ , group := range got .Groups {
106
+ groupIDs = append (groupIDs , group .ID )
107
+ }
108
+ Expect (groupIDs ).To (ConsistOf (want .GroupIDs ), "groups" )
162
109
163
- return nil
110
+ var protectedBranchIDs []int
111
+ for _ , branch := range got .ProtectedBranches {
112
+ protectedBranchIDs = append (protectedBranchIDs , branch .ID )
113
+ }
114
+ Expect (protectedBranchIDs ).To (ConsistOf (want .ProtectedBranchIDs ), "protected_branches" )
115
+ })
164
116
}
165
117
}
166
118
167
- func testAccGitLabProjectApprovalRuleConfig (
168
- randomInt int ,
169
- approvals int ,
170
- userIDs string ,
171
- groupIDs string ,
172
- ) string {
119
+ func testAccGitlabProjectApprovalRuleConfig (project , approvals , userID , groupID , protectedBranchID int ) string {
173
120
return fmt .Sprintf (`
174
- resource "gitlab_user" "foo" {
175
- name = "foo user"
176
- username = "foo-user-%[1]d"
177
- password = "foo12345"
178
- email = "foo-user%[1][email protected] "
179
- is_admin = false
180
- projects_limit = 2
181
- can_create_group = false
182
- is_external = false
183
- }
184
-
185
- resource "gitlab_user" "bar" {
186
- name = "bar user"
187
- username = "bar-user-%[1]d"
188
- password = "bar12345"
189
- email = "bar-user%[1][email protected] "
190
- is_admin = false
191
- projects_limit = 2
192
- can_create_group = false
193
- is_external = false
194
- }
195
-
196
- resource "gitlab_user" "baz" {
197
- name = "baz user"
198
- username = "baz-user-%[1]d"
199
- password = "baz12345"
200
- email = "baz-user%[1][email protected] "
201
- is_admin = false
202
- projects_limit = 2
203
- can_create_group = false
204
- is_external = false
205
- }
206
-
207
- resource "gitlab_user" "qux" {
208
- name = "qux user"
209
- username = "qux-user-%[1]d"
210
- password = "qux12345"
211
- email = "qux-user%[1][email protected] "
212
- is_admin = false
213
- projects_limit = 2
214
- can_create_group = false
215
- is_external = false
216
- }
217
-
218
- resource "gitlab_project" "foo" {
219
- name = "foo project %[1]d"
220
- path = "foo-project-%[1]d"
221
- description = "Terraform acceptance test - Approval Rule"
222
- visibility_level = "public"
223
- }
224
-
225
- resource "gitlab_branch_protection" "default" {
226
- project = gitlab_project.foo.id
227
- branch = gitlab_project.foo.default_branch
228
- push_access_level = "maintainer"
229
- merge_access_level = "developer"
230
- }
231
-
232
- resource "gitlab_project_membership" "baz" {
233
- project_id = gitlab_project.foo.id
234
- user_id = gitlab_user.baz.id
235
- access_level = "developer"
236
- }
237
-
238
- resource "gitlab_project_membership" "qux" {
239
- project_id = gitlab_project.foo.id
240
- user_id = gitlab_user.qux.id
241
- access_level = "developer"
242
- }
243
-
244
- resource "gitlab_group" "foo" {
245
- name = "foo-group %[1]d"
246
- path = "foo-group-%[1]d"
247
- description = "Terraform acceptance tests - Approval Rule"
248
- visibility_level = "public"
249
- }
250
-
251
- resource "gitlab_group" "bar" {
252
- name = "bar-group %[1]d"
253
- path = "bar-group-%[1]d"
254
- description = "Terraform acceptance tests - Approval Rule"
255
- visibility_level = "public"
256
- }
257
-
258
- resource "gitlab_group_membership" "foo" {
259
- group_id = gitlab_group.foo.id
260
- user_id = gitlab_user.foo.id
261
- access_level = "developer"
262
- }
263
-
264
- resource "gitlab_group_membership" "bar" {
265
- group_id = gitlab_group.bar.id
266
- user_id = gitlab_user.bar.id
267
- access_level = "developer"
268
- }
269
-
270
121
resource "gitlab_project_approval_rule" "foo" {
271
- project = gitlab_project.foo.id
272
- name = "foo rule %[1]d "
122
+ project = %d
123
+ name = "foo"
273
124
approvals_required = %d
274
- user_ids = [%s]
275
- group_ids = [%s]
276
- protected_branch_ids = [gitlab_branch_protection.default.id]
277
- }
278
- ` ,
279
- randomInt ,
280
- approvals ,
281
- userIDs ,
282
- groupIDs ,
283
- )
125
+ user_ids = [%d]
126
+ group_ids = [%d]
127
+ protected_branch_ids = [%d]
128
+ }` , project , approvals , userID , groupID , protectedBranchID )
284
129
}
285
130
286
131
func testAccCheckGitlabProjectApprovalRuleExists (n string , projectApprovalRule * gitlab.ProjectApprovalRule ) resource.TestCheckFunc {
@@ -314,25 +159,16 @@ func testAccCheckGitlabProjectApprovalRuleExists(n string, projectApprovalRule *
314
159
}
315
160
}
316
161
317
- return fmt .Errorf ("Rule %d not found" , ruleIDInt )
162
+ return fmt .Errorf ("rule %d not found" , ruleIDInt )
318
163
}
319
164
}
320
165
321
- func testAccCheckGitlabProjectApprovalRuleDestroy (s * terraform.State ) error {
322
- err := testAccCheckGitlabProjectDestroy (s )
323
- if err != nil {
324
- return err
325
- }
326
-
327
- err = testAccCheckGitlabGroupDestroy (s )
328
- if err != nil {
329
- return err
330
- }
331
-
332
- err = testAccCheckGitlabUserDestroy (s )
333
- if err != nil {
334
- return err
166
+ func testAccCheckGitlabProjectApprovalRuleDestroy (client * gitlab.Client , pid interface {}) resource.TestCheckFunc {
167
+ return func (s * terraform.State ) error {
168
+ return InterceptGomegaFailure (func () {
169
+ rules , _ , err := client .Projects .GetProjectApprovalRules (pid )
170
+ Expect (err ).To (BeNil ())
171
+ Expect (rules ).To (BeEmpty ())
172
+ })
335
173
}
336
-
337
- return nil
338
174
}
0 commit comments