4
4
package provider
5
5
6
6
import (
7
+ "context"
7
8
"fmt"
8
9
"os"
10
+ "regexp"
9
11
"strconv"
10
12
"testing"
11
13
@@ -25,7 +27,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
25
27
Steps : []resource.TestStep {
26
28
// Create a topic with default options
27
29
{
28
- Config : testAccGitlabTopicRequiredConfig (rInt ),
30
+ Config : testAccGitlabTopicRequiredConfig (t , rInt ),
29
31
Check : resource .ComposeTestCheckFunc (
30
32
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
31
33
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -44,7 +46,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
44
46
},
45
47
// Update the topics values
46
48
{
47
- Config : testAccGitlabTopicFullConfig (rInt ),
49
+ Config : testAccGitlabTopicFullConfig (t , rInt ),
48
50
Check : resource .ComposeTestCheckFunc (
49
51
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
50
52
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -66,7 +68,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
66
68
},
67
69
// Update back to the default topics avatar
68
70
{
69
- Config : testAccGitlabTopicFullConfig (rInt ),
71
+ Config : testAccGitlabTopicFullConfig (t , rInt ),
70
72
Check : resource .ComposeTestCheckFunc (
71
73
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
72
74
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -88,7 +90,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
88
90
},
89
91
// Update the topics avatar
90
92
{
91
- Config : testAccGitlabTopicFullUpdatedAvatarConfig (rInt ),
93
+ Config : testAccGitlabTopicFullUpdatedAvatarConfig (t , rInt ),
92
94
Check : resource .ComposeTestCheckFunc (
93
95
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
94
96
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -110,7 +112,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
110
112
},
111
113
// Update back to the default topics avatar
112
114
{
113
- Config : testAccGitlabTopicFullConfig (rInt ),
115
+ Config : testAccGitlabTopicFullConfig (t , rInt ),
114
116
Check : resource .ComposeTestCheckFunc (
115
117
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
116
118
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -132,7 +134,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
132
134
},
133
135
// Update the avatar image, but keep the filename to test the `CustomizeDiff` function
134
136
{
135
- Config : testAccGitlabTopicFullConfig (rInt ),
137
+ Config : testAccGitlabTopicFullConfig (t , rInt ),
136
138
PreConfig : func () {
137
139
// overwrite the avatar image file
138
140
if err := copyFile ("testdata/gitlab_topic/avatar.png" , "testdata/gitlab_topic/avatar.png.bak" ); err != nil {
@@ -168,7 +170,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
168
170
},
169
171
// Update the topics values back to their initial state
170
172
{
171
- Config : testAccGitlabTopicRequiredConfig (rInt ),
173
+ Config : testAccGitlabTopicRequiredConfig (t , rInt ),
172
174
Check : resource .ComposeTestCheckFunc (
173
175
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
174
176
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -189,7 +191,7 @@ func TestAccGitlabTopic_basic(t *testing.T) {
189
191
},
190
192
// Updating the topic to have a description before it is deleted
191
193
{
192
- Config : testAccGitlabTopicFullConfig (rInt ),
194
+ Config : testAccGitlabTopicFullConfig (t , rInt ),
193
195
Check : resource .ComposeTestCheckFunc (
194
196
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
195
197
testAccCheckGitlabTopicAttributes (& topic , & testAccGitlabTopicExpectedAttributes {
@@ -221,7 +223,7 @@ func TestAccGitlabTopic_withoutAvatarHash(t *testing.T) {
221
223
Steps : []resource.TestStep {
222
224
// Create a topic with avatar, but without giving a hash
223
225
{
224
- Config : testAccGitlabTopicAvatarWithoutHashConfig (rInt ),
226
+ Config : testAccGitlabTopicAvatarWithoutHashConfig (t , rInt ),
225
227
Check : resource .ComposeTestCheckFunc (
226
228
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
227
229
resource .TestCheckResourceAttrSet ("gitlab_topic.foo" , "avatar_url" ),
@@ -230,7 +232,7 @@ func TestAccGitlabTopic_withoutAvatarHash(t *testing.T) {
230
232
},
231
233
// Update the avatar image, but keep the filename to test the `CustomizeDiff` function
232
234
{
233
- Config : testAccGitlabTopicAvatarWithoutHashConfig (rInt ),
235
+ Config : testAccGitlabTopicAvatarWithoutHashConfig (t , rInt ),
234
236
ExpectNonEmptyPlan : true ,
235
237
},
236
238
},
@@ -247,7 +249,7 @@ func TestAccGitlabTopic_softDestroy(t *testing.T) {
247
249
Steps : []resource.TestStep {
248
250
// Create a topic with soft_destroy enabled
249
251
{
250
- Config : testAccGitlabTopicSoftDestroyConfig (rInt ),
252
+ Config : testAccGitlabTopicSoftDestroyConfig (t , rInt ),
251
253
Check : resource .ComposeTestCheckFunc (
252
254
testAccCheckGitlabTopicExists ("gitlab_topic.foo" , & topic ),
253
255
),
@@ -256,6 +258,46 @@ func TestAccGitlabTopic_softDestroy(t *testing.T) {
256
258
})
257
259
}
258
260
261
+ func TestAccGitlabTopic_titleSupport (t * testing.T ) {
262
+ rInt := acctest .RandInt ()
263
+
264
+ resource .Test (t , resource.TestCase {
265
+ ProviderFactories : providerFactories ,
266
+ CheckDestroy : testAccCheckGitlabTopicDestroy ,
267
+ Steps : []resource.TestStep {
268
+ {
269
+ SkipFunc : isGitLabVersionAtLeast (context .TODO (), testGitlabClient , "15.0" ),
270
+ Config : fmt .Sprintf (`
271
+ resource "gitlab_topic" "this" {
272
+ name = "foo-%d"
273
+ title = "Foo-%d"
274
+ }
275
+ ` , rInt , rInt ),
276
+ ExpectError : regexp .MustCompile (`title is not supported by your version of GitLab. At least GitLab 15.0 is required` ),
277
+ },
278
+ {
279
+ SkipFunc : isGitLabVersionLessThan (context .TODO (), testGitlabClient , "15.0" ),
280
+ Config : fmt .Sprintf (`
281
+ resource "gitlab_topic" "this" {
282
+ name = "foo-%d"
283
+ }
284
+ ` , rInt ),
285
+ ExpectError : regexp .MustCompile (`title is a required attribute for GitLab 15.0 and newer. Please specify it in the configuration.` ),
286
+ },
287
+ {
288
+ SkipFunc : isGitLabVersionLessThan (context .TODO (), testGitlabClient , "15.0" ),
289
+ Config : fmt .Sprintf (`
290
+ resource "gitlab_topic" "this" {
291
+ name = "foo-%d"
292
+ title = "Foo-%d"
293
+ }
294
+ ` , rInt , rInt ),
295
+ Check : resource .TestCheckResourceAttr ("gitlab_topic.this" , "title" , fmt .Sprintf ("Foo-%d" , rInt )),
296
+ },
297
+ },
298
+ })
299
+ }
300
+
259
301
func testAccCheckGitlabTopicExists (n string , assign * gitlab.Topic ) resource.TestCheckFunc {
260
302
return func (s * terraform.State ) (err error ) {
261
303
@@ -369,47 +411,73 @@ func testAccCheckGitlabTopicSoftDestroy(s *terraform.State) (err error) {
369
411
return nil
370
412
}
371
413
372
- func testAccGitlabTopicRequiredConfig (rInt int ) string {
414
+ func testAccGitlabTopicRequiredConfig (t * testing.T , rInt int ) string {
415
+ var titleConfig string
416
+ if testAccIsRunningAtLeast (t , "15.0" ) {
417
+ titleConfig = fmt .Sprintf (`title = "Foo Req %d"` , rInt )
418
+ }
419
+
373
420
return fmt .Sprintf (`
374
421
resource "gitlab_topic" "foo" {
375
422
name = "foo-req-%d"
376
- }` , rInt )
423
+ %s
424
+ }` , rInt , titleConfig )
377
425
}
378
426
379
- func testAccGitlabTopicFullConfig (rInt int ) string {
427
+ func testAccGitlabTopicFullConfig (t * testing.T , rInt int ) string {
428
+ var titleConfig string
429
+ if testAccIsRunningAtLeast (t , "15.0" ) {
430
+ titleConfig = fmt .Sprintf (`title = "Foo Req %d"` , rInt )
431
+ }
380
432
return fmt .Sprintf (`
381
433
resource "gitlab_topic" "foo" {
382
434
name = "foo-full-%d"
435
+ %s
383
436
description = "Terraform acceptance tests"
384
437
avatar = "${path.module}/testdata/gitlab_topic/avatar.png"
385
438
avatar_hash = filesha256("${path.module}/testdata/gitlab_topic/avatar.png")
386
- }` , rInt )
439
+ }` , rInt , titleConfig )
387
440
}
388
441
389
- func testAccGitlabTopicFullUpdatedAvatarConfig (rInt int ) string {
442
+ func testAccGitlabTopicFullUpdatedAvatarConfig (t * testing.T , rInt int ) string {
443
+ var titleConfig string
444
+ if testAccIsRunningAtLeast (t , "15.0" ) {
445
+ titleConfig = fmt .Sprintf (`title = "Foo Req %d"` , rInt )
446
+ }
390
447
return fmt .Sprintf (`
391
448
resource "gitlab_topic" "foo" {
392
449
name = "foo-full-%d"
450
+ %s
393
451
description = "Terraform acceptance tests"
394
452
avatar = "${path.module}/testdata/gitlab_topic/avatar-update.png"
395
453
avatar_hash = filesha256("${path.module}/testdata/gitlab_topic/avatar-update.png")
396
- }` , rInt )
454
+ }` , rInt , titleConfig )
397
455
}
398
456
399
- func testAccGitlabTopicAvatarWithoutHashConfig (rInt int ) string {
457
+ func testAccGitlabTopicAvatarWithoutHashConfig (t * testing.T , rInt int ) string {
458
+ var titleConfig string
459
+ if testAccIsRunningAtLeast (t , "15.0" ) {
460
+ titleConfig = fmt .Sprintf (`title = "Foo Req %d"` , rInt )
461
+ }
400
462
return fmt .Sprintf (`
401
463
resource "gitlab_topic" "foo" {
402
464
name = "foo-%d"
465
+ %s
403
466
avatar = "${path.module}/testdata/gitlab_topic/avatar.png"
404
- }` , rInt )
467
+ }` , rInt , titleConfig )
405
468
}
406
469
407
- func testAccGitlabTopicSoftDestroyConfig (rInt int ) string {
470
+ func testAccGitlabTopicSoftDestroyConfig (t * testing.T , rInt int ) string {
471
+ var titleConfig string
472
+ if testAccIsRunningAtLeast (t , "15.0" ) {
473
+ titleConfig = fmt .Sprintf (`title = "Foo Req %d"` , rInt )
474
+ }
408
475
return fmt .Sprintf (`
409
476
resource "gitlab_topic" "foo" {
410
477
name = "foo-soft-destroy-%d"
478
+ %s
411
479
description = "Terraform acceptance tests"
412
480
413
481
soft_destroy = true
414
- }` , rInt )
482
+ }` , rInt , titleConfig )
415
483
}
0 commit comments