@@ -7,113 +7,80 @@ import (
7
7
"strings"
8
8
"testing"
9
9
10
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
11
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13
12
"github.com/xanzy/go-gitlab"
14
13
)
15
14
16
15
func TestAccGitlabProjectMirror_basic (t * testing.T ) {
17
- var hook gitlab. ProjectMirror
18
- rInt := acctest . RandInt ()
16
+ ctx := testAccGitlabProjectStart ( t )
17
+ var miror gitlab. ProjectMirror
19
18
20
19
resource .Test (t , resource.TestCase {
21
20
PreCheck : func () { testAccPreCheck (t ) },
22
21
Providers : testAccProviders ,
23
22
CheckDestroy : testAccCheckGitlabProjectMirrorDestroy ,
24
23
Steps : []resource.TestStep {
25
- // Create a project and hook with default options
24
+ // Create with default options
26
25
{
27
- Config : testAccGitlabProjectMirrorConfig (rInt ),
26
+ Config : testAccGitlabProjectMirrorConfig (ctx . project . PathWithNamespace ),
28
27
Check : resource .ComposeTestCheckFunc (
29
- testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & hook ),
30
- testAccCheckGitlabProjectMirrorAttributes (& hook , & testAccGitlabProjectMirrorExpectedAttributes {
31
- URL : fmt . Sprintf ( "https://example.com/hook-%d" , rInt ) ,
28
+ testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & miror ),
29
+ testAccCheckGitlabProjectMirrorAttributes (& miror , & testAccGitlabProjectMirrorExpectedAttributes {
30
+ URL : "https://example.com/mirror" ,
32
31
Enabled : true ,
33
32
OnlyProtectedBranches : true ,
34
33
KeepDivergentRefs : true ,
35
34
}),
36
35
),
37
36
},
38
- // Update the project hook to toggle all the values to their inverse
37
+ // Update to toggle all the values to their inverse
39
38
{
40
- Config : testAccGitlabProjectMirrorUpdateConfig (rInt ),
39
+ Config : testAccGitlabProjectMirrorUpdateConfig (ctx . project . PathWithNamespace ),
41
40
Check : resource .ComposeTestCheckFunc (
42
- testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & hook ),
43
- testAccCheckGitlabProjectMirrorAttributes (& hook , & testAccGitlabProjectMirrorExpectedAttributes {
44
- URL : fmt . Sprintf ( "https://example.com/hook-%d" , rInt ) ,
41
+ testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & miror ),
42
+ testAccCheckGitlabProjectMirrorAttributes (& miror , & testAccGitlabProjectMirrorExpectedAttributes {
43
+ URL : "https://example.com/mirror" ,
45
44
Enabled : false ,
46
45
OnlyProtectedBranches : false ,
47
46
KeepDivergentRefs : false ,
48
47
}),
49
48
),
50
49
},
51
- // Update the project hook to toggle the options back
50
+ // Update to toggle the options back
52
51
{
53
- Config : testAccGitlabProjectMirrorConfig (rInt ),
52
+ Config : testAccGitlabProjectMirrorConfig (ctx . project . PathWithNamespace ),
54
53
Check : resource .ComposeTestCheckFunc (
55
- testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & hook ),
56
- testAccCheckGitlabProjectMirrorAttributes (& hook , & testAccGitlabProjectMirrorExpectedAttributes {
57
- URL : fmt . Sprintf ( "https://example.com/hook-%d" , rInt ) ,
54
+ testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & miror ),
55
+ testAccCheckGitlabProjectMirrorAttributes (& miror , & testAccGitlabProjectMirrorExpectedAttributes {
56
+ URL : "https://example.com/mirror" ,
58
57
Enabled : true ,
59
58
OnlyProtectedBranches : true ,
60
59
KeepDivergentRefs : true ,
61
60
}),
62
61
),
63
62
},
64
- },
65
- })
66
- }
67
-
68
- func TestAccGitlabProjectMirror_withPassword (t * testing.T ) {
69
- //var mirror gitlab.ProjectMirror
70
- rInt := acctest .RandInt ()
71
-
72
- resource .Test (t , resource.TestCase {
73
- PreCheck : func () { testAccPreCheck (t ) },
74
- Providers : testAccProviders ,
75
- CheckDestroy : testAccCheckGitlabProjectMirrorDestroy ,
76
- Steps : []resource.TestStep {
77
- // Create a project and mirror with a username / password.
78
- {
79
- Config : testAccGitlabProjectMirrorConfigWithPassword (rInt ),
80
- },
81
- },
82
- })
83
- }
84
-
85
- func TestAccGitlabProjectMirror_withCount (t * testing.T ) {
86
- //var mirror gitlab.ProjectMirror
87
- rInt := acctest .RandInt ()
88
-
89
- resource .Test (t , resource.TestCase {
90
- PreCheck : func () { testAccPreCheck (t ) },
91
- Providers : testAccProviders ,
92
- CheckDestroy : testAccCheckGitlabProjectMirrorDestroy ,
93
- Steps : []resource.TestStep {
63
+ // Import
94
64
{
95
- Config : testAccGitlabProjectMirrorConfigWithCount (rInt ),
65
+ ResourceName : "gitlab_project_mirror.foo" ,
66
+ ImportState : true ,
67
+ ImportStateVerify : true ,
96
68
},
97
69
},
98
70
})
99
71
}
100
72
101
- // lintignore: AT002 // TODO: Resolve this tfproviderlint issue
102
- func TestAccGitlabProjectMirror_import (t * testing.T ) {
103
- rInt := acctest .RandInt ()
73
+ func TestAccGitlabProjectMirror_withPassword (t * testing.T ) {
74
+ ctx := testAccGitlabProjectStart (t )
104
75
105
76
resource .Test (t , resource.TestCase {
106
77
PreCheck : func () { testAccPreCheck (t ) },
107
78
Providers : testAccProviders ,
108
79
CheckDestroy : testAccCheckGitlabProjectMirrorDestroy ,
109
80
Steps : []resource.TestStep {
81
+ // Create a project and mirror with a username / password.
110
82
{
111
- Config : testAccGitlabProjectMirrorConfig (rInt ),
112
- },
113
- {
114
- ResourceName : "gitlab_project_mirror.foo" ,
115
- ImportState : true ,
116
- ImportStateVerify : true ,
83
+ Config : testAccGitlabProjectMirrorConfigWithPassword (ctx .project .PathWithNamespace ),
117
84
},
118
85
},
119
86
})
@@ -180,101 +147,42 @@ func testAccCheckGitlabProjectMirrorAttributes(mirror *gitlab.ProjectMirror, wan
180
147
}
181
148
182
149
func testAccCheckGitlabProjectMirrorDestroy (s * terraform.State ) error {
183
- conn := testAccProvider .Meta ().(* gitlab.Client )
184
-
185
- for _ , rs := range s .RootModule ().Resources {
186
- if rs .Type != "gitlab_project" {
187
- continue
188
- }
189
-
190
- gotRepo , resp , err := conn .Projects .GetProject (rs .Primary .ID , nil )
191
- if err == nil {
192
- if gotRepo != nil && fmt .Sprintf ("%d" , gotRepo .ID ) == rs .Primary .ID {
193
- if gotRepo .MarkedForDeletionAt == nil {
194
- return fmt .Errorf ("Repository still exists" )
195
- }
196
- }
197
- }
198
- if resp != nil && resp .StatusCode != 404 {
199
- return err
200
- }
201
- return nil
150
+ var mirror gitlab.ProjectMirror
151
+ if err := testAccCheckGitlabProjectMirrorExists ("gitlab_project_mirror.foo" , & mirror )(s ); err != nil {
152
+ return err
153
+ }
154
+ if mirror .Enabled {
155
+ return errors .New ("mirror is enabled" )
202
156
}
203
157
return nil
204
158
}
205
159
206
- func testAccGitlabProjectMirrorConfig (rInt int ) string {
207
- return fmt .Sprintf (`
208
- resource "gitlab_project" "foo" {
209
- name = "foo-%d"
210
- description = "Terraform acceptance tests"
211
-
212
- # So that acceptance tests can be run in a gitlab organization
213
- # with no billing
214
- visibility_level = "public"
215
- }
216
-
217
- resource "gitlab_project_mirror" "foo" {
218
- project = "${gitlab_project.foo.id}"
219
- url = "https://example.com/hook-%d"
220
- }
221
- ` , rInt , rInt )
222
- }
223
-
224
- func testAccGitlabProjectMirrorConfigWithCount (rInt int ) string {
160
+ func testAccGitlabProjectMirrorConfig (project string ) string {
225
161
return fmt .Sprintf (`
226
- resource "gitlab_project" "foo" {
227
- name = "foo-%d"
228
- description = "Terraform acceptance tests"
229
-
230
- # So that acceptance tests can be run in a gitlab organization
231
- # with no billing
232
- visibility_level = "public"
233
- }
234
-
235
162
resource "gitlab_project_mirror" "foo" {
236
- project = "${gitlab_project.foo.id}"
237
- url = "https://foo:%[email protected] /mirror-%d"
238
- count = 40
163
+ project = %q
164
+ url = "https://example.com/mirror"
239
165
}
240
- ` , rInt , rInt , rInt )
166
+ ` , project )
241
167
}
242
168
243
- func testAccGitlabProjectMirrorConfigWithPassword (rInt int ) string {
169
+ func testAccGitlabProjectMirrorConfigWithPassword (project string ) string {
244
170
return fmt .Sprintf (`
245
- resource "gitlab_project" "foo" {
246
- name = "foo-%d"
247
- description = "Terraform acceptance tests"
248
-
249
- # So that acceptance tests can be run in a gitlab organization
250
- # with no billing
251
- visibility_level = "public"
252
- }
253
-
254
171
resource "gitlab_project_mirror" "foo" {
255
- project = "${gitlab_project.foo.id}"
256
- url = "https://foo:%d @example.com/mirror-%d "
172
+ project = %q
173
+ url = "https://foo:bar @example.com/mirror"
257
174
}
258
- ` , rInt , rInt , rInt )
175
+ ` , project )
259
176
}
260
177
261
- func testAccGitlabProjectMirrorUpdateConfig (rInt int ) string {
178
+ func testAccGitlabProjectMirrorUpdateConfig (project string ) string {
262
179
return fmt .Sprintf (`
263
- resource "gitlab_project" "foo" {
264
- name = "foo-%d"
265
- description = "Terraform acceptance tests"
266
-
267
- # So that acceptance tests can be run in a gitlab organization
268
- # with no billing
269
- visibility_level = "public"
270
- }
271
-
272
180
resource "gitlab_project_mirror" "foo" {
273
- project = "${gitlab_project.foo.id}"
274
- url = "https://example.com/hook-%d "
181
+ project = %q
182
+ url = "https://example.com/mirror "
275
183
enabled = false
276
184
only_protected_branches = false
277
185
keep_divergent_refs = false
278
186
}
279
- ` , rInt , rInt )
187
+ ` , project )
280
188
}
0 commit comments