1
1
package provider
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"testing"
6
7
@@ -65,64 +66,87 @@ func TestAccGitlabGroupVariable_scope(t *testing.T) {
65
66
var groupVariableA , groupVariableB gitlab.GroupVariable
66
67
rString := acctest .RandString (5 )
67
68
69
+ defaultValueA := fmt .Sprintf ("value-%s-a" , rString )
70
+ defaultValueB := fmt .Sprintf ("value-%s-b" , rString )
68
71
resource .Test (t , resource.TestCase {
69
72
PreCheck : func () { testAccPreCheck (t ) },
70
73
ProviderFactories : providerFactories ,
71
74
CheckDestroy : testAccCheckGitlabGroupVariableDestroy ,
72
75
Steps : []resource.TestStep {
73
76
// Create a group and variables with same keys, different scopes
74
77
{
75
- Config : testAccGitlabGroupVariableScopeConfig (rString , "*" , "review/*" ),
78
+ Config : testAccGitlabGroupVariableScopeConfig (rString , "*" , "review/*" , defaultValueA , defaultValueB ),
76
79
SkipFunc : isRunningInCE ,
77
80
Check : resource .ComposeTestCheckFunc (
78
81
testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.a" , & groupVariableA ),
79
82
testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.b" , & groupVariableB ),
80
83
testAccCheckGitlabGroupVariableAttributes (& groupVariableA , & testAccGitlabGroupVariableExpectedAttributes {
81
84
Key : fmt .Sprintf ("key_%s" , rString ),
82
- Value : fmt . Sprintf ( "value-%s-a" , rString ) ,
85
+ Value : defaultValueA ,
83
86
EnvironmentScope : "*" ,
84
87
}),
85
88
testAccCheckGitlabGroupVariableAttributes (& groupVariableB , & testAccGitlabGroupVariableExpectedAttributes {
86
89
Key : fmt .Sprintf ("key_%s" , rString ),
87
- Value : fmt . Sprintf ( "value-%s-b" , rString ) ,
90
+ Value : defaultValueB ,
88
91
EnvironmentScope : "review/*" ,
89
92
}),
90
93
),
91
94
},
92
95
// Change a variable's scope
93
96
{
94
- Config : testAccGitlabGroupVariableScopeConfig (rString , "my-new-scope" , "review/*" ),
97
+ Config : testAccGitlabGroupVariableScopeConfig (rString , "my-new-scope" , "review/*" , defaultValueA , defaultValueB ),
95
98
SkipFunc : isRunningInCE ,
96
99
Check : resource .ComposeTestCheckFunc (
97
100
testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.a" , & groupVariableA ),
98
101
testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.b" , & groupVariableB ),
99
102
testAccCheckGitlabGroupVariableAttributes (& groupVariableA , & testAccGitlabGroupVariableExpectedAttributes {
100
103
Key : fmt .Sprintf ("key_%s" , rString ),
101
- Value : fmt . Sprintf ( "value-%s-a" , rString ) ,
104
+ Value : defaultValueA ,
102
105
EnvironmentScope : "my-new-scope" ,
103
106
}),
104
107
testAccCheckGitlabGroupVariableAttributes (& groupVariableB , & testAccGitlabGroupVariableExpectedAttributes {
105
108
Key : fmt .Sprintf ("key_%s" , rString ),
106
- Value : fmt . Sprintf ( "value-%s-b" , rString ) ,
109
+ Value : defaultValueB ,
107
110
EnvironmentScope : "review/*" ,
108
111
}),
109
112
),
110
113
},
111
114
// Change both variables scopes at the same time
112
115
{
113
- Config : testAccGitlabGroupVariableScopeConfig (rString , "my-new-new-scope" , "review/hello-world" ),
116
+ Config : testAccGitlabGroupVariableScopeConfig (rString , "my-new-new-scope" , "review/hello-world" , defaultValueA , defaultValueB ),
114
117
SkipFunc : isRunningInCE ,
115
118
Check : resource .ComposeTestCheckFunc (
116
119
testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.a" , & groupVariableA ),
117
120
testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.b" , & groupVariableB ),
118
121
testAccCheckGitlabGroupVariableAttributes (& groupVariableA , & testAccGitlabGroupVariableExpectedAttributes {
119
122
Key : fmt .Sprintf ("key_%s" , rString ),
120
- Value : fmt . Sprintf ( "value-%s-a" , rString ) ,
123
+ Value : defaultValueA ,
121
124
EnvironmentScope : "my-new-new-scope" ,
122
125
}),
123
126
testAccCheckGitlabGroupVariableAttributes (& groupVariableB , & testAccGitlabGroupVariableExpectedAttributes {
124
127
Key : fmt .Sprintf ("key_%s" , rString ),
125
- Value : fmt .Sprintf ("value-%s-b" , rString ),
128
+ Value : defaultValueB ,
129
+ EnvironmentScope : "review/hello-world" ,
130
+ }),
131
+ ),
132
+ },
133
+ // Change value of one variable
134
+ {
135
+ Config : testAccGitlabGroupVariableScopeConfig (rString , "my-new-new-scope" , "review/hello-world" , defaultValueA , fmt .Sprintf ("new-value-for-b-%s" , rString )),
136
+ // SkipFunc: isRunningInCE,
137
+ // NOTE(TF): this test sporadically fails because of this: https://gitlab.com/gitlab-org/gitlab/-/issues/333296
138
+ SkipFunc : func () (bool , error ) { return true , nil },
139
+ Check : resource .ComposeTestCheckFunc (
140
+ testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.a" , & groupVariableA ),
141
+ testAccCheckGitlabGroupVariableExists ("gitlab_group_variable.b" , & groupVariableB ),
142
+ testAccCheckGitlabGroupVariableAttributes (& groupVariableA , & testAccGitlabGroupVariableExpectedAttributes {
143
+ Key : fmt .Sprintf ("key_%s" , rString ),
144
+ Value : defaultValueA ,
145
+ EnvironmentScope : "my-new-new-scope" ,
146
+ }),
147
+ testAccCheckGitlabGroupVariableAttributes (& groupVariableB , & testAccGitlabGroupVariableExpectedAttributes {
148
+ Key : fmt .Sprintf ("key_%s" , rString ),
149
+ Value : fmt .Sprintf ("new-value-for-b-%s" , rString ),
126
150
EnvironmentScope : "review/hello-world" ,
127
151
}),
128
152
),
@@ -146,7 +170,7 @@ func testAccCheckGitlabGroupVariableExists(n string, groupVariable *gitlab.Group
146
170
if key == "" {
147
171
return fmt .Errorf ("No variable key is set" )
148
172
}
149
- gotVariable , _ , err := testGitlabClient .GroupVariables .GetVariable (repoName , key , modifyRequestAddEnvironmentFilter ( rs .Primary .Attributes ["environment_scope" ]))
173
+ gotVariable , _ , err := testGitlabClient .GroupVariables .GetVariable (repoName , key , withEnvironmentScopeFilter ( context . Background (), rs .Primary .Attributes ["environment_scope" ]))
150
174
if err != nil {
151
175
return err
152
176
}
@@ -245,7 +269,7 @@ resource "gitlab_group_variable" "foo" {
245
269
` , rString , rString , rString , rString )
246
270
}
247
271
248
- func testAccGitlabGroupVariableScopeConfig (rString , scopeA , scopeB string ) string {
272
+ func testAccGitlabGroupVariableScopeConfig (rString , scopeA , scopeB string , valueA , valueB string ) string {
249
273
return fmt .Sprintf (`
250
274
resource "gitlab_group" "foo" {
251
275
name = "foo%v"
@@ -255,15 +279,15 @@ resource "gitlab_group" "foo" {
255
279
resource "gitlab_group_variable" "a" {
256
280
group = "${gitlab_group.foo.id}"
257
281
key = "key_%s"
258
- value = "value-%s-a "
282
+ value = "%s "
259
283
environment_scope = "%s"
260
284
}
261
285
262
286
resource "gitlab_group_variable" "b" {
263
287
group = "${gitlab_group.foo.id}"
264
288
key = "key_%s"
265
- value = "value-%s-b "
289
+ value = "%s "
266
290
environment_scope = "%s"
267
291
}
268
- ` , rString , rString , rString , rString , scopeA , rString , rString , scopeB )
292
+ ` , rString , rString , rString , valueA , scopeA , rString , valueB , scopeB )
269
293
}
0 commit comments