|
| 1 | +import pytest |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import ansys.dyna.core.run.windows_runner as windows_runner |
| 6 | +from ansys.dyna.core.run.options import MpiOption, Precision |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def tmp_workdir(tmp_path): |
| 11 | + return tmp_path |
| 12 | + |
| 13 | + |
| 14 | +def make_runner(tmp_workdir, mpi=MpiOption.SMP, precision=Precision.SINGLE): |
| 15 | + # create dummy solver executable |
| 16 | + exe = tmp_workdir / "lsdyna.exe" |
| 17 | + exe.write_text("dummy") |
| 18 | + |
| 19 | + runner = windows_runner.WindowsRunner(executable=str(exe)) |
| 20 | + runner.mpi_option = mpi |
| 21 | + runner.precision = precision |
| 22 | + runner.input_file = "input.k" |
| 23 | + runner.working_directory = str(tmp_workdir) |
| 24 | + runner.ncpu = 4 |
| 25 | + runner.get_memory_string = MagicMock(return_value="2000m") |
| 26 | + return runner |
| 27 | + |
| 28 | + |
| 29 | +def test_find_solver_with_explicit_executable(tmp_workdir): |
| 30 | + exe = tmp_workdir / "lsdyna.exe" |
| 31 | + exe.write_text("dummy") |
| 32 | + |
| 33 | + runner = windows_runner.WindowsRunner(executable=str(exe)) |
| 34 | + assert runner.solver_location == str(tmp_workdir) |
| 35 | + assert runner.solver.endswith("lsdyna.exe\"") |
| 36 | + |
| 37 | + |
| 38 | +@patch("ansys.dyna.core.run.windows_runner._get_unified_install_base_for_version") |
| 39 | +def test_find_solver_with_version(mock_install_base, tmp_workdir): |
| 40 | + exe = tmp_workdir / "ansys" / "bin" / "winx64" / "lsdyna_sp.exe" |
| 41 | + exe.parent.mkdir(parents=True) |
| 42 | + exe.write_text("dummy") |
| 43 | + |
| 44 | + mock_install_base.return_value = (tmp_workdir, None) |
| 45 | + runner = windows_runner.WindowsRunner(version=241, precision=Precision.SINGLE) |
| 46 | + assert runner.solver.endswith("lsdyna_sp.exe\"") |
| 47 | + |
| 48 | +def test_find_solver_executable_not_found(tmp_workdir): |
| 49 | + with pytest.raises(FileNotFoundError): |
| 50 | + windows_runner.WindowsRunner(executable=str(tmp_workdir / "missing.exe")) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.parametrize( |
| 54 | + "mpi,prec,expected", |
| 55 | + [ |
| 56 | + (MpiOption.SMP, Precision.SINGLE, "lsdyna_sp.exe"), |
| 57 | + (MpiOption.SMP, Precision.DOUBLE, "lsdyna_dp.exe"), |
| 58 | + (MpiOption.MPP_INTEL_MPI, Precision.SINGLE, "lsdyna_mpp_sp_impi.exe"), |
| 59 | + (MpiOption.MPP_INTEL_MPI, Precision.DOUBLE, "lsdyna_mpp_dp_impi.exe"), |
| 60 | + (MpiOption.MPP_MS_MPI, Precision.SINGLE, "lsdyna_mpp_sp_msmpi.exe"), |
| 61 | + (MpiOption.MPP_MS_MPI, Precision.DOUBLE, "lsdyna_mpp_dp_msmpi.exe"), |
| 62 | + ], |
| 63 | +) |
| 64 | +def test_get_exe_name_variants(tmp_workdir, mpi, prec, expected): |
| 65 | + runner = make_runner(tmp_workdir, mpi, prec) |
| 66 | + assert runner._get_exe_name() == expected |
| 67 | + |
| 68 | + |
| 69 | +def test_get_env_script_intel(tmp_workdir): |
| 70 | + runner = make_runner(tmp_workdir, MpiOption.MPP_INTEL_MPI) |
| 71 | + (tmp_workdir / "lsprepost_foo").mkdir() |
| 72 | + script = runner._get_env_script() |
| 73 | + assert script.endswith("lsdynaintelvar.bat") |
| 74 | + |
| 75 | + |
| 76 | +def test_write_runscript(tmp_workdir): |
| 77 | + runner = make_runner(tmp_workdir) |
| 78 | + runner._get_command_line = MagicMock(return_value="echo hello") |
| 79 | + runner._write_runscript() |
| 80 | + script_path = tmp_workdir / runner._scriptname |
| 81 | + assert "echo hello" in script_path.read_text() |
| 82 | + |
| 83 | + |
| 84 | +@patch("ansys.dyna.core.run.windows_runner.subprocess.Popen") |
| 85 | +def test_run_success(mock_popen, tmp_workdir): |
| 86 | + runner = make_runner(tmp_workdir) |
| 87 | + runner._get_command_line = MagicMock(return_value="echo hello") |
| 88 | + |
| 89 | + process = MagicMock() |
| 90 | + process.poll.side_effect = [None, 0] |
| 91 | + process.wait.return_value = 0 |
| 92 | + process.returncode = 0 |
| 93 | + mock_popen.return_value = process |
| 94 | + |
| 95 | + # fake log file |
| 96 | + log_file = tmp_workdir / "lsrun.out.txt" |
| 97 | + log_file.write_text("all good\n") |
| 98 | + |
| 99 | + runner.run() |
| 100 | + assert process.wait.called |
| 101 | + |
| 102 | + |
| 103 | +@patch("ansys.dyna.core.run.windows_runner.subprocess.Popen") |
| 104 | +def test_run_with_warning_logs(mock_popen, tmp_workdir, caplog): |
| 105 | + runner = make_runner(tmp_workdir) |
| 106 | + runner._get_command_line = MagicMock(return_value="echo hello") |
| 107 | + |
| 108 | + process = MagicMock() |
| 109 | + process.poll.side_effect = [None, 0] |
| 110 | + process.wait.return_value = 0 |
| 111 | + process.returncode = 0 |
| 112 | + mock_popen.return_value = process |
| 113 | + |
| 114 | + log_file = tmp_workdir / "lsrun.out.txt" |
| 115 | + log_file.write_text("Warning: something\n") |
| 116 | + |
| 117 | + runner.run() |
| 118 | + assert "completed with warnings" in caplog.text |
| 119 | + |
| 120 | + |
| 121 | +@patch("ansys.dyna.core.run.windows_runner.subprocess.Popen") |
| 122 | +def test_run_failure(mock_popen, tmp_workdir): |
| 123 | + runner = make_runner(tmp_workdir) |
| 124 | + runner._get_command_line = MagicMock(return_value="echo hello") |
| 125 | + |
| 126 | + process = MagicMock() |
| 127 | + process.poll.side_effect = [0] |
| 128 | + process.wait.return_value = 1 |
| 129 | + process.returncode = 1 |
| 130 | + mock_popen.return_value = process |
| 131 | + |
| 132 | + log_file = tmp_workdir / "lsrun.out.txt" |
| 133 | + log_file.write_text("Error: fail\n") |
| 134 | + |
| 135 | + with pytest.raises(RuntimeError): |
| 136 | + runner.run() |
0 commit comments