Example #1
0
def test_simple_container(docker_image_built):
    args = set_pea_parser().parse_args(['--uses', img_name])

    with ContainerRuntime(args):
        pass

    time.sleep(2)
    ContainerRuntime(args).start().close()
Example #2
0
def test_pass_arbitrary_kwargs(monkeypatch, mocker):
    import docker

    mock = mocker.Mock()

    class MockContainers:
        def __init__(self):
            pass

        def run(self, *args, **kwargs):
            mock_kwargs = {
                k: kwargs[k]
                for k in ['hello', 'ports', 'environment']
            }
            mock(**mock_kwargs)
            assert 'ports' in kwargs
            assert kwargs['ports'] is None
            assert 'environment' in kwargs
            assert kwargs['environment'] == ['VAR1=BAR', 'VAR2=FOO']
            assert 'hello' in kwargs
            assert kwargs['hello'] == 0

    class MockClient:
        def __init__(self, *args, **kwargs):
            pass

        def close(self):
            pass

        @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_pea_parser().parse_args([
        '--uses',
        'docker://jinahub/pod',
        '--docker-kwargs',
        'hello: 0',
        'environment: ["VAR1=BAR", "VAR2=FOO"]',
    ])
    runtime = ContainerRuntime(args)
    runtime._docker_run(replay=False)
    expected_args = {
        'hello': 0,
        'ports': None,
        'environment': ['VAR1=BAR', 'VAR2=FOO']
    }
    mock.assert_called_with(**expected_args)
Example #3
0
def test_simple_container_with_ext_yaml(docker_image_built):
    args = set_pea_parser().parse_args([
        '--uses', img_name, '--uses-internal',
        os.path.join(cur_dir, '../../../mwu-encoder/mwu_encoder_ext.yml')
    ])

    with ContainerRuntime(args):
        time.sleep(2)
Example #4
0
def test_simple_container_with_ext_yaml(docker_image_built):
    args = set_pea_parser().parse_args([
        '--uses', img_name, '--uses-internal',
        str(cur_dir.parent.parent.parent / 'mwu-encoder' /
            'mwu_encoder_ext.yml')
    ])

    with ContainerRuntime(args):
        time.sleep(2)
Example #5
0
def test_pass_native_arg(monkeypatch, mocker):
    import docker

    mocker.patch(
        'jina.peapods.runtimes.container.ContainerRuntime.is_ready',
        return_value=True,
    )

    class MockContainers:
        class MockContainer:
            def reload(self):
                pass

            def logs(self, **kwargs):
                return []

        def __init__(self):
            pass

        def get(self, *args):
            pass

        def run(self, *args, **kwargs):
            assert '--native' in args[1]
            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_pea_parser().parse_args([
        '--uses',
        'docker://jinahub/pod',
    ])
    _ = ContainerRuntime(args,
                         ctrl_addr='',
                         ready_event=multiprocessing.Event())
Example #6
0
def test_gpu_container(gpus_value, expected_count, expected_device,
                       expected_driver, expected_capabilities):
    args = set_pea_parser().parse_args(
        ['--uses', f'docker://{img_name}', '--gpus', gpus_value])

    device_requests = ContainerRuntime._get_gpu_device_requests(args.gpus)
    assert device_requests[0]['Count'] == expected_count
    assert device_requests[0]['DeviceIDs'] == expected_device
    assert device_requests[0]['Driver'] == expected_driver
    assert device_requests[0]['Capabilities'] == expected_capabilities
Example #7
0
def test_container_status():
    args = set_pea_parser().parse_args([
        '--uses', img_name, '--uses-internal',
        os.path.join(cur_dir, '../../../mwu-encoder/mwu_encoder_ext.yml')
    ])
    runtime = ContainerRuntime(args)
    assert not runtime.is_ready
    with runtime:
        time.sleep(2.)
        assert runtime.is_ready
Example #8
0
def test_container_status():
    args = set_pea_parser().parse_args([
        '--uses', img_name, '--uses-internal',
        str(cur_dir.parent.parent.parent / 'mwu-encoder' /
            'mwu_encoder_ext.yml')
    ])
    runtime = ContainerRuntime(args)
    assert not runtime.is_ready
    with runtime:
        time.sleep(2.)
        assert runtime.is_ready
Example #9
0
def test_container_ping(docker_image_built):
    a4 = set_pea_parser().parse_args(['--uses', img_name])
    a5 = set_ping_parser().parse_args(
        ['0.0.0.0', str(a4.port_ctrl), '--print-response'])

    # test with container
    with pytest.raises(SystemExit) as cm:
        with ContainerRuntime(a4):
            NetworkChecker(a5)

    assert cm.value.code == 0
Example #10
0
def test_pass_replica_id_shard_id(monkeypatch, mocker):
    import docker

    mocker.patch(
        'jina.peapods.runtimes.container.ContainerRuntime.is_ready',
        return_value=True,
    )

    class MockContainers:
        class MockContainer:
            def reload(self):
                pass

            def logs(self, **kwargs):
                return []

        def __init__(self):
            pass

        def get(self, *args):
            pass

        def run(self, *args, **kwargs):
            assert '--shard-id' in list(args[1])
            assert '--shards' in list(args[1])
            assert '--replica-id' in list(args[1])
            assert list(args[1])[list(args[1]).index('--shard-id') + 1] == '1'
            assert list(args[1])[list(args[1]).index('--shards') + 1] == '5'
            assert list(args[1])[list(args[1]).index('--replica-id') +
                                 1] == '1'
            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_pea_parser().parse_args([
        '--uses',
        'docker://jinahub/pod',
        '--docker-kwargs',
        'hello: 0',
        'environment: ["VAR1=BAR", "VAR2=FOO"]',
        '--shards',
        '5',
        '--shard-id',
        '1',
        '--replica-id',
        '1',
    ])
    _ = ContainerRuntime(args,
                         ctrl_addr='',
                         ready_event=multiprocessing.Event())
Example #11
0
def test_pass_arbitrary_kwargs(monkeypatch, mocker):
    import docker

    mock = mocker.Mock()
    mocker.patch(
        'jina.peapods.runtimes.container.ContainerRuntime.is_ready',
        return_value=True,
    )

    class MockContainers:
        class MockContainer:
            def reload(self):
                pass

            def logs(self, **kwargs):
                return []

        def __init__(self):
            pass

        def get(self, *args):
            pass

        def run(self, *args, **kwargs):
            mock_kwargs = {k: kwargs[k] for k in ['hello', 'environment']}
            mock(**mock_kwargs)
            assert 'ports' in kwargs
            assert 'environment' in kwargs
            assert kwargs['environment'] == ['VAR1=BAR', '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_pea_parser().parse_args([
        '--uses',
        'docker://jinahub/pod',
        '--docker-kwargs',
        'hello: 0',
        'environment: ["VAR1=BAR", "VAR2=FOO"]',
    ])
    _ = ContainerRuntime(args,
                         ctrl_addr='',
                         ready_event=multiprocessing.Event())
    expected_args = {'hello': 0, 'environment': ['VAR1=BAR', 'VAR2=FOO']}
    mock.assert_called_with(**expected_args)