forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkins_plugin.go
741 lines (611 loc) · 32.2 KB
/
jenkins_plugin.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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package builds
import (
"bytes"
"fmt"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
e2e "k8s.io/kubernetes/test/e2e/framework"
exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/jenkins"
)
const (
localPluginSnapshotImageStream = "jenkins-plugin-snapshot-test"
localPluginSnapshotImage = "openshift/" + localPluginSnapshotImageStream + ":latest"
localLoginPluginSnapshotImageStream = "jenkins-login-plugin-snapshot-test"
localLoginPluginSnapshotImage = "openshift/" + localLoginPluginSnapshotImageStream + ":latest"
originalLicenseText = "About OpenShift Pipeline Jenkins Plugin"
originalPluginName = "openshift-pipeline"
loginLicenseText = "About OpenShift Login Plugin"
loginPluginName = "openshift-login"
)
// Loads a Jenkins related template using new-app.
func loadFixture(oc *exutil.CLI, filename string) {
resourcePath := exutil.FixturePath("testdata", "jenkins-plugin", filename)
err := oc.Run("new-app").Args(resourcePath).Execute()
o.ExpectWithOffset(1, err).NotTo(o.HaveOccurred())
}
func assertEnvVars(oc *exutil.CLI, buildPrefix string, varsToFind map[string]string) {
buildList, err := oc.BuildClient().Build().Builds(oc.Namespace()).List(metav1.ListOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
// Ensure that expected start-build environment variables were injected
for _, build := range buildList.Items {
e2e.Logf("Found build: %q", build.GetName())
if strings.HasPrefix(build.GetName(), buildPrefix) {
envs := []corev1.EnvVar{}
if build.Spec.Strategy.DockerStrategy != nil && build.Spec.Strategy.DockerStrategy.Env != nil {
envs = build.Spec.Strategy.DockerStrategy.Env
} else if build.Spec.Strategy.SourceStrategy != nil && build.Spec.Strategy.SourceStrategy.Env != nil {
envs = build.Spec.Strategy.SourceStrategy.Env
} else {
continue
}
for k, v := range varsToFind {
found := false
for _, env := range envs {
e2e.Logf("Found %s=%s in build %s", env.Name, env.Value, build.GetName())
if k == env.Name && v == env.Value {
found = true
break
}
}
o.ExpectWithOffset(1, found).To(o.BeTrue())
}
}
}
}
// Stands up a simple pod which can be used for exec commands
func initExecPod(oc *exutil.CLI) *corev1.Pod {
// Create a running pod in which we can execute our commands
oc.Run("run").Args("centos", "--image", "centos:7", "--command", "--", "sleep", "1800").Execute()
var targetPod *corev1.Pod
err := wait.Poll(10*time.Second, 10*time.Minute, func() (bool, error) {
pods, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).List(metav1.ListOptions{})
o.ExpectWithOffset(1, err).NotTo(o.HaveOccurred())
for _, p := range pods.Items {
if strings.HasPrefix(p.Name, "centos") && !strings.Contains(p.Name, "deploy") && p.Status.Phase == "Running" {
targetPod = &p
return true, nil
}
}
return false, nil
})
o.ExpectWithOffset(1, err).NotTo(o.HaveOccurred())
return targetPod
}
type apiObjJob struct {
jobName string
create bool
}
// Validate create/delete of objects
func validateCreateDelete(create bool, key, out string, err error) {
e2e.Logf("\nOBJ: %s\n", out)
if create {
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(strings.Contains(out, key)).To(o.BeTrue())
} else {
o.Expect(err).To(o.HaveOccurred())
}
}
var _ = g.Describe("[Feature:Builds][Slow] openshift pipeline plugin", func() {
defer g.GinkgoRecover()
var oc = exutil.NewCLI("jenkins-plugin", exutil.KubeConfigPath())
var j *jenkins.JenkinsRef
var dcLogFollow *exec.Cmd
var dcLogStdOut, dcLogStdErr *bytes.Buffer
var ticker *time.Ticker
g.Context("", func() {
g.AfterEach(func() {
if g.CurrentGinkgoTestDescription().Failed {
exutil.DumpPodStates(oc)
exutil.DumpPodLogsStartingWith("", oc)
}
if os.Getenv(jenkins.EnableJenkinsMemoryStats) != "" {
ticker.Stop()
}
e2e.Logf("Jenkins DC description follows. If there were issues, check to see if there were any restarts in the jenkins pod.")
exutil.DumpApplicationPodLogs("jenkins", oc)
if dcLogFollow != nil && dcLogStdOut != nil && dcLogStdErr != nil {
e2e.Logf("Killing Jenkins DC log follow")
dcLogFollow.Process.Kill()
e2e.Logf("Jenkins server logs from test:\nstdout>\n%s\n\nstderr>\n%s\n\n", string(dcLogStdOut.Bytes()), string(dcLogStdErr.Bytes()))
dcLogFollow = nil
} else {
e2e.Logf("Logs were not captured!\n%v\n%v\n%v\n", dcLogFollow, dcLogStdOut, dcLogStdErr)
}
})
g.BeforeEach(func() {
exutil.DumpDockerInfo()
g.By("kick off the build for the jenkins ephemeral and application templates")
// Deploy Jenkins
// NOTE, we use these tests for both a) nightly regression runs against the latest openshift jenkins image on docker hub, and
// b) PR testing for changes to the various openshift jenkins plugins we support. With scenario b), a docker image that extends
// our jenkins image is built, where the proposed plugin change is injected, overwritting the current released version of the plugin.
// Our test/PR jobs on ci.openshift create those images, as well as set env vars this test suite looks for. When both the env var
// and test image is present, a new image stream is created using the test image, and our jenkins template is instantiated with
// an override to use that images stream and test image
var licensePrefix, pluginName string
// with the startup costs now of jenkins 2.89 or greater and trying to incur those during startup, need more memory
// to avoid deployment timeouts
newAppArgs := []string{"-f", exutil.FixturePath("..", "..", "examples", "jenkins", "jenkins-ephemeral-template.json"), "-p", "MEMORY_LIMIT=2Gi", "-p", "DISABLE_ADMINISTRATIVE_MONITORS=true"}
newAppArgs = jenkins.OverridePodTemplateImages(newAppArgs)
useSnapshotImage := false
origPluginNewAppArgs, useOrigPluginSnapshotImage := jenkins.SetupSnapshotImage(jenkins.UseLocalPluginSnapshotEnvVarName, localPluginSnapshotImage, localPluginSnapshotImageStream, newAppArgs, oc)
loginPluginNewAppArgs, useLoginPluginSnapshotImage := jenkins.SetupSnapshotImage(jenkins.UseLocalLoginPluginSnapshotEnvVarName, localLoginPluginSnapshotImage, localLoginPluginSnapshotImageStream, newAppArgs, oc)
switch {
case useOrigPluginSnapshotImage && useLoginPluginSnapshotImage:
fmt.Fprintf(g.GinkgoWriter,
"\nBOTH %s and %s for PR TESTING ARE SET. WILL NOT CHOOSE BETWEEN THE TWO SO TESTING CURRENT PLUGIN VERSIONS IN LATEST OPENSHIFT JENKINS IMAGE ON DOCKER HUB.\n",
jenkins.UseLocalPluginSnapshotEnvVarName, jenkins.UseLocalLoginPluginSnapshotEnvVarName)
case useOrigPluginSnapshotImage:
fmt.Fprintf(g.GinkgoWriter, "\nTHE UPCOMING TESTS WILL LEVERAGE AN IMAGE THAT EXTENDS THE LATEST OPENSHIFT JENKINS IMAGE AND OVERRIDES THE OPENSHIFT PIPELINE PLUGIN WITH A NEW VERSION BUILT FROM PROPOSED CHANGES TO THAT PLUGIN.\n")
licensePrefix = originalLicenseText
pluginName = originalPluginName
useSnapshotImage = true
newAppArgs = origPluginNewAppArgs
case useLoginPluginSnapshotImage:
fmt.Fprintf(g.GinkgoWriter, "\nTHE UPCOMING TESTS WILL LEVERAGE AN IMAGE THAT EXTENDS THE LATEST OPENSHIFT JENKINS IMAGE AND OVERRIDES THE OPENSHIFT LOGIN PLUGIN WITH A NEW VERSION BUILT FROM PROPOSED CHANGES TO THAT PLUGIN.\n")
licensePrefix = loginLicenseText
pluginName = loginPluginName
useSnapshotImage = true
newAppArgs = loginPluginNewAppArgs
default:
fmt.Fprintf(g.GinkgoWriter, "\nNO PR TEST ENV VARS SET SO TESTING CURRENT PLUGIN VERSIONS IN LATEST OPENSHIFT JENKINS IMAGE ON DOCKER HUB.\n")
}
err := oc.Run("new-app").Args(newAppArgs...).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for jenkins deployment")
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), "jenkins", 1, false, oc)
if err != nil {
exutil.DumpApplicationPodLogs("jenkins", oc)
}
o.Expect(err).NotTo(o.HaveOccurred())
j = jenkins.NewRef(oc)
g.By("wait for jenkins to come up")
_, err = j.WaitForContent("", 200, 10*time.Minute, "")
if err != nil {
exutil.DumpApplicationPodLogs("jenkins", oc)
}
o.Expect(err).NotTo(o.HaveOccurred())
if useSnapshotImage {
g.By("verifying the test image is being used")
// for the test image, confirm that a snapshot version of the plugin is running in the jenkins image we'll test against
_, err = j.WaitForContent(licensePrefix+` ([0-9\.]+)-SNAPSHOT`, 200, 10*time.Minute, "/pluginManager/plugin/"+pluginName+"/thirdPartyLicenses")
o.Expect(err).NotTo(o.HaveOccurred())
}
if os.Getenv(jenkins.EnableJenkinsMemoryStats) != "" {
ticker = jenkins.StartJenkinsMemoryTracking(oc, oc.Namespace())
}
// Start capturing logs from this deployment config.
// This command will terminate if the Jenkins instance crashes. This
// ensures that even if the Jenkins DC restarts, we should capture
// logs from the crash.
dcLogFollow, dcLogStdOut, dcLogStdErr, err = oc.Run("logs").Args("-f", "dc/jenkins").Background()
o.Expect(err).NotTo(o.HaveOccurred())
// Populate shared Jenkins namespace with artifacts that can be used by all tests
loadFixture(oc, "shared-resources-template.json")
// Allow resources to settle. ImageStream tags seem unavailable without this wait.
time.Sleep(10 * time.Second)
})
g.Context("jenkins-plugin test context ", func() {
g.It("jenkins-plugin test trigger build including clone", func() {
jobName := "test-build-job"
data := j.ProcessJenkinsJob("build-job.xml", oc.Namespace())
j.CreateItem(jobName, data)
jmon := j.StartJob(jobName)
err := jmon.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
// the build and deployment is by far the most time consuming portion of the test jenkins job;
// we leverage some of the openshift utilities for waiting for the deployment before we poll
// jenkins for the successful job completion
g.By("waiting for frontend, frontend-prod deployments as signs that the build has finished")
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), "frontend", 1, true, oc)
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), "frontend-prod", 1, true, oc)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build console logs and see if succeeded")
logs, err := j.WaitForContent("Finished: SUCCESS", 200, 10*time.Minute, "job/%s/lastBuild/consoleText", jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build and confirm trigger by field is correct")
out, err := oc.Run("get").Args("builds/frontend-1", "-o", "jsonpath='{.spec.triggeredBy[0].message}'").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(strings.Contains(out, "Jenkins job")).To(o.BeTrue())
jobName = "test-build-clone-job"
data = j.ProcessJenkinsJob("build-job-clone.xml", oc.Namespace())
j.CreateItem(jobName, data)
jmon = j.StartJob(jobName)
jmon.Await(10 * time.Minute)
g.By("get clone build console logs and see if succeeded")
logs, err = j.WaitForContent("Finished: SUCCESS", 200, 10*time.Minute, "job/%s/lastBuild/consoleText", jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build and confirm trigger by field is correct")
out2, err := oc.Run("get").Args("builds/frontend-2", "-o", "jsonpath='{.spec.triggeredBy[0].message}'").Output()
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(strings.Contains(out2, "Jenkins job")).To(o.BeTrue())
g.By("ensure trigger by fields for the two builds are different")
o.Expect(strings.Compare(out, out2) == 0).NotTo(o.BeTrue())
})
g.It("jenkins-plugin test trigger build with slave", func() {
jobName := "test-build-job-slave"
data := j.ProcessJenkinsJob("build-job-slave.xml", oc.Namespace())
j.CreateItem(jobName, data)
jmon := j.StartJob(jobName)
err := jmon.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
// the build and deployment is by far the most time consuming portion of the test jenkins job;
// we leverage some of the openshift utilities for waiting for the deployment before we poll
// jenkins for the successful job completion
g.By("waiting for frontend, frontend-prod deployments as signs that the build has finished")
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), "frontend", 1, true, oc)
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), "frontend-prod", 1, true, oc)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build console logs and see if succeeded")
logs, err := j.WaitForContent("Finished: SUCCESS", 200, 10*time.Minute, "job/%s/lastBuild/consoleText", jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build console logs and confirm ran on slave")
logs, err = j.WaitForContent("Building remotely on", 200, 10*time.Minute, "job/%s/lastBuild/consoleText", jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
o.Expect(err).NotTo(o.HaveOccurred())
})
g.It("jenkins-plugin test create obj delete obj", func() {
jobsToCreate := map[string]string{"test-create-obj": "create-job.xml", "test-delete-obj": "delete-job.xml", "test-delete-obj-labels": "delete-job-labels.xml", "test-delete-obj-keys": "delete-job-keys.xml"}
for jobName, jobConfig := range jobsToCreate {
data := j.ProcessJenkinsJob(jobConfig, oc.Namespace())
j.CreateItem(jobName, data)
}
jobsToRun := []apiObjJob{{"test-create-obj", true}, {"test-delete-obj", false}, {"test-create-obj", true}, {"test-delete-obj-labels", false}, {"test-create-obj", true}, {"test-delete-obj-keys", false}}
for _, job := range jobsToRun {
jmon := j.StartJob(job.jobName)
err := jmon.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(job.jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build console logs and see if succeeded")
logs, err := j.WaitForContent("Finished: SUCCESS", 200, 10*time.Minute, "job/%s/lastBuild/consoleText", job.jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
o.Expect(err).NotTo(o.HaveOccurred())
out, err := oc.Run("get").Args("bc", "forcepull-bldr").Output()
validateCreateDelete(job.create, "forcepull-bldr", out, err)
out, err = oc.Run("get").Args("is", "forcepull-extended-test-builder").Output()
validateCreateDelete(job.create, "forcepull-extended-test-builder", out, err)
}
})
g.It("jenkins-plugin test trigger build with envs", func() {
jobName := "test-build-with-env-job"
data := j.ProcessJenkinsJob("build-with-env-job.xml", oc.Namespace())
j.CreateItem(jobName, data)
jmon := j.StartJob(jobName)
err := jmon.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
logs, err := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
o.Expect(err).NotTo(o.HaveOccurred())
// the build and deployment is by far the most time consuming portion of the test jenkins job;
// we leverage some of the openshift utilities for waiting for the deployment before we poll
// jenkins for the successful job completion
g.By("waiting for frontend deployments as signs that the build has finished")
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), "frontend", 1, true, oc)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("get build console logs and see if succeeded")
_, err = j.WaitForContent("Finished: SUCCESS", 200, 10*time.Minute, "job/%s/lastBuild/consoleText", jobName)
assertEnvVars(oc, "frontend-", map[string]string{
"a": "b",
"C": "D",
"e": "",
})
})
g.It("jenkins-plugin test trigger build DSL", func() {
buildsBefore, err := oc.BuildClient().Build().Builds(oc.Namespace()).List(metav1.ListOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
data, err := j.BuildDSLJob(oc.Namespace(),
"node{",
"openshiftBuild( namespace:'PROJECT_NAME', bldCfg: 'frontend', env: [ [ name : 'a', value : 'b' ], [ name : 'C', value : 'D' ], [ name : 'e', value : '' ] ] )",
"}",
)
o.Expect(err).NotTo(o.HaveOccurred())
filename, err := exutil.CreateTempFile(data)
o.Expect(err).NotTo(o.HaveOccurred())
jobName := "test-build-dsl-job"
j.CreateItem(jobName, filename)
monitor := j.StartJob(jobName)
err = monitor.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
err = wait.Poll(10*time.Second, 10*time.Minute, func() (bool, error) {
buildsAfter, err := oc.BuildClient().Build().Builds(oc.Namespace()).List(metav1.ListOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
return (len(buildsAfter.Items) != len(buildsBefore.Items)), nil
})
buildsAfter, err := oc.BuildClient().Build().Builds(oc.Namespace()).List(metav1.ListOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(len(buildsAfter.Items)).To(o.Equal(len(buildsBefore.Items) + 1))
log, err := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("Job logs>>\n%s\n\n", log)
assertEnvVars(oc, "frontend-", map[string]string{
"a": "b",
"C": "D",
"e": "",
})
})
g.It("jenkins-plugin test exec DSL", func() {
targetPod := initExecPod(oc)
targetContainer := targetPod.Spec.Containers[0]
data, err := j.BuildDSLJob(oc.Namespace(),
"node{",
fmt.Sprintf("openshiftExec( namespace:'PROJECT_NAME', pod: '%s', command: [ 'echo', 'hello', 'world', '1' ] )", targetPod.Name),
fmt.Sprintf("openshiftExec( namespace:'PROJECT_NAME', pod: '%s', command: 'echo', arguments : [ 'hello', 'world', '2' ] )", targetPod.Name),
fmt.Sprintf("openshiftExec( namespace:'PROJECT_NAME', pod: '%s', command: 'echo', arguments : [ [ value: 'hello' ], [ value : 'world' ], [ value : '3' ] ] )", targetPod.Name),
fmt.Sprintf("openshiftExec( namespace:'PROJECT_NAME', pod: '%s', container: '%s', command: [ 'echo', 'hello', 'world', '4' ] )", targetPod.Name, targetContainer.Name),
fmt.Sprintf("openshiftExec( namespace:'PROJECT_NAME', pod: '%s', container: '%s', command: 'echo', arguments : [ 'hello', 'world', '5' ] )", targetPod.Name, targetContainer.Name),
fmt.Sprintf("openshiftExec( namespace:'PROJECT_NAME', pod: '%s', container: '%s', command: 'echo', arguments : [ [ value: 'hello' ], [ value : 'world' ], [ value : '6' ] ] )", targetPod.Name, targetContainer.Name),
"}",
)
o.Expect(err).NotTo(o.HaveOccurred())
filename, err := exutil.CreateTempFile(data)
o.Expect(err).NotTo(o.HaveOccurred())
jobName := "test-exec-dsl-job"
j.CreateItem(jobName, filename)
monitor := j.StartJob(jobName)
err = monitor.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
log, err := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("Job logs>>\n%s\n\n", log)
o.Expect(strings.Contains(log, "hello world 1")).To(o.BeTrue())
o.Expect(strings.Contains(log, "hello world 2")).To(o.BeTrue())
o.Expect(strings.Contains(log, "hello world 3")).To(o.BeTrue())
o.Expect(strings.Contains(log, "hello world 4")).To(o.BeTrue())
o.Expect(strings.Contains(log, "hello world 5")).To(o.BeTrue())
o.Expect(strings.Contains(log, "hello world 6")).To(o.BeTrue())
})
g.It("jenkins-plugin test exec freestyle", func() {
targetPod := initExecPod(oc)
targetContainer := targetPod.Spec.Containers[0]
jobName := "test-build-with-env-steps"
data := j.ProcessJenkinsJobUsingVars("build-with-exec-steps.xml", oc.Namespace(), map[string]string{
"POD_NAME": targetPod.Name,
"CONTAINER_NAME": targetContainer.Name,
})
j.CreateItem(jobName, data)
jmon := j.StartJob(jobName)
jmon.Await(2 * time.Minute)
log, err := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", log)
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(strings.Contains(log, "hello world 1")).To(o.BeTrue())
// Now run without specifying container
jobName = "test-build-with-env-steps-no-container"
data = j.ProcessJenkinsJobUsingVars("build-with-exec-steps.xml", oc.Namespace(), map[string]string{
"POD_NAME": targetPod.Name,
"CONTAINER_NAME": "",
})
j.CreateItem(jobName, data)
jmon = j.StartJob(jobName)
jmon.Await(2 * time.Minute)
log, err = j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", log)
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(strings.Contains(log, "hello world 1")).To(o.BeTrue())
})
g.It("jenkins-plugin test multitag", func() {
loadFixture(oc, "multitag-template.json")
err := wait.Poll(10*time.Second, 1*time.Minute, func() (bool, error) {
_, err := oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag3:orig", metav1.GetOptions{})
if err != nil {
return false, nil
}
return true, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
jobName := "test-multitag-job"
data := j.ProcessJenkinsJob("multitag-job.xml", oc.Namespace())
j.CreateItem(jobName, data)
monitor := j.StartJob(jobName)
err = monitor.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
log, err := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("Job logs>>\n%s\n\n", log)
// Assert stream tagging results
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag:prod", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
// 1 to N mapping
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag:prod2", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag:prod3", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag:prod4", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
// N to 1 mapping
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag:prod5", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag2:prod5", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag3:prod5", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
// N to N mapping
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag:prod6", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag2:prod7", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag3:prod8", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
// N to N mapping with creation
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag4:prod9", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag5:prod10", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag6:prod11", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
})
g.It("jenkins-plugin test multitag DSL", func() {
testNamespace := oc.Namespace()
loadFixture(oc, "multitag-template.json")
err := wait.Poll(10*time.Second, 1*time.Minute, func() (bool, error) {
_, err := oc.ImageClient().Image().ImageStreamTags(oc.Namespace()).Get("multitag3:orig", metav1.GetOptions{})
if err != nil {
return false, nil
}
return true, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
anotherNamespace := oc.Namespace() + "-multitag-target"
oc.Run("new-project").Args(anotherNamespace).Execute()
// no calls to delete these two projects here; leads to timing
// issues with the framework deleting all namespaces
time.Sleep(10 * time.Second) // Give project time to initialize policies.
// Allow jenkins service account to edit the new namespace
oc.SetNamespace(anotherNamespace)
err = oc.Run("policy").Args("add-role-to-user", "edit", "system:serviceaccount:"+j.Namespace()+":jenkins").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
oc.SetNamespace(testNamespace)
e2e.Logf("Using testNamespace: %q and currentNamespace: %q", testNamespace, oc.Namespace())
data, err := j.BuildDSLJob(oc.Namespace(),
"node{",
"openshiftTag( namespace:'PROJECT_NAME', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag', destTag: 'prod' )",
"openshiftTag( namespace:'PROJECT_NAME', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag2', destTag: 'prod1, prod2, prod3' )",
"openshiftTag( namespace:'PROJECT_NAME', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag2,multitag7', destTag: 'prod4' )",
"openshiftTag( namespace:'PROJECT_NAME', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag5,multitag6', destTag: 'prod5, prod6' )",
fmt.Sprintf("openshiftTag( namespace:'PROJECT_NAME', destinationNamespace: '%s', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag', destTag: 'prod' )", anotherNamespace),
fmt.Sprintf("openshiftTag( namespace:'PROJECT_NAME', destinationNamespace: '%s', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag2', destTag: 'prod1, prod2, prod3' )", anotherNamespace),
fmt.Sprintf("openshiftTag( namespace:'PROJECT_NAME', destinationNamespace: '%s', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag2,multitag7', destTag: 'prod4' )", anotherNamespace),
fmt.Sprintf("openshiftTag( namespace:'PROJECT_NAME', destinationNamespace: '%s', srcStream: 'multitag', srcTag: 'orig', destStream: 'multitag5,multitag6', destTag: 'prod5, prod6' )", anotherNamespace),
"}",
)
o.Expect(err).NotTo(o.HaveOccurred())
filename, err := exutil.CreateTempFile(data)
o.Expect(err).NotTo(o.HaveOccurred())
jobName := "test-multitag-dsl-job"
j.CreateItem(jobName, filename)
monitor := j.StartJob(jobName)
err = monitor.Await(10 * time.Minute)
if err != nil {
logs, _ := j.GetLastJobConsoleLogs(jobName)
e2e.Logf("\n\nJenkins logs>\n%s\n\n", logs)
}
o.Expect(err).NotTo(o.HaveOccurred())
time.Sleep(10 * time.Second)
log, err := j.GetLastJobConsoleLogs(jobName)
o.Expect(err).NotTo(o.HaveOccurred())
e2e.Logf("Job logs>>\n%s\n\n", log)
// Assert stream tagging results
for _, namespace := range []string{oc.Namespace(), anotherNamespace} {
g.By("Checking tags in namespace: " + namespace)
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag:prod", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag2:prod1", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag2:prod2", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag2:prod3", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag2:prod4", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag5:prod5", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag6:prod6", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
_, err = oc.ImageClient().Image().ImageStreamTags(namespace).Get("multitag7:prod4", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred())
}
})
testImageStreamSCM := func(jobXMLFile string) {
jobName := "test-imagestream-scm"
g.By("creating a jenkins job with an imagestream SCM")
data := j.ProcessJenkinsJob(jobXMLFile, oc.Namespace())
j.CreateItem(jobName, data)
// Because polling is enabled, a job should start automatically and fail
// Wait for it to run and fail
tree := url.QueryEscape("jobs[name,color]")
xpath := url.QueryEscape("//job/name[text()='test-imagestream-scm']/../color")
jobStatusURI := "api/xml?tree=%s&xpath=%s"
g.By("waiting for initial job to complete")
wait.Poll(10*time.Second, 10*time.Minute, func() (bool, error) {
result, status, err := j.GetResource(jobStatusURI, tree, xpath)
o.Expect(err).NotTo(o.HaveOccurred())
if status == 200 && strings.Contains(result, "red") {
return true, nil
}
return false, nil
})
// Create a new imagestream tag and expect a job to be kicked off
// that will create a new tag in the current namespace
g.By("creating an imagestream tag in the current project")
oc.Run("tag").Args("openshift/jenkins:latest", fmt.Sprintf("%s/testimage:v1", oc.Namespace())).Execute()
// Wait after the image has been tagged for the Jenkins job to run
// and create the new imagestream/tag
g.By("verifying that the job ran by looking for the resulting imagestream tag")
err := exutil.TimedWaitForAnImageStreamTag(oc, oc.Namespace(), "localjenkins", "develop", 10*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
}
g.It("jenkins-plugin test imagestream SCM", func() {
testImageStreamSCM("imagestream-scm-job.xml")
})
g.It("jenkins-plugin test imagestream SCM DSL", func() {
testImageStreamSCM("imagestream-scm-dsl-job.xml")
})
g.It("jenkins-plugin test connection test", func() {
jobName := "test-build-job"
data := j.ProcessJenkinsJob("build-job.xml", oc.Namespace())
j.CreateItem(jobName, data)
g.By("trigger test connection logic, check for success")
filename, err := exutil.CreateTempFile("apiURL=&authToken=")
o.Expect(err).NotTo(o.HaveOccurred())
result, code, err := j.Post(filename, "job/test-build-job/descriptorByName/com.openshift.jenkins.plugins.pipeline.OpenShiftBuilder/testConnection", "application/x-www-form-urlencoded")
if code != 200 {
err = fmt.Errorf("Expected return code of 200")
}
if matched, _ := regexp.MatchString(".*Connection successful.*", result); !matched {
err = fmt.Errorf("Expecting 'Connection successful', Got: %s", result)
}
o.Expect(err).NotTo(o.HaveOccurred())
g.By("trigger test connection logic, check for failure")
filename, err = exutil.CreateTempFile("apiURL=https%3A%2F%2F1.2.3.4&authToken=")
o.Expect(err).NotTo(o.HaveOccurred())
result, code, err = j.Post(filename, "job/test-build-job/descriptorByName/com.openshift.jenkins.plugins.pipeline.OpenShiftBuilder/testConnection", "application/x-www-form-urlencoded")
if code != 200 {
err = fmt.Errorf("Expected return code of 200")
}
if matched, _ := regexp.MatchString(".*Connection unsuccessful.*", result); !matched {
err = fmt.Errorf("Expecting 'Connection unsuccessful', Got: %s", result)
}
o.Expect(err).NotTo(o.HaveOccurred())
})
})
})
})