Test all cases for LIST request cache bypass
This commit is contained in:
		@@ -154,6 +154,34 @@ func (d *dummyStorage) injectError(err error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestGetListCacheBypass(t *testing.T) {
 | 
					func TestGetListCacheBypass(t *testing.T) {
 | 
				
			||||||
 | 
						type testCase struct {
 | 
				
			||||||
 | 
							opts         storage.ListOptions
 | 
				
			||||||
 | 
							expectBypass bool
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						testCases := []testCase{
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: ""}, expectBypass: true},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "0"}, expectBypass: false},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "1"}, expectBypass: false},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: false},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true},
 | 
				
			||||||
 | 
							{opts: storage.ListOptions{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tc := range testCases {
 | 
				
			||||||
 | 
							testGetListCacheBypass(t, tc.opts, tc.expectBypass)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func testGetListCacheBypass(t *testing.T, options storage.ListOptions, expectBypass bool) {
 | 
				
			||||||
	backingStorage := &dummyStorage{}
 | 
						backingStorage := &dummyStorage{}
 | 
				
			||||||
	cacher, _, err := newTestCacher(backingStorage)
 | 
						cacher, _, err := newTestCacher(backingStorage)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -161,34 +189,21 @@ func TestGetListCacheBypass(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	defer cacher.Stop()
 | 
						defer cacher.Stop()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pred := storage.SelectionPredicate{
 | 
					 | 
				
			||||||
		Limit: 500,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	result := &example.PodList{}
 | 
						result := &example.PodList{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Wait until cacher is initialized.
 | 
						// Wait until cacher is initialized.
 | 
				
			||||||
	if err := cacher.ready.wait(context.Background()); err != nil {
 | 
						if err := cacher.ready.wait(context.Background()); err != nil {
 | 
				
			||||||
		t.Fatalf("unexpected error waiting for the cache to be ready")
 | 
							t.Fatalf("unexpected error waiting for the cache to be ready")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Inject error to underlying layer and check if cacher is not bypassed.
 | 
						// Inject error to underlying layer and check if cacher is not bypassed.
 | 
				
			||||||
	backingStorage.injectError(errDummy)
 | 
						backingStorage.injectError(errDummy)
 | 
				
			||||||
	err = cacher.GetList(context.TODO(), "pods/ns", storage.ListOptions{
 | 
						err = cacher.GetList(context.TODO(), "pods/ns", options, result)
 | 
				
			||||||
		ResourceVersion: "0",
 | 
						if err != nil && err != errDummy {
 | 
				
			||||||
		Predicate:       pred,
 | 
							t.Fatalf("Unexpected error for List request with options: %v, err: %v", options, err)
 | 
				
			||||||
		Recursive:       true,
 | 
					 | 
				
			||||||
	}, result)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Errorf("GetList with Limit and RV=0 should be served from cache: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						gotBypass := err == errDummy
 | 
				
			||||||
	err = cacher.GetList(context.TODO(), "pods/ns", storage.ListOptions{
 | 
						if gotBypass != expectBypass {
 | 
				
			||||||
		ResourceVersion: "",
 | 
							t.Errorf("Unexpected bypass result for List request with options %+v, bypass expected: %v, got: %v", options, expectBypass, gotBypass)
 | 
				
			||||||
		Predicate:       pred,
 | 
					 | 
				
			||||||
		Recursive:       true,
 | 
					 | 
				
			||||||
	}, result)
 | 
					 | 
				
			||||||
	if err != errDummy {
 | 
					 | 
				
			||||||
		t.Errorf("GetList with Limit without RV=0 should bypass cacher: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user