From 3e7b08d3f350280e82eda5a31bf17c165954334f Mon Sep 17 00:00:00 2001 From: Philipp Schmitt Date: Tue, 19 Jan 2021 16:34:32 +0100 Subject: [PATCH] Unit test for named port test case generation --- tests/test_test_generator.py | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/tests/test_test_generator.py b/tests/test_test_generator.py index 950004c..d238877 100644 --- a/tests/test_test_generator.py +++ b/tests/test_test_generator.py @@ -306,6 +306,85 @@ ], id="Correctly handle portless NetworkPolicy", ), + pytest.param( + [k8s.client.V1Namespace(metadata=k8s.client.V1ObjectMeta(name="default"))], + [ + k8s.client.V1NetworkPolicy( + metadata=k8s.client.V1ObjectMeta( + name="allow-named-port", namespace="default" + ), + spec=k8s.client.V1NetworkPolicySpec( + pod_selector=k8s.client.V1LabelSelector( + match_labels={ + "test.io/test-123_XYZ": "test_456-123.RECEIVER" + } + ), + ingress=[ + k8s.client.V1NetworkPolicyIngressRule( + _from=[ + k8s.client.V1NetworkPolicyPeer( + pod_selector=k8s.client.V1LabelSelector( + match_labels={ + "test.io/test-123_XYZ": "test_456-123.SENDER" + } + ) + ) + ], + ports=[ + k8s.client.V1NetworkPolicyPort( + port="mynamedport", protocol="TCP" + ) + ], + ) + ], + ), + ) + ], + [ + NetworkTestCase( + ClusterHost( + "default", {"test.io/test-123_XYZ": "test_456-123.SENDER"} + ), + ClusterHost( + "default", {"test.io/test-123_XYZ": "test_456-123.RECEIVER"} + ), + "mynamedport", + True, + ), + NetworkTestCase( + ClusterHost( + INVERTED_ATTRIBUTE_PREFIX + "default", {"test.io/test-123_XYZ": "test_456-123.SENDER"} + ), + ClusterHost( + "default", {"test.io/test-123_XYZ": "test_456-123.RECEIVER"} + ), + "mynamedport", + False, + ), + NetworkTestCase( + ClusterHost( + "default", {INVERTED_ATTRIBUTE_PREFIX + "test.io/test-123_XYZ": "test_456-123.SENDER"} + ), + ClusterHost( + "default", {"test.io/test-123_XYZ": "test_456-123.RECEIVER"} + ), + "mynamedport", + False, + ), + NetworkTestCase( + ClusterHost( + INVERTED_ATTRIBUTE_PREFIX + "default", + {INVERTED_ATTRIBUTE_PREFIX + "test.io/test-123_XYZ": "test_456-123.SENDER"} + ), + ClusterHost( + "default", {"test.io/test-123_XYZ": "test_456-123.RECEIVER"} + ), + "mynamedport", + False, + ), + ], + id="Correctly handle NetworkPolicy with named Port", + ), ], ) def test__generate_test_cases(namespaces, networkpolicies, expected_testcases):