From a7265a435eb4018b30f81d7dc5b07c163a531dce Mon Sep 17 00:00:00 2001 From: vyasgun <87057902+vyasgun@users.noreply.github.com> Date: Thu, 8 Jul 2021 23:15:23 +0530 Subject: [PATCH] Adding tests for wait pkg (#1371) --- pkg/wait/poll_watcher_test.go | 14 ++++++++++++++ pkg/wait/wait_for_ready_test.go | 14 +++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/wait/poll_watcher_test.go b/pkg/wait/poll_watcher_test.go index bba87a89d..707c8d1fb 100644 --- a/pkg/wait/poll_watcher_test.go +++ b/pkg/wait/poll_watcher_test.go @@ -15,6 +15,7 @@ package wait import ( + "context" "fmt" "sync" "testing" @@ -84,6 +85,19 @@ type testCase struct { watchResults []watch.Event } +func TestNewWatcherWithVersion(t *testing.T) { + w, err := NewWatcherWithVersion(context.Background(), watchF(func(context.Context, metav1.ListOptions) (watch.Interface, error) { + return NewFakeWatch([]watch.Event{}), nil + }), nil, "mockNamespace", "resourceName", "mockName", "v1", 5*time.Second) + w.Stop() + assert.NilError(t, err) + w, err = NewWatcherWithVersion(context.Background(), watchF(func(context.Context, metav1.ListOptions) (watch.Interface, error) { + return NewFakeWatch([]watch.Event{}), fmt.Errorf("mockErrMsg") + }), nil, "mockNamespace", "resourceName", "mockName", "v1", 5*time.Second) + w.Stop() + assert.NilError(t, err) +} + func TestPollWatcher(t *testing.T) { cases := []testCase{ // Doesn't exist for a while, then does for a while. diff --git a/pkg/wait/wait_for_ready_test.go b/pkg/wait/wait_for_ready_test.go index b40b493f5..92bd0c5ff 100644 --- a/pkg/wait/wait_for_ready_test.go +++ b/pkg/wait/wait_for_ready_test.go @@ -15,6 +15,7 @@ package wait import ( + "bytes" "context" "errors" "fmt" @@ -26,6 +27,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/watch" + "knative.dev/client/pkg/util" "knative.dev/pkg/apis" servingv1 "knative.dev/serving/pkg/apis/serving/v1" ) @@ -73,7 +75,8 @@ func TestWaitCancellation(t *testing.T) { func(obj runtime.Object) (apis.Conditions, error) { return apis.Conditions(obj.(*servingv1.Service).Status.Conditions), nil }) - err, _ = wfr.Wait(ctx, "foobar", "", Options{Timeout: &timeout}, NoopMessageCallback()) + window := 2 * time.Second + err, _ = wfr.Wait(ctx, "foobar", "", Options{Timeout: nil, ErrorWindow: &window}, NoopMessageCallback()) assert.Assert(t, errors.Is(err, context.Canceled)) } @@ -253,6 +256,15 @@ func TestAddWaitForDelete(t *testing.T) { } } +func TestSimpleMessageCallback(t *testing.T) { + var out bytes.Buffer + callback := SimpleMessageCallback(&out) + callback(5*time.Second, "hello") + assert.Assert(t, util.ContainsAll(out.String(), "hello")) + callback(5*time.Second, "hello") + assert.Assert(t, util.ContainsAll(out.String(), "...")) +} + // Test cases which consists of a series of events to send and the expected behaviour. func prepareTestCases(name string) []waitForReadyTestCase { return []waitForReadyTestCase{