Reduce test flakiness

This commit is contained in:
Rob Watson 2022-06-07 21:14:07 +02:00
parent 59a9335017
commit 6e09e4bf20
2 changed files with 8 additions and 7 deletions

View File

@ -174,7 +174,7 @@ func TestPodWatcher(t *testing.T) {
require.Contains(t, err.Error(), tc.wantErr)
}
if tc.wantOut != nil {
assert.ElementsMatch(t, tc.wantOut, splitBuf(&buf))
assert.ElementsMatch(t, tc.wantOut, bufToLines(&buf))
}
})
}

View File

@ -85,7 +85,8 @@ func TestWatcherClosedChannel(t *testing.T) {
}
func TestWatcherWithPodWatcher(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
deploymentsWatcher := watch.NewFake()
defer deploymentsWatcher.Stop()
@ -97,8 +98,6 @@ func TestWatcherWithPodWatcher(t *testing.T) {
clientset.PrependWatchReactor("deployments", k8stest.DefaultWatchReactor(deploymentsWatcher, nil))
go func() {
defer cancel()
deployment := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "mydeployment", Namespace: "default"}}
deploymentsWatcher.Add(deployment)
time.Sleep(time.Millisecond * 250)
@ -128,10 +127,12 @@ func TestWatcherWithPodWatcher(t *testing.T) {
watcher := logs.NewWatcher("mydeployment", "mycontainer", false, client, logs.NewPodWatcher, &buf)
err := watcher.Watch(ctx)
require.EqualError(t, err, context.Canceled.Error())
assert.ElementsMatch(t, []string{"[foo] fake logs", "[bar] fake logs"}, splitBuf(&buf))
require.EqualError(t, err, context.DeadlineExceeded.Error())
lines := bufToLines(&buf)
assert.Len(t, lines, 2)
assert.ElementsMatch(t, []string{"[foo] fake logs", "[bar] fake logs"}, bufToLines(&buf))
}
func splitBuf(buf *bytes.Buffer) []string {
func bufToLines(buf *bytes.Buffer) []string {
return strings.Split(strings.TrimSpace(buf.String()), "\n")
}