Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Add a test with appropriate mocking for docker run #328

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import io
import mock
import pytest
import random

from girder_client import GirderClient


@pytest.fixture
def mock_gc():
mgc = mock.MagicMock(spec=GirderClient)
mgc.getFile.return_value = {'_id': 'BOGUS_ID', 'name': 'bogus.txt'}
return mgc


@pytest.fixture
def patch_mkdir():
with mock.patch('os.mkdir') as mkdir_mock:
yield mkdir_mock


@pytest.fixture
def patch_makedirs():
with mock.patch('os.makedirs') as m:
yield m


@pytest.fixture
def stream():
Expand Down
53 changes: 52 additions & 1 deletion tests/test_docker_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

import mock
from girder_worker.docker.transforms.girder import GirderFolderIdToVolume, GirderFileIdToVolume
from girder_worker.docker.tasks import ( # noqa F401
DockerTask,
_docker_run,
Expand Down Expand Up @@ -51,3 +52,53 @@ def test__run_select_loop():
@pytest.mark.skip
def test_docker_run():
pass


@pytest.fixture
def patch_select_loop():
with mock.patch('girder_worker.docker.tasks.utils.select_loop') as m:
yield m


@pytest.fixture
def patch_task_canceled():
with mock.patch('girder_worker.task.is_revoked', return_value=False) as m:
yield m


@pytest.fixture
def patch_docker_client_containers_run():
with mock.patch('girder_worker.docker.tasks.docker.from_env',
return_value=mock.MagicMock(name='client')) as from_env:
client = from_env.return_value
client.containers.run.return_value.attrs = {
'State': {'ExitCode': 0}
}
yield client.containers.run


def test_docker_run_folder_name_with_spaces(
mock_gc,
patch_makedirs,
patch_select_loop,
patch_task_canceled,
patch_docker_client_containers_run):

output_path = '/tmp/foo/bar'
docker_run('bogus_image', container_args=[
GirderFolderIdToVolume('BOGUS_FOLDER_ID', folder_name='Folder Name', gc=mock_gc),
GirderFileIdToVolume('BOGUS_FILE_ID', gc=mock_gc), '--someoption', output_path
], pull_image=False, runtime='nvidia')

assert patch_docker_client_containers_run.call_count >= 1
args = patch_docker_client_containers_run.call_args_list[0][0]

# args should be of the form:
# ('bogus_image',
# ['/mnt/girder_worker/afdc8ca5e6524821980cd3a88f655bbe/BOGUS_FOLDER_ID/Folder Name',
# '/mnt/girder_worker/afdc8ca5e6524821980cd3a88f655bbe/BOGUS_FILE_ID/bogus.txt'])

assert args[0] == 'bogus_image'
assert len(args[1]) == 4
assert args[1][0].endswith('BOGUS_FOLDER_ID/Folder Name')
assert args[1][1].endswith('BOGUS_FILE_ID/bogus.txt')
22 changes: 2 additions & 20 deletions tests/test_docker_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from girder_worker.app import Task
from girder_worker.utils import JobManager
from girder_client import GirderClient

from girder_worker.docker.io import (
ChunkedTransferEncodingStreamWriter,
Expand Down Expand Up @@ -50,6 +49,8 @@
GirderUploadVolumePathJobArtifact
)

from .conftest import mock_gc

BOGUS_HOST_PATH = '/bogus/volume/host_path'
BOGUS_CONTAINER_PATH = '/bogus/volume/container_path'

Expand All @@ -62,25 +63,6 @@ def transform(self, **kargs):
assert _maybe_transform(T()) == 'FOOBAR'


@pytest.fixture
def mock_gc():
mgc = mock.MagicMock(spec=GirderClient)
mgc.getFile.return_value = {'_id': 'BOGUS_ID', 'name': 'bogus.txt'}
return mgc


@pytest.fixture
def patch_mkdir():
with mock.patch('os.mkdir') as mkdir_mock:
yield mkdir_mock


@pytest.fixture
def patch_makedirs():
with mock.patch('os.makedirs') as m:
yield m


@pytest.fixture
def bogus_volume():
yield BindMountVolume(BOGUS_HOST_PATH, BOGUS_CONTAINER_PATH)
Expand Down