Skip to content

Commit 338d6fe

Browse files
committed
add unit test for combining rules
1 parent 9bc6652 commit 338d6fe

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

pkg/policyendpoints/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (m *policyEndpointsManager) processPolicyEndpoints(pes []policyinfo.PolicyE
181181
return newPEs
182182
}
183183

184-
// the controller should consolidate the ingress endpoints and put entries to one CIDR if they belong to a same cidr
184+
// the controller should consolidate the ingress and egress endpoints and put entries to one CIDR if they belong to a same CIDR
185185
func combineRulesEndpoints(ingressEndpoints []policyinfo.EndpointInfo) []policyinfo.EndpointInfo {
186186
combinedMap := make(map[string]policyinfo.EndpointInfo)
187187
for _, iep := range ingressEndpoints {

pkg/policyendpoints/manager_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
networking "k8s.io/api/networking/v1"
1010
"k8s.io/apimachinery/pkg/api/equality"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
1213

1314
policyinfo "github.com/aws/amazon-network-policy-controller-k8s/api/v1alpha1"
1415
)
@@ -494,3 +495,61 @@ func Test_policyEndpointsManager_computePolicyEndpoints(t *testing.T) {
494495
})
495496
}
496497
}
498+
499+
func Test_processPolicyEndpoints(t *testing.T) {
500+
m := &policyEndpointsManager{
501+
logger: zap.New(),
502+
}
503+
504+
p80 := int32(80)
505+
p8080 := int32(8080)
506+
pTCP := corev1.ProtocolTCP
507+
pUDP := corev1.ProtocolUDP
508+
509+
pes := m.processPolicyEndpoints([]policyinfo.PolicyEndpoint{
510+
{
511+
Spec: policyinfo.PolicyEndpointSpec{
512+
Ingress: []policyinfo.EndpointInfo{
513+
{
514+
CIDR: "1.2.3.4",
515+
Ports: []policyinfo.Port{
516+
{Port: &p80, Protocol: &pTCP},
517+
},
518+
},
519+
{
520+
CIDR: "1.2.3.4",
521+
Ports: []policyinfo.Port{
522+
{Port: &p8080, Protocol: &pTCP},
523+
},
524+
},
525+
{
526+
CIDR: "1.2.3.4",
527+
Ports: []policyinfo.Port{
528+
{Protocol: &pUDP},
529+
},
530+
},
531+
},
532+
Egress: []policyinfo.EndpointInfo{
533+
{
534+
CIDR: "1.2.3.5",
535+
Ports: []policyinfo.Port{
536+
{Port: &p80, Protocol: &pTCP},
537+
},
538+
},
539+
{
540+
CIDR: "1.2.3.5",
541+
Ports: []policyinfo.Port{
542+
{Port: &p8080, Protocol: &pTCP},
543+
},
544+
},
545+
},
546+
},
547+
},
548+
})
549+
assert.Equal(t, 1, len(pes[0].Spec.Ingress))
550+
assert.Equal(t, 1, len(pes[0].Spec.Egress))
551+
assert.Equal(t, "1.2.3.4", string(pes[0].Spec.Ingress[0].CIDR))
552+
assert.Equal(t, "1.2.3.5", string(pes[0].Spec.Egress[0].CIDR))
553+
assert.Equal(t, 3, len(pes[0].Spec.Ingress[0].Ports))
554+
assert.Equal(t, 2, len(pes[0].Spec.Egress[0].Ports))
555+
}

0 commit comments

Comments
 (0)