Skip to content

Commit 1d80426

Browse files
germa89pyansys-ci-botMaxJPRey
authored
test: faking-v150 (#3509)
* fix: raising port busy when connecting * chore: adding changelog file 3507.fixed.md [dependabot-skip] * refactor: tests * fix: check mode * fix: has_dependency * chore: adding changelog file 3509.added.md [dependabot-skip] * chore: update src/ansys/mapdl/core/launcher.py Co-authored-by: Maxime Rey <87315832+MaxJPRey@users.noreply.github.com> * feat: removing memproof * docs: update src/ansys/mapdl/core/launcher.py Co-authored-by: Maxime Rey <87315832+MaxJPRey@users.noreply.github.com> * fix: adding full directory * test: testing lazy read * chore: adding changelog file 3509.added.md [dependabot-skip] * tests: not adding proc directory * fix: test_old_version * ci: add pyfakefs to minimal * fix: test_invalid_mode * fix: patching psutil * fix: redundancy * fix: tests * fix: early exit * fix: tests * refactor: delegating to pypim first * fix: remove some warnings from pytest output * feat: running MPI fix only if on windows --------- Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Co-authored-by: Maxime Rey <87315832+MaxJPRey@users.noreply.github.com>
1 parent 076d722 commit 1d80426

File tree

5 files changed

+126
-55
lines changed

5 files changed

+126
-55
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ jobs:
821821
822822
- name: "Unit testing requirements installation"
823823
run: |
824-
python -m pip install pytest pytest-rerunfailures pytest-cov pytest-random-order
824+
python -m pip install pytest pytest-rerunfailures pytest-cov pytest-random-order pyfakefs
825825
826826
- name: "Unit testing"
827827
env:

doc/changelog.d/3509.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: faking-v150

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ tests = [
6363
"pyfakefs==5.7.2",
6464
"pyiges[full]==0.3.1",
6565
"pytest-cov==6.0.0",
66-
"pytest-memprof<0.3.0",
6766
"pytest-pyvista==0.1.9",
6867
"pytest-random-order==1.1.1",
6968
"pytest-rerunfailures==15.0",

src/ansys/mapdl/core/launcher.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,25 @@ def launch_mapdl(
14231423

14241424
pre_check_args(args)
14251425

1426+
########################################
1427+
# PyPIM connection
1428+
# ----------------
1429+
# Delegating to PyPIM if applicable
1430+
#
1431+
if _HAS_PIM and exec_file is None and pypim.is_configured():
1432+
# Start MAPDL with PyPIM if the environment is configured for it
1433+
# and the user did not pass a directive on how to launch it.
1434+
LOG.info("Starting MAPDL remotely. The startup configuration will be ignored.")
1435+
1436+
return launch_remote_mapdl(
1437+
cleanup_on_exit=args["cleanup_on_exit"], version=args["version"]
1438+
)
1439+
1440+
########################################
14261441
# SLURM settings
1442+
# --------------
1443+
# Checking if running on SLURM HPC
1444+
#
14271445
if is_running_on_slurm(args):
14281446
LOG.info("On Slurm mode.")
14291447

@@ -1499,20 +1517,6 @@ def launch_mapdl(
14991517
env_vars.setdefault("ANS_MULTIPLE_NODES", "1")
15001518
env_vars.setdefault("HYDRA_BOOTSTRAP", "slurm")
15011519

1502-
########################################
1503-
# PyPIM connection
1504-
# ----------------
1505-
# Delegating to PyPIM if applicable
1506-
#
1507-
if _HAS_PIM and exec_file is None and pypim.is_configured():
1508-
# Start MAPDL with PyPIM if the environment is configured for it
1509-
# and the user did not pass a directive on how to launch it.
1510-
LOG.info("Starting MAPDL remotely. The startup configuration will be ignored.")
1511-
1512-
return launch_remote_mapdl(
1513-
cleanup_on_exit=args["cleanup_on_exit"], version=args["version"]
1514-
)
1515-
15161520
start_parm = generate_start_parameters(args)
15171521

15181522
# Early exit for debugging.
@@ -2261,9 +2265,9 @@ def get_version(
22612265
raise VersionError(
22622266
"The MAPDL gRPC interface requires MAPDL 20.2 or later"
22632267
)
2264-
2265-
# Early exit
2266-
return
2268+
else:
2269+
# Early exit
2270+
return
22672271

22682272
if isinstance(version, float):
22692273
version = int(version * 10)

tests/test_launcher.py

Lines changed: 103 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import warnings
3131

3232
import psutil
33+
from pyfakefs.fake_filesystem import OSType
3334
import pytest
3435

3536
from ansys.mapdl import core as pymapdl
@@ -94,15 +95,11 @@
9495
from ansys.mapdl.core.launcher import get_default_ansys
9596

9697
installed_mapdl_versions = list(get_available_ansys_installations().keys())
97-
try:
98-
V150_EXEC = find_mapdl("150")[0]
99-
except ValueError:
100-
V150_EXEC = ""
10198
except:
10299
from conftest import MAPDL_VERSION
103100

104101
installed_mapdl_versions = [MAPDL_VERSION]
105-
V150_EXEC = ""
102+
106103

107104
from ansys.mapdl.core._version import SUPPORTED_ANSYS_VERSIONS as versions
108105

@@ -136,6 +133,12 @@ class myprocess:
136133
return process
137134

138135

136+
@pytest.fixture
137+
def my_fs(fs):
138+
# fs.add_real_directory("/proc", lazy_read=False)
139+
yield fs
140+
141+
139142
@pytest.fixture
140143
def fake_local_mapdl(mapdl):
141144
"""Fixture to execute asserts before and after a test is run"""
@@ -148,66 +151,131 @@ def fake_local_mapdl(mapdl):
148151
mapdl._local = False
149152

150153

151-
@requires("local")
152-
@requires("windows")
154+
@patch("os.name", "nt")
153155
def test_validate_sw():
154156
# ensure that windows adds msmpi
155157
# fake windows path
156158
version = 211
157159
add_sw = set_MPI_additional_switches("", version=version)
158160
assert "msmpi" in add_sw
159161

160-
add_sw = set_MPI_additional_switches("-mpi intelmpi", version=version)
161-
assert "msmpi" in add_sw and "intelmpi" not in add_sw
162+
with pytest.warns(
163+
UserWarning, match="Due to incompatibilities between this MAPDL version"
164+
):
165+
add_sw = set_MPI_additional_switches("-mpi intelmpi", version=version)
166+
assert "msmpi" in add_sw and "intelmpi" not in add_sw
162167

163-
add_sw = set_MPI_additional_switches("-mpi INTELMPI", version=version)
164-
assert "msmpi" in add_sw and "INTELMPI" not in add_sw
168+
with pytest.warns(
169+
UserWarning, match="Due to incompatibilities between this MAPDL version"
170+
):
171+
add_sw = set_MPI_additional_switches("-mpi INTELMPI", version=version)
172+
assert "msmpi" in add_sw and "INTELMPI" not in add_sw
165173

166174

167175
@requires("ansys-tools-path")
168-
@requires("local")
169176
@pytest.mark.parametrize("path_data", paths)
170177
def test_version_from_path(path_data):
171178
exec_file, version = path_data
172179
assert version_from_path("mapdl", exec_file) == version
173180

174181

175182
@requires("ansys-tools-path")
176-
@requires("local")
177183
def test_catch_version_from_path():
178184
with pytest.raises(RuntimeError):
179185
version_from_path("mapdl", "abc")
180186

181187

188+
@pytest.mark.parametrize(
189+
"path,version,raises",
190+
[
191+
["/ansys_inc/v221/ansys/bin/ansys221", 22.1, None],
192+
["/ansys_inc/v222/ansys/bin/mapdl", 22.2, None],
193+
["/usr/ansys_inc/v231/ansys/bin/mapdl", 23.1, None],
194+
["/usr/ansys_inc/v232/ansys/bin/mapdl", 23.2, None],
195+
["/usr/ansys_inc/v241/ansys/bin/mapdl", 24.1, None],
196+
["/ansysinc/v242/ansys/bin/ansys2", 24.2, ValueError],
197+
["/ansysinc/v242/ansys/bin/mapdl", 24.2, ValueError],
198+
],
199+
)
182200
@requires("ansys-tools-path")
183-
@requires("local")
184-
@requires("linux")
185-
def test_find_mapdl_linux():
186-
# assuming ansys is installed, should be able to find it on linux
187-
# without env var
201+
def test_find_mapdl_linux(my_fs, path, version, raises):
202+
my_fs.os = OSType.LINUX
203+
my_fs.create_file(path)
204+
188205
bin_file, ver = pymapdl.launcher.find_mapdl()
189-
assert os.path.isfile(bin_file)
190-
assert isinstance(ver, float)
206+
207+
if raises:
208+
assert not bin_file
209+
assert not ver
210+
211+
else:
212+
assert bin_file.startswith(path.replace("mapdl", ""))
213+
assert isinstance(ver, float)
214+
assert ver == version
191215

192216

193217
@requires("ansys-tools-path")
194-
@requires("local")
195-
def test_invalid_mode(mapdl, cleared):
218+
@patch("psutil.cpu_count", lambda *args, **kwargs: 2)
219+
@patch("ansys.mapdl.core.launcher._is_ubuntu", lambda *args, **kwargs: True)
220+
@patch("ansys.mapdl.core.launcher.get_process_at_port", lambda *args, **kwargs: None)
221+
def test_invalid_mode(mapdl, my_fs, cleared, monkeypatch):
222+
monkeypatch.delenv("PYMAPDL_START_INSTANCE", False)
223+
monkeypatch.delenv("PYMAPDL_IP", False)
224+
monkeypatch.delenv("PYMAPDL_PORT", False)
225+
226+
my_fs.create_file("/ansys_inc/v241/ansys/bin/ansys241")
196227
with pytest.raises(ValueError):
197-
exec_file = find_mapdl(installed_mapdl_versions[0])[0]
228+
exec_file = find_mapdl()[0]
198229
pymapdl.launch_mapdl(
199230
exec_file, port=mapdl.port + 1, mode="notamode", start_timeout=start_timeout
200231
)
201232

202233

203234
@requires("ansys-tools-path")
204-
@requires("local")
205-
@pytest.mark.skipif(not os.path.isfile(V150_EXEC), reason="Requires v150")
206-
def test_old_version(mapdl, cleared):
207-
exec_file = find_mapdl("150")[0]
208-
with pytest.raises(ValueError):
235+
@pytest.mark.parametrize("version", [120, 170, 190])
236+
@patch("psutil.cpu_count", lambda *args, **kwargs: 2)
237+
@patch("ansys.mapdl.core.launcher._is_ubuntu", lambda *args, **kwargs: True)
238+
@patch("ansys.mapdl.core.launcher.get_process_at_port", lambda *args, **kwargs: None)
239+
def test_old_version_not_version(mapdl, my_fs, cleared, monkeypatch, version):
240+
monkeypatch.delenv("PYMAPDL_START_INSTANCE", False)
241+
monkeypatch.delenv("PYMAPDL_IP", False)
242+
monkeypatch.delenv("PYMAPDL_PORT", False)
243+
244+
exec_file = f"/ansys_inc/v{version}/ansys/bin/ansys{version}"
245+
my_fs.create_file(exec_file)
246+
assert exec_file == find_mapdl()[0]
247+
248+
with pytest.raises(
249+
ValueError, match="The MAPDL gRPC interface requires MAPDL 20.2 or later"
250+
):
251+
pymapdl.launch_mapdl(
252+
exec_file=exec_file,
253+
port=mapdl.port + 1,
254+
mode="grpc",
255+
start_timeout=start_timeout,
256+
)
257+
258+
259+
@requires("ansys-tools-path")
260+
@pytest.mark.parametrize("version", [203, 213, 351])
261+
@patch("psutil.cpu_count", lambda *args, **kwargs: 2)
262+
@patch("ansys.mapdl.core.launcher._is_ubuntu", lambda *args, **kwargs: True)
263+
@patch("ansys.mapdl.core.launcher.get_process_at_port", lambda *args, **kwargs: None)
264+
def test_not_valid_versions(mapdl, my_fs, cleared, monkeypatch, version):
265+
monkeypatch.delenv("PYMAPDL_START_INSTANCE", False)
266+
monkeypatch.delenv("PYMAPDL_IP", False)
267+
monkeypatch.delenv("PYMAPDL_PORT", False)
268+
269+
exec_file = f"/ansys_inc/v{version}/ansys/bin/ansys{version}"
270+
my_fs.create_file(exec_file)
271+
272+
assert exec_file == find_mapdl()[0]
273+
with pytest.raises(ValueError, match="MAPDL version must be one of the following"):
209274
pymapdl.launch_mapdl(
210-
exec_file, port=mapdl.port + 1, mode="console", start_timeout=start_timeout
275+
exec_file=exec_file,
276+
port=mapdl.port + 1,
277+
mode="grpc",
278+
start_timeout=start_timeout,
211279
)
212280

213281

@@ -244,7 +312,6 @@ def test_license_type_keyword_names(monkeypatch, license_name):
244312
assert f"-p {license_name}" in args["additional_switches"]
245313

246314

247-
# @requires("local")
248315
@pytest.mark.parametrize("license_name", LICENSES)
249316
def test_license_type_additional_switch(license_name):
250317
args = launch_mapdl(
@@ -721,7 +788,7 @@ def test_get_slurm_options(set_env_var_context, validation):
721788
],
722789
)
723790
def test_slurm_ram(monkeypatch, ram, expected, context):
724-
monkeypatch.setenv("SLURM_MEM_PER_NODE", ram)
791+
monkeypatch.setenv("SLURM_MEM_PER_NODE", str(ram))
725792
monkeypatch.setenv("PYMAPDL_MAPDL_EXEC", "asdf/qwer/poiu")
726793

727794
args = {
@@ -1208,7 +1275,7 @@ def test_launch_grpc(tmpdir, launch_on_hpc):
12081275
@pytest.mark.parametrize("env", [None, 3, 10])
12091276
def test_get_cpus(monkeypatch, arg, env):
12101277
if env:
1211-
monkeypatch.setenv("PYMAPDL_NPROC", env)
1278+
monkeypatch.setenv("PYMAPDL_NPROC", str(env))
12121279

12131280
context = NullContext()
12141281
cores_machine = psutil.cpu_count(logical=False) # it is patched
@@ -1440,7 +1507,7 @@ def test_launch_on_hpc_not_found_ansys(mck_sc, mck_lgrpc, mck_kj, monkeypatch):
14401507

14411508
def test_launch_on_hpc_exception_launch_mapdl(monkeypatch):
14421509
monkeypatch.delenv("PYMAPDL_START_INSTANCE", False)
1443-
exec_file = "path/to/mapdl/v242/executable/ansys242"
1510+
exec_file = "path/to/mapdl/v242/ansys/bin/executable/ansys242"
14441511

14451512
process = get_fake_process("ERROR")
14461513

@@ -1475,7 +1542,7 @@ def test_launch_on_hpc_exception_launch_mapdl(monkeypatch):
14751542

14761543
def test_launch_on_hpc_exception_successfull_sbatch(monkeypatch):
14771544
monkeypatch.delenv("PYMAPDL_START_INSTANCE", False)
1478-
exec_file = "path/to/mapdl/v242/executable/ansys242"
1545+
exec_file = "path/to/mapdl/v242/ansys/bin/executable/ansys242"
14791546

14801547
def raise_exception(*args, **kwargs):
14811548
raise Exception("Fake exception when launching MAPDL")
@@ -1603,7 +1670,7 @@ def test_get_port(monkeypatch, port, port_envvar, start_instance, port_busy, res
16031670

16041671
monkeypatch.delenv("PYMAPDL_PORT", False)
16051672
if port_envvar:
1606-
monkeypatch.setenv("PYMAPDL_PORT", port_envvar)
1673+
monkeypatch.setenv("PYMAPDL_PORT", str(port_envvar))
16071674

16081675
# Testing
16091676
if port_busy:
@@ -1706,7 +1773,7 @@ def test_get_version_version_error(monkeypatch):
17061773

17071774
@pytest.mark.parametrize("version", [211, 221, 232])
17081775
def test_get_version_env_var(monkeypatch, version):
1709-
monkeypatch.setenv("PYMAPDL_MAPDL_VERSION", version)
1776+
monkeypatch.setenv("PYMAPDL_MAPDL_VERSION", str(version))
17101777

17111778
assert version == get_version(None)
17121779
assert version != get_version(241)

0 commit comments

Comments
 (0)