Merge pull request #120747 from p0lyn0mial/upstream-refactor-watch-error-test

storage/etcd3/watcher_test: refactor TestWatchErrorWhenNoNewFunc
This commit is contained in:
Kubernetes Prow Robot 2023-09-19 02:39:08 -07:00 committed by GitHub
commit ef1aed8cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,9 +29,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch" "k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/apis/example" "k8s.io/apiserver/pkg/apis/example"
"k8s.io/apiserver/pkg/features"
"k8s.io/apiserver/pkg/storage" "k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/etcd3/testserver" "k8s.io/apiserver/pkg/storage/etcd3/testserver"
storagetesting "k8s.io/apiserver/pkg/storage/testing" 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) { func TestWatch(t *testing.T) {
@ -150,25 +153,44 @@ func TestWatchErrResultNotBlockAfterCancel(t *testing.T) {
wg.Wait() wg.Wait()
} }
// TestWatchErrorWhenNoNewFunc checks if an error // TestWatchErrorIncorrectConfiguration checks if an error
// will be returned when establishing a watch // will be returned when the storage hasn't been properly
// with progressNotify options set // initialised for watch requests
// when newFunc wasn't provided func TestWatchErrorIncorrectConfiguration(t *testing.T) {
func TestWatchErrorWhenNoNewFunc(t *testing.T) { scenarios := []struct {
origCtx, store, _ := testSetup(t, func(opts *setupOptions) { opts.newFunc = nil }) name string
ctx, cancel := context.WithCancel(origCtx) setupFn func(opts *setupOptions)
defer cancel() 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}) w, err := store.watcher.Watch(ctx, "/abc", 0, scenario.requestOpts)
if err == nil { if err == nil {
t.Fatalf("expected an error but got none") t.Fatalf("expected an error but got none")
} }
if w != nil { if w != nil {
t.Fatalf("didn't expect a watcher because progress notifications cannot be delivered for a watcher without newFunc") t.Fatalf("didn't expect a watcher because the test assumes incorrect store initialisation")
} }
expectedError := apierrors.NewInternalError(errors.New("progressNotify for watch is unsupported by the etcd storage because no newFunc was provided")) if err.Error() != scenario.expectedErr.Error() {
if err.Error() != expectedError.Error() { t.Fatalf("unexpected err = %v, expected = %v", err, scenario.expectedErr)
t.Fatalf("unexpected err = %v, expected = %v", err, expectedError) }
})
} }
} }