storage/etcd3/watcher_test: refactor TestWatchErrorWhenNoNewFunc to a table test
This commit is contained in:
		@@ -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
 | 
				
			||||||
 | 
							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)
 | 
								ctx, cancel := context.WithCancel(origCtx)
 | 
				
			||||||
			defer cancel()
 | 
								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)
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user