def test_process_ports_multi_port( self, processor: K8sServiceModuleProcessor) -> None: input = { "ports": [ { "name": "a", "type": "http", "port": 1 }, { "name": "b", "type": "tcp", "port": 2 }, ], "probe_port": "b", } port_a = PortSpec("a", "http", 1, 80) port_b = PortSpec("b", "tcp", 2, 2) expected = { "ports": [port_a, port_b], "http_port": port_a, "probe_port": port_b, "service_annotations": { "nginx.opta.dev/extra-tcp-ports": '{"2": "b"}' }, } self.process_ports_assert(processor, input, expected=expected)
def test_to_json(self) -> None: spec = PortSpec("a", "http", 1, 80, "grpc", True) expected = { "name": "a", "type": "http", "port": 1, "servicePort": 80, "protocol": "grpc", "tls": True, "probeType": "tcp", } assert spec.__to_json__() == expected
def test_validate_ports_duplicate_num( self, processor: K8sServiceModuleProcessor) -> None: ports = [ PortSpec("a", "http", 1, 80), PortSpec("b", "tcp", 1, 80), ] self.validate_ports_assert( processor, ports, exception_type=UserErrors, exception_message="Duplicate port number `1`", )
def test_validate_ports_multiple_http( self, processor: K8sServiceModuleProcessor) -> None: ports = [ PortSpec("a", "http", 1, 80), PortSpec("b", "http", 2, 80), ] self.validate_ports_assert( processor, ports, exception_type=UserErrors, exception_message="Multiple `type: http` ports not supported", )
def test_validate_ports_flag_disabled_multiple( self, processor: K8sServiceModuleProcessor) -> None: processor.FLAG_MULTIPLE_PORTS_SUPPORTED = False ports = [ PortSpec("a", "http", 1, 80), PortSpec("b", "http", 2, 80), ] self.validate_ports_assert( processor, ports, exception_type=UserErrors, exception_message="Cannot specify multiple ports in this cloud", )
def test_read_ports(self, processor: K8sServiceModuleProcessor) -> None: raw = [{"name": "a", "port": 1, "type": "http"}] expected = [ PortSpec("a", "http", 1, 80), ] self.read_ports_assert(processor, raw, expected=expected)
def test_valid_type_protocols(self) -> None: protos = PortSpec.valid_type_protocols() assert len(protos) > 0 for type, protocol in protos: assert len(type) > 0 if protocol: assert len(protocol) > 0
def test_from_raw_simple(self) -> None: self.from_raw_assert( { "name": "a", "type": "http", "port": 1 }, expected=PortSpec("a", "http", 1, 80, None, False), )
def test_probe_type(self) -> None: tests: Dict[Tuple[str, Optional[str]], str] = { ("http", None): "http", ("http", "grpc"): "tcp", ("tcp", None): "tcp", } for input, expected in tests.items(): spec = PortSpec("a", input[0], 1, 80, input[1]) assert spec.probe_type == expected
def from_raw_assert( self, raw: Dict[str, Any], *, expected: Optional[PortSpec] = None, exception_type: Optional[Type[Exception]] = None, exception_message: Optional[str] = None, ) -> None: if exception_type: with pytest.raises(exception_type) as e: PortSpec.from_raw(raw) if exception_message is not None: assert str(e.value) == exception_message else: actual = PortSpec.from_raw(raw) assert actual == expected
def test_from_raw_advanced(self) -> None: self.from_raw_assert( { "name": "a", "type": "http", "port": 1, "protocol": "grpc", "tls": True }, expected=PortSpec("a", "http", 1, 80, "grpc", True), )
def test_is_tcp(self) -> None: assert PortSpec("a", "http", 1, 80).is_tcp is False assert PortSpec("a", "tcp", 1, 80).is_tcp is True
def test_legacy_port_type_mapping(self) -> None: mapping = PortSpec.legacy_port_type_mapping() valid_types = PortSpec.valid_type_protocols() for type_protocol in mapping.values(): assert type_protocol in valid_types