Skip to content

Commit

Permalink
Fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknelson committed Feb 19, 2018
1 parent 83339ff commit 1caffe1
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions pkg/apb/watch_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package apb
import (
"testing"

"time"

"fmt"

core1 "k8s.io/api/core/v1"
Expand All @@ -28,7 +26,7 @@ func TestWatchPod(t *testing.T) {
PodClient func() (v1.PodInterface, *watch.FakeWatcher)
UpdatePodStates func(watcher *watch.FakeWatcher)
ExpectError bool
Validate func(status []JobState) error
Validate func(status []string) error
}{
{
Name: "should get error and state update when pod fails",
Expand Down Expand Up @@ -63,16 +61,13 @@ func TestWatchPod(t *testing.T) {
}}
podStateUpdater(watcher, podStates)
},
Validate: func(status []JobState) error {
Validate: func(status []string) error {
if len(status) != 2 {
return fmt.Errorf("expected 2 status updates")
}
for i, s := range status {
if s.Description != fmt.Sprintf("lastop%v", i) {
return fmt.Errorf("expected description to be lastop%v but got %v", i, s.Description)
}
if s.State != StateInProgress {
return fmt.Errorf("expected state to be %v but was %v", StateInProgress, s.State)
if s != fmt.Sprintf("lastop%v", i) {
return fmt.Errorf("expected description to be lastop%v but got %v", i, s)
}
}
return nil
Expand Down Expand Up @@ -111,16 +106,13 @@ func TestWatchPod(t *testing.T) {
podStateUpdater(watcher, podStates)

},
Validate: func(status []JobState) error {
Validate: func(status []string) error {
if len(status) != 2 {
return fmt.Errorf("expected 2 status updates")
}
for i, s := range status {
if s.Description != fmt.Sprintf("lastop%v", i) {
return fmt.Errorf("expected description to be lastop%v but got %v", i, s.Description)
}
if s.State != StateInProgress {
return fmt.Errorf("expected state to be %v but was %v", StateInProgress, s.State)
if s != fmt.Sprintf("lastop%v", i) {
return fmt.Errorf("expected description to be lastop%v but got %v", i, s)
}
}
return nil
Expand Down Expand Up @@ -150,16 +142,13 @@ func TestWatchPod(t *testing.T) {
watcher.Delete(podStates[0])
},
ExpectError: true,
Validate: func(status []JobState) error {
Validate: func(status []string) error {
if len(status) != 2 {
return fmt.Errorf("expected 2 status updates")
}
for _, s := range status {
if s.Description != "lastop0" {
return fmt.Errorf("expected description to be lastop0 but got %v", s.Description)
}
if s.State != StateInProgress {
return fmt.Errorf("expected state to be %v but was %v", StateInProgress, s.State)
if s != "lastop0" {
return fmt.Errorf("expected description to be lastop0 but got %v", s)
}
}
return nil
Expand All @@ -169,22 +158,25 @@ func TestWatchPod(t *testing.T) {

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
statusReceiver := make(chan JobState)
podClient, podWatch := tc.PodClient()
time.AfterFunc(100*time.Millisecond, func() {
close(statusReceiver)
})
var watchErr error
podClient, podWatch := tc.PodClient()
descriptions := []string{}
done := make(chan bool)

go func() {
watchErr = watchPod("test", "test", podClient, statusReceiver)
watchErr = watchPod("test", "test", podClient, func(d string) {
fmt.Printf("NSK: got d -> %v\n", d)
descriptions = append(descriptions, d)
})
done <- true
}()
go tc.UpdatePodStates(podWatch)
var state []JobState
for s := range statusReceiver {
state = append(state, s)
}

<-done

if nil != tc.Validate {
if err := tc.Validate(state); err != nil {
fmt.Printf("NSK: Now trying to validate the descriptions: %v", descriptions)
if err := tc.Validate(descriptions); err != nil {
t.Fatal("unexpected errror validating job state", err)
}
}
Expand Down

0 comments on commit 1caffe1

Please sign in to comment.