Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6b7752a
Tests: schema imports, metadata view imports and workflow color in th…
TorecLuik Nov 12, 2026
5d9f220
Tests: detached resume decisions, cancellation and launcher registration
TorecLuik Nov 12, 2026
a920b38
Tests: wrap-around colours, forwarding, literal reduced text, strict …
TorecLuik Nov 12, 2026
dea7cea
Tests: cancel through every stage, cancel script, no silent skip in C…
TorecLuik Nov 12, 2026
72bf792
Tests: cancel/claim-lost through every stage, stop_check, monitor sta…
TorecLuik Nov 12, 2026
b226e94
Tests: claim lost during poll, import and conversion; late cancel com…
TorecLuik Nov 12, 2026
1aae840
Tests: input marker removed from results after import on every path
TorecLuik Nov 13, 2026
0194599
Tests: per-directory canonical sidecars and unwritable folders
TorecLuik Nov 13, 2026
9c13f93
Tests: result folder depth, failing cleanup, unreadable folder, links
TorecLuik Nov 13, 2026
ee39da5
Merge test-suite into test/remove-input-marker-after-import
TorecLuik Nov 13, 2026
04a16db
Merge remote-tracking branch 'origin/test-suite' into detached-owners…
TorecLuik Nov 13, 2026
f10d181
Tests: schema marker constant, flag states, path escape, RString outputs
TorecLuik Nov 13, 2026
3678563
Tests: batched launcher contract, cancel script runScript (admin, own…
TorecLuik Nov 13, 2026
2302731
Tests: both scripts take the Results_Path output name from core const…
TorecLuik Nov 13, 2026
0f61b3d
Merge remote-tracking branch 'origin/test-suite' into test/canonical-…
TorecLuik Nov 13, 2026
5910c93
Tests: unwritable sidecar fallback, corrupt sidecar, move rollback
TorecLuik Nov 13, 2026
13b53e3
Tests: shared output names come from core constants
TorecLuik Nov 13, 2026
8afa012
Merge test-suite into provenance-views-cleanup-tests
TorecLuik Nov 13, 2026
dd5740a
Merge branch 'scripts-b26-t98' into scripts-b26-t102
TorecLuik Nov 13, 2026
c8ccaae
Merge branch 'scripts-b26-t89' into scripts-b26-t102
TorecLuik Nov 13, 2026
1a4395e
Merge the earlier test PRs (#98, #89, #91) into use-core-output-const…
TorecLuik Nov 13, 2026
0356fa0
Review fixes: detect every literal output name, never skip the self-test
TorecLuik Nov 13, 2026
fb4eef2
Merge the sidecar test PR (#99) into use-core-output-constants-tests
TorecLuik Nov 13, 2026
2a37413
Accept the partly migrated import block of the input marker cleanup
TorecLuik Nov 13, 2026
5888171
Skip the cancel script tests for source without the cancel script
TorecLuik Nov 13, 2026
bfaed29
Merge the updated earlier test PRs (#89, #91) into use-core-output-co…
TorecLuik Nov 13, 2026
55a5517
Merge the updated detached tests (#91) into use-core-output-constants…
TorecLuik Nov 13, 2026
99f13b1
Gate the per-script check on the shared names only
TorecLuik Nov 13, 2026
42a64e9
Merge remote-tracking branch 'origin/test-suite' into fix-tests-91
TorecLuik Nov 16, 2026
6805dac
Merge remote-tracking branch 'origin/test-suite' into fix-tests-99
TorecLuik Nov 16, 2026
53edc01
Fail instead of skip when the detached batched feature lacks its pieces
TorecLuik Nov 16, 2026
a275eba
Gate sidecar tests on feature presence, not log text
TorecLuik Nov 16, 2026
e28178a
Merge remote-tracking branch 'origin/detached-ownership-tests' into f…
TorecLuik Nov 16, 2026
38c999b
Merge remote-tracking branch 'origin/test/canonical-sidecar-stubs' in…
TorecLuik Nov 16, 2026
6acf0e4
Check RESULTS_PATH_OUTPUT in the shared output names test
TorecLuik Nov 16, 2026
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
17 changes: 17 additions & 0 deletions tests/script_output_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""The shared output names, as the scripts read them from biomero.constants.

Tests that run script functions in a bare namespace provide this as
``constants.results`` (or merge it into their own). The values come from
core itself, so they cannot drift from it.
"""
from types import SimpleNamespace

from biomero.constants import results

OUTPUT_NAMES = SimpleNamespace(
MESSAGE_OUTPUT=results.MESSAGE_OUTPUT,
URL_OUTPUT=results.URL_OUTPUT,
FILE_ANNOTATION_OUTPUT=results.FILE_ANNOTATION_OUTPUT,
CANONICAL_INPUTS_OUTPUT=results.CANONICAL_INPUTS_OUTPUT,
RESULTS_PATH_OUTPUT=results.RESULTS_PATH_OUTPUT,
)
211 changes: 211 additions & 0 deletions tests/test_batched_launcher_and_cancel_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
"""Batched launcher registration and the cancel script's runScript.

Both run the real source functions against a real tracker: the launcher params
are the contract with the detached supervisor's batched parent, and the cancel
script decides admin versus owner from the OMERO session.
"""
import ast
import logging
import os
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock
from uuid import UUID

import pytest

ROOT = Path(os.environ.get('BIOMERO_SCRIPTS_ROOT', Path(__file__).parents[1]))
BATCHED = ROOT / '__workflows' / 'SLURM_Run_Workflow_Batched.py'
CANCEL = ROOT / 'admin' / 'SLURM_Cancel_Workflow.py'


def core_unavailable(reason):
if os.environ.get('CI'):
pytest.fail(f'{reason}: add the released biomero to '
f'tests/requirements.txt', pytrace=False)
pytest.skip(reason, allow_module_level=True)


try:
from biomero import constants
from biomero.database import EngineManager
from biomero.eventsourcing import WorkflowTracker, is_launcher_task
except ImportError:
core_unavailable('biomero core is not installed')
if not hasattr(WorkflowTracker, 'add_launcher_task'):
core_unavailable('Installed biomero lacks the detached mechanisms')


def source_nodes(path, names):
tree = ast.parse(path.read_text(encoding='utf-8'))
found = []
for node in tree.body:
if isinstance(node, ast.FunctionDef) and node.name in names:
found.append(node)
elif isinstance(node, ast.Assign) and any(
isinstance(t, ast.Name) and t.id in names
for t in node.targets):
found.append(node)
return found


def run_source(path, nodes, **namespace):
exec(compile(ast.Module(body=nodes, type_ignores=[]), str(path), 'exec'),
namespace)
return namespace


@pytest.fixture(autouse=True)
def tracking_database(tmp_path):
os.environ['PERSISTENCE_MODULE'] = 'eventsourcing_sqlalchemy'
os.environ['SQLALCHEMY_URL'] = f"sqlite:///{(tmp_path / 'wf.db').as_posix()}"
EngineManager.create_scoped_session()
yield
del os.environ['PERSISTENCE_MODULE']
del os.environ['SQLALCHEMY_URL']
EngineManager.close_engine()


# ---------------------- batched launcher registration ---------------------- #

class RType:
"""Stand-in for an OMERO rtype: unwrap() gives .val."""

def __init__(self, val):
self.val = val


def batched_registration():
# Skip only when the source has no detached batched pipeline at all; a
# source that registers launcher tasks must keep this function.
if not BATCHED.exists() or 'add_launcher_task' not in BATCHED.read_text(
encoding='utf-8'):
pytest.skip('Source has no detached batched launcher')
nodes = source_nodes(BATCHED, {'register_detached_batches', 'VERSION'})
assert any(isinstance(n, ast.FunctionDef) for n in nodes), (
'Batched script registers launcher tasks but lacks '
'register_detached_batches')
return run_source(
BATCHED, nodes, constants=constants,
logger=logging.getLogger('batched'),
unwrap=lambda value: getattr(value, 'val', value))


def test_batched_launcher_records_the_contract_the_supervisor_reads():
tracker = WorkflowTracker()
workflow_id = tracker.initiate_workflow('wf', '', user=1, group=2)
names = batched_registration()
client = SimpleNamespace(getInputs=lambda unwrap: {
'IDs': [1, 2, 3], 'Data_Type': 'Image', 'Batch_Size': 2})
inputs = {'IDs': RType([1, 2, 3]), 'Data_Type': RType('Image'),
'Duplicate': RType(False)}

task_id = names['register_detached_batches'](
client, SimpleNamespace(workflowTracker=tracker), workflow_id,
['analysis'], [[1, 2], [3]], 2, 'Image', inputs,
{'New Dataset': True}, 7, [1, 2, 3])

# read back through a fresh tracker, as the supervisor does
task = WorkflowTracker().repository.get(task_id)
assert task.task_name == constants.RUN_WF_BATCHED_SCRIPT
assert task.input_data == [1, 2, 3]
assert is_launcher_task(task)
assert task.params == {
'IDs': [1, 2, 3], 'Data_Type': 'Image', 'Batch_Size': 2,
'workflows': ['analysis'], 'batches': [[1, 2], [3]], 'batch_size': 2,
'effective_type': 'Image',
'base_inputs': {'IDs': [1, 2, 3], 'Data_Type': 'Image',
'Duplicate': False},
'selected_output': {'New Dataset': True}, 'group': 7,
constants.LAUNCHER_MARKER: names['VERSION']}
assert WorkflowTracker().find_launcher_task(workflow_id).id == task_id


def test_batched_launcher_params_include_every_key_the_processor_reads():
"""The processor's batched parent reads exactly these keys."""
# Hand-written pin of what NL-BIOMERO's batched parent reads; update it
# together with the processor when that set of keys changes.
processor_keys = {'batches', 'base_inputs', 'effective_type',
'selected_output', 'group', 'workflows'}
tracker = WorkflowTracker()
workflow_id = tracker.initiate_workflow('wf', '', user=1, group=2)
task_id = batched_registration()['register_detached_batches'](
SimpleNamespace(getInputs=lambda unwrap: {}),
SimpleNamespace(workflowTracker=tracker), workflow_id, [], [[1]], 1,
'Image', {}, {}, 1, [1])
assert processor_keys <= set(tracker.repository.get(task_id).params)


# ----------------------------- cancel script ----------------------------- #

def cancel_runscript(inputs, user_id, is_admin):
if not CANCEL.exists():
pytest.skip('Source has no cancel script')
nodes = source_nodes(CANCEL, {
'request_cancel', 'runScript', '_WORKFLOW_ID', '_REASON',
'_DEFAULT_REASON', '_MAX_REASON_LENGTH', 'VERSION'})
# The script file exists, so runScript is part of its contract.
assert any(isinstance(n, ast.FunctionDef) and n.name == 'runScript'
for n in nodes), 'Cancel script lacks runScript'
outputs = {}
client = SimpleNamespace(
getInputs=lambda unwrap=False: dict(inputs),
setOutput=lambda key, value: outputs.__setitem__(key, value),
closeSession=Mock())
connection = SimpleNamespace(
getUserId=lambda: user_id,
getUser=lambda: SimpleNamespace(isAdmin=lambda: is_admin))
namespace = run_source(
CANCEL, nodes,
logger=logging.getLogger('cancel-script'),
UUID=UUID, rstring=lambda value: value,
omscripts=SimpleNamespace(client=lambda *a, **k: client,
String=lambda *a, **k: None),
omero=SimpleNamespace(constants=SimpleNamespace(
namespaces=SimpleNamespace(NSDYNAMIC='dynamic'))),
BlitzGateway=lambda client_obj: connection,
WorkflowTracker=WorkflowTracker)
namespace['runScript']()
client.closeSession.assert_called_once()
return outputs['Message']


@pytest.mark.parametrize('user_id, is_admin', [(1, True), (7, False)],
ids=['administrator', 'owner'])
def test_cancel_script_runscript_lets_admin_and_owner_cancel(user_id,
is_admin):
tracker = WorkflowTracker()
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)

message = cancel_runscript(
{'Workflow_ID': f' {workflow_id} ', 'Reason': 'because'}, user_id,
is_admin)

assert 'Cancel requested' in message
cold = WorkflowTracker()
assert cold.is_workflow_cancel_requested(workflow_id)
assert cold.repository.get(workflow_id).cancel_reason == 'because'


def test_cancel_script_runscript_refuses_another_user():
tracker = WorkflowTracker()
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)

message = cancel_runscript(
{'Workflow_ID': str(workflow_id), 'Reason': 'because'}, 8, False)

assert message.startswith('Error:') and 'belongs to another user' in message
assert not WorkflowTracker().is_workflow_cancel_requested(workflow_id)


def test_cancel_script_runscript_reports_a_bad_workflow_id():
message = cancel_runscript({'Workflow_ID': 'not-a-uuid'}, 1, True)
assert message.startswith('Error:')


def test_cancel_script_runscript_uses_the_default_reason():
tracker = WorkflowTracker()
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
cancel_runscript({'Workflow_ID': str(workflow_id)}, 7, False)
assert WorkflowTracker().repository.get(
workflow_id).cancel_reason == 'Cancelled by user'
95 changes: 95 additions & 0 deletions tests/test_cancel_workflow_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""The admin script that requests a cancel for a workflow (real tracker)."""
import ast
import logging
import os
from pathlib import Path

import pytest

ROOT = Path(os.environ.get('BIOMERO_SCRIPTS_ROOT', Path(__file__).parents[1]))
SOURCE = ROOT / 'admin' / 'SLURM_Cancel_Workflow.py'
if not SOURCE.exists():
pytest.skip('Source has no cancel script', allow_module_level=True)
TREE = ast.parse(SOURCE.read_text(encoding='utf-8'))
FUNCTION = [n for n in TREE.body
if isinstance(n, ast.FunctionDef) and n.name == 'request_cancel']
CONSTANTS = [n for n in TREE.body if isinstance(n, ast.Assign)
and n.targets[0].id in ('_MAX_REASON_LENGTH',)]

try:
from biomero.database import EngineManager
from biomero.eventsourcing import WorkflowTracker
except ImportError:
if os.environ.get('CI'):
pytest.fail('biomero must be installed in CI', pytrace=False)
pytest.skip('biomero core is not installed', allow_module_level=True)
if not hasattr(WorkflowTracker, 'is_workflow_finished'):
if os.environ.get('CI'):
pytest.fail('biomero lacks is_workflow_finished', pytrace=False)
pytest.skip('Installed biomero is too old', allow_module_level=True)


@pytest.fixture
def tracker(tmp_path):
os.environ['PERSISTENCE_MODULE'] = 'eventsourcing_sqlalchemy'
os.environ['SQLALCHEMY_URL'] = f"sqlite:///{(tmp_path / 'wf.db').as_posix()}"
EngineManager.create_scoped_session()
yield WorkflowTracker()
del os.environ['PERSISTENCE_MODULE']
del os.environ['SQLALCHEMY_URL']
EngineManager.close_engine()


def request_cancel(*args):
namespace = dict(logger=logging.getLogger('cancel-script'))
exec(compile(ast.Module(body=CONSTANTS + FUNCTION, type_ignores=[]),
str(SOURCE), 'exec'), namespace)
return namespace['request_cancel'](*args)


def test_owner_can_cancel_their_workflow(tracker):
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
message = request_cancel(tracker, workflow_id, 7, False, 'because')
assert tracker.is_workflow_cancel_requested(workflow_id)
assert tracker.repository.get(workflow_id).cancel_reason == 'because'
assert 'Cancel requested' in message


def test_admin_can_cancel_anyones_workflow(tracker):
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
request_cancel(tracker, workflow_id, 1, True, 'because')
assert tracker.is_workflow_cancel_requested(workflow_id)


def test_other_user_cannot_cancel(tracker):
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
with pytest.raises(PermissionError, match='belongs to another user'):
request_cancel(tracker, workflow_id, 8, False, 'because')
assert not tracker.is_workflow_cancel_requested(workflow_id)


def test_second_request_changes_nothing(tracker):
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
request_cancel(tracker, workflow_id, 7, False, 'first')
message = request_cancel(tracker, workflow_id, 7, False, 'second')
assert tracker.repository.get(workflow_id).cancel_reason == 'first'
assert 'already requested' in message


@pytest.mark.parametrize('finish', ['complete_workflow', 'fail_workflow'])
def test_finished_workflow_is_not_given_a_cancel_request(tracker, finish,
caplog):
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
getattr(tracker, finish)(*((workflow_id,) if finish == 'complete_workflow'
else (workflow_id, 'broke')))
with caplog.at_level(logging.INFO, logger='cancel-script'):
message = request_cancel(tracker, workflow_id, 7, False, 'late')
assert not tracker.is_workflow_cancel_requested(workflow_id)
assert 'already finished' in message
assert any('already finished' in r.getMessage() for r in caplog.records)


def test_reason_is_bounded(tracker):
workflow_id = tracker.initiate_workflow('wf', '', user=7, group=1)
request_cancel(tracker, workflow_id, 7, False, 'x' * 5000)
assert len(tracker.repository.get(workflow_id).cancel_reason) == 200
Loading