Skip to content

Commit 16bd84e

Browse files
authored
Merge pull request #25 from fasterci/ap/early_it_termination
more conditions to terminate integration test earlier
2 parents 055f2e7 + 371d8ee commit 16bd84e

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

testing/it_sidecar/it_sidecar.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func contains(v []string, item string) bool {
9191
}
9292
return false
9393
}
94+
9495
// listReadyApps converts a list returned from podsInformer.GetStore().List() to a map containing apps with ready status
9596
// app is determined by app label
9697
func listReadyApps(list []interface{}) (readypods, notReady []string) {
@@ -138,7 +139,7 @@ func listenForEvents(ctx context.Context, clientset *kubernetes.Clientset, onFai
138139
return
139140
}
140141
log.Printf("EVENT %s %s %s %s\n", event.Namespace, event.InvolvedObject.Name, event.Reason, event.Message)
141-
if event.Reason == "Failed" {
142+
if event.Reason == "Failed" || event.Reason == "BackOff" {
142143
onFailure(event)
143144
}
144145
}
@@ -370,7 +371,13 @@ func main() {
370371
clientset = kubernetes.NewForConfigOrDie(config)
371372
defer cleanup(clientset)
372373

373-
go stern.Run(ctx, *namespace, clientset)
374+
go func() {
375+
err := stern.Run(ctx, *namespace, clientset, allowErrors)
376+
if err != nil {
377+
log.Print(err)
378+
}
379+
cancel()
380+
}()
374381

375382
listenForEvents(ctx, clientset, func(event *v1.Event) {
376383
if !allowErrors {

testing/it_sidecar/stern/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import (
2323
)
2424

2525
// Run starts the main run loop
26-
func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset) error {
26+
func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset, allowErrors bool) error {
2727

2828
tails := make(map[string]*Tail)
2929

30-
err := Watch(ctx, clientset.CoreV1().Pods(namespace), RUNNING, labels.Everything(), func(p *Target) {
30+
err := Watch(ctx, clientset.CoreV1().Pods(namespace), RUNNING, labels.Everything(), allowErrors, func(p *Target) {
3131
id := p.GetID()
3232
if tails[id] != nil {
3333
return
@@ -45,7 +45,7 @@ func Run(ctx context.Context, namespace string, clientset *kubernetes.Clientset)
4545
delete(tails, id)
4646
})
4747
if err != nil {
48-
return fmt.Errorf("failed to set up watch: %v", err)
48+
return fmt.Errorf("watch error: %v", err)
4949
}
5050

5151
return nil

testing/it_sidecar/stern/watch.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (t *Target) GetID() string {
4141
// Watch starts listening to Kubernetes events and emits modified
4242
// containers/pods. The first result is targets added, the second is targets
4343
// removed
44-
func Watch(ctx context.Context, i v1.PodInterface, containerState ContainerState, labelSelector labels.Selector, onAdded, onRemoved func(*Target)) error {
44+
func Watch(ctx context.Context, i v1.PodInterface, containerState ContainerState, labelSelector labels.Selector, allowErrors bool, onAdded, onRemoved func(*Target)) error {
4545
watcher, err := i.Watch(ctx, metav1.ListOptions{Watch: true, LabelSelector: labelSelector.String()})
4646
if err != nil {
4747
return fmt.Errorf("failed to set up watch: %s", err)
@@ -83,6 +83,12 @@ func Watch(ctx context.Context, i v1.PodInterface, containerState ContainerState
8383

8484
log.Print("container ", c.Name, " has state ", c.State)
8585

86+
if !allowErrors {
87+
if t := c.State.Terminated; t != nil && t.ExitCode != 0 && t.Reason == "Error" {
88+
return fmt.Errorf("container %s failed with exit code %d and reason '%s'", c.Name, t.ExitCode, t.Reason)
89+
}
90+
}
91+
8692
if containerState.Match(c.State) {
8793
onAdded(&Target{
8894
Namespace: pod.Namespace,

0 commit comments

Comments
 (0)