@@ -21,6 +21,7 @@ import (
2121 "k8s.io/apimachinery/pkg/runtime"
2222 "k8s.io/apimachinery/pkg/types"
2323 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
24+ "k8s.io/apimachinery/pkg/util/sets"
2425 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2526 "k8s.io/client-go/util/workqueue"
2627 controllerruntime "sigs.k8s.io/controller-runtime"
@@ -154,20 +155,8 @@ func TestNetworkInfoReconciler_Reconcile(t *testing.T) {
154155 {
155156 name : "Empty" ,
156157 prepareFunc : func (t * testing.T , r * NetworkInfoReconciler , ctx context.Context ) (patches * gomonkey.Patches ) {
157- patches = gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (bool , error ) {
158- return false , nil
159- })
160- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
161- return nil
162- })
163- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetNetworkconfigNameFromNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (string , error ) {
164- return "" , nil
165- })
166- patches .ApplyMethod (reflect .TypeOf (r .Service ), "ListVPC" , func (_ * vpc.VPCService ) []model.Vpc {
167- return nil
168- })
169- patches .ApplyFunc (deleteVPCNetworkConfigurationStatus , func (ctx context.Context , client client.Client , ncName string , staleVPCs []* model.Vpc , aliveVPCs []model.Vpc ) {
170- return
158+ patches = gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
159+ return []* model.Vpc {}
171160 })
172161 return patches
173162 },
@@ -779,35 +768,41 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
779768 expectErrStr string
780769 }{
781770 {
782- name : "shared namespace , skip deletion" ,
771+ name : "no VPC exists for the Namespace, , skip deletion" ,
783772 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
784- patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS " , func (_ * vpc.VPCService , ctx context. Context , _ string ) ( bool , error ) {
785- return true , nil
773+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace " , func (_ * vpc.VPCService , _ string ) [] * model. Vpc {
774+ return [] * model. Vpc {}
786775 })
787776 return patches
788777 },
789- },
790- {
791- name : "non-shared namespace, no VPCs found" ,
778+ }, {
779+ name : "NSX VPC is used by a valid Namespace,, skip deletion" ,
792780 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
793- patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (bool , error ) {
794- return false , nil
781+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
782+ return []* model.Vpc {
783+ {Tags : []model.Tag {
784+ {Scope : servicecommon .String (servicecommon .TagScopeNamespaceUID ), Tag : servicecommon .String ("vpc1" )}}}}
795785 })
796- patches .ApplyMethod (reflect .TypeOf (r . Service ), "GetVPCsByNamespace " , func (_ * vpc. VPCService , _ string ) [] * model. Vpc {
797- return nil
786+ patches .ApplyPrivateMethod (reflect .TypeOf (r ), "listNamespaceCRsNameIDSet " , func (_ * NetworkInfoReconciler , _ context. Context ) (sets. Set [ string ], sets. Set [ string ], error ) {
787+ return sets. Set [ string ]{}, sets. New [ string ]( "vpc1" ), nil
798788 })
799789 return patches
800790 },
801791 },
802792 {
803793 name : "failed to delete VPC" ,
804794 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
805- patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (bool , error ) {
806- return false , nil
795+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
796+ return []* model.Vpc {
797+ {
798+ Tags : []model.Tag {
799+ {Scope : servicecommon .String (servicecommon .TagScopeNamespaceUID ), Tag : servicecommon .String ("vpc1" )},
800+ },
801+ Path : servicecommon .String ("/orgs/default/projects/default/vpcs/vpc1" ),
802+ }}
807803 })
808- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
809- vpcPath := "/vpc/1"
810- return []* model.Vpc {{Path : & vpcPath }}
804+ patches .ApplyPrivateMethod (reflect .TypeOf (r ), "listNamespaceCRsNameIDSet" , func (_ * NetworkInfoReconciler , _ context.Context ) (sets.Set [string ], sets.Set [string ], error ) {
805+ return sets.Set [string ]{}, sets.Set [string ]{}, nil
811806 })
812807 patches .ApplyMethod (reflect .TypeOf (r .Service ), "DeleteVPC" , func (_ * vpc.VPCService , _ string ) error {
813808 return fmt .Errorf ("delete failed" )
@@ -819,26 +814,23 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
819814 {
820815 name : "successful deletion of VPCs" ,
821816 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
822- patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (bool , error ) {
823- return false , nil
817+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
818+ return []* model.Vpc {
819+ {
820+ Tags : []model.Tag {
821+ {Scope : servicecommon .String (servicecommon .TagScopeNamespaceUID ), Tag : servicecommon .String ("vpc1" )},
822+ },
823+ Path : servicecommon .String ("/orgs/default/projects/default/vpcs/vpc1" ),
824+ }}
824825 })
825- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
826- vpcPath1 := "/vpc/1"
827- vpcPath2 := "/vpc/2"
828- displayName1 := "fakeDisplayName"
829- displayName2 := "fakeDisplayName"
830- return []* model.Vpc {{Path : & vpcPath1 , DisplayName : & displayName1 }, {Path : & vpcPath2 , DisplayName : & displayName2 }}
826+ patches .ApplyPrivateMethod (reflect .TypeOf (r ), "listNamespaceCRsNameIDSet" , func (_ * NetworkInfoReconciler , _ context.Context ) (sets.Set [string ], sets.Set [string ], error ) {
827+ return sets.Set [string ]{}, sets.Set [string ]{}, nil
831828 })
832829 patches .ApplyMethod (reflect .TypeOf (r .Service ), "DeleteVPC" , func (_ * vpc.VPCService , _ string ) error {
833830 return nil
834831 })
835- patches .ApplyMethod (reflect .TypeOf (r .Service ), "ListVPC" , func (_ * vpc.VPCService ) []model.Vpc {
836- vpcPath := "/vpc/1"
837- displayName := "fakeDisplayName"
838- return []model.Vpc {{Path : & vpcPath , DisplayName : & displayName }}
839- })
840- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetNetworkconfigNameFromNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (string , error ) {
841- return "" , nil
832+ patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCNetworkConfigByNamespace" , func (_ * vpc.VPCService , _ string ) * servicecommon.VPCNetworkConfigInfo {
833+ return nil
842834 })
843835 return patches
844836 },
@@ -860,26 +852,37 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
860852 },
861853 },
862854 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
863- patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (bool , error ) {
864- return false , nil
855+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
856+ return []* model.Vpc {
857+ {
858+ Tags : []model.Tag {
859+ {Scope : servicecommon .String (servicecommon .TagScopeNamespaceUID ), Tag : servicecommon .String ("vpc1" )},
860+ },
861+ Path : servicecommon .String ("/orgs/default/projects/default/vpcs/vpc1" ),
862+ DisplayName : servicecommon .String ("fakeDisplayName1" ),
863+ }}
865864 })
866- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
867- vpcPath1 := "/vpc/1"
868- vpcPath2 := "/vpc/2"
869- displayName1 := "fakeDisplayName1"
870- displayName2 := "fakeDisplayName2"
871- return []* model.Vpc {{Path : & vpcPath1 , DisplayName : & displayName1 }, {Path : & vpcPath2 , DisplayName : & displayName2 }}
865+ patches .ApplyPrivateMethod (reflect .TypeOf (r ), "listNamespaceCRsNameIDSet" , func (_ * NetworkInfoReconciler , _ context.Context ) (sets.Set [string ], sets.Set [string ], error ) {
866+ return sets.Set [string ]{}, sets.Set [string ]{}, nil
872867 })
873868 patches .ApplyMethod (reflect .TypeOf (r .Service ), "DeleteVPC" , func (_ * vpc.VPCService , _ string ) error {
874869 return nil
875870 })
876- patches .ApplyMethod (reflect .TypeOf (r .Service ), "ListVPC" , func (_ * vpc.VPCService ) []model.Vpc {
877- vpcPath := "/vpc/1"
878- displayName := "fakeDisplayName1"
879- return []model.Vpc {{Path : & vpcPath , DisplayName : & displayName }}
871+ patches .ApplyPrivateMethod (reflect .TypeOf (r ), "listVPCsByNetworkConfigName" , func (_ * NetworkInfoReconciler , _ string ) []* model.Vpc {
872+ return []* model.Vpc {
873+ {
874+ Tags : []model.Tag {
875+ {Scope : servicecommon .String (servicecommon .TagScopeNamespaceUID ), Tag : servicecommon .String ("vpc1" )},
876+ },
877+ Path : servicecommon .String ("/orgs/default/projects/default/vpcs/vpc2" ),
878+ DisplayName : servicecommon .String ("fakeDisplayName2" ),
879+ },
880+ }
880881 })
881- patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetNetworkconfigNameFromNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (string , error ) {
882- return "fakeNetworkconfigName" , nil
882+ patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCNetworkConfigByNamespace" , func (_ * vpc.VPCService , _ string ) * servicecommon.VPCNetworkConfigInfo {
883+ return & servicecommon.VPCNetworkConfigInfo {
884+ Name : "fakeNetworkconfigName" ,
885+ }
883886 })
884887 return patches
885888 },
@@ -902,7 +905,7 @@ func TestNetworkInfoReconciler_deleteStaleVPCs(t *testing.T) {
902905 defer patches .Reset ()
903906 }
904907
905- err := r .deleteVPCsByName (ctx , namespace )
908+ err := r .deleteVPCsByNamespace (ctx , namespace )
906909
907910 if tc .expectErrStr != "" {
908911 assert .ErrorContains (t , err , tc .expectErrStr )
@@ -926,28 +929,30 @@ func TestNetworkInfoReconciler_DeleteNetworkInfo(t *testing.T) {
926929 {
927930 name : "Delete NetworkInfo and Namespace not existed" ,
928931 existingNamespace : nil ,
929- expectErrStr : "" ,
930- expectRes : common .ResultNormal ,
931- req : reconcile.Request {NamespacedName : types.NamespacedName {Namespace : "testNamespace" , Name : "testNetworkInfo" }},
932+ prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
933+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
934+ return []* model.Vpc {}
935+ })
936+ return patches
937+ },
938+ expectErrStr : "" ,
939+ expectRes : common .ResultNormal ,
940+ req : reconcile.Request {NamespacedName : types.NamespacedName {Namespace : "testNamespace" , Name : "testNetworkInfo" }},
932941 },
933942 {
934943 name : "Delete NetworkInfo with delete stale VPC error" ,
935944 existingNamespace : & corev1.Namespace {
936945 ObjectMeta : metav1.ObjectMeta {Name : "testNamespace" , UID : "fakeNamespaceUID" },
937946 },
938947 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
939- patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service .VpcStore ), "GetByIndex" , func (_ * vpc.VPCStore , key string , value string ) []* model.Vpc {
940- vpcName := "fakeVPCName"
941- vpcPath := "fakeVPCPath"
948+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , _ string ) []* model.Vpc {
942949 return []* model.Vpc {
943950 {
944- DisplayName : & vpcName ,
945- Path : & vpcPath ,
946- },
947- }
948- })
949- patches .ApplyMethod (reflect .TypeOf (r .Service ), "IsSharedVPCNamespaceByNS" , func (_ * vpc.VPCService , ctx context.Context , _ string ) (bool , error ) {
950- return false , nil
951+ Tags : []model.Tag {
952+ {Scope : servicecommon .String (servicecommon .TagScopeNamespaceUID ), Tag : servicecommon .String ("vpc1" )},
953+ },
954+ Path : servicecommon .String ("/orgs/default/projects/default/vpcs/vpc1" ),
955+ }}
951956 })
952957 patches .ApplyMethod (reflect .TypeOf (r .Service ), "DeleteVPC" , func (_ * vpc.VPCService , _ string ) error {
953958 return fmt .Errorf ("delete failed" )
@@ -987,7 +992,7 @@ func TestNetworkInfoReconciler_DeleteNetworkInfo(t *testing.T) {
987992 Status : corev1.NamespaceStatus {},
988993 },
989994 prepareFuncs : func (r * NetworkInfoReconciler ) * gomonkey.Patches {
990- return gomonkey .ApplyMethod (reflect .TypeOf (r .Service .VpcStore ), "GetByIndex" , func (_ * vpc.VPCStore , key string , value string ) []* model.Vpc {
995+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service .VpcStore ), "GetByIndex" , func (_ * vpc.VPCStore , key string , value string ) []* model.Vpc {
991996 id := "fakeNamespaceUID"
992997 scope := servicecommon .TagScopeNamespaceUID
993998 tag1 := []model.Tag {
@@ -1004,6 +1009,10 @@ func TestNetworkInfoReconciler_DeleteNetworkInfo(t *testing.T) {
10041009 },
10051010 }
10061011 })
1012+ patches .ApplyPrivateMethod (reflect .TypeOf (r ), "deleteVPCs" , func (_ * NetworkInfoReconciler , _ context.Context , _ []* model.Vpc , ns string ) error {
1013+ return fmt .Errorf ("failed to get VPCNetworkConfiguration for Namespace when deleting stale VPCs" )
1014+ })
1015+ return patches
10071016 },
10081017 expectErrStr : "failed to get VPCNetworkConfiguration for Namespace when deleting stale VPCs" ,
10091018 existingNetworkInfo : & v1alpha1.NetworkInfo {
@@ -1410,3 +1419,22 @@ func TestRegisterAllNetworkInfo(t *testing.T) {
14101419 err = r .RegisterAllNetworkInfo (context .Background ())
14111420 assert .NoError (t , err )
14121421}
1422+
1423+ func TestListVPCsByNetworkConfigName (t * testing.T ) {
1424+ r := & NetworkInfoReconciler {
1425+ Service : & vpc.VPCService {},
1426+ }
1427+ patches := gomonkey .ApplyMethod (reflect .TypeOf (r .Service ), "GetNamespacesByNetworkconfigName" , func (_ * vpc.VPCService , _c string ) []string {
1428+ return []string {"ns1" , "ns2" }
1429+ })
1430+ patches .ApplyMethod (reflect .TypeOf (r .Service ), "GetVPCsByNamespace" , func (_ * vpc.VPCService , ns string ) []* model.Vpc {
1431+ if ns == "ns1" {
1432+ return []* model.Vpc {{Id : servicecommon .String ("vpc1" )}}
1433+ }
1434+ return []* model.Vpc {{Id : servicecommon .String ("vpc2" )}}
1435+ })
1436+ defer patches .Reset ()
1437+ expVPCs := []* model.Vpc {{Id : servicecommon .String ("vpc1" )}, {Id : servicecommon .String ("vpc2" )}}
1438+ actVPCs := r .listVPCsByNetworkConfigName ("nc1" )
1439+ assert .ElementsMatch (t , expVPCs , actVPCs )
1440+ }
0 commit comments