|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import AsyncGenerator |
| 4 | +from datetime import datetime, timezone |
| 5 | + |
| 6 | +import pytest |
| 7 | +import sqlalchemy |
| 8 | + |
| 9 | +from diracx.core.models import JobMetaData |
| 10 | +from diracx.db.os.job_parameters import JobParametersDB as RealJobParametersDB |
| 11 | +from diracx.db.sql.job.db import JobDB |
| 12 | +from diracx.logic.jobs.status import set_job_parameters_or_attributes |
| 13 | +from diracx.testing.mock_osdb import MockOSDBMixin |
| 14 | +from diracx.testing.time import mock_sqlite_time |
| 15 | + |
| 16 | + |
| 17 | +# Reuse the generic MockOSDBMixin to build a mock JobParameters DB implementation |
| 18 | +class _MockJobParametersDB(MockOSDBMixin, RealJobParametersDB): |
| 19 | + def __init__(self): # type: ignore[override] |
| 20 | + super().__init__({"sqlalchemy_dsn": "sqlite+aiosqlite:///:memory:"}) |
| 21 | + |
| 22 | + def upsert(self, vo, doc_id, document): |
| 23 | + """Override to add JobID to the document.""" |
| 24 | + # Add JobID to the document, which is required by the base class |
| 25 | + document["JobID"] = doc_id |
| 26 | + return super().upsert(vo, doc_id, document) |
| 27 | + |
| 28 | + |
| 29 | +# -------------------------------------------------------------------------------------- |
| 30 | +# Test setup fixtures |
| 31 | +# -------------------------------------------------------------------------------------- |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +async def job_db() -> AsyncGenerator[JobDB, None]: |
| 36 | + """Create a fake sandbox metadata database.""" |
| 37 | + db = JobDB(db_url="sqlite+aiosqlite:///:memory:") |
| 38 | + async with db.engine_context(): |
| 39 | + sqlalchemy.event.listen(db.engine.sync_engine, "connect", mock_sqlite_time) |
| 40 | + |
| 41 | + async with db.engine.begin() as conn: |
| 42 | + await conn.run_sync(db.metadata.create_all) |
| 43 | + |
| 44 | + yield db |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +async def job_parameters_db() -> AsyncGenerator[_MockJobParametersDB, None]: |
| 49 | + db = _MockJobParametersDB() |
| 50 | + # Need engine_context entered before creating tables |
| 51 | + async with db.client_context(): |
| 52 | + await db.create_index_template() |
| 53 | + yield db |
| 54 | + |
| 55 | + |
| 56 | +TEST_JDL = """ |
| 57 | + Arguments = "jobDescription.xml -o LogLevel=INFO"; |
| 58 | + Executable = "dirac-jobexec"; |
| 59 | + JobGroup = jobGroup; |
| 60 | + JobName = jobName; |
| 61 | + JobType = User; |
| 62 | + LogLevel = INFO; |
| 63 | + OutputSandbox = |
| 64 | + { |
| 65 | + Script1_CodeOutput.log, |
| 66 | + std.err, |
| 67 | + std.out |
| 68 | + }; |
| 69 | + Priority = 1; |
| 70 | + Site = ANY; |
| 71 | + StdError = std.err; |
| 72 | + StdOutput = std.out; |
| 73 | +""" |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture |
| 77 | +async def valid_job_id(job_db: JobDB) -> int: |
| 78 | + """Create a minimal job record and return its JobID.""" |
| 79 | + async with job_db: |
| 80 | + job_id = await job_db.create_job("") # original JDL unused in these tests |
| 81 | + # Insert initial attributes (mimic job submission) |
| 82 | + await job_db.insert_job_attributes( |
| 83 | + { |
| 84 | + job_id: { |
| 85 | + "Status": "Received", |
| 86 | + "MinorStatus": "Job accepted", |
| 87 | + "ApplicationStatus": "Unknown", |
| 88 | + "VO": "lhcb", |
| 89 | + "Owner": "tester", |
| 90 | + "OwnerGroup": "lhcb_user", |
| 91 | + "VerifiedFlag": True, |
| 92 | + "JobType": "User", |
| 93 | + } |
| 94 | + } |
| 95 | + ) |
| 96 | + return job_id |
| 97 | + |
| 98 | + |
| 99 | +# -------------------------------------------------------------------------------------- |
| 100 | +# Tests |
| 101 | +# -------------------------------------------------------------------------------------- |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_patch_metadata_updates_attributes_and_parameters( |
| 106 | + job_db: JobDB, job_parameters_db: _MockJobParametersDB, valid_job_id: int |
| 107 | +): |
| 108 | + """Patch metadata mixing: |
| 109 | + - Attribute only (UserPriority) |
| 110 | + - Attribute + parameter (JobType) |
| 111 | + - Parameter only (CPUNormalizationFactor) |
| 112 | + - Attribute (HeartBeatTime) |
| 113 | + - Non identified Metadata (does_not_exist) |
| 114 | + and verify correct persistence in the two backends. |
| 115 | + """ |
| 116 | + hbt = datetime.now(timezone.utc) |
| 117 | + |
| 118 | + updates = { |
| 119 | + valid_job_id: JobMetaData( |
| 120 | + user_priority=2, # alias UserPriority |
| 121 | + job_type="VerySpecialIndeed", # alias JobType (attr + param) |
| 122 | + cpu_normalization_factor=10, # alias CPUNormalizationFactor (param only) |
| 123 | + heart_beat_time=hbt, # alias HeartBeatTime (attribute) |
| 124 | + does_not_exist="unknown", # Does not exist shoult be treated as a job parameter |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + # Act |
| 129 | + async with job_db: # ensure open connection for updates |
| 130 | + await set_job_parameters_or_attributes(updates, job_db, job_parameters_db) |
| 131 | + |
| 132 | + # Assert job attributes (SQL) |
| 133 | + async with job_db: |
| 134 | + _, rows = await job_db.search( |
| 135 | + parameters=[ |
| 136 | + "JobID", |
| 137 | + "UserPriority", |
| 138 | + "JobType", |
| 139 | + "HeartBeatTime", |
| 140 | + "Status", |
| 141 | + "MinorStatus", |
| 142 | + "ApplicationStatus", |
| 143 | + ], |
| 144 | + search=[{"parameter": "JobID", "operator": "eq", "value": valid_job_id}], |
| 145 | + sorts=[], |
| 146 | + ) |
| 147 | + assert len(rows) == 1 |
| 148 | + row = rows[0] |
| 149 | + assert int(row["JobID"]) == valid_job_id |
| 150 | + assert row["UserPriority"] == 2 |
| 151 | + assert row["JobType"] == "VerySpecialIndeed" |
| 152 | + # HeartBeatTime stored as ISO string (without tz) in DB helper; just ensure present |
| 153 | + assert row["HeartBeatTime"] is not None |
| 154 | + |
| 155 | + # Assert job parameters (mocked OS / sqlite) |
| 156 | + params_rows = await job_parameters_db.search( |
| 157 | + parameters=["JobType", "CPUNormalizationFactor", "does_not_exist"], |
| 158 | + search=[{"parameter": "JobID", "operator": "eq", "value": valid_job_id}], |
| 159 | + sorts=[], |
| 160 | + ) |
| 161 | + prow = params_rows[0] |
| 162 | + assert prow["JobType"] == "VerySpecialIndeed" |
| 163 | + assert prow["CPUNormalizationFactor"] == 10 |
| 164 | + assert prow["does_not_exist"] == "unknown" |
0 commit comments