@@ -3996,3 +3996,196 @@ func Test_validateService(t *testing.T) {
39963996 })
39973997 }
39983998}
3999+
4000+ func Test_getListenersNetworkLoadBalancer (t * testing.T ) {
4001+ testOneListenerName := "TCP_AND_UDP-67"
4002+ testOneBackendSetName := "TCP_AND_UDP-67"
4003+ testOneProtocol := "TCP_AND_UDP"
4004+ testOnePort := 67
4005+
4006+ testTwoListenerNameOne := "TCP-67"
4007+ testTwoBackendSetNameOne := "TCP-67"
4008+ testTwoProtocolOne := "TCP"
4009+ testTwoPortOne := 67
4010+
4011+ testTwoListenerNameTwo := "UDP-68"
4012+ testTwoBackendSetNameTwo := "UDP-68"
4013+ testTwoProtocolTwo := "UDP"
4014+ testTwoPortTwo := 68
4015+
4016+ testThreeListenerName := "TCP-67"
4017+ testThreeBackendSetName := "TCP-67"
4018+ testThreeProtocol := "TCP"
4019+ testThreePort := 67
4020+
4021+ testFourListenerName := "UDP-67"
4022+ testFourBackendSetName := "UDP-67"
4023+ testFourProtocol := "UDP"
4024+ testFourPort := 67
4025+
4026+ testCases := map [string ]struct {
4027+ service * v1.Service
4028+ wantListeners map [string ]client.GenericListener
4029+ err error
4030+ }{
4031+ "NLB_with_mixed_protocol_on_same_port" : {
4032+ service : & v1.Service {
4033+ Spec : v1.ServiceSpec {
4034+ SessionAffinity : v1 .ServiceAffinityNone ,
4035+ Ports : []v1.ServicePort {
4036+ {
4037+ Protocol : v1 .ProtocolTCP ,
4038+ Port : int32 (67 ),
4039+ },
4040+ {
4041+ Protocol : v1 .ProtocolUDP ,
4042+ Port : int32 (67 ),
4043+ },
4044+ },
4045+ },
4046+ ObjectMeta : metav1.ObjectMeta {
4047+ Annotations : map [string ]string {
4048+ ServiceAnnotationLoadBalancerType : "nlb" ,
4049+ },
4050+ },
4051+ },
4052+ wantListeners : map [string ]client.GenericListener {
4053+ "TCP_AND_UDP-67" : {
4054+ Name : & testOneListenerName ,
4055+ DefaultBackendSetName : common .String (testOneBackendSetName ),
4056+ Protocol : & testOneProtocol ,
4057+ Port : & testOnePort ,
4058+ },
4059+ },
4060+ err : nil ,
4061+ },
4062+ "NLB_with_mixed_protocol_on_different_port" : {
4063+ service : & v1.Service {
4064+ Spec : v1.ServiceSpec {
4065+ SessionAffinity : v1 .ServiceAffinityNone ,
4066+ Ports : []v1.ServicePort {
4067+ {
4068+ Protocol : v1 .ProtocolTCP ,
4069+ Port : int32 (67 ),
4070+ },
4071+ {
4072+ Protocol : v1 .ProtocolUDP ,
4073+ Port : int32 (68 ),
4074+ },
4075+ },
4076+ },
4077+ ObjectMeta : metav1.ObjectMeta {
4078+ Annotations : map [string ]string {
4079+ ServiceAnnotationLoadBalancerType : "nlb" ,
4080+ },
4081+ },
4082+ },
4083+ wantListeners : map [string ]client.GenericListener {
4084+ "TCP-67" : {
4085+ Name : & testTwoListenerNameOne ,
4086+ DefaultBackendSetName : common .String (testTwoBackendSetNameOne ),
4087+ Protocol : & testTwoProtocolOne ,
4088+ Port : & testTwoPortOne ,
4089+ },
4090+ "UDP-68" : {
4091+ Name : & testTwoListenerNameTwo ,
4092+ DefaultBackendSetName : common .String (testTwoBackendSetNameTwo ),
4093+ Protocol : & testTwoProtocolTwo ,
4094+ Port : & testTwoPortTwo ,
4095+ },
4096+ },
4097+ err : nil ,
4098+ },
4099+ "NLB_with_only_TCP_protocol" : {
4100+ service : & v1.Service {
4101+ Spec : v1.ServiceSpec {
4102+ SessionAffinity : v1 .ServiceAffinityNone ,
4103+ Ports : []v1.ServicePort {
4104+ {
4105+ Protocol : v1 .ProtocolTCP ,
4106+ Port : int32 (67 ),
4107+ },
4108+ },
4109+ },
4110+ ObjectMeta : metav1.ObjectMeta {
4111+ Annotations : map [string ]string {
4112+ ServiceAnnotationLoadBalancerType : "nlb" ,
4113+ },
4114+ },
4115+ },
4116+ wantListeners : map [string ]client.GenericListener {
4117+ "TCP-67" : {
4118+ Name : & testThreeListenerName ,
4119+ DefaultBackendSetName : common .String (testThreeBackendSetName ),
4120+ Protocol : & testThreeProtocol ,
4121+ Port : & testThreePort ,
4122+ },
4123+ },
4124+ err : nil ,
4125+ },
4126+ "NLB_with_only_UDP_protocol" : {
4127+ service : & v1.Service {
4128+ Spec : v1.ServiceSpec {
4129+ SessionAffinity : v1 .ServiceAffinityNone ,
4130+ Ports : []v1.ServicePort {
4131+ {
4132+ Protocol : v1 .ProtocolUDP ,
4133+ Port : int32 (67 ),
4134+ },
4135+ },
4136+ },
4137+ ObjectMeta : metav1.ObjectMeta {
4138+ Annotations : map [string ]string {
4139+ ServiceAnnotationLoadBalancerType : "nlb" ,
4140+ },
4141+ },
4142+ },
4143+ wantListeners : map [string ]client.GenericListener {
4144+ "UDP-67" : {
4145+ Name : & testFourListenerName ,
4146+ DefaultBackendSetName : common .String (testFourBackendSetName ),
4147+ Protocol : & testFourProtocol ,
4148+ Port : & testFourPort ,
4149+ },
4150+ },
4151+ err : nil ,
4152+ },
4153+ }
4154+ for name , tc := range testCases {
4155+ t .Run (name , func (t * testing.T ) {
4156+ gotListeners , err := getListenersNetworkLoadBalancer (tc .service )
4157+ if tc .err != nil && err == nil {
4158+ t .Errorf ("Expected \n %+v\n but got\n %+v" , tc .err , err )
4159+ }
4160+ if err != nil && tc .err == nil {
4161+ t .Errorf ("Error: expected\n %+v\n but got\n %+v" , tc .err , err )
4162+ }
4163+ if err != nil && err .Error () != tc .err .Error () {
4164+ t .Errorf ("Expected \n %+v\n but got\n %+v" , tc .err , err )
4165+ }
4166+ if len (gotListeners ) != len (tc .wantListeners ) {
4167+ t .Errorf ("Number of excpected listeners \n %+v\n but got\n %+v" , len (tc .wantListeners ), len (gotListeners ))
4168+ }
4169+ if len (gotListeners ) != 0 {
4170+ for name , listener := range tc .wantListeners {
4171+ gotListener , ok := gotListeners [name ]
4172+ if ! ok {
4173+ t .Errorf ("Expected listener with name \n %+v\n but listener not present" , * listener .Name )
4174+ }
4175+ if * gotListener .Name != * listener .Name {
4176+ t .Errorf ("Expected listener name \n %+v\n but got listener name \n %+v" , * listener .Name , * gotListener .Name )
4177+ }
4178+ if * gotListener .DefaultBackendSetName != * listener .DefaultBackendSetName {
4179+ t .Errorf ("Expected default backend set name \n %+v\n but got default backend set name \n %+v" , * listener .DefaultBackendSetName , * gotListener .DefaultBackendSetName )
4180+ }
4181+ if * gotListener .Protocol != * listener .Protocol {
4182+ t .Errorf ("Expected protocol \n %+v\n but got protocol \n %+v" , * listener .Protocol , * gotListener .Protocol )
4183+ }
4184+ if * gotListener .Port != * listener .Port {
4185+ t .Errorf ("Expected port number \n %+v\n but got port number \n %+v" , * listener .Port , * gotListener .Port )
4186+ }
4187+ }
4188+ }
4189+ })
4190+ }
4191+ }
0 commit comments