|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from jupyter_scheduler.models import DescribeJob |
| 8 | +from jupyter_scheduler.orm import Job |
| 9 | +from jupyter_scheduler.python_executor import PythonScriptExecutionManager |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def python_script_staging_dir(jp_scheduler_staging_dir) -> Path: |
| 14 | + """Create a staging directory with a simple Python script.""" |
| 15 | + job_staging_dir = jp_scheduler_staging_dir / "job-py-1" |
| 16 | + job_staging_dir.mkdir() |
| 17 | + return job_staging_dir |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def simple_script(python_script_staging_dir) -> Path: |
| 22 | + """Create a simple print script.""" |
| 23 | + script_path = python_script_staging_dir / "test_script.py" |
| 24 | + script_path.write_text('print("Hello from Python script!")\n') |
| 25 | + return script_path |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def script_with_params(python_script_staging_dir) -> Path: |
| 30 | + """Create a script that reads JUPYTER_PARAM_* env vars.""" |
| 31 | + script_path = python_script_staging_dir / "param_script.py" |
| 32 | + script_path.write_text( |
| 33 | + """import os |
| 34 | +learning_rate = os.environ.get('JUPYTER_PARAM_learning_rate', 'not_set') |
| 35 | +batch_size = os.environ.get('JUPYTER_PARAM_batch_size', 'not_set') |
| 36 | +print(f"lr={learning_rate}, batch={batch_size}") |
| 37 | +""" |
| 38 | + ) |
| 39 | + return script_path |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +def failing_script(python_script_staging_dir) -> Path: |
| 44 | + """Create a script that exits with non-zero code.""" |
| 45 | + script_path = python_script_staging_dir / "failing_script.py" |
| 46 | + script_path.write_text('import sys; print("error message", file=sys.stderr); sys.exit(1)\n') |
| 47 | + return script_path |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def script_with_side_effects(python_script_staging_dir) -> Path: |
| 52 | + """Create a script that creates output files.""" |
| 53 | + script_path = python_script_staging_dir / "side_effects_script.py" |
| 54 | + script_path.write_text( |
| 55 | + """ |
| 56 | +with open('output.txt', 'w') as f: |
| 57 | + f.write('Generated output') |
| 58 | +print("Created output.txt") |
| 59 | +""" |
| 60 | + ) |
| 61 | + return script_path |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def python_job_record(simple_script, jp_scheduler_db) -> str: |
| 66 | + """Create a job record for the Python script.""" |
| 67 | + job = Job( |
| 68 | + name="test_python_job", |
| 69 | + runtime_environment_name="default", |
| 70 | + input_filename=simple_script.name, |
| 71 | + ) |
| 72 | + jp_scheduler_db.add(job) |
| 73 | + jp_scheduler_db.commit() |
| 74 | + return job.job_id |
| 75 | + |
| 76 | + |
| 77 | +@pytest.fixture |
| 78 | +def python_job_with_params(script_with_params, jp_scheduler_db) -> str: |
| 79 | + """Create a job record with parameters.""" |
| 80 | + job = Job( |
| 81 | + name="test_python_job_with_params", |
| 82 | + runtime_environment_name="default", |
| 83 | + input_filename=script_with_params.name, |
| 84 | + parameters={"learning_rate": "0.01", "batch_size": "32"}, |
| 85 | + ) |
| 86 | + jp_scheduler_db.add(job) |
| 87 | + jp_scheduler_db.commit() |
| 88 | + return job.job_id |
| 89 | + |
| 90 | + |
| 91 | +class TestPythonScriptExecutionManager: |
| 92 | + def test_execute_simple_script( |
| 93 | + self, |
| 94 | + python_job_record, |
| 95 | + simple_script, |
| 96 | + jp_scheduler_root_dir, |
| 97 | + jp_scheduler_db_url, |
| 98 | + jp_scheduler_db, |
| 99 | + ): |
| 100 | + """Execute a simple print script and verify stdout is captured.""" |
| 101 | + manager = PythonScriptExecutionManager( |
| 102 | + job_id=python_job_record, |
| 103 | + root_dir=str(jp_scheduler_root_dir), |
| 104 | + db_url=jp_scheduler_db_url, |
| 105 | + staging_paths={"input": str(simple_script)}, |
| 106 | + ) |
| 107 | + |
| 108 | + # Execute should not raise |
| 109 | + manager.execute() |
| 110 | + |
| 111 | + # Check stdout.log was created |
| 112 | + stdout_path = simple_script.parent / "stdout.log" |
| 113 | + assert stdout_path.exists() |
| 114 | + assert "Hello from Python script!" in stdout_path.read_text() |
| 115 | + |
| 116 | + def test_execute_with_parameters( |
| 117 | + self, |
| 118 | + python_job_with_params, |
| 119 | + script_with_params, |
| 120 | + jp_scheduler_root_dir, |
| 121 | + jp_scheduler_db_url, |
| 122 | + jp_scheduler_db, |
| 123 | + ): |
| 124 | + """Parameters are passed as JUPYTER_PARAM_* env vars.""" |
| 125 | + manager = PythonScriptExecutionManager( |
| 126 | + job_id=python_job_with_params, |
| 127 | + root_dir=str(jp_scheduler_root_dir), |
| 128 | + db_url=jp_scheduler_db_url, |
| 129 | + staging_paths={"input": str(script_with_params)}, |
| 130 | + ) |
| 131 | + |
| 132 | + manager.execute() |
| 133 | + |
| 134 | + stdout_path = script_with_params.parent / "stdout.log" |
| 135 | + content = stdout_path.read_text() |
| 136 | + assert "lr=0.01" in content |
| 137 | + assert "batch=32" in content |
| 138 | + |
| 139 | + def test_execute_script_failure( |
| 140 | + self, |
| 141 | + failing_script, |
| 142 | + jp_scheduler_root_dir, |
| 143 | + jp_scheduler_db_url, |
| 144 | + jp_scheduler_db, |
| 145 | + ): |
| 146 | + """Non-zero exit code raises RuntimeError.""" |
| 147 | + job = Job( |
| 148 | + name="test_failing_script", |
| 149 | + runtime_environment_name="default", |
| 150 | + input_filename=failing_script.name, |
| 151 | + ) |
| 152 | + jp_scheduler_db.add(job) |
| 153 | + jp_scheduler_db.commit() |
| 154 | + |
| 155 | + manager = PythonScriptExecutionManager( |
| 156 | + job_id=job.job_id, |
| 157 | + root_dir=str(jp_scheduler_root_dir), |
| 158 | + db_url=jp_scheduler_db_url, |
| 159 | + staging_paths={"input": str(failing_script)}, |
| 160 | + ) |
| 161 | + |
| 162 | + with pytest.raises(RuntimeError) as exc_info: |
| 163 | + manager.execute() |
| 164 | + |
| 165 | + assert "exited with code 1" in str(exc_info.value) |
| 166 | + assert "error message" in str(exc_info.value) |
| 167 | + |
| 168 | + def test_stdout_stderr_captured( |
| 169 | + self, |
| 170 | + failing_script, |
| 171 | + jp_scheduler_root_dir, |
| 172 | + jp_scheduler_db_url, |
| 173 | + jp_scheduler_db, |
| 174 | + ): |
| 175 | + """Both stdout and stderr are written to files even on failure.""" |
| 176 | + job = Job( |
| 177 | + name="test_stderr_capture", |
| 178 | + runtime_environment_name="default", |
| 179 | + input_filename=failing_script.name, |
| 180 | + ) |
| 181 | + jp_scheduler_db.add(job) |
| 182 | + jp_scheduler_db.commit() |
| 183 | + |
| 184 | + manager = PythonScriptExecutionManager( |
| 185 | + job_id=job.job_id, |
| 186 | + root_dir=str(jp_scheduler_root_dir), |
| 187 | + db_url=jp_scheduler_db_url, |
| 188 | + staging_paths={"input": str(failing_script)}, |
| 189 | + ) |
| 190 | + |
| 191 | + with pytest.raises(RuntimeError): |
| 192 | + manager.execute() |
| 193 | + |
| 194 | + stderr_path = failing_script.parent / "stderr.log" |
| 195 | + assert stderr_path.exists() |
| 196 | + assert "error message" in stderr_path.read_text() |
| 197 | + |
| 198 | + def test_side_effects_captured( |
| 199 | + self, |
| 200 | + script_with_side_effects, |
| 201 | + jp_scheduler_root_dir, |
| 202 | + jp_scheduler_db_url, |
| 203 | + jp_scheduler_db, |
| 204 | + ): |
| 205 | + """Files created by the script are recorded in packaged_files.""" |
| 206 | + job = Job( |
| 207 | + name="test_side_effects", |
| 208 | + runtime_environment_name="default", |
| 209 | + input_filename=script_with_side_effects.name, |
| 210 | + ) |
| 211 | + jp_scheduler_db.add(job) |
| 212 | + jp_scheduler_db.commit() |
| 213 | + |
| 214 | + manager = PythonScriptExecutionManager( |
| 215 | + job_id=job.job_id, |
| 216 | + root_dir=str(jp_scheduler_root_dir), |
| 217 | + db_url=jp_scheduler_db_url, |
| 218 | + staging_paths={"input": str(script_with_side_effects)}, |
| 219 | + ) |
| 220 | + |
| 221 | + manager.execute() |
| 222 | + |
| 223 | + # Refresh job from DB |
| 224 | + jp_scheduler_db.expire_all() |
| 225 | + job = jp_scheduler_db.query(Job).filter(Job.job_id == job.job_id).one() |
| 226 | + |
| 227 | + # output.txt should be in packaged_files |
| 228 | + assert "output.txt" in job.packaged_files |
| 229 | + |
| 230 | + def test_supported_features(self): |
| 231 | + """Verify supported features match expected values.""" |
| 232 | + from jupyter_scheduler.models import JobFeature |
| 233 | + |
| 234 | + features = PythonScriptExecutionManager.supported_features() |
| 235 | + |
| 236 | + assert features[JobFeature.job_name] is True |
| 237 | + assert features[JobFeature.output_formats] is False |
| 238 | + assert features[JobFeature.stop_job] is True |
| 239 | + assert features[JobFeature.delete_job] is True |
0 commit comments