-
Notifications
You must be signed in to change notification settings - Fork 323
/
resource_gitlab_group_share_group_test.go
145 lines (127 loc) · 4.28 KB
/
resource_gitlab_group_share_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
package provider
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/xanzy/go-gitlab"
)
func TestAccGitlabGroupShareGroup_basic(t *testing.T) {
testAccCheck(t)
mainGroup := testAccCreateGroups(t, 1)[0]
sharedGroup := testAccCreateGroups(t, 1)[0]
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
CheckDestroy: testAccCheckGitlabShareGroupDestroy,
Steps: []resource.TestStep{
// Share a new group with another group
{
Config: testAccGitlabGroupShareGroupConfig(mainGroup.ID, sharedGroup.ID,
`
group_access = "guest"
expires_at = "2099-01-01"
`,
),
Check: testAccCheckGitlabGroupSharedWithGroup(mainGroup.Name, sharedGroup.Name, "2099-01-01", gitlab.GuestPermissions),
},
{
// Verify Import
ResourceName: "gitlab_group_share_group.test",
ImportState: true,
ImportStateVerify: true,
},
// Update the share group
{
Config: testAccGitlabGroupShareGroupConfig(mainGroup.ID, sharedGroup.ID, `group_access = "reporter"`),
Check: testAccCheckGitlabGroupSharedWithGroup(mainGroup.Name, sharedGroup.Name, "", gitlab.ReporterPermissions),
},
{
// Verify Import
ResourceName: "gitlab_group_share_group.test",
ImportState: true,
ImportStateVerify: true,
},
// Update share group back to initial settings
{
Config: testAccGitlabGroupShareGroupConfig(mainGroup.ID, sharedGroup.ID,
`
group_access = "guest"
expires_at = "2099-01-01"
`,
),
Check: testAccCheckGitlabGroupSharedWithGroup(mainGroup.Name, sharedGroup.Name, "2099-01-01", gitlab.GuestPermissions),
},
{
// Verify Import
ResourceName: "gitlab_group_share_group.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckGitlabGroupSharedWithGroup(mainGroupName string, sharedGroupName string, expireTime string, accessLevel gitlab.AccessLevelValue) resource.TestCheckFunc {
return func(_ *terraform.State) error {
mainGroup, _, err := testGitlabClient.Groups.GetGroup(mainGroupName, nil)
if err != nil {
return err
}
sharedGroupsCount := len(mainGroup.SharedWithGroups)
if sharedGroupsCount != 1 {
return fmt.Errorf("Number of shared groups was %d (wanted %d)", sharedGroupsCount, 1)
}
sharedGroup := mainGroup.SharedWithGroups[0]
if sharedGroup.GroupName != sharedGroupName {
return fmt.Errorf("group name was %s (wanted %s)", sharedGroup.GroupName, sharedGroupName)
}
if gitlab.AccessLevelValue(sharedGroup.GroupAccessLevel) != accessLevel {
return fmt.Errorf("groupAccessLevel was %d (wanted %d)", sharedGroup.GroupAccessLevel, accessLevel)
}
if sharedGroup.ExpiresAt == nil && expireTime != "" {
return fmt.Errorf("expire time was nil (wanted %s)", expireTime)
} else if sharedGroup.ExpiresAt != nil && sharedGroup.ExpiresAt.String() != expireTime {
return fmt.Errorf("expire time was %s (wanted %s)", sharedGroup.ExpiresAt.String(), expireTime)
}
return nil
}
}
func testAccCheckGitlabShareGroupDestroy(s *terraform.State) error {
var groupId string
var sharedGroupId int
var err error
for _, rs := range s.RootModule().Resources {
if rs.Type == "gitlab_group_share_group" {
groupId, sharedGroupId, err = groupIdsFromId(rs.Primary.ID)
if err != nil {
return fmt.Errorf("[ERROR] cannot get Group ID and ShareGroupId from input: %v", rs.Primary.ID)
}
// Get Main Group
group, _, err := testGitlabClient.Groups.GetGroup(groupId, nil)
if err != nil {
return err
}
// Make sure that SharedWithGroups attribute on the main group does not contain the shared group id at all
for _, sharedGroup := range group.SharedWithGroups {
if sharedGroupId == sharedGroup.GroupID {
return fmt.Errorf("GitLab Group Share %d still exists", sharedGroupId)
}
}
}
}
return nil
}
func testAccGitlabGroupShareGroupConfig(mainGroupId int, shareGroupId int, shareGroupSettings string) string {
return fmt.Sprintf(
`
resource "gitlab_group_share_group" "test" {
group_id = %[1]d
share_group_id = %[2]d
%[3]s
}
`,
mainGroupId,
shareGroupId,
shareGroupSettings,
)
}