@@ -2,6 +2,8 @@ package gitlab
2
2
3
3
import (
4
4
"fmt"
5
+ "regexp"
6
+ "strconv"
5
7
"testing"
6
8
7
9
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
@@ -24,8 +26,8 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
24
26
{
25
27
Config : testAccGitlabBranchProtectionConfig (rInt ),
26
28
Check : resource .ComposeTestCheckFunc (
27
- testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect " , & pb ),
28
- testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect " , & pb ),
29
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect " , & pb ),
30
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect " , & pb ),
29
31
testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
30
32
Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
31
33
PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
@@ -37,8 +39,8 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
37
39
{
38
40
Config : testAccGitlabBranchProtectionUpdateConfig (rInt ),
39
41
Check : resource .ComposeTestCheckFunc (
40
- testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect " , & pb ),
41
- testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect " , & pb ),
42
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect " , & pb ),
43
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect " , & pb ),
42
44
testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
43
45
Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
44
46
PushAccessLevel : accessLevel [gitlab .MasterPermissions ],
@@ -50,8 +52,107 @@ func TestAccGitlabBranchProtection_basic(t *testing.T) {
50
52
{
51
53
Config : testAccGitlabBranchProtectionConfig (rInt ),
52
54
Check : resource .ComposeTestCheckFunc (
53
- testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.BranchProtect" , & pb ),
54
- testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.BranchProtect" , & pb ),
55
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
56
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
57
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
58
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
59
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
60
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
61
+ }),
62
+ ),
63
+ },
64
+ // Update the the Branch Protection code owner approval setting
65
+ {
66
+ SkipFunc : isRunningInCE ,
67
+ Config : testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt ),
68
+ Check : resource .ComposeTestCheckFunc (
69
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
70
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
71
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
72
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
73
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
74
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
75
+ CodeOwnerApprovalRequired : true ,
76
+ }),
77
+ ),
78
+ },
79
+ // Attempting to update code owner approval setting on CE should fail safely and with an informative error message
80
+ {
81
+ SkipFunc : isRunningInEE ,
82
+ Config : testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt ),
83
+ ExpectError : regexp .MustCompile ("feature unavailable: code owner approvals" ),
84
+ Check : resource .ComposeTestCheckFunc (
85
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
86
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
87
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
88
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
89
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
90
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
91
+ }),
92
+ ),
93
+ },
94
+ // Update the Branch Protection to get back to initial settings
95
+ {
96
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
97
+ Check : resource .ComposeTestCheckFunc (
98
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
99
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
100
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
101
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
102
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
103
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
104
+ }),
105
+ ),
106
+ },
107
+ },
108
+ })
109
+ }
110
+
111
+ func TestAccGitlabBranchProtection_createWithCodeOwnerApproval (t * testing.T ) {
112
+ var pb gitlab.ProtectedBranch
113
+ rInt := acctest .RandInt ()
114
+
115
+ resource .Test (t , resource.TestCase {
116
+ PreCheck : func () { testAccPreCheck (t ) },
117
+ Providers : testAccProviders ,
118
+ CheckDestroy : testAccCheckGitlabBranchProtectionDestroy ,
119
+ Steps : []resource.TestStep {
120
+ // Create a project and Branch Protection with code owner approval enabled
121
+ {
122
+ SkipFunc : isRunningInCE ,
123
+ Config : testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt ),
124
+ Check : resource .ComposeTestCheckFunc (
125
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
126
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
127
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
128
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
129
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
130
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
131
+ CodeOwnerApprovalRequired : true ,
132
+ }),
133
+ ),
134
+ },
135
+ // Attempting to update code owner approval setting on CE should fail safely and with an informative error message
136
+ {
137
+ SkipFunc : isRunningInEE ,
138
+ Config : testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt ),
139
+ ExpectError : regexp .MustCompile ("feature unavailable: code owner approvals" ),
140
+ Check : resource .ComposeTestCheckFunc (
141
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
142
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
143
+ testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
144
+ Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
145
+ PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
146
+ MergeAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
147
+ }),
148
+ ),
149
+ },
150
+ // Update the Branch Protection to get back to initial settings
151
+ {
152
+ Config : testAccGitlabBranchProtectionConfig (rInt ),
153
+ Check : resource .ComposeTestCheckFunc (
154
+ testAccCheckGitlabBranchProtectionExists ("gitlab_branch_protection.branch_protect" , & pb ),
155
+ testAccCheckGitlabBranchProtectionPersistsInStateCorrectly ("gitlab_branch_protection.branch_protect" , & pb ),
55
156
testAccCheckGitlabBranchProtectionAttributes (& pb , & testAccGitlabBranchProtectionExpectedAttributes {
56
157
Name : fmt .Sprintf ("BranchProtect-%d" , rInt ),
57
158
PushAccessLevel : accessLevel [gitlab .DeveloperPermissions ],
@@ -78,6 +179,10 @@ func testAccCheckGitlabBranchProtectionPersistsInStateCorrectly(n string, pb *gi
78
179
return fmt .Errorf ("push access level not persisted in state correctly" )
79
180
}
80
181
182
+ if rs .Primary .Attributes ["code_owner_approval_required" ] != strconv .FormatBool (pb .CodeOwnerApprovalRequired ) {
183
+ return fmt .Errorf ("code_owner_approval_required not persisted in state correctly" )
184
+ }
185
+
81
186
return nil
82
187
}
83
188
}
@@ -110,9 +215,10 @@ func testAccCheckGitlabBranchProtectionExists(n string, pb *gitlab.ProtectedBran
110
215
}
111
216
112
217
type testAccGitlabBranchProtectionExpectedAttributes struct {
113
- Name string
114
- PushAccessLevel string
115
- MergeAccessLevel string
218
+ Name string
219
+ PushAccessLevel string
220
+ MergeAccessLevel string
221
+ CodeOwnerApprovalRequired bool
116
222
}
117
223
118
224
func testAccCheckGitlabBranchProtectionAttributes (pb * gitlab.ProtectedBranch , want * testAccGitlabBranchProtectionExpectedAttributes ) resource.TestCheckFunc {
@@ -129,6 +235,10 @@ func testAccCheckGitlabBranchProtectionAttributes(pb *gitlab.ProtectedBranch, wa
129
235
return fmt .Errorf ("got Merge access levels %q; want %q" , pb .MergeAccessLevels [0 ].AccessLevel , accessLevelID [want .MergeAccessLevel ])
130
236
}
131
237
238
+ if pb .CodeOwnerApprovalRequired != want .CodeOwnerApprovalRequired {
239
+ return fmt .Errorf ("got code_owner_approval_required %v; want %v" , pb .CodeOwnerApprovalRequired , want .CodeOwnerApprovalRequired )
240
+ }
241
+
132
242
return nil
133
243
}
134
244
}
@@ -168,8 +278,8 @@ resource "gitlab_project" "foo" {
168
278
visibility_level = "public"
169
279
}
170
280
171
- resource "gitlab_branch_protection" "BranchProtect " {
172
- project = "${ gitlab_project.foo.id}"
281
+ resource "gitlab_branch_protection" "branch_protect " {
282
+ project = gitlab_project.foo.id
173
283
branch = "BranchProtect-%d"
174
284
push_access_level = "developer"
175
285
merge_access_level = "developer"
@@ -188,11 +298,32 @@ resource "gitlab_project" "foo" {
188
298
visibility_level = "public"
189
299
}
190
300
191
- resource "gitlab_branch_protection" "BranchProtect " {
192
- project = "${ gitlab_project.foo.id}"
301
+ resource "gitlab_branch_protection" "branch_protect " {
302
+ project = gitlab_project.foo.id
193
303
branch = "BranchProtect-%d"
194
304
push_access_level = "maintainer"
195
305
merge_access_level = "maintainer"
196
306
}
197
307
` , rInt , rInt )
198
308
}
309
+
310
+ func testAccGitlabBranchProtectionUpdateConfigCodeOwnerTrue (rInt int ) string {
311
+ return fmt .Sprintf (`
312
+ resource "gitlab_project" "foo" {
313
+ name = "foo-%d"
314
+ description = "Terraform acceptance tests"
315
+
316
+ # So that acceptance tests can be run in a gitlab organization
317
+ # with no billing
318
+ visibility_level = "public"
319
+ }
320
+
321
+ resource "gitlab_branch_protection" "branch_protect" {
322
+ project = gitlab_project.foo.id
323
+ branch = "BranchProtect-%d"
324
+ push_access_level = "developer"
325
+ merge_access_level = "developer"
326
+ code_owner_approval_required = true
327
+ }
328
+ ` , rInt , rInt )
329
+ }
0 commit comments