Adding tests for wait pkg (#1371)

This commit is contained in:
vyasgun 2021-07-08 23:15:23 +05:30 committed by GitHub
parent dfc8665d12
commit a7265a435e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -15,6 +15,7 @@
package wait package wait
import ( import (
"context"
"fmt" "fmt"
"sync" "sync"
"testing" "testing"
@ -84,6 +85,19 @@ type testCase struct {
watchResults []watch.Event 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) { func TestPollWatcher(t *testing.T) {
cases := []testCase{ cases := []testCase{
// Doesn't exist for a while, then does for a while. // Doesn't exist for a while, then does for a while.

View File

@ -15,6 +15,7 @@
package wait package wait
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -26,6 +27,7 @@ import (
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch" "k8s.io/apimachinery/pkg/watch"
"knative.dev/client/pkg/util"
"knative.dev/pkg/apis" "knative.dev/pkg/apis"
servingv1 "knative.dev/serving/pkg/apis/serving/v1" servingv1 "knative.dev/serving/pkg/apis/serving/v1"
) )
@ -73,7 +75,8 @@ func TestWaitCancellation(t *testing.T) {
func(obj runtime.Object) (apis.Conditions, error) { func(obj runtime.Object) (apis.Conditions, error) {
return apis.Conditions(obj.(*servingv1.Service).Status.Conditions), nil 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)) 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. // Test cases which consists of a series of events to send and the expected behaviour.
func prepareTestCases(name string) []waitForReadyTestCase { func prepareTestCases(name string) []waitForReadyTestCase {
return []waitForReadyTestCase{ return []waitForReadyTestCase{