Skip to content

Commit 1890b2e

Browse files
committed
Add common test helpers for setting up test data
1 parent d019ff4 commit 1890b2e

File tree

4 files changed

+262
-23
lines changed

4 files changed

+262
-23
lines changed

gitlab/helper_test.go

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ package gitlab
33
import (
44
"errors"
55
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
"github.com/onsi/gomega"
9+
"os"
610
"strings"
11+
"testing"
712

813
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
914
"github.com/xanzy/go-gitlab"
1015
)
1116

17+
func init() {
18+
// We are using the gomega package for its matchers only, but it requires us to register a handler anyway.
19+
gomega.RegisterFailHandler(func(_ string, _ ...int) {
20+
panic("gomega fail handler should not be used") // lintignore: R009
21+
})
22+
}
23+
1224
// testAccCompareGitLabAttribute compares an attribute in two ResourceData's for
1325
// equivalency.
1426
func testAccCompareGitLabAttribute(attr string, expected, received *schema.ResourceData) error {
@@ -51,3 +63,203 @@ func isRunningInCE() (bool, error) {
5163
isEE, err := isRunningInEE()
5264
return !isEE, err
5365
}
66+
67+
// testAccCheck is a test helper that skips the current test if it is not an acceptance test.
68+
func testAccCheck(t *testing.T) {
69+
t.Helper()
70+
71+
if os.Getenv(resource.TestEnvVar) == "" {
72+
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", resource.TestEnvVar))
73+
}
74+
}
75+
76+
// testAccNewClient is a test helper that initializes a gitlab.Client to use in tests.
77+
// This is preferable to using the provider metadata, which can cause unexpected behavior and breaks encapsulation.
78+
func testAccNewClient(t *testing.T) *gitlab.Client {
79+
t.Helper()
80+
81+
var options []gitlab.ClientOptionFunc
82+
baseURL := os.Getenv("GITLAB_BASE_URL")
83+
if baseURL != "" {
84+
options = append(options, gitlab.WithBaseURL(baseURL))
85+
}
86+
87+
client, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), options...)
88+
if err != nil {
89+
t.Fatalf("could not initialize test client: %v", err)
90+
}
91+
92+
return client
93+
}
94+
95+
// testAccCheckEE is a test helper that skips the current test if the GitLab version is not GitLab Enterprise.
96+
// This is useful when the version needs to be checked during setup, before the Terraform acceptance test starts.
97+
func testAccCheckEE(t *testing.T, client *gitlab.Client) {
98+
t.Helper()
99+
100+
version, _, err := client.Version.GetVersion()
101+
if err != nil {
102+
t.Fatalf("could not check GitLab version: %v", err)
103+
}
104+
105+
if !strings.HasSuffix(version.Version, "-ee") {
106+
t.Skipf("Test is skipped for non-Enterprise version of GitLab (was %q)", version.String())
107+
}
108+
}
109+
110+
// testAccCurrentUser is a test helper for getting the current user of the provided client.
111+
func testAccCurrentUser(t *testing.T, client *gitlab.Client) *gitlab.User {
112+
t.Helper()
113+
114+
user, _, err := client.Users.CurrentUser()
115+
if err != nil {
116+
t.Fatalf("could not get current user: %v", err)
117+
}
118+
119+
return user
120+
}
121+
122+
// testAccCreateGroups is a test helper for creating a project.
123+
func testAccCreateProject(t *testing.T, client *gitlab.Client) *gitlab.Project {
124+
t.Helper()
125+
126+
project, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
127+
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
128+
Description: gitlab.String("Terraform acceptance tests"),
129+
// So that acceptance tests can be run in a gitlab organization with no billing.
130+
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
131+
// So that a branch is created.
132+
InitializeWithReadme: gitlab.Bool(true),
133+
})
134+
if err != nil {
135+
t.Fatalf("could not create test project: %v", err)
136+
}
137+
138+
t.Cleanup(func() {
139+
if _, err := client.Projects.DeleteProject(project.ID); err != nil {
140+
t.Fatalf("could not cleanup test project: %v", err)
141+
}
142+
})
143+
144+
return project
145+
}
146+
147+
// testAccCreateUsers is a test helper for creating a specified number of users.
148+
func testAccCreateUsers(t *testing.T, client *gitlab.Client, n int) []*gitlab.User {
149+
t.Helper()
150+
151+
users := make([]*gitlab.User, n)
152+
153+
for i := range users {
154+
var err error
155+
username := acctest.RandomWithPrefix("acctest-user")
156+
users[i], _, err = client.Users.CreateUser(&gitlab.CreateUserOptions{
157+
Name: gitlab.String(username),
158+
Username: gitlab.String(username),
159+
Email: gitlab.String(username + "@example.com"),
160+
Password: gitlab.String(acctest.RandString(16)),
161+
SkipConfirmation: gitlab.Bool(true),
162+
})
163+
if err != nil {
164+
t.Fatalf("could not create test user: %v", err)
165+
}
166+
167+
userID := users[i].ID // Needed for closure.
168+
t.Cleanup(func() {
169+
if _, err := client.Users.DeleteUser(userID); err != nil {
170+
t.Fatalf("could not cleanup test user: %v", err)
171+
}
172+
})
173+
}
174+
175+
return users
176+
}
177+
178+
// testAccCreateGroups is a test helper for creating a specified number of groups.
179+
func testAccCreateGroups(t *testing.T, client *gitlab.Client, n int) []*gitlab.Group {
180+
t.Helper()
181+
182+
groups := make([]*gitlab.Group, n)
183+
184+
for i := range groups {
185+
var err error
186+
name := acctest.RandomWithPrefix("acctest-group")
187+
groups[i], _, err = client.Groups.CreateGroup(&gitlab.CreateGroupOptions{
188+
Name: gitlab.String(name),
189+
Path: gitlab.String(name),
190+
// So that acceptance tests can be run in a gitlab organization with no billing.
191+
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
192+
})
193+
if err != nil {
194+
t.Fatalf("could not create test group: %v", err)
195+
}
196+
197+
groupID := groups[i].ID // Needed for closure.
198+
t.Cleanup(func() {
199+
if _, err := client.Groups.DeleteGroup(groupID); err != nil {
200+
t.Fatalf("could not cleanup test group: %v", err)
201+
}
202+
})
203+
}
204+
205+
return groups
206+
}
207+
208+
// testAccCreateProtectedBranches is a test helper for creating a specified number of protected branches.
209+
// It assumes the project will be destroyed at the end of the test and will not cleanup created branches.
210+
func testAccCreateProtectedBranches(t *testing.T, client *gitlab.Client, project *gitlab.Project, n int) []*gitlab.ProtectedBranch {
211+
t.Helper()
212+
213+
protectedBranches := make([]*gitlab.ProtectedBranch, n)
214+
215+
for i := range protectedBranches {
216+
branch, _, err := client.Branches.CreateBranch(project.ID, &gitlab.CreateBranchOptions{
217+
Branch: gitlab.String(acctest.RandomWithPrefix("acctest")),
218+
Ref: gitlab.String(project.DefaultBranch),
219+
})
220+
if err != nil {
221+
t.Fatalf("could not create test branch: %v", err)
222+
}
223+
224+
protectedBranches[i], _, err = client.ProtectedBranches.ProtectRepositoryBranches(project.ID, &gitlab.ProtectRepositoryBranchesOptions{
225+
Name: gitlab.String(branch.Name),
226+
})
227+
if err != nil {
228+
t.Fatalf("could not protect test branch: %v", err)
229+
}
230+
}
231+
232+
return protectedBranches
233+
}
234+
235+
// testAccAddProjectMembers is a test helper for adding users as members of a project.
236+
// It assumes the project will be destroyed at the end of the test and will not cleanup members.
237+
func testAccAddProjectMembers(t *testing.T, client *gitlab.Client, pid interface{}, users []*gitlab.User) {
238+
t.Helper()
239+
240+
for _, user := range users {
241+
_, _, err := client.ProjectMembers.AddProjectMember(pid, &gitlab.AddProjectMemberOptions{
242+
UserID: user.ID,
243+
AccessLevel: gitlab.AccessLevel(gitlab.DeveloperPermissions),
244+
})
245+
if err != nil {
246+
t.Fatalf("could not add test project member: %v", err)
247+
}
248+
}
249+
}
250+
251+
// testAccAddGroupMembers is a test helper for adding users as members of a group.
252+
// It assumes the group will be destroyed at the end of the test and will not cleanup members.
253+
func testAccAddGroupMembers(t *testing.T, client *gitlab.Client, gid interface{}, users []*gitlab.User) {
254+
t.Helper()
255+
256+
for _, user := range users {
257+
_, _, err := client.GroupMembers.AddGroupMember(gid, &gitlab.AddGroupMemberOptions{
258+
UserID: gitlab.Int(user.ID),
259+
AccessLevel: gitlab.AccessLevel(gitlab.DeveloperPermissions),
260+
})
261+
if err != nil {
262+
t.Fatalf("could not add test group member: %v", err)
263+
}
264+
}
265+
}

gitlab/resource_gitlab_project_variable_test.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package gitlab
22

33
import (
44
"fmt"
5-
"os"
65
"regexp"
76
"strconv"
87
"testing"
@@ -31,21 +30,9 @@ func (c testAccGitlabProjectContext) finish() {
3130
// testAccGitlabProjectStart initializes the GitLab client and creates a test project. Remember to
3231
// call testAccGitlabProjectContext.finish() when finished with the testAccGitlabProjectContext.
3332
func testAccGitlabProjectStart(t *testing.T) testAccGitlabProjectContext {
34-
if os.Getenv(resource.TestEnvVar) == "" {
35-
t.Skip(fmt.Sprintf("Acceptance tests skipped unless env '%s' set", resource.TestEnvVar))
36-
return testAccGitlabProjectContext{}
37-
}
38-
39-
var options []gitlab.ClientOptionFunc
40-
baseURL := os.Getenv("GITLAB_BASE_URL")
41-
if baseURL != "" {
42-
options = append(options, gitlab.WithBaseURL(baseURL))
43-
}
33+
testAccCheck(t)
4434

45-
client, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), options...)
46-
if err != nil {
47-
t.Fatal(err)
48-
}
35+
client := testAccNewClient(t)
4936

5037
project, _, err := client.Projects.CreateProject(&gitlab.CreateProjectOptions{
5138
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require (
77
github.com/hashicorp/go-retryablehttp v0.7.0
88
github.com/hashicorp/terraform-plugin-sdk v1.16.1
99
github.com/mitchellh/hashstructure v1.0.0
10+
github.com/onsi/gomega v1.14.0
1011
github.com/xanzy/go-gitlab v0.50.0
1112
)

0 commit comments

Comments
 (0)