# Path: slicer_cli_web/girder_worker_plugin/direct_docker_run.py
# class DirectGirderFileIdToVolume(GirderFileIdToVolume):
#     def __init__(self, _id, filename=None, direct_file_path=None, **kwargs):
#         superc = super()
#         superc.__init__(_id, filename=_get_basename(filename, direct_file_path),
#                         **kwargs)
#         self._direct_file_path = direct_file_path
#         self._direct_container_path = None
# 
#     def resolve_direct_file_path(self):
#         if not self._direct_file_path:
#             return None
# 
#         path = abspath(self._direct_file_path)
#         if isfile(path) and access(path, R_OK):
#             self._direct_container_path = join(TEMP_VOLUME_DIRECT_MOUNT_PREFIX, self._filename)
#             return BindMountVolume(path, self._direct_container_path, mode='ro')
#         return None
# 
#     def transform(self, **kwargs):
#         if self._direct_container_path:
#             return self._direct_container_path
#         return super().transform(**kwargs)
# 
# @app.task(base=DirectDockerTask, bind=True)
# def run(task, **kwargs):
#     """Wraps docker_run to support direct mount volumnes."""
# 
#     pull_image = kwargs.get('pull_image')
#     if pull_image == 'if-not-present':
#         kwargs['pull_image'] = not _has_image(kwargs['image'])
# 
#     if hasattr(task, 'job_manager'):
#         stream_connectors = kwargs.setdefault('stream_connectors', [])
#         stream_connectors.append(FDReadStreamConnector(
#             input=ContainerStdOut(),
#             output=CLIProgressCLIWriter(task.job_manager)
#         ))
# 
#     return _docker_run(task, **kwargs)
# 
# TEMP_VOLUME_DIRECT_MOUNT_PREFIX = '/mnt/girder_direct_worker'


import pytest
from os.path import basename
from slicer_cli_web.girder_worker_plugin.direct_docker_run import DirectGirderFileIdToVolume, \
    run, TEMP_VOLUME_DIRECT_MOUNT_PREFIX
    from girder.models.file import File
            f.write('dummy')


@pytest.mark.plugin('slicer_cli_web')
def test_direct_docker_run(mocker, server, adminToken, file):

    docker_run_mock = mocker.patch(
        'slicer_cli_web.girder_worker_plugin.direct_docker_run._docker_run')
    docker_run_mock.return_value = []

    gc_mock = MockedGirderClient()

    path = File().getLocalFilePath(file)

    run(image='test', container_args=[DirectGirderFileIdToVolume(
        file['_id'], filename=basename(path), direct_file_path=None, gc=gc_mock)])
    docker_run_mock.assert_called_once()
    kwargs = docker_run_mock.call_args[1]
    # image
    assert kwargs['image'] == 'test'
    # container args
    assert len(kwargs['container_args']) == 1
    assert kwargs['container_args'][0].endswith(basename(path))
    # volumes
    assert len(kwargs['volumes']) == 1

    target_path = '%s/%s' % (TEMP_VOLUME_DIRECT_MOUNT_PREFIX, basename(path))

    docker_run_mock.reset_mock()


# Path: slicer_cli_web/girder_worker_plugin/direct_docker_run.py
# class DirectGirderFileIdToVolume(GirderFileIdToVolume):
#     def __init__(self, _id, filename=None, direct_file_path=None, **kwargs):
#         superc = super()
#         superc.__init__(_id, filename=_get_basename(filename, direct_file_path),
#                         **kwargs)
#         self._direct_file_path = direct_file_path
#         self._direct_container_path = None
# 
#     def resolve_direct_file_path(self):
#         if not self._direct_file_path:
#             return None
# 
#         path = abspath(self._direct_file_path)
#         if isfile(path) and access(path, R_OK):
#             self._direct_container_path = join(TEMP_VOLUME_DIRECT_MOUNT_PREFIX, self._filename)
#             return BindMountVolume(path, self._direct_container_path, mode='ro')
#         return None
# 
#     def transform(self, **kwargs):
#         if self._direct_container_path:
#             return self._direct_container_path
#         return super().transform(**kwargs)
# 
# @app.task(base=DirectDockerTask, bind=True)
# def run(task, **kwargs):
#     """Wraps docker_run to support direct mount volumnes."""
# 
#     pull_image = kwargs.get('pull_image')
#     if pull_image == 'if-not-present':
#         kwargs['pull_image'] = not _has_image(kwargs['image'])
# 
#     if hasattr(task, 'job_manager'):
#         stream_connectors = kwargs.setdefault('stream_connectors', [])
#         stream_connectors.append(FDReadStreamConnector(
#             input=ContainerStdOut(),
#             output=CLIProgressCLIWriter(task.job_manager)
#         ))
# 
#     return _docker_run(task, **kwargs)
# 
# TEMP_VOLUME_DIRECT_MOUNT_PREFIX = '/mnt/girder_direct_worker'


# Path: tests/girder_worker_plugin/test_direct_docker_run.py
import pytest
from os.path import basename
from slicer_cli_web.girder_worker_plugin.direct_docker_run import DirectGirderFileIdToVolume, \
    run, TEMP_VOLUME_DIRECT_MOUNT_PREFIX
    from girder.models.file import File

            f.write('dummy')


@pytest.mark.plugin('slicer_cli_web')
def test_direct_docker_run(mocker, server, adminToken, file):

    docker_run_mock = mocker.patch(
        'slicer_cli_web.girder_worker_plugin.direct_docker_run._docker_run')
    docker_run_mock.return_value = []

    gc_mock = MockedGirderClient()

    path = File().getLocalFilePath(file)

    run(image='test', container_args=[DirectGirderFileIdToVolume(
        file['_id'], filename=basename(path), direct_file_path=None, gc=gc_mock)])
    docker_run_mock.assert_called_once()
    kwargs = docker_run_mock.call_args[1]
    # image
    assert kwargs['image'] == 'test'
    # container args
    assert len(kwargs['container_args']) == 1
    assert kwargs['container_args'][0].endswith(basename(path))
    # volumes
    assert len(kwargs['volumes']) == 1

    target_path = '%s/%s' % (TEMP_VOLUME_DIRECT_MOUNT_PREFIX, basename(path))

    docker_run_mock.reset_mock()


    run(image='test', container_args=[DirectGirderFileIdToVolume(