storage/etcd3/watcher_test: refactor TestWatchErrorWhenNoNewFunc to a table test

This commit is contained in:
Lukasz Szaszkiewicz 2023-09-19 10:35:09 +02:00
parent 12dc19d46f
commit 4b60c18183

View File

@ -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)
}
})
}
}