@@ -1409,3 +1409,69 @@ func TestValidateStorageClassParameters(t *testing.T) {
1409
1409
})
1410
1410
}
1411
1411
}
1412
+
1413
+ func TestIsTopologyInUse (t * testing.T ) {
1414
+ ctx := context .TODO ()
1415
+ _ , plugin := newMockPlugin (t )
1416
+
1417
+ tt := map [string ]struct {
1418
+ labels map [string ]string
1419
+ injectError bool
1420
+ expected bool
1421
+ }{
1422
+ "node with nil labels" : {
1423
+ labels : nil ,
1424
+ expected : false ,
1425
+ },
1426
+ "node with empty labels" : {
1427
+ labels : map [string ]string {},
1428
+ expected : false ,
1429
+ },
1430
+ "node with labels, but no topology labels" : {
1431
+ labels : map [string ]string {"hostname.kubernetes.io/name" : "host1" },
1432
+ expected : false ,
1433
+ },
1434
+ "node with non-region topology label" : {
1435
+ labels : map [string ]string {"topology.kubernetes.io/zone" : "zone1" },
1436
+ expected : false ,
1437
+ },
1438
+ "node with multiple topology labels" : {
1439
+ labels : map [string ]string {"topology.kubernetes.io/region" : "region1" , "topology.kubernetes.io/zone" : "zone1" },
1440
+ expected : true ,
1441
+ },
1442
+ "error while listing the nodes" : {
1443
+ labels : map [string ]string {"topology.kubernetes.io/region" : "region1" , "topology.kubernetes.io/zone" : "zone1" },
1444
+ injectError : true ,
1445
+ expected : false ,
1446
+ },
1447
+ }
1448
+
1449
+ for name , test := range tt {
1450
+ t .Run (name , func (t * testing.T ) {
1451
+ // create fake nodes and add to a fake k8s client
1452
+ fakeNode := & v1.Node {ObjectMeta : metav1.ObjectMeta {Name : "fakeNode" , Labels : test .labels }}
1453
+ clientSet := k8sfake .NewSimpleClientset (fakeNode )
1454
+
1455
+ // add reactor to either return the list or return error if required
1456
+ clientSet .Fake .PrependReactor (
1457
+ "list" /* use '*' for all operations */ , "*" , /* use '*' all object types */
1458
+ func (_ k8stesting.Action ) (handled bool , ret runtime.Object , err error ) {
1459
+ if test .injectError {
1460
+ status := & k8serrors.StatusError {ErrStatus : metav1.Status {Reason : metav1 .StatusFailure }}
1461
+ return true , nil , status
1462
+ } else {
1463
+ return true , & v1.NodeList {Items : []v1.Node {* fakeNode }}, nil
1464
+ }
1465
+ },
1466
+ )
1467
+
1468
+ // add the fake client to the plugin
1469
+ plugin .kubeClient = clientSet
1470
+
1471
+ // check if the topology is in use
1472
+ result := plugin .IsTopologyInUse (ctx )
1473
+
1474
+ assert .Equal (t , test .expected , result , fmt .Sprintf ("topology usage not as expected; expected %v, got %v" , test .expected , result ))
1475
+ })
1476
+ }
1477
+ }
0 commit comments