def test_container_pod_gateway_runtime(protocol, gateway_runtime_docker_image_built): import docker with ContainerPod(set_gateway_parser().parse_args([ '--uses', 'docker://gateway-runtime', '--graph-description', '{"start-gateway": ["pod0"], "pod0": ["end-gateway"]}', '--deployments-addresses', '{"pod0": ["0.0.0.0:1234"]}', '--protocol', protocol, ])) as pod: container = pod._container status = pod._container.status time.sleep( 2 ) # to avoid desync between the start and close process which could lead to container never get terminated assert status == 'running' client = docker.from_env() containers = client.containers.list() assert container.id not in containers with pytest.raises(docker.errors.NotFound): pod._container
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 _create_head_pod(port): args = set_pod_parser().parse_args([]) args.port = port args.name = 'head' args.pod_role = PodRoleType.HEAD args.polling = PollingType.ANY args.uses = 'docker://head-runtime' return ContainerPod(args)
def _create_head_pod(port, connection_list_dict): args = set_pod_parser().parse_args([]) args.port = port args.name = 'head' args.pod_role = PodRoleType.HEAD args.polling = PollingType.ANY args.uses = 'docker://head-runtime' args.connection_list = json.dumps(connection_list_dict) return ContainerPod(args)
def test_failing_executor(fail_start_docker_image_built): import docker args = set_pod_parser().parse_args([ '--uses', 'docker://fail-start', ]) with pytest.raises(RuntimeFailToStart): pod = ContainerPod(args) with pod: pass with pytest.raises(docker.errors.NotFound): pod._container
def test_container_pod_head_runtime(head_runtime_docker_image_built): import docker with ContainerPod(set_pod_parser().parse_args([ '--uses', 'docker://head-runtime', ])) as pod: container = pod._container status = pod._container.status assert status == 'running' client = docker.from_env() containers = client.containers.list() assert container.id not in containers with pytest.raises(docker.errors.NotFound): pod._container
def test_container_pod_pass_envs(env_checker_docker_image_built): import docker with ContainerPod(set_pod_parser().parse_args([ '--uses', 'docker://env-checker', '--env', 'key1=value1', '--env', 'key2=value2', ])) as pod: container = pod._container status = container.status assert status == 'running' client = docker.from_env() containers = client.containers.list() assert container.id not in containers with pytest.raises(docker.errors.NotFound): pod._container
def test_container_pod_pass_envs(env_checker_docker_image_built): import docker with ContainerPod(set_pod_parser().parse_args([ '--uses', 'docker://env-checker', '--env', 'key1=value1', '--env', 'key2=value2', ])) as pod: container = pod._container status = container.status time.sleep( 2 ) # to avoid desync between the start and close process which could lead to container never get terminated assert status == 'running' client = docker.from_env() containers = client.containers.list() assert container.id not in containers
def test_container_pod_volume_setting( pod_args, expected_source, expected_destination, dummy_exec_docker_image_built, tmpdir, ): if expected_source: expected_source = os.path.join(tmpdir, expected_source) volume_arg = str(expected_source) + ':' + expected_destination pod_args.append(volume_arg) default_workspace = os.path.join(Path.home(), 'mock-workspace') with mock.patch.dict( os.environ, { 'JINA_DEFAULT_WORKSPACE_BASE': str(os.path.join(tmpdir, default_workspace)) }, ): with ContainerPod(set_pod_parser().parse_args(pod_args)) as pod: container = pod._container source = container.attrs['Mounts'][0]['Source'] destination = container.attrs['Mounts'][0]['Destination'] time.sleep( 2 ) # to avoid desync between the start and close process which could lead to container never get terminated expected_source = (os.path.abspath(expected_source) if expected_source else os.path.abspath(default_workspace)) expected_destination = expected_destination if expected_destination else '/app' assert source.startswith( expected_source) # there is a random workspace id at the end! assert destination == expected_destination
def test_pass_arbitrary_kwargs(monkeypatch, mocker): import docker mocker.patch( 'jina.serve.runtimes.asyncio.AsyncNewLoopRuntime.is_ready', return_value=True, ) class MockContainers: class MockContainer: def reload(self): pass def logs(self, **kwargs): return [] @property def id(self): return 'mock-id' def reload(self): pass def kill(self, signal, *args): assert signal == 'SIGTERM' def __init__(self): pass def list(self): return [] def get(self, *args): return MockContainers.MockContainer() def run(self, *args, **kwargs): assert 'ports' in kwargs assert 'environment' in kwargs envs = kwargs['environment'] assert 'JINA_DEPLOYMENT_NAME' in envs assert 'VAR1' in envs assert envs['VAR1'] == 'BAR' assert 'VAR2' in envs assert envs['VAR2'] == 'FOO' assert 'hello' in kwargs assert kwargs['hello'] == 0 return MockContainers.MockContainer() class MockClient: def __init__(self, *args, **kwargs): pass def close(self): pass def version(self): return {'Version': '20.0.1'} @property def networks(self): return {'bridge': None} @property def containers(self): return MockContainers() @property def images(self): return {} monkeypatch.setattr(docker, 'from_env', MockClient) args = set_pod_parser().parse_args([ '--uses', 'docker://jinahub/pod', '--env', '--env', 'VAR1=BAR', '--env', 'VAR2=FOO', '--docker-kwargs', 'hello: 0', ]) with ContainerPod(args) as pod: pass pod.join() assert pod.worker.exitcode == 0
def _create_worker_pod(port): args = set_pod_parser().parse_args([]) args.port = port args.name = 'worker' args.uses = 'docker://worker-runtime' return ContainerPod(args)