@@ -34,6 +34,7 @@ import (
34
34
"k8s.io/apimachinery/pkg/types"
35
35
"k8s.io/apimachinery/pkg/util/intstr"
36
36
"k8s.io/apiserver/pkg/storage/names"
37
+ "k8s.io/client-go/informers"
37
38
"k8s.io/client-go/kubernetes/fake"
38
39
core "k8s.io/client-go/testing"
39
40
"k8s.io/kubernetes/pkg/controller"
@@ -1402,3 +1403,84 @@ func TestReplicasAnnotationsNeedUpdate(t *testing.T) {
1402
1403
})
1403
1404
}
1404
1405
}
1406
+
1407
+ func TestGetDeploymentsForReplicaSet (t * testing.T ) {
1408
+ fakeInformerFactory := informers .NewSharedInformerFactory (& fake.Clientset {}, 0 * time .Second )
1409
+ var deployments []* apps.Deployment
1410
+ for i := 0 ; i < 3 ; i ++ {
1411
+ deployment := & apps.Deployment {
1412
+ ObjectMeta : metav1.ObjectMeta {
1413
+ Name : fmt .Sprintf ("deployment-%d" , i ),
1414
+ Namespace : "test" ,
1415
+ },
1416
+ Spec : apps.DeploymentSpec {
1417
+ Selector : & metav1.LabelSelector {
1418
+ MatchLabels : map [string ]string {
1419
+ "app" : fmt .Sprintf ("test-%d" , i ),
1420
+ },
1421
+ },
1422
+ },
1423
+ }
1424
+ deployments = append (deployments , deployment )
1425
+ fakeInformerFactory .Apps ().V1 ().Deployments ().Informer ().GetStore ().Add (deployment )
1426
+ }
1427
+ var rss []* apps.ReplicaSet
1428
+ for i := 0 ; i < 5 ; i ++ {
1429
+ rs := & apps.ReplicaSet {
1430
+ ObjectMeta : metav1.ObjectMeta {
1431
+ Namespace : "test" ,
1432
+ Name : fmt .Sprintf ("test-replicaSet-%d" , i ),
1433
+ Labels : map [string ]string {
1434
+ "app" : fmt .Sprintf ("test-%d" , i ),
1435
+ "label" : fmt .Sprintf ("label-%d" , i ),
1436
+ },
1437
+ },
1438
+ }
1439
+ rss = append (rss , rs )
1440
+ }
1441
+ tests := []struct {
1442
+ name string
1443
+ rs * apps.ReplicaSet
1444
+ err error
1445
+ expect []* apps.Deployment
1446
+ }{
1447
+ {
1448
+ name : "GetDeploymentsForReplicaSet for rs-0" ,
1449
+ rs : rss [0 ],
1450
+ expect : []* apps.Deployment {deployments [0 ]},
1451
+ },
1452
+ {
1453
+ name : "GetDeploymentsForReplicaSet for rs-1" ,
1454
+ rs : rss [1 ],
1455
+ expect : []* apps.Deployment {deployments [1 ]},
1456
+ },
1457
+ {
1458
+ name : "GetDeploymentsForReplicaSet for rs-2" ,
1459
+ rs : rss [2 ],
1460
+ expect : []* apps.Deployment {deployments [2 ]},
1461
+ },
1462
+ {
1463
+ name : "GetDeploymentsForReplicaSet for rs-3" ,
1464
+ rs : rss [3 ],
1465
+ err : fmt .Errorf ("could not find deployments set for ReplicaSet %s in namespace %s with labels: %v" , rss [3 ].Name , rss [3 ].Namespace , rss [3 ].Labels ),
1466
+ },
1467
+ {
1468
+ name : "GetDeploymentsForReplicaSet for rs-4" ,
1469
+ rs : rss [4 ],
1470
+ err : fmt .Errorf ("could not find deployments set for ReplicaSet %s in namespace %s with labels: %v" , rss [4 ].Name , rss [4 ].Namespace , rss [4 ].Labels ),
1471
+ },
1472
+ }
1473
+ for _ , test := range tests {
1474
+ t .Run (test .name , func (t * testing.T ) {
1475
+ get , err := GetDeploymentsForReplicaSet (fakeInformerFactory .Apps ().V1 ().Deployments ().Lister (), test .rs )
1476
+ if err != nil {
1477
+ if err .Error () != test .err .Error () {
1478
+ t .Errorf ("Error from GetDeploymentsForReplicaSet: %v" , err )
1479
+ }
1480
+ } else if ! reflect .DeepEqual (get , test .expect ) {
1481
+ t .Errorf ("Expect deployments %v, but got %v" , test .expect , get )
1482
+ }
1483
+ })
1484
+ }
1485
+
1486
+ }
0 commit comments