forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
framework.go
1300 lines (1165 loc) · 43.3 KB
/
framework.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package util
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"
kapi "k8s.io/kubernetes/pkg/api"
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
batchv1 "k8s.io/kubernetes/pkg/apis/batch/v1"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
kbatchclient "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/batch/v1"
kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/clientset/typed/core/v1"
kinternalcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
"k8s.io/kubernetes/pkg/quota"
"k8s.io/kubernetes/test/e2e/framework"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/client"
deployapi "github.com/openshift/origin/pkg/deploy/apis/apps"
deployutil "github.com/openshift/origin/pkg/deploy/util"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
"github.com/openshift/origin/pkg/util/namer"
"github.com/openshift/origin/test/extended/testdata"
)
const pvPrefix = "pv-"
// WaitForOpenShiftNamespaceImageStreams waits for the standard set of imagestreams to be imported
func WaitForOpenShiftNamespaceImageStreams(oc *CLI) error {
langs := []string{"ruby", "nodejs", "perl", "php", "python", "wildfly", "mysql", "postgresql", "mongodb", "jenkins"}
scan := func() bool {
for _, lang := range langs {
fmt.Fprintf(g.GinkgoWriter, "Checking language %v \n", lang)
is, err := oc.Client().ImageStreams("openshift").Get(lang, metav1.GetOptions{})
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "ImageStream Error: %#v \n", err)
return false
}
for tag := range is.Spec.Tags {
fmt.Fprintf(g.GinkgoWriter, "Checking tag %v \n", tag)
if _, ok := is.Status.Tags[tag]; !ok {
fmt.Fprintf(g.GinkgoWriter, "Tag Error: %#v \n", ok)
return false
}
}
}
return true
}
success := false
for i := 0; i < 10; i++ {
fmt.Fprintf(g.GinkgoWriter, "Running scan #%v \n", i)
success = scan()
if success {
break
}
fmt.Fprintf(g.GinkgoWriter, "Sleeping for 3 seconds \n")
time.Sleep(3 * time.Second)
}
if success {
fmt.Fprintf(g.GinkgoWriter, "Success! \n")
return nil
}
DumpImageStreams(oc)
return fmt.Errorf("Failed to import expected imagestreams")
}
// CheckOpenShiftNamespaceImageStreams is a temporary workaround for the intermittent
// issue seen in extended tests where *something* is deleteing the pre-loaded, languange
// imagestreams from the OpenShift namespace
func CheckOpenShiftNamespaceImageStreams(oc *CLI) {
missing := false
langs := []string{"ruby", "nodejs", "perl", "php", "python", "wildfly", "mysql", "postgresql", "mongodb", "jenkins"}
for _, lang := range langs {
_, err := oc.Client().ImageStreams("openshift").Get(lang, metav1.GetOptions{})
if err != nil {
missing = true
break
}
}
if missing {
fmt.Fprint(g.GinkgoWriter, "\n\n openshift namespace image streams corrupted \n\n")
DumpImageStreams(oc)
out, err := oc.Run("get").Args("is", "-n", "openshift", "--config", KubeConfigPath()).Output()
err = fmt.Errorf("something has tampered with the image streams in the openshift namespace; look at audits in master log; \n%s\n", out)
o.Expect(err).NotTo(o.HaveOccurred())
} else {
fmt.Fprint(g.GinkgoWriter, "\n\n openshift namespace image streams OK \n\n")
}
}
//DumpImageStreams will dump both the openshift namespace and local namespace imagestreams
// as part of debugging when the language imagestreams in the openshift namespace seem to disappear
func DumpImageStreams(oc *CLI) {
out, err := oc.Run("get").Args("is", "-n", "openshift", "-o", "yaml", "--config", KubeConfigPath()).Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n imagestreams in openshift namespace: \n%s\n", out)
} else {
fmt.Fprintf(g.GinkgoWriter, "\n error on getting imagestreams in openshift namespace: %+v\n%#v\n", err, out)
}
out, err = oc.Run("get").Args("is", "-o", "yaml").Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n imagestreams in dynamic test namespace: \n%s\n", out)
} else {
fmt.Fprintf(g.GinkgoWriter, "\n error on getting imagestreams in dynamic test namespace: %+v\n%#v\n", err, out)
}
ids, err := ListImages()
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "\n got error on docker images %+v\n", err)
} else {
for _, id := range ids {
fmt.Fprintf(g.GinkgoWriter, " found local image %s\n", id)
}
}
}
// DumpBuildLogs will dump the latest build logs for a BuildConfig for debug purposes
func DumpBuildLogs(bc string, oc *CLI) {
buildOutput, err := oc.Run("logs").Args("-f", "bc/"+bc, "--timestamps").Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n\n build logs : %s\n\n", buildOutput)
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on build logs %+v\n\n", err)
}
// if we suspect that we are filling up the registry file system, call ExamineDiskUsage / ExaminePodDiskUsage
// also see if manipulations of the quota around /mnt/openshift-xfs-vol-dir exist in the extended test set up scripts
ExamineDiskUsage()
ExaminePodDiskUsage(oc)
}
func GetDeploymentConfigPods(oc *CLI, dcName string, version int64) (*kapiv1.PodList, error) {
return oc.KubeClient().CoreV1().Pods(oc.Namespace()).List(metav1.ListOptions{LabelSelector: ParseLabelsOrDie(fmt.Sprintf("%s=%s-%d", deployapi.DeployerPodForDeploymentLabel, dcName, version)).String()})
}
func GetApplicationPods(oc *CLI, dcName string) (*kapiv1.PodList, error) {
return oc.KubeClient().CoreV1().Pods(oc.Namespace()).List(metav1.ListOptions{LabelSelector: ParseLabelsOrDie(fmt.Sprintf("deploymentconfig=%s", dcName)).String()})
}
// DumpDeploymentLogs will dump the latest deployment logs for a DeploymentConfig for debug purposes
func DumpDeploymentLogs(dcName string, version int64, oc *CLI) {
fmt.Fprintf(g.GinkgoWriter, "Dumping deployment logs for deploymentconfig %q\n", dcName)
pods, err := GetDeploymentConfigPods(oc, dcName, version)
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "Unable to retrieve pods for deploymentconfig %q: %v\n", dcName, err)
return
}
DumpPodLogs(pods.Items, oc)
}
// DumpApplicationPodLogs will dump the latest application logs for a DeploymentConfig for debug purposes
func DumpApplicationPodLogs(dcName string, oc *CLI) {
fmt.Fprintf(g.GinkgoWriter, "Dumping application logs for deploymentconfig %q\n", dcName)
pods, err := GetApplicationPods(oc, dcName)
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "Unable to retrieve pods for deploymentconfig %q: %v\n", dcName, err)
return
}
DumpPodLogs(pods.Items, oc)
}
func DumpPodLogs(pods []kapiv1.Pod, oc *CLI) {
for _, pod := range pods {
descOutput, err := oc.Run("describe").Args("pod/" + pod.Name).Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "Describing pod %q\n%s\n\n", pod.Name, descOutput)
} else {
fmt.Fprintf(g.GinkgoWriter, "Error retrieving description for pod %q: %v\n\n", pod.Name, err)
}
depOutput, err := oc.Run("logs").Args("pod/" + pod.Name).Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "Log for pod %q\n---->\n%s\n<----end of log for %[1]q\n", pod.Name, depOutput)
} else {
fmt.Fprintf(g.GinkgoWriter, "Error retrieving logs for pod %q: %v\n\n", pod.Name, err)
}
}
}
// GetMasterThreadDump will get a golang thread stack dump
func GetMasterThreadDump(oc *CLI) {
out, err := oc.AsAdmin().Run("get").Args("--raw", "/debug/pprof/goroutine?debug=2").Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n\n Master thread stack dump:\n\n%s\n\n", string(out))
return
}
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on oc get --raw /debug/pprof/goroutine?godebug=2: %v\n\n", err)
}
// ExamineDiskUsage will dump df output on the testing system; leveraging this as part of diagnosing
// the registry's disk filling up during external tests on jenkins
func ExamineDiskUsage() {
out, err := exec.Command("/bin/df", "-m").Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n\n df -m output: %s\n\n", string(out))
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on df %v\n\n", err)
}
out, err = exec.Command("/bin/docker", "info").Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n\n docker info output: \n%s\n\n", string(out))
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on docker inspect %v\n\n", err)
}
}
// ExaminePodDiskUsage will dump df/du output on registry pod; leveraging this as part of diagnosing
// the registry's disk filling up during external tests on jenkins
func ExaminePodDiskUsage(oc *CLI) {
out, err := oc.Run("get").Args("pods", "-o", "json", "-n", "default", "--config", KubeConfigPath()).Output()
var podName string
if err == nil {
b := []byte(out)
var list kapiv1.PodList
err = json.Unmarshal(b, &list)
if err == nil {
for _, pod := range list.Items {
fmt.Fprintf(g.GinkgoWriter, "\n\n looking at pod %s \n\n", pod.ObjectMeta.Name)
if strings.Contains(pod.ObjectMeta.Name, "docker-registry-") && !strings.Contains(pod.ObjectMeta.Name, "deploy") {
podName = pod.ObjectMeta.Name
break
}
}
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got json unmarshal err: %v\n\n", err)
}
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on get pods: %v\n\n", err)
}
if len(podName) == 0 {
fmt.Fprintf(g.GinkgoWriter, "Unable to determine registry pod name, so we can't examine its disk usage.")
return
}
out, err = oc.Run("exec").Args("-n", "default", podName, "df", "--config", KubeConfigPath()).Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n\n df from registry pod: \n%s\n\n", out)
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on reg pod df: %v\n", err)
}
out, err = oc.Run("exec").Args("-n", "default", podName, "du", "/registry", "--config", KubeConfigPath()).Output()
if err == nil {
fmt.Fprintf(g.GinkgoWriter, "\n\n du from registry pod: \n%s\n\n", out)
} else {
fmt.Fprintf(g.GinkgoWriter, "\n\n got error on reg pod du: %v\n", err)
}
}
// VarSubOnFile reads in srcFile, finds instances of ${key} from the map
// and replaces them with their associated values.
func VarSubOnFile(srcFile string, destFile string, vars map[string]string) error {
srcData, err := ioutil.ReadFile(srcFile)
if err == nil {
srcString := string(srcData)
for k, v := range vars {
k = "${" + k + "}"
srcString = strings.Replace(srcString, k, v, -1) // -1 means unlimited replacements
}
err = ioutil.WriteFile(destFile, []byte(srcString), 0644)
}
return err
}
// StartBuild executes OC start-build with the specified arguments. StdOut and StdErr from the process
// are returned as separate strings.
func StartBuild(oc *CLI, args ...string) (stdout, stderr string, err error) {
stdout, stderr, err = oc.Run("start-build").Args(args...).Outputs()
fmt.Fprintf(g.GinkgoWriter, "\n\nstart-build output with args %v:\nError>%v\nStdOut>\n%s\nStdErr>\n%s\n\n", args, err, stdout, stderr)
return stdout, stderr, err
}
var buildPathPattern = regexp.MustCompile(`^build/([\w\-\._]+)$`)
type LogDumperFunc func(oc *CLI, br *BuildResult) (string, error)
func NewBuildResult(oc *CLI, build *buildapi.Build) *BuildResult {
return &BuildResult{
oc: oc,
BuildName: build.Name,
BuildPath: "builds/" + build.Name,
}
}
type BuildResult struct {
// BuildPath is a resource qualified name (e.g. "build/test-1").
BuildPath string
// BuildName is the non-resource qualified name.
BuildName string
// StartBuildStdErr is the StdErr output generated by oc start-build.
StartBuildStdErr string
// StartBuildStdOut is the StdOut output generated by oc start-build.
StartBuildStdOut string
// StartBuildErr is the error, if any, returned by the direct invocation of the start-build command.
StartBuildErr error
// The buildconfig which generated this build.
BuildConfigName string
// Build is the resource created. May be nil if there was a timeout.
Build *buildapi.Build
// BuildAttempt represents that a Build resource was created.
// false indicates a severe error unrelated to Build success or failure.
BuildAttempt bool
// BuildSuccess is true if the build was finshed successfully.
BuildSuccess bool
// BuildFailure is true if the build was finished with an error.
BuildFailure bool
// BuildCancelled is true if the build was canceled.
BuildCancelled bool
// BuildTimeout is true if there was a timeout waiting for the build to finish.
BuildTimeout bool
// Alternate log dumper function. If set, this is called instead of 'oc logs'
LogDumper LogDumperFunc
// The openshift client which created this build.
oc *CLI
}
// DumpLogs sends logs associated with this BuildResult to the GinkgoWriter.
func (t *BuildResult) DumpLogs() {
fmt.Fprintf(g.GinkgoWriter, "\n\n*****************************************\n")
fmt.Fprintf(g.GinkgoWriter, "Dumping Build Result: %#v\n", *t)
if t == nil {
fmt.Fprintf(g.GinkgoWriter, "No build result available!\n\n")
return
}
desc, err := t.oc.Run("describe").Args(t.BuildPath).Output()
fmt.Fprintf(g.GinkgoWriter, "\n** Build Description:\n")
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "Error during description retrieval: %+v\n", err)
} else {
fmt.Fprintf(g.GinkgoWriter, "%s\n", desc)
}
fmt.Fprintf(g.GinkgoWriter, "\n** Build Logs:\n")
buildOuput, err := t.Logs()
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "Error during log retrieval: %+v\n", err)
} else {
fmt.Fprintf(g.GinkgoWriter, "%s\n", buildOuput)
}
fmt.Fprintf(g.GinkgoWriter, "\n\n")
t.dumpRegistryLogs()
// if we suspect that we are filling up the registry file system, call ExamineDiskUsage / ExaminePodDiskUsage
// also see if manipulations of the quota around /mnt/openshift-xfs-vol-dir exist in the extended test set up scripts
/*
ExamineDiskUsage()
ExaminePodDiskUsage(t.oc)
fmt.Fprintf(g.GinkgoWriter, "\n\n")
*/
}
func (t *BuildResult) dumpRegistryLogs() {
var buildStarted *time.Time
oc := t.oc
fmt.Fprintf(g.GinkgoWriter, "\n** Registry Logs:\n")
if t.Build != nil && !t.Build.CreationTimestamp.IsZero() {
buildStarted = &t.Build.CreationTimestamp.Time
} else {
proj, err := oc.Client().Projects().Get(oc.Namespace(), metav1.GetOptions{})
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "Failed to get project %s: %v\n", oc.Namespace(), err)
} else {
buildStarted = &proj.CreationTimestamp.Time
}
}
if buildStarted == nil {
fmt.Fprintf(g.GinkgoWriter, "Could not determine test' start time\n\n\n")
return
}
since := time.Now().Sub(*buildStarted)
// Changing the namespace on the derived client still changes it on the original client
// because the kubeFramework field is only copied by reference. Saving the original namespace
// here so we can restore it when done with registry logs
savedNamespace := t.oc.Namespace()
oadm := t.oc.AsAdmin().SetNamespace("default")
out, err := oadm.Run("logs").Args("dc/docker-registry", "--since="+since.String()).Output()
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "Error during log retrieval: %+v\n", err)
} else {
fmt.Fprintf(g.GinkgoWriter, "%s\n", out)
}
t.oc.SetNamespace(savedNamespace)
fmt.Fprintf(g.GinkgoWriter, "\n\n")
}
// Logs returns the logs associated with this build.
func (t *BuildResult) Logs() (string, error) {
if t == nil || t.BuildPath == "" {
return "", fmt.Errorf("Not enough information to retrieve logs for %#v", *t)
}
if t.LogDumper != nil {
return t.LogDumper(t.oc, t)
}
buildOuput, err := t.oc.Run("logs").Args("-f", t.BuildPath, "--timestamps").Output()
if err != nil {
return "", fmt.Errorf("Error retrieving logs for %#v: %v", *t, err)
}
return buildOuput, nil
}
// Dumps logs and triggers a Ginkgo assertion if the build did NOT succeed.
func (t *BuildResult) AssertSuccess() *BuildResult {
if !t.BuildSuccess {
t.DumpLogs()
}
o.ExpectWithOffset(1, t.BuildSuccess).To(o.BeTrue())
return t
}
// Dumps logs and triggers a Ginkgo assertion if the build did NOT have an error (this will not assert on timeouts)
func (t *BuildResult) AssertFailure() *BuildResult {
if !t.BuildFailure {
t.DumpLogs()
}
o.ExpectWithOffset(1, t.BuildFailure).To(o.BeTrue())
return t
}
func StartBuildResult(oc *CLI, args ...string) (result *BuildResult, err error) {
args = append(args, "-o=name") // ensure that the build name is the only thing send to stdout
stdout, stderr, err := StartBuild(oc, args...)
// Usually, with -o=name, we only expect the build path.
// However, the caller may have added --follow which can add
// content to stdout. So just grab the first line.
buildPath := strings.TrimSpace(strings.Split(stdout, "\n")[0])
result = &BuildResult{
Build: nil,
BuildPath: buildPath,
StartBuildStdOut: stdout,
StartBuildStdErr: stderr,
StartBuildErr: nil,
BuildAttempt: false,
BuildSuccess: false,
BuildFailure: false,
BuildCancelled: false,
BuildTimeout: false,
oc: oc,
}
// An error here does not necessarily mean we could not run start-build. For example
// when --wait is specified, start-build returns an error if the build fails. Therefore,
// we continue to collect build information even if we see an error.
result.StartBuildErr = err
matches := buildPathPattern.FindStringSubmatch(buildPath)
if len(matches) != 2 {
return result, fmt.Errorf("Build path output did not match expected format 'build/name' : %q", buildPath)
}
result.BuildName = matches[1]
return result, nil
}
// StartBuildAndWait executes OC start-build with the specified arguments on an existing buildconfig.
// Note that start-build will be run with "-o=name" as a parameter when using this method.
// If no error is returned from this method, it means that the build attempted successfully, NOT that
// the build completed. For completion information, check the BuildResult object.
func StartBuildAndWait(oc *CLI, args ...string) (result *BuildResult, err error) {
result, err = StartBuildResult(oc, args...)
if err != nil {
return result, err
}
return result, WaitForBuildResult(oc.Client().Builds(oc.Namespace()), result)
}
// WaitForBuildResult updates result wit the state of the build
func WaitForBuildResult(c client.BuildInterface, result *BuildResult) error {
fmt.Fprintf(g.GinkgoWriter, "Waiting for %s to complete\n", result.BuildName)
err := WaitForABuild(c, result.BuildName,
func(b *buildapi.Build) bool {
result.Build = b
result.BuildSuccess = CheckBuildSuccessFn(b)
return result.BuildSuccess
},
func(b *buildapi.Build) bool {
result.Build = b
result.BuildFailure = CheckBuildFailedFn(b)
return result.BuildFailure
},
func(b *buildapi.Build) bool {
result.Build = b
result.BuildCancelled = CheckBuildCancelledFn(b)
return result.BuildCancelled
},
)
if result.Build == nil {
// We only abort here if the build progress was unobservable. Only known cause would be severe, non-build related error in WaitForABuild.
return fmt.Errorf("Severe error waiting for build: %v", err)
}
result.BuildAttempt = true
result.BuildTimeout = !(result.BuildFailure || result.BuildSuccess || result.BuildCancelled)
fmt.Fprintf(g.GinkgoWriter, "Done waiting for %s: %#v\n with error: %v\n", result.BuildName, *result, err)
return nil
}
// WaitForABuild waits for a Build object to match either isOK or isFailed conditions.
func WaitForABuild(c client.BuildInterface, name string, isOK, isFailed, isCanceled func(*buildapi.Build) bool) error {
if isOK == nil {
isOK = CheckBuildSuccessFn
}
if isFailed == nil {
isFailed = CheckBuildFailedFn
}
if isCanceled == nil {
isCanceled = CheckBuildCancelledFn
}
// wait 2 minutes for build to exist
err := wait.Poll(1*time.Second, 2*time.Minute, func() (bool, error) {
if _, err := c.Get(name, metav1.GetOptions{}); err != nil {
return false, nil
}
return true, nil
})
if err == wait.ErrWaitTimeout {
return fmt.Errorf("Timed out waiting for build %q to be created", name)
}
if err != nil {
return err
}
// wait longer for the build to run to completion
err = wait.Poll(5*time.Second, 60*time.Minute, func() (bool, error) {
list, err := c.List(metav1.ListOptions{FieldSelector: fields.Set{"name": name}.AsSelector().String()})
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "error listing builds: %v", err)
return false, err
}
for i := range list.Items {
if name == list.Items[i].Name && (isOK(&list.Items[i]) || isCanceled(&list.Items[i])) {
return true, nil
}
if name != list.Items[i].Name {
return false, fmt.Errorf("While listing builds named %s, found unexpected build %#v", name, list.Items[i])
}
if isFailed(&list.Items[i]) {
return false, fmt.Errorf("The build %q status is %q", name, list.Items[i].Status.Phase)
}
}
return false, nil
})
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "WaitForABuild returning with error: %v", err)
}
if err == wait.ErrWaitTimeout {
return fmt.Errorf("Timed out waiting for build %q to complete", name)
}
return err
}
// CheckBuildSuccessFn returns true if the build succeeded
var CheckBuildSuccessFn = func(b *buildapi.Build) bool {
return b.Status.Phase == buildapi.BuildPhaseComplete
}
// CheckBuildFailedFn return true if the build failed
var CheckBuildFailedFn = func(b *buildapi.Build) bool {
return b.Status.Phase == buildapi.BuildPhaseFailed || b.Status.Phase == buildapi.BuildPhaseError
}
// CheckBuildCancelledFn return true if the build was canceled
var CheckBuildCancelledFn = func(b *buildapi.Build) bool {
return b.Status.Phase == buildapi.BuildPhaseCancelled
}
// WaitForBuilderAccount waits until the builder service account gets fully
// provisioned
func WaitForBuilderAccount(c kcoreclient.ServiceAccountInterface) error {
waitFn := func() (bool, error) {
sc, err := c.Get("builder", metav1.GetOptions{})
if err != nil {
// If we can't access the service accounts, let's wait till the controller
// create it.
if errors.IsForbidden(err) {
return false, nil
}
return false, err
}
for _, s := range sc.Secrets {
if strings.Contains(s.Name, "dockercfg") {
return true, nil
}
}
return false, nil
}
return wait.Poll(time.Duration(100*time.Millisecond), 1*time.Minute, waitFn)
}
// WaitForAnImageStream waits for an ImageStream to fulfill the isOK function
func WaitForAnImageStream(client client.ImageStreamInterface,
name string,
isOK, isFailed func(*imageapi.ImageStream) bool) error {
for {
list, err := client.List(metav1.ListOptions{FieldSelector: fields.Set{"name": name}.AsSelector().String()})
if err != nil {
return err
}
for i := range list.Items {
if isOK(&list.Items[i]) {
return nil
}
if isFailed(&list.Items[i]) {
return fmt.Errorf("The image stream %q status is %q",
name, list.Items[i].Annotations[imageapi.DockerImageRepositoryCheckAnnotation])
}
}
rv := list.ResourceVersion
w, err := client.Watch(metav1.ListOptions{FieldSelector: fields.Set{"name": name}.AsSelector().String(), ResourceVersion: rv})
if err != nil {
return err
}
defer w.Stop()
for {
val, ok := <-w.ResultChan()
if !ok {
// reget and re-watch
break
}
if e, ok := val.Object.(*imageapi.ImageStream); ok {
if isOK(e) {
return nil
}
if isFailed(e) {
return fmt.Errorf("The image stream %q status is %q",
name, e.Annotations[imageapi.DockerImageRepositoryCheckAnnotation])
}
}
}
}
}
// WaitForAnImageStreamTag waits until an image stream with given name has non-empty history for given tag.
// Defaults to waiting for 300 seconds
func WaitForAnImageStreamTag(oc *CLI, namespace, name, tag string) error {
return TimedWaitForAnImageStreamTag(oc, namespace, name, tag, time.Second*300)
}
// TimedWaitForAnImageStreamTag waits until an image stream with given name has non-empty history for given tag.
// Gives up waiting after the specified waitTimeout
func TimedWaitForAnImageStreamTag(oc *CLI, namespace, name, tag string, waitTimeout time.Duration) error {
g.By(fmt.Sprintf("waiting for an is importer to import a tag %s into a stream %s", tag, name))
start := time.Now()
c := make(chan error)
go func() {
err := WaitForAnImageStream(
oc.Client().ImageStreams(namespace),
name,
func(is *imageapi.ImageStream) bool {
if history, exists := is.Status.Tags[tag]; !exists || len(history.Items) == 0 {
return false
}
return true
},
func(is *imageapi.ImageStream) bool {
return time.Now().After(start.Add(waitTimeout))
})
c <- err
}()
select {
case e := <-c:
return e
case <-time.After(waitTimeout):
return fmt.Errorf("timed out while waiting of an image stream tag %s/%s:%s", namespace, name, tag)
}
}
// CheckImageStreamLatestTagPopulatedFn returns true if the imagestream has a ':latest' tag filed
var CheckImageStreamLatestTagPopulatedFn = func(i *imageapi.ImageStream) bool {
_, ok := i.Status.Tags["latest"]
return ok
}
// CheckImageStreamTagNotFoundFn return true if the imagestream update was not successful
var CheckImageStreamTagNotFoundFn = func(i *imageapi.ImageStream) bool {
return strings.Contains(i.Annotations[imageapi.DockerImageRepositoryCheckAnnotation], "not") ||
strings.Contains(i.Annotations[imageapi.DockerImageRepositoryCheckAnnotation], "error")
}
// WaitForDeploymentConfig waits for a DeploymentConfig to complete transition
// to a given version and report minimum availability.
func WaitForDeploymentConfig(kc kclientset.Interface, oc client.Interface, namespace, name string, version int64, cli *CLI) error {
fmt.Fprintf(g.GinkgoWriter, "waiting for deploymentconfig %s/%s to be available with version %d\n", namespace, name, version)
var dc *deployapi.DeploymentConfig
start := time.Now()
err := wait.Poll(time.Second, 15*time.Minute, func() (done bool, err error) {
dc, err = oc.DeploymentConfigs(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return false, err
}
// TODO re-enable this check once @mfojtik introduces a test that ensures we'll only ever get
// exactly one deployment triggered.
/*
if dc.Status.LatestVersion > version {
return false, fmt.Errorf("latestVersion %d passed %d", dc.Status.LatestVersion, version)
}
*/
if dc.Status.LatestVersion < version {
return false, nil
}
var progressing, available *deployapi.DeploymentCondition
for i, condition := range dc.Status.Conditions {
switch condition.Type {
case deployapi.DeploymentProgressing:
progressing = &dc.Status.Conditions[i]
case deployapi.DeploymentAvailable:
available = &dc.Status.Conditions[i]
}
}
if progressing != nil && progressing.Status == kapi.ConditionFalse {
return false, fmt.Errorf("not progressing")
}
if progressing != nil &&
progressing.Status == kapi.ConditionTrue &&
progressing.Reason == deployapi.NewRcAvailableReason &&
available != nil &&
available.Status == kapi.ConditionTrue {
return true, nil
}
return false, nil
})
if err != nil {
fmt.Fprintf(g.GinkgoWriter, "got error %q when waiting for deploymentconfig %s/%s to be available with version %d\n", err, namespace, name, version)
cli.Run("get").Args("dc", dc.Name, "-o", "yaml").Execute()
DumpDeploymentLogs(name, version, cli)
DumpApplicationPodLogs(name, cli)
return err
}
requirement, err := labels.NewRequirement(deployapi.DeploymentLabel, selection.Equals, []string{deployutil.LatestDeploymentNameForConfig(dc)})
if err != nil {
return err
}
podnames, err := GetPodNamesByFilter(kc.CoreV1().Pods(namespace), labels.NewSelector().Add(*requirement), func(kapiv1.Pod) bool { return true })
if err != nil {
return err
}
fmt.Fprintf(g.GinkgoWriter, "deploymentconfig %s/%s available after %s\npods: %s\n", namespace, name, time.Now().Sub(start), strings.Join(podnames, ", "))
return nil
}
func isUsageSynced(received, expected kapi.ResourceList, expectedIsUpperLimit bool) bool {
resourceNames := quota.ResourceNames(expected)
masked := quota.Mask(received, resourceNames)
if len(masked) != len(expected) {
return false
}
if expectedIsUpperLimit {
if le, _ := quota.LessThanOrEqual(masked, expected); !le {
return false
}
} else {
if le, _ := quota.LessThanOrEqual(expected, masked); !le {
return false
}
}
return true
}
// WaitForResourceQuotaSync watches given resource quota until its usage is updated to desired level or a
// timeout occurs. If successful, used quota values will be returned for expected resources. Otherwise an
// ErrWaitTimeout will be returned. If expectedIsUpperLimit is true, given expected usage must compare greater
// or equal to quota's usage, which is useful for expected usage increment. Otherwise expected usage must
// compare lower or equal to quota's usage, which is useful for expected usage decrement.
func WaitForResourceQuotaSync(
client kinternalcoreclient.ResourceQuotaInterface,
name string,
expectedUsage kapi.ResourceList,
expectedIsUpperLimit bool,
timeout time.Duration,
) (kapi.ResourceList, error) {
startTime := time.Now()
endTime := startTime.Add(timeout)
expectedResourceNames := quota.ResourceNames(expectedUsage)
list, err := client.List(metav1.ListOptions{FieldSelector: fields.Set{"metadata.name": name}.AsSelector().String()})
if err != nil {
return nil, err
}
for i := range list.Items {
used := quota.Mask(list.Items[i].Status.Used, expectedResourceNames)
if isUsageSynced(used, expectedUsage, expectedIsUpperLimit) {
return used, nil
}
}
rv := list.ResourceVersion
w, err := client.Watch(metav1.ListOptions{FieldSelector: fields.Set{"metadata.name": name}.AsSelector().String(), ResourceVersion: rv})
if err != nil {
return nil, err
}
defer w.Stop()
for time.Now().Before(endTime) {
select {
case val, ok := <-w.ResultChan():
if !ok {
// reget and re-watch
continue
}
if rq, ok := val.Object.(*kapi.ResourceQuota); ok {
used := quota.Mask(rq.Status.Used, expectedResourceNames)
if isUsageSynced(used, expectedUsage, expectedIsUpperLimit) {
return used, nil
}
}
case <-time.After(endTime.Sub(time.Now())):
return nil, wait.ErrWaitTimeout
}
}
return nil, wait.ErrWaitTimeout
}
// GetPodNamesByFilter looks up pods that satisfy the predicate and returns their names.
func GetPodNamesByFilter(c kcoreclient.PodInterface, label labels.Selector, predicate func(kapiv1.Pod) bool) (podNames []string, err error) {
podList, err := c.List(metav1.ListOptions{LabelSelector: label.String()})
if err != nil {
return nil, err
}
for _, pod := range podList.Items {
if predicate(pod) {
podNames = append(podNames, pod.Name)
}
}
return podNames, nil
}
func WaitForAJob(c kbatchclient.JobInterface, name string, timeout time.Duration) error {
return wait.Poll(1*time.Second, timeout, func() (bool, error) {
j, e := c.Get(name, metav1.GetOptions{})
if e != nil {
return true, e
}
// TODO soltysh: replace this with a function once such exist, currently
// it's private in the controller
for _, c := range j.Status.Conditions {
if (c.Type == batchv1.JobComplete || c.Type == batchv1.JobFailed) && c.Status == kapiv1.ConditionTrue {
return true, nil
}
}
return false, nil
})
}
// WaitForPods waits until given number of pods that match the label selector and
// satisfy the predicate are found
func WaitForPods(c kcoreclient.PodInterface, label labels.Selector, predicate func(kapiv1.Pod) bool, count int, timeout time.Duration) ([]string, error) {
var podNames []string
err := wait.Poll(1*time.Second, timeout, func() (bool, error) {
p, e := GetPodNamesByFilter(c, label, predicate)
if e != nil {
return true, e
}
if len(p) != count {
return false, nil
}
podNames = p
return true, nil
})
return podNames, err
}
// CheckPodIsRunningFn returns true if the pod is running
var CheckPodIsRunningFn = func(pod kapiv1.Pod) bool {
return pod.Status.Phase == kapiv1.PodRunning
}
// CheckPodIsSucceededFn returns true if the pod status is "Succdeded"
var CheckPodIsSucceededFn = func(pod kapiv1.Pod) bool {
return pod.Status.Phase == kapiv1.PodSucceeded
}
// CheckPodIsReadyFn returns true if the pod's ready probe determined that the pod is ready.
var CheckPodIsReadyFn = func(pod kapiv1.Pod) bool {
if pod.Status.Phase != kapiv1.PodRunning {
return false
}
for _, cond := range pod.Status.Conditions {
if cond.Type != kapiv1.PodReady {
continue
}
return cond.Status == kapiv1.ConditionTrue
}
return false
}
// WaitUntilPodIsGone waits until the named Pod will disappear
func WaitUntilPodIsGone(c kcoreclient.PodInterface, podName string, timeout time.Duration) error {
return wait.Poll(1*time.Second, timeout, func() (bool, error) {
_, err := c.Get(podName, metav1.GetOptions{})
if err != nil {
if strings.Contains(err.Error(), "not found") {
return true, nil
}
return true, err
}
return false, nil
})
}
// GetDockerImageReference retrieves the full Docker pull spec from the given ImageStream
// and tag
func GetDockerImageReference(c client.ImageStreamInterface, name, tag string) (string, error) {
imageStream, err := c.Get(name, metav1.GetOptions{})
if err != nil {
return "", err
}
isTag, ok := imageStream.Status.Tags[tag]
if !ok {
return "", fmt.Errorf("ImageStream %q does not have tag %q", name, tag)
}
if len(isTag.Items) == 0 {
return "", fmt.Errorf("ImageStreamTag %q is empty", tag)
}
return isTag.Items[0].DockerImageReference, nil
}
// GetPodForContainer creates a new Pod that runs specified container
func GetPodForContainer(container kapiv1.Container) *kapiv1.Pod {
name := namer.GetPodName("test-pod", string(uuid.NewUUID()))
return &kapiv1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{"name": name},
},
Spec: kapiv1.PodSpec{
Containers: []kapiv1.Container{container},
RestartPolicy: kapiv1.RestartPolicyNever,
},
}
}