-
Notifications
You must be signed in to change notification settings - Fork 323
/
helper_test.go
577 lines (485 loc) · 17.9 KB
/
helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//go:build acceptance
// +build acceptance
package provider
import (
"context"
"fmt"
"io"
"os"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/onsi/gomega"
"github.com/xanzy/go-gitlab"
)
type SkipFunc = func() (bool, error)
func init() {
// We are using the gomega package for its matchers only, but it requires us to register a handler anyway.
gomega.RegisterFailHandler(func(_ string, _ ...int) {
panic("gomega fail handler should not be used") // lintignore: R009
})
}
// testAccCompareGitLabAttribute compares an attribute in two ResourceData's for
// equivalency.
func testAccCompareGitLabAttribute(attr string, expected, received *schema.ResourceData) error {
e := expected.Get(attr)
r := received.Get(attr)
switch e.(type) { // nolint // TODO: Resolve this golangci-lint issue: S1034: assigning the result of this type assertion to a variable (switch e := e.(type)) could eliminate type assertions in switch cases (gosimple)
case *schema.Set:
if !e.(*schema.Set).Equal(r) { // nolint // TODO: Resolve this golangci-lint issue: S1034(related information): could eliminate this type assertion (gosimple)
return fmt.Errorf(`attribute set %s expected "%+v" received "%+v"`, attr, e, r)
}
default:
// Stringify to check because of type differences
if fmt.Sprintf("%v", e) != fmt.Sprintf("%v", r) {
return fmt.Errorf(`attribute %s expected "%+v" received "%+v"`, attr, e, r)
}
}
return nil
}
// Returns true if the acceptance test is running Gitlab EE.
// Meant to be used as SkipFunc to skip tests that work only on Gitlab CE.
func isRunningInEE() (bool, error) {
version, _, err := testGitlabClient.Version.GetVersion()
if err != nil {
return false, err
}
if strings.Contains(version.String(), "-ee") {
return true, nil
}
return false, nil
}
// Returns true if the acceptance test is running Gitlab CE.
// Meant to be used as SkipFunc to skip tests that work only on Gitlab EE.
func isRunningInCE() (bool, error) {
isEE, err := isRunningInEE()
return !isEE, err
}
// testAccCheckEE is a test helper that skips the current test if the GitLab version is not GitLab Enterprise.
// This is useful when the version needs to be checked during setup, before the Terraform acceptance test starts.
func testAccCheckEE(t *testing.T) {
t.Helper()
version, _, err := testGitlabClient.Version.GetVersion()
if err != nil {
t.Fatalf("could not check GitLab version: %v", err)
}
if !strings.HasSuffix(version.Version, "-ee") {
t.Skipf("Test is skipped for non-Enterprise version of GitLab (was %q)", version.String())
}
}
func testAccRequiresLessThan(t *testing.T, requiredMaxVersion string) {
isLessThan, err := isGitLabVersionLessThan(context.TODO(), testGitlabClient, requiredMaxVersion)()
if err != nil {
t.Fatalf("Failed to fetch GitLab version: %+v", err)
}
if !isLessThan {
t.Skipf("This test is only valid for GitLab versions less than %s", requiredMaxVersion)
}
}
func testAccRequiresAtLeast(t *testing.T, requiredMinVersion string) {
isAtLeast, err := isGitLabVersionAtLeast(context.TODO(), testGitlabClient, requiredMinVersion)()
if err != nil {
t.Fatalf("Failed to fetch GitLab version: %+v", err)
}
if !isAtLeast {
t.Skipf("This test is only valid for GitLab versions newer than %s", requiredMinVersion)
}
}
func testAccIsRunningAtLeast(t *testing.T, requiredMinVersion string) bool {
isAtLeast, err := isGitLabVersionAtLeast(context.TODO(), testGitlabClient, requiredMinVersion)()
if err != nil {
t.Fatalf("Failed to fetch GitLab version: %+v", err)
}
return isAtLeast
}
// testAccCurrentUser is a test helper for getting the current user of the provided client.
func testAccCurrentUser(t *testing.T) *gitlab.User {
t.Helper()
user, _, err := testGitlabClient.Users.CurrentUser()
if err != nil {
t.Fatalf("could not get current user: %v", err)
}
return user
}
// testAccCreateProject is a test helper for creating a project.
func testAccCreateProject(t *testing.T) *gitlab.Project {
return testAccCreateProjectWithNamespace(t, 0)
}
// testAccCreateProjectWithNamespace is a test helper for creating a project. This method accepts a namespace to great a project
// within a group
func testAccCreateProjectWithNamespace(t *testing.T, namespaceID int) *gitlab.Project {
t.Helper()
options := &gitlab.CreateProjectOptions{
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
Description: gitlab.String("Terraform acceptance tests"),
// So that acceptance tests can be run in a gitlab organization with no billing.
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
// So that a branch is created.
InitializeWithReadme: gitlab.Bool(true),
}
//Apply a namespace if one is passed in.
if namespaceID != 0 {
options.NamespaceID = gitlab.Int(namespaceID)
}
project, _, err := testGitlabClient.Projects.CreateProject(options)
if err != nil {
t.Fatalf("could not create test project: %v", err)
}
t.Cleanup(func() {
if _, err := testGitlabClient.Projects.DeleteProject(project.ID); err != nil {
t.Fatalf("could not cleanup test project: %v", err)
}
})
return project
}
// testAccCreateUsers is a test helper for creating a specified number of users.
func testAccCreateUsers(t *testing.T, n int) []*gitlab.User {
t.Helper()
users := make([]*gitlab.User, n)
for i := range users {
var err error
username := acctest.RandomWithPrefix("acctest-user")
users[i], _, err = testGitlabClient.Users.CreateUser(&gitlab.CreateUserOptions{
Name: gitlab.String(username),
Username: gitlab.String(username),
Email: gitlab.String(username + "@example.com"),
Password: gitlab.String(acctest.RandString(16)),
SkipConfirmation: gitlab.Bool(true),
})
if err != nil {
t.Fatalf("could not create test user: %v", err)
}
userID := users[i].ID // Needed for closure.
t.Cleanup(func() {
if _, err := testGitlabClient.Users.DeleteUser(userID); err != nil {
t.Fatalf("could not cleanup test user: %v", err)
}
})
}
return users
}
// testAccCreateGroups is a test helper for creating a specified number of groups.
func testAccCreateGroups(t *testing.T, n int) []*gitlab.Group {
t.Helper()
groups := make([]*gitlab.Group, n)
for i := range groups {
var err error
name := acctest.RandomWithPrefix("acctest-group")
groups[i], _, err = testGitlabClient.Groups.CreateGroup(&gitlab.CreateGroupOptions{
Name: gitlab.String(name),
Path: gitlab.String(name),
// So that acceptance tests can be run in a gitlab organization with no billing.
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
})
if err != nil {
t.Fatalf("could not create test group: %v", err)
}
groupID := groups[i].ID // Needed for closure.
t.Cleanup(func() {
if _, err := testGitlabClient.Groups.DeleteGroup(groupID); err != nil {
t.Fatalf("could not cleanup test group: %v", err)
}
})
}
return groups
}
// testAccCreateBranches is a test helper for creating a specified number of branches.
// It assumes the project will be destroyed at the end of the test and will not cleanup created branches.
func testAccCreateBranches(t *testing.T, project *gitlab.Project, n int) []*gitlab.Branch {
t.Helper()
branches := make([]*gitlab.Branch, n)
for i := range branches {
var err error
branches[i], _, err = testGitlabClient.Branches.CreateBranch(project.ID, &gitlab.CreateBranchOptions{
Branch: gitlab.String(acctest.RandomWithPrefix("acctest")),
Ref: gitlab.String(project.DefaultBranch),
})
if err != nil {
t.Fatalf("could not create test branches: %v", err)
}
}
return branches
}
// testAccCreateProtectedBranches is a test helper for creating a specified number of protected branches.
// It assumes the project will be destroyed at the end of the test and will not cleanup created branches.
func testAccCreateProtectedBranches(t *testing.T, project *gitlab.Project, n int) []*gitlab.ProtectedBranch {
t.Helper()
branches := testAccCreateBranches(t, project, n)
protectedBranches := make([]*gitlab.ProtectedBranch, n)
for i := range make([]int, n) {
var err error
protectedBranches[i], _, err = testGitlabClient.ProtectedBranches.ProtectRepositoryBranches(project.ID, &gitlab.ProtectRepositoryBranchesOptions{
Name: gitlab.String(branches[i].Name),
})
if err != nil {
t.Fatalf("could not protect test branches: %v", err)
}
}
return protectedBranches
}
// testAccCreateReleases is a test helper for creating a specified number of releases.
// It assumes the project will be destroyed at the end of the test and will not cleanup created releases.
func testAccCreateReleases(t *testing.T, project *gitlab.Project, n int) []*gitlab.Release {
t.Helper()
releases := make([]*gitlab.Release, n)
linkType := gitlab.LinkTypeValue("other")
linkURL1 := fmt.Sprintf("https://test/%v", *gitlab.String(acctest.RandomWithPrefix("acctest")))
linkURL2 := fmt.Sprintf("https://test/%v", *gitlab.String(acctest.RandomWithPrefix("acctest")))
for i := range releases {
var err error
releases[i], _, err = testGitlabClient.Releases.CreateRelease(project.ID, &gitlab.CreateReleaseOptions{
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
TagName: gitlab.String(acctest.RandomWithPrefix("acctest")),
Ref: &project.DefaultBranch,
Assets: &gitlab.ReleaseAssetsOptions{
Links: []*gitlab.ReleaseAssetLinkOptions{
{
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
URL: &linkURL1,
LinkType: &linkType,
},
{
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
URL: &linkURL2,
LinkType: &linkType,
},
},
},
})
if err != nil {
t.Fatalf("could not create test releases: %v", err)
}
}
return releases
}
// testAccAddProjectMembers is a test helper for adding users as members of a project.
// It assumes the project will be destroyed at the end of the test and will not cleanup members.
func testAccAddProjectMembers(t *testing.T, pid interface{}, users []*gitlab.User) {
t.Helper()
for _, user := range users {
_, _, err := testGitlabClient.ProjectMembers.AddProjectMember(pid, &gitlab.AddProjectMemberOptions{
UserID: user.ID,
AccessLevel: gitlab.AccessLevel(gitlab.DeveloperPermissions),
})
if err != nil {
t.Fatalf("could not add test project member: %v", err)
}
}
}
func testAccCreateClusterAgents(t *testing.T, pid interface{}, n int) []*gitlab.Agent {
t.Helper()
var clusterAgents []*gitlab.Agent
for i := 0; i < n; i++ {
clusterAgent, _, err := testGitlabClient.ClusterAgents.RegisterAgent(pid, &gitlab.RegisterAgentOptions{
Name: gitlab.String(fmt.Sprintf("agent-%d", i)),
})
if err != nil {
t.Fatalf("could not create test cluster agent: %v", err)
}
t.Cleanup(func() {
_, err := testGitlabClient.ClusterAgents.DeleteAgent(pid, clusterAgent.ID)
if err != nil {
t.Fatalf("could not cleanup test cluster agent: %v", err)
}
})
clusterAgents = append(clusterAgents, clusterAgent)
}
return clusterAgents
}
func testAccCreateProjectIssues(t *testing.T, pid interface{}, n int) []*gitlab.Issue {
t.Helper()
dueDate := gitlab.ISOTime(time.Now().Add(time.Hour))
var issues []*gitlab.Issue
for i := 0; i < n; i++ {
issue, _, err := testGitlabClient.Issues.CreateIssue(pid, &gitlab.CreateIssueOptions{
Title: gitlab.String(fmt.Sprintf("Issue %d", i)),
Description: gitlab.String(fmt.Sprintf("Description %d", i)),
DueDate: &dueDate,
})
if err != nil {
t.Fatalf("could not create test issue: %v", err)
}
issues = append(issues, issue)
}
return issues
}
func testAccCreateProjectIssueBoard(t *testing.T, pid interface{}) *gitlab.IssueBoard {
t.Helper()
issueBoard, _, err := testGitlabClient.Boards.CreateIssueBoard(pid, &gitlab.CreateIssueBoardOptions{Name: gitlab.String(acctest.RandomWithPrefix("acctest"))})
if err != nil {
t.Fatalf("could not create test issue board: %v", err)
}
return issueBoard
}
func testAccCreateProjectLabels(t *testing.T, pid interface{}, n int) []*gitlab.Label {
t.Helper()
var labels []*gitlab.Label
for i := 0; i < n; i++ {
label, _, err := testGitlabClient.Labels.CreateLabel(pid, &gitlab.CreateLabelOptions{Name: gitlab.String(acctest.RandomWithPrefix("acctest")), Color: gitlab.String("#000000")})
if err != nil {
t.Fatalf("could not create test label: %v", err)
}
labels = append(labels, label)
}
return labels
}
// testAccAddGroupMembers is a test helper for adding users as members of a group.
// It assumes the group will be destroyed at the end of the test and will not cleanup members.
func testAccAddGroupMembers(t *testing.T, gid interface{}, users []*gitlab.User) {
t.Helper()
for _, user := range users {
_, _, err := testGitlabClient.GroupMembers.AddGroupMember(gid, &gitlab.AddGroupMemberOptions{
UserID: gitlab.Int(user.ID),
AccessLevel: gitlab.AccessLevel(gitlab.DeveloperPermissions),
})
if err != nil {
t.Fatalf("could not add test group member: %v", err)
}
}
}
// testAccAddProjectMilestones is a test helper for adding milestones to project.
// It assumes the group will be destroyed at the end of the test and will not cleanup milestones.
func testAccAddProjectMilestones(t *testing.T, project *gitlab.Project, n int) []*gitlab.Milestone {
t.Helper()
milestones := make([]*gitlab.Milestone, n)
for i := range milestones {
var err error
milestones[i], _, err = testGitlabClient.Milestones.CreateMilestone(project.ID, &gitlab.CreateMilestoneOptions{
Title: gitlab.String(fmt.Sprintf("Milestone %d", i)),
Description: gitlab.String(fmt.Sprintf("Description %d", i)),
})
if err != nil {
t.Fatalf("Could not create test milestones: %v", err)
}
}
return milestones
}
func testAccCreateDeployKey(t *testing.T, projectID int, options *gitlab.AddDeployKeyOptions) *gitlab.ProjectDeployKey {
deployKey, _, err := testGitlabClient.DeployKeys.AddDeployKey(projectID, options)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if _, err := testGitlabClient.DeployKeys.DeleteDeployKey(projectID, deployKey.ID); err != nil {
t.Fatal(err)
}
})
return deployKey
}
// testAccCreateProjectEnvironment is a test helper function for creating a project environment
func testAccCreateProjectEnvironment(t *testing.T, projectID int, options *gitlab.CreateEnvironmentOptions) *gitlab.Environment {
t.Helper()
projectEnvironment, _, err := testGitlabClient.Environments.CreateEnvironment(projectID, options)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if projectEnvironment.State != "stopped" {
_, err = testGitlabClient.Environments.StopEnvironment(projectID, projectEnvironment.ID)
if err != nil {
t.Fatal(err)
}
}
if _, err := testGitlabClient.Environments.DeleteEnvironment(projectID, projectEnvironment.ID); err != nil {
t.Fatal(err)
}
})
return projectEnvironment
}
func testAccCreateProjectVariable(t *testing.T, projectID int) *gitlab.ProjectVariable {
variable, _, err := testGitlabClient.ProjectVariables.CreateVariable(projectID, &gitlab.CreateProjectVariableOptions{
Key: gitlab.String(fmt.Sprintf("test_key_%d", acctest.RandInt())),
Value: gitlab.String("test_value"),
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if _, err := testGitlabClient.ProjectVariables.RemoveVariable(projectID, variable.Key, nil); err != nil {
t.Fatal(err)
}
})
return variable
}
func testAccCreateGroupVariable(t *testing.T, groupID int) *gitlab.GroupVariable {
variable, _, err := testGitlabClient.GroupVariables.CreateVariable(groupID, &gitlab.CreateGroupVariableOptions{
Key: gitlab.String(fmt.Sprintf("test_key_%d", acctest.RandInt())),
Value: gitlab.String("test_value"),
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if _, err := testGitlabClient.GroupVariables.RemoveVariable(groupID, variable.Key, nil); err != nil {
t.Fatal(err)
}
})
return variable
}
func testAccCreateInstanceVariable(t *testing.T) *gitlab.InstanceVariable {
variable, _, err := testGitlabClient.InstanceVariables.CreateVariable(&gitlab.CreateInstanceVariableOptions{
Key: gitlab.String(fmt.Sprintf("test_key_%d", acctest.RandInt())),
Value: gitlab.String("test_value"),
})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if _, err := testGitlabClient.InstanceVariables.RemoveVariable(variable.Key, nil); err != nil {
t.Fatal(err)
}
})
return variable
}
// testAccGitlabProjectContext encapsulates a GitLab client and test project to be used during an
// acceptance test.
type testAccGitlabProjectContext struct {
t *testing.T
project *gitlab.Project
}
// testAccGitlabProjectStart initializes the GitLab client and creates a test project. Remember to
// call testAccGitlabProjectContext.finish() when finished with the testAccGitlabProjectContext.
func testAccGitlabProjectStart(t *testing.T) testAccGitlabProjectContext {
if os.Getenv(resource.EnvTfAcc) == "" {
t.Skipf("Acceptance tests skipped unless env '%s' set", resource.EnvTfAcc)
return testAccGitlabProjectContext{}
}
project, _, err := testGitlabClient.Projects.CreateProject(&gitlab.CreateProjectOptions{
Name: gitlab.String(acctest.RandomWithPrefix("acctest")),
Description: gitlab.String("Terraform acceptance tests"),
// So that acceptance tests can be run in a gitlab organization with no billing
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
})
if err != nil {
t.Fatalf("could not create test project: %v", err)
}
t.Cleanup(func() {
if _, err := testGitlabClient.Projects.DeleteProject(project.ID); err != nil {
t.Fatalf("could not delete test project: %v", err)
}
})
return testAccGitlabProjectContext{
t: t,
project: project,
}
}
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}