Skip to content

Commit 66126bb

Browse files
authored
Merge pull request kubernetes#125721 from p0lyn0mial/upstream-cacher-tests-decrease-time
apiserver/storage: decrease running time of RunWatchSemantics
2 parents 7996836 + c6ef512 commit 66126bb

File tree

1 file changed

+63
-46
lines changed

1 file changed

+63
-46
lines changed

staging/src/k8s.io/apiserver/pkg/storage/testing/watcher_tests.go

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
apiequality "k8s.io/apimachinery/pkg/api/equality"
3030
"k8s.io/apimachinery/pkg/api/errors"
31+
"k8s.io/apimachinery/pkg/api/meta"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
"k8s.io/apimachinery/pkg/fields"
3334
"k8s.io/apimachinery/pkg/labels"
@@ -1236,6 +1237,7 @@ func RunSendInitialEventsBackwardCompatibility(ctx context.Context, t *testing.T
12361237
// - false indicates the value of the param was set to "false" by a test case
12371238
// - true indicates the value of the param was set to "true" by a test case
12381239
func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interface) {
1240+
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.WatchList, true)
12391241
trueVal, falseVal := true, false
12401242
addEventsFromCreatedPods := func(createdInitialPods []*example.Pod) []watch.Event {
12411243
var ret []watch.Event
@@ -1244,16 +1246,16 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
12441246
}
12451247
return ret
12461248
}
1247-
initialEventsEndFromLastCreatedPod := func(createdInitialPods []*example.Pod) []watch.Event {
1248-
return []watch.Event{{
1249+
initialEventsEndFromLastCreatedPod := func(createdInitialPods []*example.Pod) watch.Event {
1250+
return watch.Event{
12491251
Type: watch.Bookmark,
12501252
Object: &example.Pod{
12511253
ObjectMeta: metav1.ObjectMeta{
12521254
ResourceVersion: createdInitialPods[len(createdInitialPods)-1].ResourceVersion,
12531255
Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"},
12541256
},
12551257
},
1256-
}}
1258+
}
12571259
}
12581260
scenarios := []struct {
12591261
name string
@@ -1267,19 +1269,19 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
12671269
initialPods []*example.Pod
12681270
podsAfterEstablishingWatch []*example.Pod
12691271

1270-
expectedInitialEvents func(createdInitialPods []*example.Pod) []watch.Event
1271-
expectedInitialEventsBookmark func(createdInitialPods []*example.Pod) []watch.Event
1272-
expectedEventsAfterEstablishingWatch func(createdPodsAfterWatch []*example.Pod) []watch.Event
1272+
expectedInitialEvents func(createdInitialPods []*example.Pod) []watch.Event
1273+
expectedInitialEventsBookmarkWithMinimalRV func(createdInitialPods []*example.Pod) watch.Event
1274+
expectedEventsAfterEstablishingWatch func(createdPodsAfterWatch []*example.Pod) []watch.Event
12731275
}{
12741276
{
1275-
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=unset",
1276-
allowWatchBookmarks: true,
1277-
sendInitialEvents: &trueVal,
1278-
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1279-
expectedInitialEvents: addEventsFromCreatedPods,
1280-
expectedInitialEventsBookmark: initialEventsEndFromLastCreatedPod,
1281-
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1282-
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
1277+
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=unset",
1278+
allowWatchBookmarks: true,
1279+
sendInitialEvents: &trueVal,
1280+
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1281+
expectedInitialEvents: addEventsFromCreatedPods,
1282+
expectedInitialEventsBookmarkWithMinimalRV: initialEventsEndFromLastCreatedPod,
1283+
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1284+
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
12831285
},
12841286
{
12851287
name: "allowWatchBookmarks=true, sendInitialEvents=false, RV=unset",
@@ -1306,15 +1308,15 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
13061308
},
13071309

13081310
{
1309-
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=0",
1310-
allowWatchBookmarks: true,
1311-
sendInitialEvents: &trueVal,
1312-
resourceVersion: "0",
1313-
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1314-
expectedInitialEvents: addEventsFromCreatedPods,
1315-
expectedInitialEventsBookmark: initialEventsEndFromLastCreatedPod,
1316-
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1317-
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
1311+
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=0",
1312+
allowWatchBookmarks: true,
1313+
sendInitialEvents: &trueVal,
1314+
resourceVersion: "0",
1315+
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1316+
expectedInitialEvents: addEventsFromCreatedPods,
1317+
expectedInitialEventsBookmarkWithMinimalRV: initialEventsEndFromLastCreatedPod,
1318+
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1319+
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
13181320
},
13191321
{
13201322
name: "allowWatchBookmarks=true, sendInitialEvents=false, RV=0",
@@ -1344,15 +1346,15 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
13441346
},
13451347

13461348
{
1347-
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=1",
1348-
allowWatchBookmarks: true,
1349-
sendInitialEvents: &trueVal,
1350-
resourceVersion: "1",
1351-
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1352-
expectedInitialEvents: addEventsFromCreatedPods,
1353-
expectedInitialEventsBookmark: initialEventsEndFromLastCreatedPod,
1354-
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1355-
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
1349+
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=1",
1350+
allowWatchBookmarks: true,
1351+
sendInitialEvents: &trueVal,
1352+
resourceVersion: "1",
1353+
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1354+
expectedInitialEvents: addEventsFromCreatedPods,
1355+
expectedInitialEventsBookmarkWithMinimalRV: initialEventsEndFromLastCreatedPod,
1356+
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1357+
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
13561358
},
13571359
{
13581360
name: "allowWatchBookmarks=true, sendInitialEvents=false, RV=1",
@@ -1384,15 +1386,15 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
13841386
},
13851387

13861388
{
1387-
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=useCurrentRV",
1388-
allowWatchBookmarks: true,
1389-
sendInitialEvents: &trueVal,
1390-
useCurrentRV: true,
1391-
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1392-
expectedInitialEvents: addEventsFromCreatedPods,
1393-
expectedInitialEventsBookmark: initialEventsEndFromLastCreatedPod,
1394-
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1395-
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
1389+
name: "allowWatchBookmarks=true, sendInitialEvents=true, RV=useCurrentRV",
1390+
allowWatchBookmarks: true,
1391+
sendInitialEvents: &trueVal,
1392+
useCurrentRV: true,
1393+
initialPods: []*example.Pod{makePod("1"), makePod("2"), makePod("3")},
1394+
expectedInitialEvents: addEventsFromCreatedPods,
1395+
expectedInitialEventsBookmarkWithMinimalRV: initialEventsEndFromLastCreatedPod,
1396+
podsAfterEstablishingWatch: []*example.Pod{makePod("4"), makePod("5")},
1397+
expectedEventsAfterEstablishingWatch: addEventsFromCreatedPods,
13961398
},
13971399
{
13981400
name: "allowWatchBookmarks=true, sendInitialEvents=false, RV=useCurrentRV",
@@ -1439,14 +1441,11 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
14391441
}
14401442
for idx, scenario := range scenarios {
14411443
t.Run(scenario.name, func(t *testing.T) {
1444+
t.Parallel()
14421445
// set up env
1443-
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.WatchList, true)
14441446
if scenario.expectedInitialEvents == nil {
14451447
scenario.expectedInitialEvents = func(_ []*example.Pod) []watch.Event { return nil }
14461448
}
1447-
if scenario.expectedInitialEventsBookmark == nil {
1448-
scenario.expectedInitialEventsBookmark = func(_ []*example.Pod) []watch.Event { return nil }
1449-
}
14501449
if scenario.expectedEventsAfterEstablishingWatch == nil {
14511450
scenario.expectedEventsAfterEstablishingWatch = func(_ []*example.Pod) []watch.Event { return nil }
14521451
}
@@ -1480,7 +1479,25 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
14801479

14811480
// make sure we only get initial events
14821481
testCheckResultsInStrictOrder(t, w, scenario.expectedInitialEvents(createdPods))
1483-
testCheckResultsInStrictOrder(t, w, scenario.expectedInitialEventsBookmark(createdPods))
1482+
1483+
// make sure that the actual bookmark has at least RV >= to the expected one
1484+
if scenario.expectedInitialEventsBookmarkWithMinimalRV != nil {
1485+
testCheckResultFunc(t, w, func(actualEvent watch.Event) {
1486+
expectedBookmarkEventWithMinRV := scenario.expectedInitialEventsBookmarkWithMinimalRV(createdPods)
1487+
expectedObj, err := meta.Accessor(expectedBookmarkEventWithMinRV.Object)
1488+
require.NoError(t, err)
1489+
1490+
actualObj, err := meta.Accessor(actualEvent.Object)
1491+
require.NoError(t, err)
1492+
1493+
require.GreaterOrEqual(t, actualObj.GetResourceVersion(), expectedObj.GetResourceVersion())
1494+
1495+
// once we know that the RV is at least >= the expected one
1496+
// rewrite it so that we can compare the objs
1497+
expectedObj.SetResourceVersion(actualObj.GetResourceVersion())
1498+
expectNoDiff(t, "incorrect event", expectedBookmarkEventWithMinRV, actualEvent)
1499+
})
1500+
}
14841501

14851502
createdPods = []*example.Pod{}
14861503
// add a pod that is greater than the storage's RV when the watch was started

0 commit comments

Comments
 (0)