@@ -658,7 +658,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
658658 {
659659 ContainerPort : port80 ,
660660 Protocol : corev1 .ProtocolTCP ,
661- Name : "test -port" ,
661+ Name : "src -port" ,
662662 },
663663 },
664664 },
@@ -668,6 +668,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
668668 PodIP : "1.0.0.1" ,
669669 },
670670 }
671+
671672 dstPodOne := corev1.Pod {
672673 ObjectMeta : metav1.ObjectMeta {
673674 Name : "pod2" ,
@@ -681,7 +682,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
681682 {
682683 ContainerPort : port8080 ,
683684 Protocol : corev1 .ProtocolTCP ,
684- Name : "test -port" ,
685+ Name : "dst -port" ,
685686 },
686687 },
687688 },
@@ -715,6 +716,12 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
715716 },
716717 }
717718
719+ portsMap := map [string ]int32 {
720+ "src-port" : port80 ,
721+ "dst-port" : port8080 ,
722+ }
723+
724+ // the policy is applied to dst namespace on dst pod
718725 policy := & networking.NetworkPolicy {
719726 ObjectMeta : metav1.ObjectMeta {
720727 Name : "netpol" ,
@@ -737,7 +744,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
737744 Ports : []networking.NetworkPolicyPort {
738745 {
739746 Protocol : & protocolTCP ,
740- Port : & intstr.IntOrString {Type : intstr .String , StrVal : "test -port" },
747+ Port : & intstr.IntOrString {Type : intstr .String , StrVal : "dst -port" },
741748 },
742749 },
743750 },
@@ -756,7 +763,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
756763 Ports : []networking.NetworkPolicyPort {
757764 {
758765 Protocol : & protocolTCP ,
759- Port : & intstr.IntOrString {Type : intstr .Int , IntVal : port8080 },
766+ Port : & intstr.IntOrString {Type : intstr .String , StrVal : "src-port" },
760767 EndPort : & port9090 ,
761768 },
762769 },
@@ -798,6 +805,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
798805 // getting ingress endpoint calls listing pods with dst NS first
799806 mockClient .EXPECT ().List (gomock .Any (), podList , gomock .Any ()).DoAndReturn (
800807 func (ctx context.Context , podList * corev1.PodList , opts ... client.ListOption ) error {
808+ podList .Items = []corev1.Pod {dstPodOne , dstPodTwo }
801809 podList .Items = []corev1.Pod {dstPodOne , dstPodTwo }
802810 return nil
803811 },
@@ -820,7 +828,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
820828
821829 dstNS := corev1.Namespace {
822830 ObjectMeta : metav1.ObjectMeta {
823- Name : "dst " ,
831+ Name : "src " ,
824832 },
825833 }
826834
@@ -834,6 +842,7 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
834842 ),
835843 mockClient .EXPECT ().List (gomock .Any (), podList , gomock .Any ()).DoAndReturn (
836844 func (ctx context.Context , podList * corev1.PodList , opts ... client.ListOption ) error {
845+ podList .Items = []corev1.Pod {dstPodOne , dstPodTwo }
837846 podList .Items = []corev1.Pod {dstPodOne , dstPodTwo }
838847 return nil
839848 },
@@ -866,16 +875,23 @@ func TestEndpointsResolver_ResolveNetworkPeers(t *testing.T) {
866875 }
867876 }
868877
878+ // the policy is applied to dst namespace
879+ // the ingress should have cidr from src pod and ports from dst pod
880+ // the egress should have cidr from src pod and ports from src pod
869881 for _ , ingPE := range ingressEndpoints {
870882 assert .Equal (t , srcPod .Status .PodIP , string (ingPE .CIDR ))
871883 assert .Equal (t , dstPodOne .Spec .Containers [0 ].Ports [0 ].ContainerPort , * ingPE .Ports [0 ].Port )
872884 assert .Equal (t , 1 , len (ingPE .Ports ))
885+ assert .Equal (t , dstPodOne .Spec .Containers [0 ].Ports [0 ].ContainerPort , * ingPE .Ports [0 ].Port )
886+ assert .Equal (t , 1 , len (ingPE .Ports ))
873887 }
874888
875889 for _ , egPE := range egressEndpoints {
876890 assert .True (t , string (egPE .CIDR ) == dstPodOne .Status .PodIP || string (egPE .CIDR ) == dstPodTwo .Status .PodIP )
877891 assert .Equal (t , dstPodOne .Spec .Containers [0 ].Ports [0 ].ContainerPort , * egPE .Ports [0 ].Port )
878- assert .Equal (t , policy .Spec .Egress [0 ].Ports [0 ].Port .IntVal , * egPE .Ports [0 ].Port )
892+ assert .Equal (t , srcPod .Status .PodIP , string (egPE .CIDR ))
893+ assert .Equal (t , srcPod .Spec .Containers [0 ].Ports [0 ].ContainerPort , * egPE .Ports [0 ].Port )
894+ assert .Equal (t , portsMap [policy .Spec .Egress [0 ].Ports [0 ].Port .StrVal ], * egPE .Ports [0 ].Port )
879895 assert .Equal (t , * policy .Spec .Egress [0 ].Ports [0 ].EndPort , * egPE .Ports [0 ].EndPort )
880896 }
881897}
0 commit comments