Skip to content

Commit 1b2bacd

Browse files
committed
Only test requests that pass validation
1 parent 75531cc commit 1b2bacd

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131

3232
"github.com/stretchr/testify/require"
3333
"google.golang.org/grpc/metadata"
34+
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
35+
"k8s.io/apimachinery/pkg/apis/meta/internalversion/validation"
3436

3537
apiequality "k8s.io/apimachinery/pkg/api/equality"
3638
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -205,64 +207,71 @@ func TestGetListCacheBypass(t *testing.T) {
205207
Continue string
206208
}
207209
testCases := map[opts]bool{}
210+
testCases[opts{}] = false
211+
testCases[opts{Limit: 100}] = false
212+
testCases[opts{Continue: "continue"}] = true
213+
testCases[opts{Limit: 100, Continue: "continue"}] = true
214+
testCases[opts{ResourceVersion: "0"}] = false
215+
testCases[opts{ResourceVersion: "0", Limit: 100}] = false
216+
testCases[opts{ResourceVersion: "0", Continue: "continue"}] = true
217+
testCases[opts{ResourceVersion: "0", Limit: 100, Continue: "continue"}] = true
218+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
219+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
220+
testCases[opts{ResourceVersion: "1"}] = false
221+
testCases[opts{ResourceVersion: "1", Limit: 100}] = true
222+
testCases[opts{ResourceVersion: "1", Continue: "continue"}] = true
223+
testCases[opts{ResourceVersion: "1", Limit: 100, Continue: "continue"}] = true
224+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
225+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
226+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
227+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
228+
208229
for _, rv := range []string{"", "0", "1"} {
209-
for _, match := range []metav1.ResourceVersionMatch{"", "Invalid", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan} {
230+
for _, match := range []metav1.ResourceVersionMatch{"", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan} {
210231
for _, c := range []string{"", "continue"} {
211232
for _, limit := range []int64{0, 100} {
212-
testCases[opts{
233+
errs := validation.ValidateListOptions(&internalversion.ListOptions{
213234
ResourceVersion: rv,
214235
ResourceVersionMatch: match,
236+
Limit: limit,
215237
Continue: c,
238+
}, false)
239+
if len(errs) != 0 {
240+
continue
241+
}
242+
opt := opts{
243+
ResourceVersion: rv,
244+
ResourceVersionMatch: match,
216245
Limit: limit,
217-
}] = false
246+
Continue: c,
247+
}
248+
_, found := testCases[opt]
249+
if !found {
250+
t.Errorf("Test case not covered, but passes validation: %+v", opt)
251+
}
252+
218253
}
219254
}
220255
}
221256
}
222-
testCases[opts{ResourceVersion: "0", Continue: "continue"}] = true
223-
testCases[opts{ResourceVersion: "0", Limit: 100, Continue: "continue"}] = true
224-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
225-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
226-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Continue: "continue"}] = true
227-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100, Continue: "continue"}] = true
228-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Continue: "continue"}] = true
229-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100, Continue: "continue"}] = true
230-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid"}] = true
231-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid", Continue: "continue"}] = true
232-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid", Limit: 100}] = true
233-
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid", Limit: 100, Continue: "continue"}] = true
234-
testCases[opts{ResourceVersion: "1", Limit: 100}] = true
235-
testCases[opts{ResourceVersion: "1", Continue: "continue"}] = true
236-
testCases[opts{ResourceVersion: "1", Limit: 100, Continue: "continue"}] = true
237-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Continue: "continue"}] = true
238-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100, Continue: "continue"}] = true
239-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
240-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
241-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Continue: "continue"}] = true
242-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100, Continue: "continue"}] = true
243-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid"}] = true
244-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid", Continue: "continue"}] = true
245-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid", Limit: 100}] = true
246-
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid", Limit: 100, Continue: "continue"}] = true
247-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = true
248-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Continue: "continue"}] = true
249-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100, Continue: "continue"}] = true
250-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
251-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact, Continue: "continue"}] = true
252-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100, Continue: "continue"}] = true
253-
testCases[opts{ResourceVersionMatch: "Invalid"}] = true
254-
testCases[opts{ResourceVersionMatch: "Invalid", Continue: "continue"}] = true
255-
testCases[opts{ResourceVersionMatch: "Invalid", Limit: 100}] = true
256-
testCases[opts{ResourceVersionMatch: "Invalid", Limit: 100, Continue: "continue"}] = true
257-
testCases[opts{Limit: 100, Continue: "continue"}] = true
258-
testCases[opts{Continue: "continue"}] = true
257+
258+
for opt := range testCases {
259+
errs := validation.ValidateListOptions(&internalversion.ListOptions{
260+
ResourceVersion: opt.ResourceVersion,
261+
ResourceVersionMatch: opt.ResourceVersionMatch,
262+
Limit: opt.Limit,
263+
Continue: opt.Continue,
264+
}, false)
265+
if len(errs) != 0 {
266+
t.Errorf("Invalid LIST request that should not be tested %+v", opt)
267+
continue
268+
}
269+
}
259270

260271
t.Run("ConsistentListFromStorage", func(t *testing.T) {
261272
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, false)
262273
testCases[opts{}] = true
263274
testCases[opts{Limit: 100}] = true
264-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = true
265-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
266275
for opt, expectBypass := range testCases {
267276
testGetListCacheBypass(t, storage.ListOptions{
268277
ResourceVersion: opt.ResourceVersion,
@@ -288,8 +297,6 @@ func TestGetListCacheBypass(t *testing.T) {
288297

289298
testCases[opts{}] = false
290299
testCases[opts{Limit: 100}] = false
291-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
292-
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
293300
for opt, expectBypass := range testCases {
294301
testGetListCacheBypass(t, storage.ListOptions{
295302
ResourceVersion: opt.ResourceVersion,

0 commit comments

Comments
 (0)