diff --git a/logs/pod_watcher_test.go b/logs/pod_watcher_test.go index e69c10b..ee5e834 100644 --- a/logs/pod_watcher_test.go +++ b/logs/pod_watcher_test.go @@ -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)) } }) } diff --git a/logs/watcher_test.go b/logs/watcher_test.go index 2427be6..6e24a87 100644 --- a/logs/watcher_test.go +++ b/logs/watcher_test.go @@ -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") }