@@ -1290,12 +1290,14 @@ func TestIngressPolicy(t *testing.T) {
12901290 tcp := v1 .ProtocolTCP
12911291 targetPodMatchType := policies .EitherMatch
12921292 peerMatchType := policies .SrcMatch
1293+ emptyString := intstr .FromString ("" )
12931294 // TODO(jungukcho): add test cases with more complex rules
12941295 tests := []struct {
12951296 name string
12961297 targetSelector * metav1.LabelSelector
12971298 rules []networkingv1.NetworkPolicyIngressRule
12981299 npmNetPol * policies.NPMNetworkPolicy
1300+ wantErr bool
12991301 }{
13001302 {
13011303 name : "only port in ingress rules" ,
@@ -1557,6 +1559,100 @@ func TestIngressPolicy(t *testing.T) {
15571559 },
15581560 },
15591561 },
1562+ {
1563+ name : "error" ,
1564+ targetSelector : & metav1.LabelSelector {
1565+ MatchLabels : map [string ]string {
1566+ "label" : "src" ,
1567+ },
1568+ },
1569+ rules : []networkingv1.NetworkPolicyIngressRule {
1570+ {
1571+ Ports : []networkingv1.NetworkPolicyPort {
1572+ {
1573+ Protocol : & tcp ,
1574+ Port : & emptyString ,
1575+ },
1576+ },
1577+ },
1578+ },
1579+ npmNetPol : & policies.NPMNetworkPolicy {
1580+ Name : "serve-tcp" ,
1581+ NameSpace : "default" ,
1582+ PodSelectorIPSets : []* ipsets.TranslatedIPSet {
1583+ ipsets .NewTranslatedIPSet ("label:src" , ipsets .KeyValueLabelOfPod ),
1584+ ipsets .NewTranslatedIPSet ("default" , ipsets .Namespace ),
1585+ },
1586+ PodSelectorList : []policies.SetInfo {
1587+ policies .NewSetInfo ("label:src" , ipsets .KeyValueLabelOfPod , included , targetPodMatchType ),
1588+ policies .NewSetInfo ("default" , ipsets .Namespace , included , targetPodMatchType ),
1589+ },
1590+ ACLs : []* policies.ACLPolicy {
1591+ {
1592+ PolicyID : "azure-acl-default-serve-tcp" ,
1593+ Target : policies .Allowed ,
1594+ Direction : policies .Ingress ,
1595+ Protocol : "TCP" ,
1596+ },
1597+ defaultDropACL ("default" , "serve-tcp" , policies .Ingress ),
1598+ },
1599+ },
1600+ wantErr : true ,
1601+ },
1602+ {
1603+ name : "allow all ingress rules" ,
1604+ targetSelector : & metav1.LabelSelector {
1605+ MatchLabels : map [string ]string {
1606+ "label" : "src" ,
1607+ },
1608+ },
1609+ rules : []networkingv1.NetworkPolicyIngressRule {
1610+ {},
1611+ },
1612+ npmNetPol : & policies.NPMNetworkPolicy {
1613+ Name : "serve-tcp" ,
1614+ NameSpace : "default" ,
1615+ PodSelectorIPSets : []* ipsets.TranslatedIPSet {
1616+ ipsets .NewTranslatedIPSet ("label:src" , ipsets .KeyValueLabelOfPod ),
1617+ ipsets .NewTranslatedIPSet ("default" , ipsets .Namespace ),
1618+ },
1619+ PodSelectorList : []policies.SetInfo {
1620+ policies .NewSetInfo ("label:src" , ipsets .KeyValueLabelOfPod , included , targetPodMatchType ),
1621+ policies .NewSetInfo ("default" , ipsets .Namespace , included , targetPodMatchType ),
1622+ },
1623+ ACLs : []* policies.ACLPolicy {
1624+ {
1625+ PolicyID : "azure-acl-default-serve-tcp" ,
1626+ Target : policies .Allowed ,
1627+ Direction : policies .Ingress ,
1628+ },
1629+ },
1630+ },
1631+ },
1632+ {
1633+ name : "deny all in ingress rules" ,
1634+ targetSelector : & metav1.LabelSelector {
1635+ MatchLabels : map [string ]string {
1636+ "label" : "src" ,
1637+ },
1638+ },
1639+ rules : nil ,
1640+ npmNetPol : & policies.NPMNetworkPolicy {
1641+ Name : "serve-tcp" ,
1642+ NameSpace : "default" ,
1643+ PodSelectorIPSets : []* ipsets.TranslatedIPSet {
1644+ ipsets .NewTranslatedIPSet ("label:src" , ipsets .KeyValueLabelOfPod ),
1645+ ipsets .NewTranslatedIPSet ("default" , ipsets .Namespace ),
1646+ },
1647+ PodSelectorList : []policies.SetInfo {
1648+ policies .NewSetInfo ("label:src" , ipsets .KeyValueLabelOfPod , included , targetPodMatchType ),
1649+ policies .NewSetInfo ("default" , ipsets .Namespace , included , targetPodMatchType ),
1650+ },
1651+ ACLs : []* policies.ACLPolicy {
1652+ defaultDropACL ("default" , "serve-tcp" , policies .Ingress ),
1653+ },
1654+ },
1655+ },
15601656 }
15611657
15621658 for _ , tt := range tests {
@@ -1571,8 +1667,12 @@ func TestIngressPolicy(t *testing.T) {
15711667 npmNetPol .PodSelectorIPSets , npmNetPol .PodSelectorList , err = podSelectorWithNS (npmNetPol .NameSpace , policies .EitherMatch , tt .targetSelector )
15721668 require .NoError (t , err )
15731669 err = ingressPolicy (npmNetPol , tt .rules )
1574- require .NoError (t , err )
1575- require .Equal (t , tt .npmNetPol , npmNetPol )
1670+ if tt .wantErr {
1671+ require .Error (t , err )
1672+ } else {
1673+ require .NoError (t , err )
1674+ require .Equal (t , tt .npmNetPol , npmNetPol )
1675+ }
15761676 })
15771677 }
15781678}
0 commit comments