|
| 1 | +import asyncio |
| 2 | +import pytest |
| 3 | +import subprocess |
| 4 | +from argx import Namespace |
| 5 | +from unittest.mock import patch, MagicMock, AsyncMock |
| 6 | +from xqute import __version__ |
| 7 | +from xqute.__main__ import ( |
| 8 | + _update_status, |
| 9 | + check_version_and_plugins, |
| 10 | + launch_job, |
| 11 | + main, |
| 12 | + SchedulerXquteInconsistencyWarning, |
| 13 | +) |
| 14 | +from xqute.defaults import JobStatus |
| 15 | + |
| 16 | +# FILE: xqute/test___main__.py |
| 17 | + |
| 18 | + |
| 19 | +def test_update_status(tmp_path): |
| 20 | + metadir = tmp_path / "metadir" |
| 21 | + metadir.mkdir() |
| 22 | + status = JobStatus.RUNNING |
| 23 | + _update_status(str(metadir), status) |
| 24 | + assert (metadir / "job.status").read_text() == str(status) |
| 25 | + |
| 26 | + |
| 27 | +def test_check_version_and_plugins(): |
| 28 | + args = MagicMock() |
| 29 | + args.version = "1.0.0" |
| 30 | + args.plugin = ["plugin1:1.0.0", "plugin2:2.0.0"] |
| 31 | + plugin1 = Namespace(name="plugin1", version="1.0.0") |
| 32 | + plugin2 = Namespace(name="plugin2", version="2.0.0") |
| 33 | + |
| 34 | + with patch("xqute.__main__.__version__", "1.0.0"): |
| 35 | + with patch( |
| 36 | + "xqute.__main__.plugin.get_enabled_plugins", |
| 37 | + return_value={"plugin1": "1.0.0", "plugin2": "2.0.0"}, |
| 38 | + ): |
| 39 | + check_version_and_plugins(args) |
| 40 | + |
| 41 | + with patch("xqute.__main__.__version__", "1.0.0"): |
| 42 | + with patch( |
| 43 | + "xqute.__main__.plugin.get_enabled_plugins", |
| 44 | + return_value={"plugin1": "1.0.0"}, |
| 45 | + ): |
| 46 | + with pytest.warns(SchedulerXquteInconsistencyWarning): |
| 47 | + check_version_and_plugins(args) |
| 48 | + |
| 49 | + with patch("xqute.__main__.__version__", "1.0.0"): |
| 50 | + with patch( |
| 51 | + "xqute.__main__.plugin.get_enabled_plugins", |
| 52 | + return_value={"plugin1": plugin1, "plugin2": plugin2}, |
| 53 | + ): |
| 54 | + with pytest.warns(SchedulerXquteInconsistencyWarning): |
| 55 | + check_version_and_plugins(args) |
| 56 | + |
| 57 | + |
| 58 | +def test_check_version_and_plugins_version_mismatch(): |
| 59 | + args = MagicMock() |
| 60 | + args.version = "1.0.0" |
| 61 | + |
| 62 | + with patch("xqute.__main__.__version__", "2.0.0"): |
| 63 | + with pytest.warns( |
| 64 | + SchedulerXquteInconsistencyWarning, |
| 65 | + match="The xqute on the scheduler system is 2.0.0", |
| 66 | + ): |
| 67 | + check_version_and_plugins(args) |
| 68 | + |
| 69 | + args.plugin = ["plugin1:1.0.0"] |
| 70 | + with patch("xqute.__main__.__version__", "1.0.0"): |
| 71 | + with patch( |
| 72 | + "xqute.__main__.plugin.get_enabled_plugins", |
| 73 | + return_value={"plugin1": "2.0.0"}, |
| 74 | + ): |
| 75 | + with pytest.warns( |
| 76 | + SchedulerXquteInconsistencyWarning, |
| 77 | + match="Xqute plugin version mismatch: plugin1 1.0.0 != 2.0.0", |
| 78 | + ): |
| 79 | + check_version_and_plugins(args) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_launch_job(tmp_path): |
| 84 | + args = MagicMock() |
| 85 | + args.metadir = str(tmp_path / "metadir") |
| 86 | + args.cmd = ["echo", "hello"] |
| 87 | + (tmp_path / "metadir").mkdir() |
| 88 | + |
| 89 | + with patch( |
| 90 | + "xqute.__main__.plugin.hooks.on_jobsched_started", new_callable=AsyncMock |
| 91 | + ): |
| 92 | + with patch( |
| 93 | + "xqute.__main__.plugin.hooks.on_jobsched_ended", new_callable=AsyncMock |
| 94 | + ): |
| 95 | + await launch_job(args) |
| 96 | + |
| 97 | + assert (tmp_path / "metadir" / "job.stdout").read_text() == "hello\n" |
| 98 | + assert (tmp_path / "metadir" / "job.rc").read_text() == "0" |
| 99 | + assert (tmp_path / "metadir" / "job.status").read_text() == str(JobStatus.FINISHED) |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.asyncio |
| 103 | +async def test_launch_job_failed(tmp_path): |
| 104 | + metadir = tmp_path / "metadir" |
| 105 | + metadir.mkdir() |
| 106 | + args = Namespace( |
| 107 | + metadir=str(metadir), |
| 108 | + cmd=["bash", "-c", "exit 1"], |
| 109 | + scheduler="local", |
| 110 | + version=__version__, |
| 111 | + plugin=[], |
| 112 | + ) |
| 113 | + |
| 114 | + with pytest.raises(SystemExit): |
| 115 | + await launch_job(args) |
| 116 | + assert (metadir / "job.rc").read_text() != "0" |
| 117 | + |
| 118 | + args = Namespace( |
| 119 | + metadir=str(metadir), |
| 120 | + cmd=["__NoNeXisT__"], |
| 121 | + scheduler="local", |
| 122 | + version=__version__, |
| 123 | + plugin=[], |
| 124 | + ) |
| 125 | + |
| 126 | + with pytest.raises(SystemExit): |
| 127 | + await launch_job(args) |
| 128 | + assert (metadir / "job.rc").read_text() != "0" |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.asyncio |
| 132 | +async def test_launch_job_cancelled(tmp_path): |
| 133 | + args = MagicMock() |
| 134 | + args.metadir = str(tmp_path / "metadir") |
| 135 | + args.cmd = ["echo", "hello"] |
| 136 | + (tmp_path / "metadir").mkdir() |
| 137 | + |
| 138 | + with patch( |
| 139 | + "xqute.__main__.plugin.hooks.on_jobsched_started", new_callable=AsyncMock |
| 140 | + ): |
| 141 | + with patch( |
| 142 | + "xqute.__main__.plugin.hooks.on_jobsched_ended", new_callable=AsyncMock |
| 143 | + ): |
| 144 | + with patch( |
| 145 | + "asyncio.create_subprocess_exec", side_effect=asyncio.CancelledError |
| 146 | + ): |
| 147 | + with pytest.raises(asyncio.CancelledError): |
| 148 | + await launch_job(args) |
| 149 | + |
| 150 | + assert (tmp_path / "metadir" / "job.rc").read_text() == "1" |
| 151 | + assert (tmp_path / "metadir" / "job.status").read_text() == str(JobStatus.FAILED) |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.asyncio |
| 155 | +async def test_main(tmp_path): |
| 156 | + metadir = tmp_path / "metadir" |
| 157 | + metadir.mkdir() |
| 158 | + args = Namespace( |
| 159 | + metadir=str(metadir), |
| 160 | + cmd=["echo", "hello"], |
| 161 | + scheduler="local", |
| 162 | + version=__version__, |
| 163 | + plugin=[], |
| 164 | + ) |
| 165 | + |
| 166 | + await main(args) |
| 167 | + assert (metadir / "job.stdout").read_text() == "hello\n" |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.asyncio |
| 171 | +async def test_cli(tmp_path): |
| 172 | + metadir = tmp_path / "metadir" |
| 173 | + metadir.mkdir() |
| 174 | + |
| 175 | + # Use the real command line arguments |
| 176 | + p = subprocess.run( |
| 177 | + [ |
| 178 | + "python", |
| 179 | + "-m", |
| 180 | + "xqute", |
| 181 | + "++scheduler", |
| 182 | + "local", |
| 183 | + "++version", |
| 184 | + __version__, |
| 185 | + "++metadir", |
| 186 | + str(metadir), |
| 187 | + "++cmd", |
| 188 | + "echo", |
| 189 | + "hello", |
| 190 | + ] |
| 191 | + ) |
| 192 | + assert p.returncode == 0 |
| 193 | + assert (metadir / "job.stdout").read_text() == "hello\n" |
0 commit comments