-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Cadair/globus_transfer
Add support for Globus transfer and monitoring
- Loading branch information
Showing
8 changed files
with
408 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added support for starting and monitoring Globus transfer tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
""" | ||
from .auth import * | ||
from .endpoints import * | ||
from .transfer import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from unittest import mock | ||
import pytest | ||
|
||
from dkist.utils.globus.endpoints import get_transfer_client | ||
|
||
|
||
@pytest.fixture() | ||
def transfer_client(mocker): | ||
mocker.patch("globus_sdk.TransferClient.get_submission_id", | ||
return_value={'value': "1234"}) | ||
|
||
tc = get_transfer_client() | ||
|
||
mocker.patch("dkist.utils.globus.endpoints.get_transfer_client", | ||
return_value=tc) | ||
mocker.patch("dkist.utils.globus.transfer.get_transfer_client", | ||
return_value=tc) | ||
mocker.patch("dkist.utils.globus.auth.get_refresh_token_authorizer", | ||
return_value=None) | ||
return tc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import pathlib | ||
from unittest import mock | ||
|
||
import pytest | ||
from globus_sdk import GlobusResponse | ||
|
||
from dkist.utils.globus.transfer import (_get_speed, _process_task_events, | ||
start_transfer_from_file_list) | ||
|
||
|
||
@pytest.fixture | ||
def mock_endpoints(mocker): | ||
def id_mock(endpoint, tc): | ||
return endpoint | ||
mocker.patch("dkist.utils.globus.transfer.auto_activate_endpoint") | ||
return mocker.patch("dkist.utils.globus.transfer.get_endpoint_id", | ||
side_effect=id_mock) | ||
|
||
|
||
@pytest.fixture | ||
def mock_task_event_list(mocker): | ||
task_list = [ | ||
GlobusResponse({ | ||
'DATA_TYPE': 'event', | ||
'code': 'STARTED', | ||
'description': 'started', | ||
'details': | ||
'{\n "type": "GridFTP Transfer", \n "concurrency": 2, \n "protocol": "Mode S"\n}', | ||
'is_error': False, | ||
'parent_task_id': None, | ||
'time': '2019-05-16 10:13:26+00:00' | ||
}), | ||
GlobusResponse({ | ||
'DATA_TYPE': 'event', | ||
'code': 'SUCCEEDED', | ||
'description': 'succeeded', | ||
'details': 'Scanned 100 file(s)', | ||
'is_error': False, | ||
'parent_task_id': None, | ||
'time': '2019-05-16 10:13:24+00:00' | ||
}), | ||
GlobusResponse({ | ||
'DATA_TYPE': 'event', | ||
'code': 'STARTED', | ||
'description': 'started', | ||
'details': 'Starting sync scan', | ||
'is_error': False, | ||
'parent_task_id': None, | ||
'time': '2019-05-16 10:13:20+00:00' | ||
}) | ||
] | ||
return mocker.patch("globus_sdk.TransferClient.task_event_list", | ||
return_value=task_list) | ||
|
||
|
||
def test_start_transfer(mocker, transfer_client, mock_endpoints): | ||
submit_mock = mocker.patch("globus_sdk.TransferClient.submit_transfer", | ||
return_value={"task_id": "task_id"}) | ||
file_list = list(map(pathlib.Path, ["/a/name.fits", "/a/name2.fits"])) | ||
start_transfer_from_file_list("a", "b", "/", file_list) | ||
calls = mock_endpoints.call_args_list | ||
assert calls[0][0][0] == "a" | ||
assert calls[1][0][0] == "b" | ||
|
||
submit_mock.assert_called_once() | ||
transfer_manifest = submit_mock.call_args_list[0][0][0]['DATA'] | ||
|
||
for filepath, tfr in zip(file_list, transfer_manifest): | ||
assert str(filepath) == tfr['source_path'] | ||
assert "/" + filepath.name == tfr['destination_path'] | ||
|
||
|
||
def test_start_transfer_src_base(mocker, transfer_client, mock_endpoints): | ||
submit_mock = mocker.patch("globus_sdk.TransferClient.submit_transfer", | ||
return_value={"task_id": "task_id"}) | ||
file_list = list(map(pathlib.Path, ["/a/b/name.fits", "/a/b/name2.fits"])) | ||
start_transfer_from_file_list("a", "b", "/", file_list, "/a") | ||
calls = mock_endpoints.call_args_list | ||
assert calls[0][0][0] == "a" | ||
assert calls[1][0][0] == "b" | ||
|
||
submit_mock.assert_called_once() | ||
transfer_manifest = submit_mock.call_args_list[0][0][0]['DATA'] | ||
|
||
for filepath, tfr in zip(file_list, transfer_manifest): | ||
assert str(filepath) == tfr['source_path'] | ||
assert "/b/" + filepath.name == tfr['destination_path'] | ||
|
||
|
||
def test_process_event_list(transfer_client, mock_task_event_list): | ||
(events, | ||
json_events, | ||
message_events) = _process_task_events("1234", set(), transfer_client) | ||
|
||
assert isinstance(events, set) | ||
assert all([isinstance(e, tuple) for e in events]) | ||
assert all([all([isinstance(item, tuple) for item in e]) for e in events]) | ||
|
||
print(events) | ||
assert len(json_events) == 1 | ||
assert isinstance(json_events, tuple) | ||
assert isinstance(json_events[0], dict) | ||
assert isinstance(json_events[0]['details'], dict) | ||
assert json_events[0]['code'] == 'STARTED' | ||
|
||
assert len(message_events) == 2 | ||
assert isinstance(message_events, tuple) | ||
assert isinstance(message_events[0], dict) | ||
assert isinstance(message_events[0]['details'], str) | ||
|
||
|
||
def test_process_event_list_message_only(transfer_client, mock_task_event_list): | ||
# Filter out the json event | ||
prev_events = tuple(map(lambda x: tuple(x.data.items()), | ||
mock_task_event_list.return_value)) | ||
prev_events = set(prev_events[0:1]) | ||
|
||
(events, | ||
json_events, | ||
message_events) = _process_task_events("1234", prev_events, transfer_client) | ||
|
||
assert isinstance(events, set) | ||
assert all([isinstance(e, tuple) for e in events]) | ||
assert all([all([isinstance(item, tuple) for item in e]) for e in events]) | ||
|
||
assert len(json_events) == 0 | ||
assert isinstance(json_events, tuple) | ||
|
||
assert len(message_events) == 2 | ||
assert isinstance(message_events, tuple) | ||
assert isinstance(message_events[0], dict) | ||
assert isinstance(message_events[0]['details'], str) | ||
|
||
|
||
def test_get_speed(): | ||
speed = _get_speed({'code': "PROGRESS", 'details': {'mbps': 10}}) | ||
assert speed == 10 | ||
speed = _get_speed({}) | ||
assert speed is None | ||
speed = _get_speed({'code': "progress", "details": "hello"}) | ||
assert speed is None | ||
speed = _get_speed({'code': "progress", "details": {"hello": "world"}}) | ||
assert speed is None |
Oops, something went wrong.