@@ -2,6 +2,7 @@ package gitlab
2
2
3
3
import (
4
4
"fmt"
5
+ "regexp"
5
6
"testing"
6
7
7
8
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
@@ -54,6 +55,46 @@ func TestAccGitlabProjectVariable_basic(t *testing.T) {
54
55
}),
55
56
),
56
57
},
58
+ // Update the project variable to enable "masked" for a value that does not meet masking requirements, and expect an error with no state change.
59
+ // ref: https://docs.gitlab.com/ce/ci/variables/README.html#masked-variable-requirements
60
+ {
61
+ Config : testAccGitlabProjectVariableUpdateConfigMaskedBad (rString ),
62
+ Check : resource .ComposeTestCheckFunc (
63
+ testAccCheckGitlabProjectVariableExists ("gitlab_project_variable.foo" , & projectVariable ),
64
+ testAccCheckGitlabProjectVariableAttributes (& projectVariable , & testAccGitlabProjectVariableExpectedAttributes {
65
+ Key : fmt .Sprintf ("key_%s" , rString ),
66
+ Value : fmt .Sprintf ("value-%s" , rString ),
67
+ }),
68
+ ),
69
+ ExpectError : regexp .MustCompile (regexp .QuoteMeta (
70
+ "Invalid value for a masked variable. Check the masked variable requirements: https://docs.gitlab.com/ee/ci/variables/#masked-variable-requirements" ,
71
+ )),
72
+ },
73
+ // Update the project variable to to enable "masked" and meet masking requirements
74
+ // ref: https://docs.gitlab.com/ce/ci/variables/README.html#masked-variable-requirements
75
+ {
76
+ Config : testAccGitlabProjectVariableUpdateConfigMaskedGood (rString ),
77
+ Check : resource .ComposeTestCheckFunc (
78
+ testAccCheckGitlabProjectVariableExists ("gitlab_project_variable.foo" , & projectVariable ),
79
+ testAccCheckGitlabProjectVariableAttributes (& projectVariable , & testAccGitlabProjectVariableExpectedAttributes {
80
+ Key : fmt .Sprintf ("key_%s" , rString ),
81
+ Value : fmt .Sprintf ("value-%s" , rString ),
82
+ Masked : true ,
83
+ }),
84
+ ),
85
+ },
86
+ // Update the project variable to toggle the options back
87
+ {
88
+ Config : testAccGitlabProjectVariableConfig (rString ),
89
+ Check : resource .ComposeTestCheckFunc (
90
+ testAccCheckGitlabProjectVariableExists ("gitlab_project_variable.foo" , & projectVariable ),
91
+ testAccCheckGitlabProjectVariableAttributes (& projectVariable , & testAccGitlabProjectVariableExpectedAttributes {
92
+ Key : fmt .Sprintf ("key_%s" , rString ),
93
+ Value : fmt .Sprintf ("value-%s" , rString ),
94
+ Protected : false ,
95
+ }),
96
+ ),
97
+ },
57
98
},
58
99
})
59
100
}
@@ -88,6 +129,7 @@ type testAccGitlabProjectVariableExpectedAttributes struct {
88
129
Key string
89
130
Value string
90
131
Protected bool
132
+ Masked bool
91
133
}
92
134
93
135
func testAccCheckGitlabProjectVariableAttributes (variable * gitlab.ProjectVariable , want * testAccGitlabProjectVariableExpectedAttributes ) resource.TestCheckFunc {
@@ -104,6 +146,10 @@ func testAccCheckGitlabProjectVariableAttributes(variable *gitlab.ProjectVariabl
104
146
return fmt .Errorf ("got protected %t; want %t" , variable .Protected , want .Protected )
105
147
}
106
148
149
+ if variable .Masked != want .Masked {
150
+ return fmt .Errorf ("got masked %t; want %t" , variable .Masked , want .Masked )
151
+ }
152
+
107
153
return nil
108
154
}
109
155
}
@@ -171,3 +217,48 @@ resource "gitlab_project_variable" "foo" {
171
217
}
172
218
` , rString , rString , rString )
173
219
}
220
+
221
+ func testAccGitlabProjectVariableUpdateConfigMaskedBad (rString string ) string {
222
+ return fmt .Sprintf (`
223
+ resource "gitlab_project" "foo" {
224
+ name = "foo-%s"
225
+ description = "Terraform acceptance tests"
226
+
227
+ # So that acceptance tests can be run in a gitlab organization
228
+ # with no billing
229
+ visibility_level = "public"
230
+ }
231
+
232
+ resource "gitlab_project_variable" "foo" {
233
+ project = "${gitlab_project.foo.id}"
234
+ key = "key_%s"
235
+ value = <<EOF
236
+ value-%s"
237
+ i am multiline
238
+ EOF
239
+ variable_type = "env_var"
240
+ masked = true
241
+ }
242
+ ` , rString , rString , rString )
243
+ }
244
+
245
+ func testAccGitlabProjectVariableUpdateConfigMaskedGood (rString string ) string {
246
+ return fmt .Sprintf (`
247
+ resource "gitlab_project" "foo" {
248
+ name = "foo-%s"
249
+ description = "Terraform acceptance tests"
250
+
251
+ # So that acceptance tests can be run in a gitlab organization
252
+ # with no billing
253
+ visibility_level = "public"
254
+ }
255
+
256
+ resource "gitlab_project_variable" "foo" {
257
+ project = "${gitlab_project.foo.id}"
258
+ key = "key_%s"
259
+ value = "value-%s"
260
+ variable_type = "env_var"
261
+ masked = true
262
+ }
263
+ ` , rString , rString , rString )
264
+ }
0 commit comments