From 1256d2b4e1584665fc48813f2c9eb68d5c0be7f5 Mon Sep 17 00:00:00 2001 From: Christopher Kotfila Date: Mon, 3 Dec 2018 09:10:22 -0500 Subject: [PATCH] Add a test with appropriate mocking for docker run --- tests/conftest.py | 22 ++++++++++++++ tests/test_docker_tasks.py | 53 ++++++++++++++++++++++++++++++++- tests/test_docker_transforms.py | 22 ++------------ 3 files changed, 76 insertions(+), 21 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 75d62b97..748dfa45 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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(): diff --git a/tests/test_docker_tasks.py b/tests/test_docker_tasks.py index a0755f58..5ce7c335 100644 --- a/tests/test_docker_tasks.py +++ b/tests/test_docker_tasks.py @@ -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, @@ -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') diff --git a/tests/test_docker_transforms.py b/tests/test_docker_transforms.py index 8993ab52..73e64e18 100644 --- a/tests/test_docker_transforms.py +++ b/tests/test_docker_transforms.py @@ -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, @@ -50,6 +49,8 @@ GirderUploadVolumePathJobArtifact ) +from .conftest import mock_gc + BOGUS_HOST_PATH = '/bogus/volume/host_path' BOGUS_CONTAINER_PATH = '/bogus/volume/container_path' @@ -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)