diff --git a/pkg/netpol/connlist/exposure_analysis_test.go b/pkg/netpol/connlist/exposure_analysis_test.go index 6831bf5a..34b94294 100644 --- a/pkg/netpol/connlist/exposure_analysis_test.go +++ b/pkg/netpol/connlist/exposure_analysis_test.go @@ -337,16 +337,41 @@ func checkExpectedVsActualData(t *testing.T, testName string, actualExp ExposedP "test: %q, mismatch in is egress protected for peer %q", testName, actualExp.ExposedPeer().String()) require.Equal(t, expectedData.isIngressProtected, actualExp.IsProtectedByIngressNetpols(), "test: %q, mismatch in is ingress protected for peer %q", testName, actualExp.ExposedPeer().String()) - require.Equal(t, expectedData.lenIngressExposedConns, len(actualExp.IngressExposure()), + ingressExposure := actualExp.IngressExposure() + require.Equal(t, expectedData.lenIngressExposedConns, len(ingressExposure), "test: %q, mismatch in length of ingress exposure slice for peer %q", testName, actualExp.ExposedPeer().String()) for i := range expectedData.ingressExp { - require.Contains(t, actualExp.IngressExposure(), expectedData.ingressExp[i], + require.True(t, checkXgressExposureContainment(ingressExposure, expectedData.ingressExp[i]), "test: %q, expected ingress data %v is not contained in actual results", testName, expectedData.ingressExp[i]) } - require.Equal(t, expectedData.lenEgressExposedConns, len(actualExp.EgressExposure()), + egressExposure := actualExp.EgressExposure() + require.Equal(t, expectedData.lenEgressExposedConns, len(egressExposure), "test: %q, mismatch in length of egress exposure slice for peer %q", testName, actualExp.ExposedPeer().String()) for i := range expectedData.egressExp { - require.Contains(t, actualExp.EgressExposure(), expectedData.egressExp[i], + require.True(t, checkXgressExposureContainment(egressExposure, expectedData.egressExp[i]), "test: %q, expected egress data %v is not contained in actual results", testName, expectedData.egressExp[i]) } } + +func checkXgressExposureContainment(actualArray []XgressExposureData, expectedItem *xgressExposure) bool { + for i := range actualArray { + currItem := actualArray[i].(*xgressExposure) + if currItem.IsExposedToEntireCluster() != expectedItem.IsExposedToEntireCluster() { + continue + } + if !currItem.IsExposedToEntireCluster() { + if currItem.namespaceLabels.String() != expectedItem.namespaceLabels.String() { + continue + } + if currItem.podLabels.String() != expectedItem.podLabels.String() { + continue + } + } + v1 := expectedItem.PotentialConnectivity().(*common.ConnectionSet) + v2 := currItem.PotentialConnectivity().(*common.ConnectionSet) + if v1.Equal(v2) { + return true + } + } + return false +}