Skip to content

Commit 6002c55

Browse files
committed
resource/gitlab_repository_file: Refactor tests to use helper functions
1 parent d18aee4 commit 6002c55

File tree

1 file changed

+48
-136
lines changed

1 file changed

+48
-136
lines changed

internal/provider/resource_gitlab_repository_file_test.go

Lines changed: 48 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
109
gitlab "github.com/xanzy/go-gitlab"
1110
)
1211

13-
func TestAccGitlabRepositoryFile_create(t *testing.T) {
12+
func TestAccGitlabRepositoryFile_basic(t *testing.T) {
13+
testAccCheck(t)
14+
1415
var file gitlab.File
15-
rInt := acctest.RandInt()
16+
testProject := testAccCreateProject(t)
1617

1718
resource.Test(t, resource.TestCase{
1819
PreCheck: func() { testAccPreCheck(t) },
1920
ProviderFactories: providerFactories,
2021
CheckDestroy: testAccCheckGitlabRepositoryFileDestroy,
2122
Steps: []resource.TestStep{
2223
{
23-
Config: testAccGitlabRepositoryFileConfig(rInt),
24+
Config: testAccGitlabRepositoryFileConfig(testProject.ID),
2425
Check: resource.ComposeTestCheckFunc(
2526
testAccCheckGitlabRepositoryFileExists("gitlab_repository_file.this", &file),
2627
testAccCheckGitlabRepositoryFileAttributes(&file, &testAccGitlabRepositoryFileAttributes{
@@ -29,22 +30,47 @@ func TestAccGitlabRepositoryFile_create(t *testing.T) {
2930
}),
3031
),
3132
},
33+
{
34+
ResourceName: "gitlab_repository_file.this",
35+
ImportState: true,
36+
ImportStateVerify: true,
37+
ImportStateVerifyIgnore: []string{"author_email", "author_name", "commit_message"},
38+
},
39+
{
40+
Config: testAccGitlabRepositoryFileUpdateConfig(testProject.ID),
41+
Check: resource.ComposeTestCheckFunc(
42+
testAccCheckGitlabRepositoryFileExists("gitlab_repository_file.this", &file),
43+
testAccCheckGitlabRepositoryFileAttributes(&file, &testAccGitlabRepositoryFileAttributes{
44+
FilePath: "meow.txt",
45+
Content: "bWVvdyBtZW93IG1lb3cgbWVvdyBtZW93Cg==",
46+
}),
47+
),
48+
},
49+
{
50+
ResourceName: "gitlab_repository_file.this",
51+
ImportState: true,
52+
ImportStateVerify: true,
53+
ImportStateVerifyIgnore: []string{"author_email", "author_name", "commit_message"},
54+
},
3255
},
3356
})
3457
}
3558

3659
func TestAccGitlabRepositoryFile_createSameFileDifferentRepository(t *testing.T) {
60+
testAccCheck(t)
61+
3762
var fooFile gitlab.File
3863
var barFile gitlab.File
39-
rInt := acctest.RandInt()
64+
firstTestProject := testAccCreateProject(t)
65+
secondTestProject := testAccCreateProject(t)
4066

4167
resource.Test(t, resource.TestCase{
4268
PreCheck: func() { testAccPreCheck(t) },
4369
ProviderFactories: providerFactories,
4470
CheckDestroy: testAccCheckGitlabRepositoryFileDestroy,
4571
Steps: []resource.TestStep{
4672
{
47-
Config: testAccGitlabRepositoryFileSameFileDifferentRepositoryConfig(rInt),
73+
Config: testAccGitlabRepositoryFileSameFileDifferentRepositoryConfig(firstTestProject.ID, secondTestProject.ID),
4874
Check: resource.ComposeTestCheckFunc(
4975
testAccCheckGitlabRepositoryFileExists("gitlab_repository_file.foo_file", &fooFile),
5076
testAccCheckGitlabRepositoryFileAttributes(&fooFile, &testAccGitlabRepositoryFileAttributes{
@@ -112,39 +138,18 @@ func TestAccGitlabRepositoryFile_validationOfBase64Content(t *testing.T) {
112138
}
113139

114140
func TestAccGitlabRepositoryFile_createOnNewBranch(t *testing.T) {
115-
var file gitlab.File
116-
rInt := acctest.RandInt()
117-
118-
resource.Test(t, resource.TestCase{
119-
PreCheck: func() { testAccPreCheck(t) },
120-
ProviderFactories: providerFactories,
121-
CheckDestroy: testAccCheckGitlabRepositoryFileDestroy,
122-
Steps: []resource.TestStep{
123-
{
124-
Config: testAccGitlabRepositoryFileStartBranchConfig(rInt),
125-
Check: resource.ComposeTestCheckFunc(
126-
testAccCheckGitlabRepositoryFileExists("gitlab_repository_file.this", &file),
127-
testAccCheckGitlabRepositoryFileAttributes(&file, &testAccGitlabRepositoryFileAttributes{
128-
FilePath: "meow.txt",
129-
Content: "bWVvdyBtZW93IG1lb3c=",
130-
}),
131-
),
132-
},
133-
},
134-
})
135-
}
141+
testAccCheck(t)
136142

137-
func TestAccGitlabRepositoryFile_update(t *testing.T) {
138143
var file gitlab.File
139-
rInt := acctest.RandInt()
144+
testProject := testAccCreateProject(t)
140145

141146
resource.Test(t, resource.TestCase{
142147
PreCheck: func() { testAccPreCheck(t) },
143148
ProviderFactories: providerFactories,
144149
CheckDestroy: testAccCheckGitlabRepositoryFileDestroy,
145150
Steps: []resource.TestStep{
146151
{
147-
Config: testAccGitlabRepositoryFileConfig(rInt),
152+
Config: testAccGitlabRepositoryFileStartBranchConfig(testProject.ID),
148153
Check: resource.ComposeTestCheckFunc(
149154
testAccCheckGitlabRepositoryFileExists("gitlab_repository_file.this", &file),
150155
testAccCheckGitlabRepositoryFileAttributes(&file, &testAccGitlabRepositoryFileAttributes{
@@ -153,39 +158,6 @@ func TestAccGitlabRepositoryFile_update(t *testing.T) {
153158
}),
154159
),
155160
},
156-
{
157-
Config: testAccGitlabRepositoryFileUpdateConfig(rInt),
158-
Check: resource.ComposeTestCheckFunc(
159-
testAccCheckGitlabRepositoryFileExists("gitlab_repository_file.this", &file),
160-
testAccCheckGitlabRepositoryFileAttributes(&file, &testAccGitlabRepositoryFileAttributes{
161-
FilePath: "meow.txt",
162-
Content: "bWVvdyBtZW93IG1lb3cgbWVvdyBtZW93Cg==",
163-
}),
164-
),
165-
},
166-
},
167-
})
168-
}
169-
170-
// lintignore: AT002 // TODO: Resolve this tfproviderlint issue
171-
func TestAccGitlabRepositoryFile_import(t *testing.T) {
172-
rInt := acctest.RandInt()
173-
resourceName := "gitlab_repository_file.this"
174-
175-
resource.Test(t, resource.TestCase{
176-
PreCheck: func() { testAccPreCheck(t) },
177-
ProviderFactories: providerFactories,
178-
CheckDestroy: testAccCheckGitlabPipelineTriggerDestroy,
179-
Steps: []resource.TestStep{
180-
{
181-
Config: testAccGitlabRepositoryFileConfig(rInt),
182-
},
183-
{
184-
ResourceName: resourceName,
185-
ImportState: true,
186-
ImportStateVerify: true,
187-
ImportStateVerifyIgnore: []string{"author_email", "author_name", "commit_message"},
188-
},
189161
},
190162
})
191163
}
@@ -266,48 +238,24 @@ func testAccCheckGitlabRepositoryFileDestroy(s *terraform.State) error {
266238
return nil
267239
}
268240

269-
func testAccGitlabRepositoryFileConfig(rInt int) string {
241+
func testAccGitlabRepositoryFileConfig(projectID int) string {
270242
return fmt.Sprintf(`
271-
resource "gitlab_project" "foo" {
272-
name = "foo-%d"
273-
description = "Terraform acceptance tests"
274-
275-
default_branch = "main"
276-
277-
# So that acceptance tests can be run in a gitlab organization
278-
# with no billing
279-
visibility_level = "public"
280-
initialize_with_readme = true
281-
}
282-
283243
resource "gitlab_repository_file" "this" {
284-
project = "${gitlab_project.foo.id}"
244+
project = %d
285245
file_path = "meow.txt"
286246
branch = "main"
287247
content = "bWVvdyBtZW93IG1lb3c="
288248
author_email = "[email protected]"
289249
author_name = "Meow Meowington"
290250
commit_message = "feature: add launch codes"
291251
}
292-
`, rInt)
252+
`, projectID)
293253
}
294254

295-
func testAccGitlabRepositoryFileStartBranchConfig(rInt int) string {
255+
func testAccGitlabRepositoryFileStartBranchConfig(projectID int) string {
296256
return fmt.Sprintf(`
297-
resource "gitlab_project" "foo" {
298-
name = "foo-%d"
299-
description = "Terraform acceptance tests"
300-
301-
default_branch = "main"
302-
303-
# So that acceptance tests can be run in a gitlab organization
304-
# with no billing
305-
visibility_level = "public"
306-
initialize_with_readme = true
307-
}
308-
309257
resource "gitlab_repository_file" "this" {
310-
project = "${gitlab_project.foo.id}"
258+
project = %d
311259
file_path = "meow.txt"
312260
branch = "meow-branch"
313261
start_branch = "main"
@@ -316,63 +264,27 @@ resource "gitlab_repository_file" "this" {
316264
author_name = "Meow Meowington"
317265
commit_message = "feature: add launch codes"
318266
}
319-
`, rInt)
267+
`, projectID)
320268
}
321269

322-
func testAccGitlabRepositoryFileUpdateConfig(rInt int) string {
270+
func testAccGitlabRepositoryFileUpdateConfig(projectID int) string {
323271
return fmt.Sprintf(`
324-
resource "gitlab_project" "foo" {
325-
name = "foo-%d"
326-
description = "Terraform acceptance tests"
327-
328-
default_branch = "main"
329-
330-
# So that acceptance tests can be run in a gitlab organization
331-
# with no billing
332-
visibility_level = "public"
333-
initialize_with_readme = true
334-
}
335-
336272
resource "gitlab_repository_file" "this" {
337-
project = "${gitlab_project.foo.id}"
273+
project = %d
338274
file_path = "meow.txt"
339275
branch = "main"
340276
content = "bWVvdyBtZW93IG1lb3cgbWVvdyBtZW93Cg=="
341277
author_email = "[email protected]"
342278
author_name = "Meow Meowington"
343279
commit_message = "feature: change launch codes"
344280
}
345-
`, rInt)
281+
`, projectID)
346282
}
347283

348-
func testAccGitlabRepositoryFileSameFileDifferentRepositoryConfig(rInt int) string {
284+
func testAccGitlabRepositoryFileSameFileDifferentRepositoryConfig(firstProjectID, secondProjectID int) string {
349285
return fmt.Sprintf(`
350-
resource "gitlab_project" "foo" {
351-
name = "foo-%d"
352-
description = "Terraform acceptance tests"
353-
354-
default_branch = "main"
355-
356-
# So that acceptance tests can be run in a gitlab organization
357-
# with no billing
358-
visibility_level = "public"
359-
initialize_with_readme = true
360-
}
361-
362-
resource "gitlab_project" "bar" {
363-
name = "bar-%d"
364-
description = "Terraform acceptance tests"
365-
366-
default_branch = "main"
367-
368-
# So that acceptance tests can be run in a gitlab organization
369-
# with no billing
370-
visibility_level = "public"
371-
initialize_with_readme = true
372-
}
373-
374286
resource "gitlab_repository_file" "foo_file" {
375-
project = "${gitlab_project.foo.id}"
287+
project = %d
376288
file_path = "meow.txt"
377289
branch = "main"
378290
content = "bWVvdyBtZW93IG1lb3c="
@@ -382,15 +294,15 @@ resource "gitlab_repository_file" "foo_file" {
382294
}
383295
384296
resource "gitlab_repository_file" "bar_file" {
385-
project = "${gitlab_project.bar.id}"
297+
project = %d
386298
file_path = "meow.txt"
387299
branch = "main"
388300
content = "bWVvdyBtZW93IG1lb3c="
389301
author_email = "[email protected]"
390302
author_name = "Meow Meowington"
391303
commit_message = "feature: add launch codes"
392304
}
393-
`, rInt, rInt)
305+
`, firstProjectID, secondProjectID)
394306
}
395307

396308
func testAccGitlabRepositoryFileConcurrentResourcesConfig(projectID int) string {

0 commit comments

Comments
 (0)