@@ -28,6 +28,7 @@ import (
28
28
29
29
apiequality "k8s.io/apimachinery/pkg/api/equality"
30
30
"k8s.io/apimachinery/pkg/api/errors"
31
+ "k8s.io/apimachinery/pkg/api/meta"
31
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
33
"k8s.io/apimachinery/pkg/fields"
33
34
"k8s.io/apimachinery/pkg/labels"
@@ -1236,6 +1237,7 @@ func RunSendInitialEventsBackwardCompatibility(ctx context.Context, t *testing.T
1236
1237
// - false indicates the value of the param was set to "false" by a test case
1237
1238
// - true indicates the value of the param was set to "true" by a test case
1238
1239
func RunWatchSemantics (ctx context.Context , t * testing.T , store storage.Interface ) {
1240
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .WatchList , true )
1239
1241
trueVal , falseVal := true , false
1240
1242
addEventsFromCreatedPods := func (createdInitialPods []* example.Pod ) []watch.Event {
1241
1243
var ret []watch.Event
@@ -1244,16 +1246,16 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1244
1246
}
1245
1247
return ret
1246
1248
}
1247
- initialEventsEndFromLastCreatedPod := func (createdInitialPods []* example.Pod ) [] watch.Event {
1248
- return [] watch.Event { {
1249
+ initialEventsEndFromLastCreatedPod := func (createdInitialPods []* example.Pod ) watch.Event {
1250
+ return watch.Event {
1249
1251
Type : watch .Bookmark ,
1250
1252
Object : & example.Pod {
1251
1253
ObjectMeta : metav1.ObjectMeta {
1252
1254
ResourceVersion : createdInitialPods [len (createdInitialPods )- 1 ].ResourceVersion ,
1253
1255
Annotations : map [string ]string {metav1 .InitialEventsAnnotationKey : "true" },
1254
1256
},
1255
1257
},
1256
- }}
1258
+ }
1257
1259
}
1258
1260
scenarios := []struct {
1259
1261
name string
@@ -1267,19 +1269,19 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1267
1269
initialPods []* example.Pod
1268
1270
podsAfterEstablishingWatch []* example.Pod
1269
1271
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
1273
1275
}{
1274
1276
{
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 ,
1283
1285
},
1284
1286
{
1285
1287
name : "allowWatchBookmarks=true, sendInitialEvents=false, RV=unset" ,
@@ -1306,15 +1308,15 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1306
1308
},
1307
1309
1308
1310
{
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 ,
1318
1320
},
1319
1321
{
1320
1322
name : "allowWatchBookmarks=true, sendInitialEvents=false, RV=0" ,
@@ -1344,15 +1346,15 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1344
1346
},
1345
1347
1346
1348
{
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 ,
1356
1358
},
1357
1359
{
1358
1360
name : "allowWatchBookmarks=true, sendInitialEvents=false, RV=1" ,
@@ -1384,15 +1386,15 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1384
1386
},
1385
1387
1386
1388
{
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 ,
1396
1398
},
1397
1399
{
1398
1400
name : "allowWatchBookmarks=true, sendInitialEvents=false, RV=useCurrentRV" ,
@@ -1439,14 +1441,11 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1439
1441
}
1440
1442
for idx , scenario := range scenarios {
1441
1443
t .Run (scenario .name , func (t * testing.T ) {
1444
+ t .Parallel ()
1442
1445
// set up env
1443
- featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .WatchList , true )
1444
1446
if scenario .expectedInitialEvents == nil {
1445
1447
scenario .expectedInitialEvents = func (_ []* example.Pod ) []watch.Event { return nil }
1446
1448
}
1447
- if scenario .expectedInitialEventsBookmark == nil {
1448
- scenario .expectedInitialEventsBookmark = func (_ []* example.Pod ) []watch.Event { return nil }
1449
- }
1450
1449
if scenario .expectedEventsAfterEstablishingWatch == nil {
1451
1450
scenario .expectedEventsAfterEstablishingWatch = func (_ []* example.Pod ) []watch.Event { return nil }
1452
1451
}
@@ -1480,7 +1479,25 @@ func RunWatchSemantics(ctx context.Context, t *testing.T, store storage.Interfac
1480
1479
1481
1480
// make sure we only get initial events
1482
1481
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
+ }
1484
1501
1485
1502
createdPods = []* example.Pod {}
1486
1503
// add a pod that is greater than the storage's RV when the watch was started
0 commit comments