Skip to content

Commit 327684e

Browse files
authored
Move testAccGitlabProjectContext to helper_test.go and use it in resource_gitlab_project_mirror_test.go (#795)
1 parent f3b0f31 commit 327684e

File tree

3 files changed

+96
-180
lines changed

3 files changed

+96
-180
lines changed

gitlab/helper_test.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package gitlab
33
import (
44
"errors"
55
"fmt"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8-
"github.com/onsi/gomega"
96
"os"
107
"strings"
118
"testing"
129

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1312
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/onsi/gomega"
1414
"github.com/xanzy/go-gitlab"
1515
)
1616

@@ -263,3 +263,53 @@ func testAccAddGroupMembers(t *testing.T, client *gitlab.Client, gid interface{}
263263
}
264264
}
265265
}
266+
267+
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
268+
// acceptance test.
269+
type testAccGitlabProjectContext struct {
270+
t *testing.T
271+
client *gitlab.Client
272+
project *gitlab.Project
273+
}
274+
275+
// testAccGitlabProjectStart initializes the GitLab client and creates a test project. Remember to
276+
// call testAccGitlabProjectContext.finish() when finished with the testAccGitlabProjectContext.
277+
func testAccGitlabProjectStart(t *testing.T) testAccGitlabProjectContext {
278+
if os.Getenv(resource.TestEnvVar) == "" {
279+
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", resource.TestEnvVar))
280+
return testAccGitlabProjectContext{}
281+
}
282+
283+
var options []gitlab.ClientOptionFunc
284+
baseURL := os.Getenv("GITLAB_BASE_URL")
285+
if baseURL != "" {
286+
options = append(options, gitlab.WithBaseURL(baseURL))
287+
}
288+
289+
client, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), options...)
290+
if err != nil {
291+
t.Fatal(err)
292+
}
293+
294+
project, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
295+
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
296+
Description: gitlab.String("Terraform acceptance tests"),
297+
// So that acceptance tests can be run in a gitlab organization with no billing
298+
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
299+
})
300+
if err != nil {
301+
t.Fatalf("could not create test project: %v", err)
302+
}
303+
304+
t.Cleanup(func() {
305+
if _, err := client.Projects.DeleteProject(project.ID); err != nil {
306+
t.Fatalf("could not delete test project: %v", err)
307+
}
308+
})
309+
310+
return testAccGitlabProjectContext{
311+
t: t,
312+
client: client,
313+
project: project,
314+
}
315+
}

gitlab/resource_gitlab_project_mirror_test.go

Lines changed: 43 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -7,113 +7,80 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1312
"github.com/xanzy/go-gitlab"
1413
)
1514

1615
func TestAccGitlabProjectMirror_basic(t *testing.T) {
17-
var hook gitlab.ProjectMirror
18-
rInt := acctest.RandInt()
16+
ctx := testAccGitlabProjectStart(t)
17+
var miror gitlab.ProjectMirror
1918

2019
resource.Test(t, resource.TestCase{
2120
PreCheck: func() { testAccPreCheck(t) },
2221
Providers: testAccProviders,
2322
CheckDestroy: testAccCheckGitlabProjectMirrorDestroy,
2423
Steps: []resource.TestStep{
25-
// Create a project and hook with default options
24+
// Create with default options
2625
{
27-
Config: testAccGitlabProjectMirrorConfig(rInt),
26+
Config: testAccGitlabProjectMirrorConfig(ctx.project.PathWithNamespace),
2827
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",
3231
Enabled: true,
3332
OnlyProtectedBranches: true,
3433
KeepDivergentRefs: true,
3534
}),
3635
),
3736
},
38-
// Update the project hook to toggle all the values to their inverse
37+
// Update to toggle all the values to their inverse
3938
{
40-
Config: testAccGitlabProjectMirrorUpdateConfig(rInt),
39+
Config: testAccGitlabProjectMirrorUpdateConfig(ctx.project.PathWithNamespace),
4140
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",
4544
Enabled: false,
4645
OnlyProtectedBranches: false,
4746
KeepDivergentRefs: false,
4847
}),
4948
),
5049
},
51-
// Update the project hook to toggle the options back
50+
// Update to toggle the options back
5251
{
53-
Config: testAccGitlabProjectMirrorConfig(rInt),
52+
Config: testAccGitlabProjectMirrorConfig(ctx.project.PathWithNamespace),
5453
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",
5857
Enabled: true,
5958
OnlyProtectedBranches: true,
6059
KeepDivergentRefs: true,
6160
}),
6261
),
6362
},
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
9464
{
95-
Config: testAccGitlabProjectMirrorConfigWithCount(rInt),
65+
ResourceName: "gitlab_project_mirror.foo",
66+
ImportState: true,
67+
ImportStateVerify: true,
9668
},
9769
},
9870
})
9971
}
10072

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)
10475

10576
resource.Test(t, resource.TestCase{
10677
PreCheck: func() { testAccPreCheck(t) },
10778
Providers: testAccProviders,
10879
CheckDestroy: testAccCheckGitlabProjectMirrorDestroy,
10980
Steps: []resource.TestStep{
81+
// Create a project and mirror with a username / password.
11082
{
111-
Config: testAccGitlabProjectMirrorConfig(rInt),
112-
},
113-
{
114-
ResourceName: "gitlab_project_mirror.foo",
115-
ImportState: true,
116-
ImportStateVerify: true,
83+
Config: testAccGitlabProjectMirrorConfigWithPassword(ctx.project.PathWithNamespace),
11784
},
11885
},
11986
})
@@ -180,101 +147,42 @@ func testAccCheckGitlabProjectMirrorAttributes(mirror *gitlab.ProjectMirror, wan
180147
}
181148

182149
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")
202156
}
203157
return nil
204158
}
205159

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 {
225161
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-
235162
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"
239165
}
240-
`, rInt, rInt, rInt)
166+
`, project)
241167
}
242168

243-
func testAccGitlabProjectMirrorConfigWithPassword(rInt int) string {
169+
func testAccGitlabProjectMirrorConfigWithPassword(project string) string {
244170
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-
254171
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"
257174
}
258-
`, rInt, rInt, rInt)
175+
`, project)
259176
}
260177

261-
func testAccGitlabProjectMirrorUpdateConfig(rInt int) string {
178+
func testAccGitlabProjectMirrorUpdateConfig(project string) string {
262179
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-
272180
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"
275183
enabled = false
276184
only_protected_branches = false
277185
keep_divergent_refs = false
278186
}
279-
`, rInt, rInt)
187+
`, project)
280188
}

gitlab/resource_gitlab_project_variable_test.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,11 @@ import (
66
"strconv"
77
"testing"
88

9-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1211
"github.com/xanzy/go-gitlab"
1312
)
1413

15-
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
16-
// acceptance test.
17-
type testAccGitlabProjectContext struct {
18-
t *testing.T
19-
client *gitlab.Client
20-
project *gitlab.Project
21-
}
22-
23-
// finish deletes the test project. Call this when the test is finished, usually in a defer.
24-
func (c testAccGitlabProjectContext) finish() {
25-
if _, err := c.client.Projects.DeleteProject(c.project.ID); err != nil {
26-
c.t.Fatalf("could not delete test project: %v", err)
27-
}
28-
}
29-
30-
// testAccGitlabProjectStart initializes the GitLab client and creates a test project. Remember to
31-
// call testAccGitlabProjectContext.finish() when finished with the testAccGitlabProjectContext.
32-
func testAccGitlabProjectStart(t *testing.T) testAccGitlabProjectContext {
33-
testAccCheck(t)
34-
35-
client := testAccNewClient(t)
36-
37-
project, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
38-
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
39-
Description: gitlab.String("Terraform acceptance tests"),
40-
// So that acceptance tests can be run in a gitlab organization with no billing
41-
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
42-
})
43-
if err != nil {
44-
t.Fatalf("could not create test project: %v", err)
45-
}
46-
47-
return testAccGitlabProjectContext{
48-
t: t,
49-
client: client,
50-
project: project,
51-
}
52-
}
53-
5414
func testAccCheckGitlabProjectVariableExists(client *gitlab.Client, name string) resource.TestCheckFunc {
5515
var (
5616
key string
@@ -110,7 +70,6 @@ func testAccGitlabProjectVariableCheckAllVariablesDestroyed(ctx testAccGitlabPro
11070

11171
func TestAccGitlabProjectVariable_basic(t *testing.T) {
11272
ctx := testAccGitlabProjectStart(t)
113-
defer ctx.finish()
11473

11574
resource.Test(t, resource.TestCase{
11675
PreCheck: func() { testAccPreCheck(t) },
@@ -213,7 +172,6 @@ EOF
213172

214173
func TestAccGitlabProjectVariable_scoped(t *testing.T) {
215174
ctx := testAccGitlabProjectStart(t)
216-
defer ctx.finish()
217175

218176
resource.Test(t, resource.TestCase{
219177
PreCheck: func() { testAccPreCheck(t) },

0 commit comments

Comments
 (0)