-
Notifications
You must be signed in to change notification settings - Fork 323
/
resource_gitlab_group_test.go
606 lines (548 loc) · 20.3 KB
/
resource_gitlab_group_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
package provider
import (
"fmt"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/xanzy/go-gitlab"
)
func TestAccGitlabGroup_basic(t *testing.T) {
var group gitlab.Group
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabGroupDestroy,
Steps: []resource.TestStep{
// Create a group
{
Config: testAccGitlabGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("foo-name-%d", rInt),
Path: fmt.Sprintf("foo-path-%d", rInt),
Description: "Terraform acceptance tests",
LFSEnabled: true,
Visibility: "public", // default value
ProjectCreationLevel: "maintainer", // default value
SubGroupCreationLevel: "owner", // default value
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
}),
),
},
// Update the group to change the description
{
Config: testAccGitlabGroupUpdateConfig(rInt, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("bar-name-%d", rInt),
Path: fmt.Sprintf("bar-path-%d", rInt),
Description: "Terraform acceptance tests! Updated description",
LFSEnabled: false,
Visibility: "public", // default value
RequestAccessEnabled: true,
ProjectCreationLevel: "developer",
SubGroupCreationLevel: "maintainer",
RequireTwoFactorAuth: true,
TwoFactorGracePeriod: 56,
AutoDevopsEnabled: true,
EmailsDisabled: true,
MentionsDisabled: true,
ShareWithGroupLock: true,
DefaultBranchProtection: 1,
}),
),
},
// Update the group to use zero-value `default_branch_protection`
{
Config: testAccGitlabGroupUpdateConfig(rInt, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("bar-name-%d", rInt),
Path: fmt.Sprintf("bar-path-%d", rInt),
Description: "Terraform acceptance tests! Updated description",
LFSEnabled: false,
Visibility: "public", // default value
RequestAccessEnabled: true,
ProjectCreationLevel: "developer",
SubGroupCreationLevel: "maintainer",
RequireTwoFactorAuth: true,
TwoFactorGracePeriod: 56,
AutoDevopsEnabled: true,
EmailsDisabled: true,
MentionsDisabled: true,
ShareWithGroupLock: true,
DefaultBranchProtection: 0,
}),
),
},
// Update the group to put the name and description back
{
Config: testAccGitlabGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupAttributes(&group, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("foo-name-%d", rInt),
Path: fmt.Sprintf("foo-path-%d", rInt),
Description: "Terraform acceptance tests",
LFSEnabled: true,
Visibility: "public", // default value
ProjectCreationLevel: "maintainer", // default value
SubGroupCreationLevel: "owner", // default value
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
}),
),
},
},
})
}
// lintignore: AT002 // TODO: Resolve this tfproviderlint issue
func TestAccGitlabGroup_import(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabGroupConfig(rInt),
},
{
ResourceName: "gitlab_group.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccGitlabGroup_nested(t *testing.T) {
var group gitlab.Group
var group2 gitlab.Group
var nestedGroup gitlab.Group
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabNestedGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("nfoo-name-%d", rInt),
Path: fmt.Sprintf("nfoo-path-%d", rInt),
Description: "Terraform acceptance tests",
LFSEnabled: true,
Visibility: "public", // default value
ProjectCreationLevel: "maintainer", // default value
SubGroupCreationLevel: "owner", // default value
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
Parent: &group,
}),
),
},
{
Config: testAccGitlabNestedGroupChangeParentConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("nfoo-name-%d", rInt),
Path: fmt.Sprintf("nfoo-path-%d", rInt),
Description: "Terraform acceptance tests - new parent",
LFSEnabled: true,
Visibility: "public", // default value
ProjectCreationLevel: "maintainer", // default value
SubGroupCreationLevel: "owner", // default value
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
Parent: &group2,
}),
),
},
{
Config: testAccGitlabNestedGroupRemoveParentConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
Name: fmt.Sprintf("nfoo-name-%d", rInt),
Path: fmt.Sprintf("nfoo-path-%d", rInt),
Description: "Terraform acceptance tests - updated",
LFSEnabled: true,
Visibility: "public", // default value
ProjectCreationLevel: "maintainer", // default value
SubGroupCreationLevel: "owner", // default value
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
}),
),
},
// TODO In EE version, re-creating on the same path where a previous group was soft-deleted doesn't work.
// {
// Config: testAccGitlabNestedGroupConfig(rInt),
// Check: resource.ComposeTestCheckFunc(
// testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
// testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
// testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
// testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
// Name: fmt.Sprintf("nfoo-name-%d", rInt),
// Path: fmt.Sprintf("nfoo-path-%d", rInt),
// Description: "Terraform acceptance tests",
// LFSEnabled: true,
// Visibility: "public", // default value
// ProjectCreationLevel: "maintainer", // default value
// SubGroupCreationLevel: "owner", // default value
// TwoFactorGracePeriod: 48, // default value
// DefaultBranchProtection: 2, // default value
// Parent: &group,
// }),
// ),
// },
},
})
}
func TestAccGitlabGroup_disappears(t *testing.T) {
var group gitlab.Group
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccGitlabGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
testAccCheckGitlabGroupDisappears(&group),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func TestAccGitlabGroup_PreventForkingOutsideGroup(t *testing.T) {
var group gitlab.Group
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabGroupDestroy,
Steps: []resource.TestStep{
{
SkipFunc: isRunningInCE,
Config: testAccGitlabGroupPreventForkingOutsideGroupConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
resource.TestCheckResourceAttr("gitlab_group.foo", "prevent_forking_outside_group", "true"),
),
},
{
SkipFunc: isRunningInCE,
Config: testAccGitlabGroupPreventForkingOutsideGroupUpdateConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
resource.TestCheckResourceAttr("gitlab_group.foo", "prevent_forking_outside_group", "false"),
),
},
},
})
}
func testAccCheckGitlabGroupDisappears(group *gitlab.Group) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, err := testGitlabClient.Groups.DeleteGroup(group.ID)
if err != nil {
return err
}
// Fixes groups API async deletion issue
// https://github.com/gitlabhq/terraform-provider-gitlab/issues/319
for start := time.Now(); time.Since(start) < 15*time.Second; {
g, _, err := testGitlabClient.Groups.GetGroup(group.ID, nil)
if is404(err) {
return nil
}
if g != nil && g.MarkedForDeletionOn != nil {
return nil
}
if err != nil {
return err
}
}
return fmt.Errorf("waited for more than 15 seconds for group to be asynchronously deleted")
}
}
func testAccCheckGitlabGroupExists(n string, group *gitlab.Group) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not Found: %s", n)
}
groupID := rs.Primary.ID
if groupID == "" {
return fmt.Errorf("No group ID is set")
}
gotGroup, _, err := testGitlabClient.Groups.GetGroup(groupID, nil)
if err != nil {
return err
}
*group = *gotGroup
return nil
}
}
type testAccGitlabGroupExpectedAttributes struct {
Name string
Path string
Description string
Parent *gitlab.Group
LFSEnabled bool
RequestAccessEnabled bool
Visibility gitlab.VisibilityValue
ShareWithGroupLock bool
AutoDevopsEnabled bool
EmailsDisabled bool
MentionsDisabled bool
ProjectCreationLevel gitlab.ProjectCreationLevelValue
SubGroupCreationLevel gitlab.SubGroupCreationLevelValue
RequireTwoFactorAuth bool
TwoFactorGracePeriod int
DefaultBranchProtection int
}
func testAccCheckGitlabGroupAttributes(group *gitlab.Group, want *testAccGitlabGroupExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {
if group.Name != want.Name {
return fmt.Errorf("got repo %q; want %q", group.Name, want.Name)
}
if group.Path != want.Path {
return fmt.Errorf("got path %q; want %q", group.Path, want.Path)
}
if group.Description != want.Description {
return fmt.Errorf("got description %q; want %q", group.Description, want.Description)
}
if group.LFSEnabled != want.LFSEnabled {
return fmt.Errorf("got lfs_enabled %t; want %t", group.LFSEnabled, want.LFSEnabled)
}
if group.Visibility != want.Visibility {
return fmt.Errorf("got request_visibility_level: %q; want %q", group.Visibility, want.Visibility)
}
if group.AutoDevopsEnabled != want.AutoDevopsEnabled {
return fmt.Errorf("got request_auto_devops_enabled: %t; want %t", group.AutoDevopsEnabled, want.AutoDevopsEnabled)
}
if group.EmailsDisabled != want.EmailsDisabled {
return fmt.Errorf("got request_emails_disabled: %t; want %t", group.EmailsDisabled, want.EmailsDisabled)
}
if group.MentionsDisabled != want.MentionsDisabled {
return fmt.Errorf("got request_mentions_disabled: %t; want %t", group.MentionsDisabled, want.MentionsDisabled)
}
if group.RequestAccessEnabled != want.RequestAccessEnabled {
return fmt.Errorf("got request_access_enabled %t; want %t", group.RequestAccessEnabled, want.RequestAccessEnabled)
}
if group.ProjectCreationLevel != want.ProjectCreationLevel {
return fmt.Errorf("got project_creation_level %s; want %s", group.ProjectCreationLevel, want.ProjectCreationLevel)
}
if group.SubGroupCreationLevel != want.SubGroupCreationLevel {
return fmt.Errorf("got subgroup_creation_level %s; want %s", group.SubGroupCreationLevel, want.SubGroupCreationLevel)
}
if group.RequireTwoFactorAuth != want.RequireTwoFactorAuth {
return fmt.Errorf("got require_two_factor_authentication %t; want %t", group.RequireTwoFactorAuth, want.RequireTwoFactorAuth)
}
if group.TwoFactorGracePeriod != want.TwoFactorGracePeriod {
return fmt.Errorf("got two_factor_grace_period %d; want %d", group.TwoFactorGracePeriod, want.TwoFactorGracePeriod)
}
if group.ShareWithGroupLock != want.ShareWithGroupLock {
return fmt.Errorf("got share_with_group_lock %t; want %t", group.ShareWithGroupLock, want.ShareWithGroupLock)
}
if group.DefaultBranchProtection != want.DefaultBranchProtection {
return fmt.Errorf("got default_branch_protection %d; want %d", group.DefaultBranchProtection, want.DefaultBranchProtection)
}
if want.Parent != nil {
if group.ParentID != want.Parent.ID {
return fmt.Errorf("got parent_id %d; want %d", group.ParentID, want.Parent.ID)
}
} else {
if group.ParentID != 0 {
return fmt.Errorf("got parent_id %d; want %d", group.ParentID, 0)
}
}
return nil
}
}
func testAccCheckGitlabGroupDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "gitlab_group" {
continue
}
group, _, err := testGitlabClient.Groups.GetGroup(rs.Primary.ID, nil)
if err == nil {
if group != nil && fmt.Sprintf("%d", group.ID) == rs.Primary.ID {
if group.MarkedForDeletionOn == nil {
return fmt.Errorf("Group still exists")
}
}
}
if !is404(err) {
return err
}
return nil
}
return nil
}
func testAccGitlabGroupConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
`, rInt, rInt)
}
func testAccGitlabGroupUpdateConfig(rInt int, defaultBranchProtection int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "bar-name-%d"
path = "bar-path-%d"
description = "Terraform acceptance tests! Updated description"
lfs_enabled = false
request_access_enabled = true
project_creation_level = "developer"
subgroup_creation_level = "maintainer"
require_two_factor_authentication = true
two_factor_grace_period = 56
auto_devops_enabled = true
emails_disabled = true
mentions_disabled = true
share_with_group_lock = true
default_branch_protection = %d
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
`, rInt, rInt, defaultBranchProtection)
}
func testAccGitlabNestedGroupConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_group" "foo2" {
name = "foo2-name-%d"
path = "foo2-path-%d"
description = "Terraform acceptance tests - parent2"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_group" "nested_foo" {
name = "nfoo-name-%d"
path = "nfoo-path-%d"
parent_id = "${gitlab_group.foo.id}"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
`, rInt, rInt, rInt, rInt, rInt, rInt)
}
func testAccGitlabNestedGroupRemoveParentConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_group" "foo2" {
name = "foo2-name-%d"
path = "foo2-path-%d"
description = "Terraform acceptance tests - parent2"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_group" "nested_foo" {
name = "nfoo-name-%d"
path = "nfoo-path-%d"
description = "Terraform acceptance tests - updated"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
`, rInt, rInt, rInt, rInt, rInt, rInt)
}
func testAccGitlabNestedGroupChangeParentConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_group" "foo2" {
name = "foo2-name-%d"
path = "foo2-path-%d"
description = "Terraform acceptance tests - parent2"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
resource "gitlab_group" "nested_foo" {
name = "nfoo-name-%d"
path = "nfoo-path-%d"
description = "Terraform acceptance tests - new parent"
parent_id = "${gitlab_group.foo2.id}"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
}
`, rInt, rInt, rInt, rInt, rInt, rInt)
}
func testAccGitlabGroupPreventForkingOutsideGroupConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
prevent_forking_outside_group = true
}
`, rInt, rInt)
}
func testAccGitlabGroupPreventForkingOutsideGroupUpdateConfig(rInt int) string {
return fmt.Sprintf(`
resource "gitlab_group" "foo" {
name = "foo-name-%d"
path = "foo-path-%d"
description = "Terraform acceptance tests"
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
prevent_forking_outside_group = false
}
`, rInt, rInt)
}