Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangs18 committed Jan 30, 2024
1 parent 5caae42 commit 9a91857
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/loaders/common/loader_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from src.loaders.common.loader_common_names import (
COLLECTION_SOURCE_DIR,
CONTAINERS_CONF_PARAMS,
CONTAINERS_CONF_PATH,
DOCKER_HOST,
FATAL_ERROR,
FATAL_STACKTRACE,
Expand Down Expand Up @@ -299,8 +300,9 @@ def _get_containers_config(conf_path: str):
return config


def is_config_modification_required(conf_path: str):
def is_config_modification_required():
"""check if the config requires modification."""
conf_path = os.path.expanduser(CONTAINERS_CONF_PATH)
config = _get_containers_config(conf_path)
if not config.has_section("containers"):
return True
Expand All @@ -310,8 +312,9 @@ def is_config_modification_required(conf_path: str):
return False


def setup_callback_server_logs(conf_path: str):
def setup_callback_server_logs():
"""Set up containers.conf file for the callback server logs."""
conf_path = os.path.expanduser(CONTAINERS_CONF_PATH)
with open(conf_path, "w") as writer:
try:
fcntl.flock(writer.fileno(), fcntl.LOCK_EX)
Expand Down
7 changes: 4 additions & 3 deletions src/loaders/workspace_uploader/workspace_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ def _upload_assemblies_to_workspace(
"""
Upload assembly files to the target workspace in batch. The bulk method fails
and an error will be thrown if any of the assembly files in batch fails to upload.
Returns the list of workspace UPAs for the created objects in the same order as
`assembly_tuples`.
"""
inputs = [
{
Expand Down Expand Up @@ -646,14 +648,13 @@ def main():

try:
# setup container.conf file for the callback server logs if needed
conf_path = os.path.expanduser(loader_common_names.CONTAINERS_CONF_PATH)
if loader_helper.is_config_modification_required(conf_path):
if loader_helper.is_config_modification_required():
if click.confirm(
f"The config file at {loader_common_names.CONTAINERS_CONF_PATH}\n"
f"needs to be modified to allow for container logging.\n"
f"Params 'seccomp_profile' and 'log_driver' will be added/updated under section [containers]. Do so now?\n"
):
loader_helper.setup_callback_server_logs(conf_path)
loader_helper.setup_callback_server_logs()
else:
print("Permission denied and exiting ...")
return
Expand Down
89 changes: 82 additions & 7 deletions test/src/loaders/workspace_uploader/workspace_uploader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,11 @@ def test_upload_assembly_files_in_parallel(setup_and_teardown):
for assembly_name, assembly_dir in zip(ASSEMBLY_NAMES, assembly_dirs)
}

# ws.get_object_info3() is unused in this test case
ws = create_autospec(Workspace, spec_set=True, instance=True)
asu = create_autospec(AssemblyUtil, spec_set=True, instance=True)
asu.save_assemblies_from_fastas.return_value = {
"results":[
"results": [
{"upa": "12345/58/1"},
{"upa": "12345/60/1"}
]
Expand All @@ -328,6 +329,28 @@ def test_upload_assembly_files_in_parallel(setup_and_teardown):

assert uploaded_count == 2

# assert that no interactions occurred with ws
ws.get_object_info3.assert_not_called()

# assert that asu was called correctly
asu.save_assemblies_from_fastas.assert_called_once_with(
{
"workspace_id": 12345,
"inputs": [
{
"file": "/kb/module/work/tmp/GCF_000979855.1_gtlEnvA5udCFS_genomic.fna.gz",
"assembly_name": "GCF_000979855.1_gtlEnvA5udCFS_genomic.fna.gz",
"object_metadata": {"load_id": "214"},
},
{
"file": "/kb/module/work/tmp/GCF_000979175.1_gtlEnvA5udCFS_genomic.fna.gz",
"assembly_name": "GCF_000979175.1_gtlEnvA5udCFS_genomic.fna.gz",
"object_metadata": {"load_id": "214"},
}
]
}
)

# check softlink for post_process
assert os.readlink(os.path.join(upload_dir, "12345_58_1")) == os.path.join(
output_dir, "12345_58_1"
Expand All @@ -338,11 +361,13 @@ def test_upload_assembly_files_in_parallel(setup_and_teardown):

# check hardlink for post_process
assert os.path.samefile(
src_files[0], os.path.join(os.path.join(output_dir, "12345_58_1"), f"12345_58_1.fna.gz")
src_files[0],
os.path.join(os.path.join(output_dir, "12345_58_1"), "12345_58_1.fna.gz")
)

assert os.path.samefile(
src_files[1], os.path.join(os.path.join(output_dir, "12345_60_1"), f"12345_60_1.fna.gz")
src_files[1],
os.path.join(os.path.join(output_dir, "12345_60_1"), "12345_60_1.fna.gz")
)


Expand All @@ -362,9 +387,9 @@ def test_fail_upload_assembly_files_in_parallel(setup_and_teardown):
ws = create_autospec(Workspace, spec_set=True, instance=True)
asu = create_autospec(AssemblyUtil, spec_set=True, instance=True)
asu.save_assemblies_from_fastas.side_effect = Exception("Illegal character in object name")

loader_helper.list_objects = create_autospec(loader_helper.list_objects)
loader_helper.list_objects.return_value = []
ws.get_object_info3.return_value = {
'infos': [None, None], 'paths': [None, None]
}

uploaded_count = workspace_uploader._upload_assembly_files_in_parallel(
asu,
Expand All @@ -380,8 +405,39 @@ def test_fail_upload_assembly_files_in_parallel(setup_and_teardown):

assert uploaded_count == 0

# assert that asu was called correctly
asu.save_assemblies_from_fastas.assert_called_once_with(
{
"workspace_id": 12345,
"inputs": [
{
"file": "/kb/module/work/tmp/GCF_000979855.1_gtlEnvA5udCFS_genomic.fna.gz",
"assembly_name": "GCF_000979855.1_gtlEnvA5udCFS_genomic.fna.gz",
"object_metadata": {"load_id": "214"},
},
{
"file": "/kb/module/work/tmp/GCF_000979175.1_gtlEnvA5udCFS_genomic.fna.gz",
"assembly_name": "GCF_000979175.1_gtlEnvA5udCFS_genomic.fna.gz",
"object_metadata": {"load_id": "214"},
}
]
}
)

def test_query_workspace_with_load_id_mass(setup_and_teardown):
# assert that ws was called correctly
ws.get_object_info3.assert_called_once_with(
{
"objects": [
{"wsid": 12345, "name": "GCF_000979855.1_gtlEnvA5udCFS_genomic.fna.gz"},
{"wsid": 12345, "name": "GCF_000979175.1_gtlEnvA5udCFS_genomic.fna.gz"}
],
"ignoreErrors": 1,
"includeMetadata": 1
}
)


def test_fail_query_workspace_with_load_id_mass(setup_and_teardown):
ws = create_autospec(Workspace, spec_set=True, instance=True)
with pytest.raises(
Exception, match="The effective max batch size must be <= 10000"
Expand All @@ -394,7 +450,13 @@ def test_query_workspace_with_load_id_mass(setup_and_teardown):
10001,
)

# assert that no interactions occurred with ws
ws.get_object_info3.assert_not_called()


def test_query_workspace_with_load_id_mass(setup_and_teardown):
# happy test
ws = create_autospec(Workspace, spec_set=True, instance=True)
ws.get_object_info3.return_value = {
'infos': [
[
Expand Down Expand Up @@ -454,3 +516,16 @@ def test_query_workspace_with_load_id_mass(setup_and_teardown):
"GCF_000979375.1_gtlEnvA5udCFS_genomic.fna.gz",
]
assert obj_upas == ["69046_1086_18", "69046_1068_18"]

# assert that ws was called correctly
ws.get_object_info3.assert_called_once_with(
{
"objects": [
{"wsid": 69046, "name": "GCF_000980105.1_gtlEnvA5udCFS_genomic.fna.gz"},
{"wsid": 69046, "name": "GCF_000979375.1_gtlEnvA5udCFS_genomic.fna.gz"},
{"wsid": 69046, "name": "aloha"}
],
"ignoreErrors": 1,
"includeMetadata": 1
}
)

0 comments on commit 9a91857

Please sign in to comment.