def test_fromYaml_simpleSampleYaml_returnsExpectedCase(): testYaml = "localhost:\n namespc:label=val: ['-80']\n" expectedHost = ClusterHost("namespc", {"label": "val"}) expected = NetworkTestCase(LocalHost(), expectedHost, 80, False) actual = from_yaml(testYaml) assert len(actual) == 1 assert actual[0] == expected
def test_portString_shouldConnectTrue_outputsPortOnly(): port = 80 test_case = NetworkTestCase(LocalHost(), LocalHost(), port, True) assert test_case.port_string == str(port)
def test_NetworkTestCase_eq_sameFieldsLocalHostsOnly_returnsTrue(): port = 80 case1 = NetworkTestCase(LocalHost(), LocalHost(), port, True) case2 = NetworkTestCase(LocalHost(), LocalHost(), port, True) assert case1 == case2
def test_NetworkTestCase_eq_differentShouldConnect_returnsFalse(): port = 80 case1 = NetworkTestCase(LocalHost(), LocalHost(), port, True) case2 = NetworkTestCase(LocalHost(), LocalHost(), port, False) assert case1 != case2
def test_NetworkTestCase_eq_differentToHost_returnsFalse(): port = 80 case1 = NetworkTestCase(LocalHost(), LocalHost(), port, True) case2 = NetworkTestCase(LocalHost(), test_host1, port, True) assert case1 != case2
def test_toYaml_oneTestCase_returnsExpectedYaml(): testHost = ClusterHost("namespc", {"label": "val"}) case = NetworkTestCase(LocalHost(), testHost, 80, False) expected = "localhost:\n namespc:label=val: ['-80']\n" assert to_yaml([case]) == expected
def test_portString_shouldConnectFalse_outputsPortWithMinusPrefix(): port = 80 test_case = NetworkTestCase(LocalHost(), LocalHost(), port, False) assert test_case.port_string == "-" + str(port)
def test_ClusterHost_matches_podGivenAndFromHostIsClusterHostsWithSubsetLabelsOfPod_returnsTrue( ): meta = k8s.client.V1ObjectMeta(namespace="default", labels={ "app": "web", "load": "high" }) test_host = ClusterHost(meta.namespace, {"app": meta.labels["app"]}) non_matching_pod = k8s.client.V1Pod(metadata=meta) assert test_host.matches(non_matching_pod) is True def test_TestCase_matches_podGivenAndHostsAreNotClusterHosts_returnsFalse(): case = NetworkTestCase(LocalHost(), LocalHost(), 80, False) meta = k8s.client.V1ObjectMeta(namespace=test_host1.namespace, labels=test_host1.pod_labels) non_matching_pod = k8s.client.V1Pod(metadata=meta) assert case.matches([non_matching_pod]) is False def test_TestCase_matches_podGivenAndOnlyFromHostMatches_returnsFalse(): case = NetworkTestCase(test_host1, LocalHost(), 80, False) meta = k8s.client.V1ObjectMeta(namespace=test_host1.namespace, labels=test_host1.pod_labels) from_matching_pod = k8s.client.V1Pod(metadata=meta) assert case.matches([from_matching_pod]) is False def test_TestCase_matches_podGivenAndOnlyToHostMatches_returnsFalse():
import pytest from illuminatio.host import ( ClusterHost, ConcreteClusterHost, ExternalHost, LocalHost, GenericClusterHost, Host, ) @pytest.mark.parametrize( "identifier,expected_host", [ pytest.param("localhost", LocalHost(), id="LocalHost"), pytest.param( "127.0.0.1", ExternalHost("127.0.0.1"), id="ExternalHost with localhost IPv4", ), pytest.param( "123.123.123.123", ExternalHost("123.123.123.123"), id="ExternalHost with IPv4", ), pytest.param( "fe80::1ff:fe23:4567:890a", ExternalHost("fe80::1ff:fe23:4567:890a"), id="ExternalHost with IPv6", ),