|
| 1 | +import secrets |
1 | 2 | import uuid |
2 | 3 | from pathlib import Path |
| 4 | +from typing import List, Callable, Dict |
3 | 5 |
|
4 | | -from psij import SubmitException, Job, JobSpec, JobState |
5 | | -from tempfile import TemporaryDirectory |
| 6 | +import pytest |
| 7 | + |
| 8 | +from psij import SubmitException, Job, JobSpec, JobState, JobExecutor, JobAttributes, ResourceSpecV1 |
| 9 | +from tempfile import TemporaryDirectory, TemporaryFile |
6 | 10 |
|
7 | 11 | from executor_test_params import ExecutorTestParams |
8 | 12 | from _test_tools import _get_executor_instance, _get_timeout, assert_completed, _make_test_dir |
| 13 | +from psij.executors.batch.batch_scheduler_executor import BatchSchedulerExecutor |
9 | 14 |
|
10 | 15 |
|
11 | 16 | def test_simple_job(execparams: ExecutorTestParams) -> None: |
@@ -161,3 +166,58 @@ def test_list(execparams: ExecutorTestParams) -> None: |
161 | 166 | assert job.native_id is not None |
162 | 167 | ids = ex.list() |
163 | 168 | assert job.native_id in ids |
| 169 | + |
| 170 | + |
| 171 | +def _get_batch_executors() -> List[str]: |
| 172 | + r = [] |
| 173 | + for name in JobExecutor.get_executor_names(): |
| 174 | + try: |
| 175 | + ex = JobExecutor.get_instance(name) |
| 176 | + if isinstance(ex, BatchSchedulerExecutor): |
| 177 | + r.append(name) |
| 178 | + except Exception: |
| 179 | + pass |
| 180 | + return r |
| 181 | + |
| 182 | + |
| 183 | +def _check_str_attrs(ex: BatchSchedulerExecutor, job: Job, names: List[str], |
| 184 | + l: Callable[[str, str], None]) -> None: |
| 185 | + for name in names: |
| 186 | + tok = secrets.token_hex() |
| 187 | + l(name, tok) |
| 188 | + with TemporaryFile(mode='w+') as f: |
| 189 | + ex.generate_submit_script(job, ex._create_script_context(job), f) |
| 190 | + f.seek(0) |
| 191 | + contents = f.read() |
| 192 | + if contents.find(tok) == -1: |
| 193 | + print('Failed to find "%s" in:\n%s' % (tok, contents)) |
| 194 | + pytest.fail('Script generation failed for %s' % name) |
| 195 | + |
| 196 | + |
| 197 | +_PREFIX_TR = {'pbspro': 'pbs'} |
| 198 | +def _get_attr_prefix(exec_name: str) -> str: |
| 199 | + if exec_name in _PREFIX_TR: |
| 200 | + return _PREFIX_TR[exec_name] |
| 201 | + else: |
| 202 | + return exec_name |
| 203 | + |
| 204 | + |
| 205 | +@pytest.mark.parametrize('exec_name', _get_batch_executors()) |
| 206 | +def test_submit_script_generation(exec_name: str) -> None: |
| 207 | + ex = JobExecutor.get_instance(exec_name) |
| 208 | + assert isinstance(ex, BatchSchedulerExecutor) |
| 209 | + |
| 210 | + c_attrs: Dict[str, object] = {} |
| 211 | + attrs = JobAttributes(custom_attributes=c_attrs) |
| 212 | + res = ResourceSpecV1() |
| 213 | + spec = JobSpec(resources=res, attributes=attrs) |
| 214 | + spec.launcher = 'single' |
| 215 | + job = Job(spec=spec) |
| 216 | + |
| 217 | + prefix = _get_attr_prefix(exec_name) |
| 218 | + _check_str_attrs(ex, job, ['executable', 'directory'], |
| 219 | + lambda k, v: setattr(spec, k, v)) |
| 220 | + _check_str_attrs(ex, job, ['queue_name', 'project_name', 'reservation_id'], |
| 221 | + lambda k, v: setattr(attrs, k, v)) |
| 222 | + _check_str_attrs(ex, job, [prefix + '.cust_attr1', prefix + '.cust_attr2'], |
| 223 | + lambda k, v: c_attrs.__setitem__(k, v)) |
0 commit comments