|
| 1 | +import subprocess |
| 2 | +from unittest.mock import MagicMock, Mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from cloudai._core.exceptions import JobIdRetrievalError |
| 6 | +from cloudai._core.test import Test |
| 7 | +from cloudai._core.test_scenario import TestScenario |
| 8 | +from cloudai._core.test_template import TestTemplate |
| 9 | +from cloudai.runner.slurm.slurm_runner import SlurmRunner |
| 10 | +from cloudai.schema.system import SlurmSystem |
| 11 | +from cloudai.schema.system.slurm import SlurmNode, SlurmNodeState |
| 12 | +from cloudai.util import CommandShell |
| 13 | + |
| 14 | + |
| 15 | +class MockCommandShell(CommandShell): |
| 16 | + def execute(self, command): |
| 17 | + mock_popen = Mock(spec=subprocess.Popen) |
| 18 | + mock_popen.communicate.return_value = ( |
| 19 | + "", |
| 20 | + "sbatch: error: Batch job submission failed: Requested node configuration is not available", |
| 21 | + ) |
| 22 | + return mock_popen |
| 23 | + |
| 24 | + |
| 25 | +class MockTest(Test): |
| 26 | + def __init__(self, section_name): |
| 27 | + self.name = "Mock Test" |
| 28 | + self.description = "A mock test description" |
| 29 | + self.test_template = MagicMock(spec=TestTemplate) |
| 30 | + self.env_vars = {} |
| 31 | + self.cmd_args = {} |
| 32 | + self.extra_env_vars = {} |
| 33 | + self.extra_cmd_args = "" |
| 34 | + self.section_name = "Tests.1" |
| 35 | + self.current_iteration = 0 |
| 36 | + |
| 37 | + def gen_exec_command(self, output_path): |
| 38 | + return "sbatch mock_script.sh" |
| 39 | + |
| 40 | + def get_job_id(self, stdout, stderr): |
| 41 | + return None |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def slurm_system(tmpdir): |
| 46 | + nodes = [ |
| 47 | + SlurmNode(name="nodeA001", partition="main", state=SlurmNodeState.UNKNOWN_STATE), |
| 48 | + SlurmNode(name="nodeB001", partition="main", state=SlurmNodeState.UNKNOWN_STATE), |
| 49 | + ] |
| 50 | + system = SlurmSystem( |
| 51 | + name="test_system", |
| 52 | + install_path=tmpdir, |
| 53 | + output_path=tmpdir, |
| 54 | + default_partition="main", |
| 55 | + partitions={"main": nodes}, |
| 56 | + ) |
| 57 | + return system |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def slurm_runner(slurm_system): |
| 62 | + test_scenario = TestScenario(name="Test Scenario", tests=[MockTest(section_name="Mock Test")]) |
| 63 | + runner = SlurmRunner(mode="run", system=slurm_system, test_scenario=test_scenario) |
| 64 | + runner.cmd_shell = MockCommandShell() |
| 65 | + return runner |
| 66 | + |
| 67 | + |
| 68 | +def test_job_id_retrieval_error(slurm_runner): |
| 69 | + test = slurm_runner.test_scenario.tests[0] |
| 70 | + with pytest.raises(JobIdRetrievalError) as excinfo: |
| 71 | + slurm_runner._submit_test(test) |
| 72 | + assert "Failed to retrieve job ID from command output." in str(excinfo.value) |
| 73 | + assert "sbatch: error: Batch job submission failed: Requested node configuration is not available" in str( |
| 74 | + excinfo.value |
| 75 | + ) |
0 commit comments