-
Notifications
You must be signed in to change notification settings - Fork 787
/
test_helpers.go
462 lines (420 loc) · 15.5 KB
/
test_helpers.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
package cmd
import (
"io/ioutil"
"os"
"strings"
"testing"
"time"
v1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
v1fake "github.com/jenkins-x/jx/pkg/client/clientset/versioned/fake"
typev1 "github.com/jenkins-x/jx/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/jx/cmd/clients"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/kube/resources"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
apifake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
"github.com/ghodss/yaml"
)
// ConfigureTestOptions lets configure the options for use in tests
// using fake APIs to k8s cluster
func ConfigureTestOptions(o *CommonOptions, git gits.Gitter, helm helm.Helmer) {
ConfigureTestOptionsWithResources(o, nil, nil, git, nil, helm, nil)
}
// ConfigureTestOptions lets configure the options for use in tests
// using fake APIs to k8s cluster.
func ConfigureTestOptionsWithResources(o *CommonOptions, k8sObjects []runtime.Object, jxObjects []runtime.Object,
git gits.Gitter, fakeGitProvider *gits.FakeProvider, helm helm.Helmer, resourcesInstaller resources.Installer) {
//o.Out = tests.Output()
o.BatchMode = true
if o.factory == nil {
o.factory = clients.NewFactory()
}
o.currentNamespace = "jx"
namespacesRequired := []string{o.currentNamespace}
namespaceMap := map[string]*corev1.Namespace{}
for _, ro := range k8sObjects {
ns, ok := ro.(*corev1.Namespace)
if ok {
namespaceMap[ns.Name] = ns
}
}
hasDev := false
for _, ro := range jxObjects {
env, ok := ro.(*v1.Environment)
if ok {
ns := env.Spec.Namespace
if ns != "" && util.StringArrayIndex(namespacesRequired, ns) < 0 {
namespacesRequired = append(namespacesRequired, ns)
}
if env.Name == "dev" {
hasDev = true
}
}
}
// ensure we've the dev environment
if !hasDev {
devEnv := kube.NewPermanentEnvironment("dev")
devEnv.Spec.Namespace = o.currentNamespace
devEnv.Spec.Kind = v1.EnvironmentKindTypeDevelopment
jxObjects = append(jxObjects, devEnv)
}
// add any missing namespaces
for _, ns := range namespacesRequired {
if namespaceMap[ns] == nil {
k8sObjects = append(k8sObjects, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
Labels: map[string]string{
"tag": "",
},
},
})
}
}
client := fake.NewSimpleClientset(k8sObjects...)
o.SetKubeClient(client)
o.jxClient = v1fake.NewSimpleClientset(jxObjects...)
o.apiExtensionsClient = apifake.NewSimpleClientset()
o.git = git
if fakeGitProvider != nil {
o.fakeGitProvider = fakeGitProvider
}
o.helm = helm
o.resourcesInstaller = resourcesInstaller
}
//CreateTestEnvironmentDir will create a temporary environment dir for the tests, copying over any existing config,
// and updating CommonOptions.EnvironmentDir() - this is useful for testing git operations on the environments without
// clobbering the local environments and risking the cluster getting contaminated - use with gits.GitLocal
func CreateTestEnvironmentDir(o *CommonOptions) error {
var err error
// Create a temp dir for environments
origEnvironmentsDir, err := o.EnvironmentsDir()
if err != nil {
return err
}
o.environmentsDir, err = ioutil.TempDir("", "jx-environments")
if err != nil {
return err
}
// Copy over any existing environments
err = util.CopyDir(origEnvironmentsDir, o.environmentsDir, true)
if err != nil {
return err
}
return nil
}
// CleanupTestEnvironmentDir should be called in a deferred function whenever CreateTestEnvironmentDir is called
func CleanupTestEnvironmentDir(o *CommonOptions) error {
// Let's not accidentally remove the real one!
if strings.HasPrefix(o.environmentsDir, os.TempDir()) {
err := os.RemoveAll(o.environmentsDir)
if err != nil {
return err
}
}
return nil
}
func CreateTestPipelineActivity(jxClient versioned.Interface, ns string, folder string, repo string, branch string, build string, workflow string) (*v1.PipelineActivity, error) {
activities := jxClient.JenkinsV1().PipelineActivities(ns)
key := &kube.PromoteStepActivityKey{
PipelineActivityKey: kube.PipelineActivityKey{
Name: folder + "-" + repo + "-" + branch + "-" + build,
Pipeline: folder + "/" + repo + "/" + branch,
Build: build,
GitInfo: &gits.GitRepository{
Name: "my-app",
Organisation: "myorg",
},
},
}
a, _, err := key.GetOrCreate(jxClient, ns)
version := "1.0." + build
a.Spec.GitOwner = folder
a.Spec.GitRepository = repo
a.Spec.GitURL = "https://fake.git/" + folder + "/" + repo + ".git"
a.Spec.Version = version
a.Spec.Workflow = workflow
_, err = activities.Update(a)
return a, err
}
func AssertHasPullRequestForEnv(t *testing.T, activities typev1.PipelineActivityInterface, name string, envName string) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
for _, step := range activity.Spec.Steps {
promote := step.Promote
if promote != nil {
if promote.Environment == envName {
failed := false
pullRequestStep := promote.PullRequest
if pullRequestStep == nil {
assert.Fail(t, "No PullRequest object on PipelineActivity %s for Promote step for Environment %s", name, envName)
failed = true
}
u := pullRequestStep.PullRequestURL
log.Infof("Found Promote PullRequest %s on PipelineActivity %s for Environment %s\n", u, name, envName)
if !assert.True(t, u != "", "No PullRequest URL on PipelineActivity %s for Promote step for Environment %s", name, envName) {
failed = true
}
if failed {
dumpFailedActivity(activity)
}
return
}
}
}
assert.Fail(t, "Missing Promote", "No Promote found on PipelineActivity %s for Environment %s", name, envName)
dumpFailedActivity(activity)
}
func WaitForPullRequestForEnv(t *testing.T, activities typev1.PipelineActivityInterface, name string, envName string) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
waitTime, _ := time.ParseDuration("20s")
end := time.Now().Add(waitTime)
for {
for _, step := range activity.Spec.Steps {
promote := step.Promote
if promote != nil {
if promote.Environment == envName {
failed := false
pullRequestStep := promote.PullRequest
if pullRequestStep == nil {
failed = true
}
u := pullRequestStep.PullRequestURL
log.Infof("Found Promote PullRequest %s on PipelineActivity %s for Environment %s\n", u, name, envName)
if !assert.True(t, u != "", "No PullRequest URL on PipelineActivity %s for Promote step for Environment %s", name, envName) {
failed = true
}
if !failed {
return
}
}
}
}
if time.Now().After(end) {
log.Infof("No Promote PR found on PipelineActivity %s for Environment %s\n", name, envName)
//assert.Fail(t, "Missing Promote PR", "No Promote PR found on PipelineActivity %s for Environment %s", name, envName)
//dumpFailedActivity(activity)
return
}
log.Infof("Waiting 1s for PullRequest in Enviroment %s\n", envName)
v, _ := time.ParseDuration("2s")
time.Sleep(v)
activity, _ = activities.Get(name, metav1.GetOptions{})
}
}
func AssertWorkflowStatus(t *testing.T, activities typev1.PipelineActivityInterface, name string, status v1.ActivityStatusType) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
if !assert.Equal(t, string(status), string(activity.Spec.Status), "PipelineActivity status for %s", activity.Name) ||
!assert.Equal(t, string(status), string(activity.Spec.WorkflowStatus), "PipelineActivity workflow status for %s", activity.Name) {
dumpFailedActivity(activity)
}
}
func AssertSetPullRequestComplete(t *testing.T, provider *gits.FakeProvider, repository *gits.FakeRepository, prNumber int) bool {
fakePR := repository.PullRequests[prNumber]
if !assert.NotNil(t, fakePR, "No PullRequest found on repository %s for number #%d", repository.String(), prNumber) {
return false
}
l := len(fakePR.Commits)
if l > 0 {
fakePR.Commits[l-1].Status = gits.CommitSatusSuccess
// ensure the commit is on the repo r
lastCommit := fakePR.Commits[l-1]
if len(repository.Commits) == 0 {
repository.Commits = append(repository.Commits, lastCommit)
} else {
repository.Commits[len(repository.Commits)-1] = lastCommit
}
log.Infof("PR %s has commit status success\n", fakePR.PullRequest.URL)
}
// validate the fake Git provider concurs
repoOwner := repository.Owner
repoName := repository.Name()
testGitInfo := &gits.GitRepository{
Organisation: repoOwner,
Name: repoName,
}
pr, err := provider.GetPullRequest(repoOwner, testGitInfo, prNumber)
assert.NoError(t, err, "Finding PullRequest %d", prNumber)
if !assert.NotNil(t, pr, "Could not find PR %d", prNumber) {
return false
}
if !assert.NotNil(t, pr.MergeCommitSHA, "PR %d has no MergeCommitSHA", prNumber) {
return false
}
statuses, err := provider.ListCommitStatus(repoOwner, repoName, *pr.MergeCommitSHA)
assert.NoError(t, err, "Finding PullRequest %d commit status", prNumber)
if assert.True(t, len(statuses) > 0, "PullRequest %d statuses are empty", prNumber) {
lastStatus := statuses[len(statuses)-1]
return assert.Equal(t, "success", lastStatus.State, "Last commit status of PullRequest 1 at %s", pr.URL)
}
return false
}
func SetSuccessCommitStatusInPR(t *testing.T, repository *gits.FakeRepository, prNumber int) {
fakePR := repository.PullRequests[prNumber]
assert.NotNil(t, fakePR, "No PullRequest found on repository %s for number #%d", repository.String(), prNumber)
l := len(fakePR.Commits)
if l > 0 {
fakePR.Commits[l-1].Status = gits.CommitSatusSuccess
}
}
func AssertHasPromoteStatus(t *testing.T, activities typev1.PipelineActivityInterface, name string, envName string, status v1.ActivityStatusType) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
for _, step := range activity.Spec.Steps {
promote := step.Promote
if promote != nil {
if promote.Environment == envName {
if !assert.Equal(t, string(status), string(promote.Status), "activity status for %s promote %s", name, envName) {
dumpFailedActivity(activity)
}
return
}
}
}
assert.Fail(t, "Missing Promote", "No Promote found on PipelineActivity %s for Environment %s", name, envName)
dumpFailedActivity(activity)
}
func AssertHasPipelineStatus(t *testing.T, activities typev1.PipelineActivityInterface, name string, status v1.ActivityStatusType) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
if !assert.Equal(t, string(status), string(activity.Spec.Status), "activity status for PipelineActivity %s", name) {
dumpFailedActivity(activity)
}
}
func AssertAllPromoteStepsSuccessful(t *testing.T, activities typev1.PipelineActivityInterface, name string) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
assert.Equal(t, string(v1.ActivityStatusTypeSucceeded), string(activity.Spec.Status), "PipelineActivity status for %s", activity.Name)
assert.Equal(t, string(v1.ActivityStatusTypeSucceeded), string(activity.Spec.WorkflowStatus), "PipelineActivity workflow status for %s", activity.Name)
for _, step := range activity.Spec.Steps {
promote := step.Promote
if promote != nil {
assert.Equal(t, string(v1.ActivityStatusTypeSucceeded), string(promote.Status), "PipelineActivity %s status for Promote to Environment %s", activity.Name, promote.Environment)
}
}
}
func AssertHasNoPullRequestForEnv(t *testing.T, activities typev1.PipelineActivityInterface, name string, envName string) {
activity, err := activities.Get(name, metav1.GetOptions{})
if err != nil {
assert.NoError(t, err, "Could not find PipelineActivity %s", name)
return
}
for _, step := range activity.Spec.Steps {
promote := step.Promote
if promote != nil {
if promote.Environment == envName {
assert.Fail(t, "Should not have a Promote for Environment %s but has %v", envName, promote)
return
}
}
}
}
func SetPullRequestClosed(pr *gits.FakePullRequest) {
now := time.Now()
pr.PullRequest.ClosedAt = &now
log.Infof("PR %s is now closed\n", pr.PullRequest.URL)
}
// AssertSetPullRequestMerged validates that the fake PR has merged
func AssertSetPullRequestMerged(t *testing.T, provider *gits.FakeProvider, orgName string, repositoryName string,
prNumber int) bool {
repos := provider.Repositories[orgName]
var repository *gits.FakeRepository
for _, r := range repos {
if r.Name() == repositoryName {
repository = r
}
}
assert.NotNil(t, repository)
fakePR := repository.PullRequests[prNumber]
if !assert.NotNil(t, fakePR, "No PullRequest found on repository %s for number #%d", repository.String(), prNumber) {
return false
}
commitLen := len(fakePR.Commits)
if !assert.True(t, commitLen > 0, "PullRequest #%d on repository %s has no commits", prNumber, repository.String()) {
return false
}
lastFakeCommit := fakePR.Commits[commitLen-1].Commit
if !assert.NotNil(t, lastFakeCommit, "PullRequest #%d on repository %s last commit status has no commits", prNumber, repository.String()) {
return false
}
sha := lastFakeCommit.SHA
merged := true
fakePR.PullRequest.MergeCommitSHA = &sha
fakePR.PullRequest.Merged = &merged
log.Infof("PR %s is now merged\n", fakePR.PullRequest.URL)
// validate the fake Git provider concurs
testGitInfo := &gits.GitRepository{
Organisation: repository.Owner,
Name: repository.Name(),
}
pr, err := provider.GetPullRequest(repository.Owner, testGitInfo, prNumber)
assert.NoError(t, err, "Finding PullRequest %d", prNumber)
return assert.True(t, pr.Merged != nil && *pr.Merged, "Fake PR %d is merged", prNumber)
}
func AssertPromoteStep(t *testing.T, step *v1.WorkflowStep, expectedEnvironment string) {
promote := step.Promote
assert.True(t, promote != nil, "step is a promote step")
if promote != nil {
assert.Equal(t, expectedEnvironment, promote.Environment, "environment name")
}
}
func PollGitStatusAndReactToPipelineChanges(t *testing.T, o *ControllerWorkflowOptions, jxClient versioned.Interface, ns string) error {
o.ReloadAndPollGitPipelineStatuses(jxClient, ns)
err := o.Run()
assert.NoError(t, err, "Failed to react to PipelineActivity changes")
return err
}
func dumpActivity(t *testing.T, activities typev1.PipelineActivityInterface, name string) *v1.PipelineActivity {
activity, err := activities.Get(name, metav1.GetOptions{})
assert.NoError(t, err)
if err != nil {
return nil
}
assert.NotNil(t, activity, "No PipelineActivity found for name %s", name)
if activity != nil {
dumpFailedActivity(activity)
}
return activity
}
func dumpFailedActivity(activity *v1.PipelineActivity) {
data, err := yaml.Marshal(activity)
if err == nil {
log.Warnf("YAML: %s\n", string(data))
}
}
func dumpPipelineMap(o *ControllerWorkflowOptions) {
log.Infof("Dumping PipelineMap {\n")
for k, v := range o.PipelineMap() {
log.Infof(" Pipeline %s %s\n", k, v.Name)
}
log.Infof("}\n")
}