diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go index c5edcc6a0af..902750fe1fd 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher_test.go @@ -29,9 +29,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/watch" "k8s.io/apiserver/pkg/apis/example" + "k8s.io/apiserver/pkg/features" "k8s.io/apiserver/pkg/storage" "k8s.io/apiserver/pkg/storage/etcd3/testserver" storagetesting "k8s.io/apiserver/pkg/storage/testing" + utilfeature "k8s.io/apiserver/pkg/util/feature" + featuregatetesting "k8s.io/component-base/featuregate/testing" ) func TestWatch(t *testing.T) { @@ -150,25 +153,44 @@ func TestWatchErrResultNotBlockAfterCancel(t *testing.T) { wg.Wait() } -// TestWatchErrorWhenNoNewFunc checks if an error -// will be returned when establishing a watch -// with progressNotify options set -// when newFunc wasn't provided -func TestWatchErrorWhenNoNewFunc(t *testing.T) { - origCtx, store, _ := testSetup(t, func(opts *setupOptions) { opts.newFunc = nil }) - ctx, cancel := context.WithCancel(origCtx) - defer cancel() +// TestWatchErrorIncorrectConfiguration checks if an error +// will be returned when the storage hasn't been properly +// initialised for watch requests +func TestWatchErrorIncorrectConfiguration(t *testing.T) { + scenarios := []struct { + name string + setupFn func(opts *setupOptions) + requestOpts storage.ListOptions + enableWatchList bool + expectedErr error + }{ + { + name: "no newFunc provided", + setupFn: func(opts *setupOptions) { opts.newFunc = nil }, + requestOpts: storage.ListOptions{ProgressNotify: true}, + expectedErr: apierrors.NewInternalError(errors.New("progressNotify for watch is unsupported by the etcd storage because no newFunc was provided")), + }, + } + for _, scenario := range scenarios { + t.Run(scenario.name, func(t *testing.T) { + if scenario.enableWatchList { + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.WatchList, true)() + } + origCtx, store, _ := testSetup(t, scenario.setupFn) + ctx, cancel := context.WithCancel(origCtx) + defer cancel() - w, err := store.watcher.Watch(ctx, "/abc", 0, storage.ListOptions{ProgressNotify: true}) - if err == nil { - t.Fatalf("expected an error but got none") - } - if w != nil { - t.Fatalf("didn't expect a watcher because progress notifications cannot be delivered for a watcher without newFunc") - } - expectedError := apierrors.NewInternalError(errors.New("progressNotify for watch is unsupported by the etcd storage because no newFunc was provided")) - if err.Error() != expectedError.Error() { - t.Fatalf("unexpected err = %v, expected = %v", err, expectedError) + w, err := store.watcher.Watch(ctx, "/abc", 0, scenario.requestOpts) + if err == nil { + t.Fatalf("expected an error but got none") + } + if w != nil { + t.Fatalf("didn't expect a watcher because the test assumes incorrect store initialisation") + } + if err.Error() != scenario.expectedErr.Error() { + t.Fatalf("unexpected err = %v, expected = %v", err, scenario.expectedErr) + } + }) } }