def build_pod(args: 'Namespace') -> Type['BasePod']: """Build an implementation of a `BasePod` interface :param args: deployment arguments parsed from the CLI. :return: the created BaseDeployment """ # copy to update but forward original cargs = deepcopy(args) if cargs.host != __default_host__ and not cargs.disable_remote: cargs.timeout_ready = -1 return JinaDPod(cargs) if is_valid_huburi(cargs.uses): _hub_args = deepcopy(args) _hub_args.uri = args.uses _hub_args.no_usage = True cargs.uses = HubIO(_hub_args).pull() if ( cargs.pod_role != PodRoleType.HEAD and cargs.uses and cargs.uses.startswith('docker://') ): return ContainerPod(cargs) else: return Pod(args)
def test_non_gateway_runtimes(runtime_cls): args = set_pod_parser().parse_args([ '--runtime-cls', runtime_cls, ]) with Pod(args) as p: assert p.runtime_cls.__name__ == runtime_cls
def _create_worker_pod(port, name='', executor=None): args = set_pod_parser().parse_args([]) args.port = port args.name = name args.no_block_on_start = True if executor: args.uses = executor return Pod(args)
def test_failing_executor(): args = set_pod_parser().parse_args([ '--uses', 'RaisingExecutor', ]) with pytest.raises(RuntimeFailToStart): with Pod(args): pass
def _create_gateway_pod(graph_description, pod_addresses, port): return Pod(set_gateway_parser().parse_args([ '--graph-description', graph_description, '--deployments-addresses', pod_addresses, '--port', str(port), ]))
def test_failing_head(): args = set_pod_parser().parse_args([ '--runtime-cls', 'HeadRuntime', ]) args.port = None with pytest.raises(RuntimeFailToStart): with Pod(args): pass
def test_gateway_args(protocol, expected): args = set_gateway_parser().parse_args([ '--host', 'jina-custom-gateway', '--port', '23456', '--protocol', protocol, ]) p = Pod(args) assert p.runtime_cls.__name__ == expected
def test_gateway_runtimes(protocol, expected): args = set_gateway_parser().parse_args([ '--graph-description', '{"start-gateway": ["pod0"], "pod0": ["end-gateway"]}', '--deployments-addresses', '{"pod0": ["0.0.0.0:1234"]}', '--protocol', protocol, ]) with Pod(args) as p: assert p.runtime_cls.__name__ == expected
def test_failing_gateway_runtimes(protocol, expected): args = set_gateway_parser().parse_args([ '--graph-description', '{"start-gateway": ["pod0"], "pod0": ["end-gateway"]}', '--deployments-addresses', '{_INVALIDJSONINTENTIONALLY_pod0": ["0.0.0.0:1234"]}', '--protocol', protocol, ]) with pytest.raises(RuntimeFailToStart): with Pod(args): pass
def _create_head_pod(port, name='', polling='ANY', uses_before=None, uses_after=None): args = set_pod_parser().parse_args([]) args.port = port args.name = name args.pod_cls = 'HeadRuntime' args.pod_role = PodRoleType.HEAD args.no_block_on_start = True args.polling = PollingType.ANY if polling == 'ANY' else PollingType.ALL if uses_before: args.uses_before_address = uses_before if uses_after: args.uses_after_address = uses_after return Pod(args)
def _create_gateway_pod(graph_description, pod_addresses, port_expose): return Pod( set_gateway_parser().parse_args( [ '--graph-description', graph_description, '--deployments-addresses', pod_addresses, '--port-expose', str(port_expose), '--noblock-on-start', ] ) )
def test_pod_runtime_env_setting(fake_env): with Pod(set_pod_parser().parse_args([ '--uses', 'EnvChecker1', '--env', 'key1=value1', '--env', 'key2=value2', ])): pass # should not affect the main process assert 'key1' not in os.environ assert 'key2' not in os.environ assert 'key_parent' in os.environ
def _create_gateway_pod(graph_description, pod_addresses, port, protocol='grpc'): return Pod(set_gateway_parser().parse_args([ '--graph-description', graph_description, '--deployments-addresses', pod_addresses, '--port', str(port), '--noblock-on-start', '--protocol', protocol, ]))
def test_pod_runtime_env_setting_in_thread(fake_env): os.environ['key_parent'] = 'value3' with Pod(set_pod_parser().parse_args([ '--uses', 'EnvChecker2', '--env', 'key1=value1', '--env', 'key2=value2', '--runtime-backend', 'thread', ])): pass # should not affect the main process assert 'key1' not in os.environ assert 'key2' not in os.environ assert 'key_parent' in os.environ os.environ.pop('key_parent')
def test_close_before_start_slow_enter(monkeypatch): class SlowFakeRuntime: def __init__(self, *args, **kwargs): pass def __enter__(self): time.sleep(5.0) def __exit__(self, exc_type, exc_val, exc_tb): pass def run_forever(self): pass monkeypatch.setattr( runtimes, 'get_runtime', lambda *args, **kwargs: SlowFakeRuntime, ) pod = Pod(set_pod_parser().parse_args(['--noblock-on-start'])) pod.start() pod.close()