From 1d8042688d395c2d5606c03f5527c2a846730d32 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:29:13 +0100 Subject: [PATCH] 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> --- .github/workflows/ci.yml | 2 +- doc/changelog.d/3509.added.md | 1 + pyproject.toml | 1 - src/ansys/mapdl/core/launcher.py | 38 +++++---- tests/test_launcher.py | 139 +++++++++++++++++++++++-------- 5 files changed, 126 insertions(+), 55 deletions(-) create mode 100644 doc/changelog.d/3509.added.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dffd9a9385..2928f842f5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -821,7 +821,7 @@ jobs: - name: "Unit testing requirements installation" run: | - python -m pip install pytest pytest-rerunfailures pytest-cov pytest-random-order + python -m pip install pytest pytest-rerunfailures pytest-cov pytest-random-order pyfakefs - name: "Unit testing" env: diff --git a/doc/changelog.d/3509.added.md b/doc/changelog.d/3509.added.md new file mode 100644 index 0000000000..cf9b46ca69 --- /dev/null +++ b/doc/changelog.d/3509.added.md @@ -0,0 +1 @@ +test: faking-v150 \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 5c002273b3..58641de398 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,7 +63,6 @@ tests = [ "pyfakefs==5.7.2", "pyiges[full]==0.3.1", "pytest-cov==6.0.0", - "pytest-memprof<0.3.0", "pytest-pyvista==0.1.9", "pytest-random-order==1.1.1", "pytest-rerunfailures==15.0", diff --git a/src/ansys/mapdl/core/launcher.py b/src/ansys/mapdl/core/launcher.py index 31a3c8e54a..c1fe799fc3 100644 --- a/src/ansys/mapdl/core/launcher.py +++ b/src/ansys/mapdl/core/launcher.py @@ -1423,7 +1423,25 @@ def launch_mapdl( pre_check_args(args) + ######################################## + # PyPIM connection + # ---------------- + # Delegating to PyPIM if applicable + # + if _HAS_PIM and exec_file is None and pypim.is_configured(): + # Start MAPDL with PyPIM if the environment is configured for it + # and the user did not pass a directive on how to launch it. + LOG.info("Starting MAPDL remotely. The startup configuration will be ignored.") + + return launch_remote_mapdl( + cleanup_on_exit=args["cleanup_on_exit"], version=args["version"] + ) + + ######################################## # SLURM settings + # -------------- + # Checking if running on SLURM HPC + # if is_running_on_slurm(args): LOG.info("On Slurm mode.") @@ -1499,20 +1517,6 @@ def launch_mapdl( env_vars.setdefault("ANS_MULTIPLE_NODES", "1") env_vars.setdefault("HYDRA_BOOTSTRAP", "slurm") - ######################################## - # PyPIM connection - # ---------------- - # Delegating to PyPIM if applicable - # - if _HAS_PIM and exec_file is None and pypim.is_configured(): - # Start MAPDL with PyPIM if the environment is configured for it - # and the user did not pass a directive on how to launch it. - LOG.info("Starting MAPDL remotely. The startup configuration will be ignored.") - - return launch_remote_mapdl( - cleanup_on_exit=args["cleanup_on_exit"], version=args["version"] - ) - start_parm = generate_start_parameters(args) # Early exit for debugging. @@ -2261,9 +2265,9 @@ def get_version( raise VersionError( "The MAPDL gRPC interface requires MAPDL 20.2 or later" ) - - # Early exit - return + else: + # Early exit + return if isinstance(version, float): version = int(version * 10) diff --git a/tests/test_launcher.py b/tests/test_launcher.py index 83790ba031..8fbe9a0c54 100644 --- a/tests/test_launcher.py +++ b/tests/test_launcher.py @@ -30,6 +30,7 @@ import warnings import psutil +from pyfakefs.fake_filesystem import OSType import pytest from ansys.mapdl import core as pymapdl @@ -94,15 +95,11 @@ from ansys.mapdl.core.launcher import get_default_ansys installed_mapdl_versions = list(get_available_ansys_installations().keys()) - try: - V150_EXEC = find_mapdl("150")[0] - except ValueError: - V150_EXEC = "" except: from conftest import MAPDL_VERSION installed_mapdl_versions = [MAPDL_VERSION] - V150_EXEC = "" + from ansys.mapdl.core._version import SUPPORTED_ANSYS_VERSIONS as versions @@ -136,6 +133,12 @@ class myprocess: return process +@pytest.fixture +def my_fs(fs): + # fs.add_real_directory("/proc", lazy_read=False) + yield fs + + @pytest.fixture def fake_local_mapdl(mapdl): """Fixture to execute asserts before and after a test is run""" @@ -148,8 +151,7 @@ def fake_local_mapdl(mapdl): mapdl._local = False -@requires("local") -@requires("windows") +@patch("os.name", "nt") def test_validate_sw(): # ensure that windows adds msmpi # fake windows path @@ -157,15 +159,20 @@ def test_validate_sw(): add_sw = set_MPI_additional_switches("", version=version) assert "msmpi" in add_sw - add_sw = set_MPI_additional_switches("-mpi intelmpi", version=version) - assert "msmpi" in add_sw and "intelmpi" not in add_sw + with pytest.warns( + UserWarning, match="Due to incompatibilities between this MAPDL version" + ): + add_sw = set_MPI_additional_switches("-mpi intelmpi", version=version) + assert "msmpi" in add_sw and "intelmpi" not in add_sw - add_sw = set_MPI_additional_switches("-mpi INTELMPI", version=version) - assert "msmpi" in add_sw and "INTELMPI" not in add_sw + with pytest.warns( + UserWarning, match="Due to incompatibilities between this MAPDL version" + ): + add_sw = set_MPI_additional_switches("-mpi INTELMPI", version=version) + assert "msmpi" in add_sw and "INTELMPI" not in add_sw @requires("ansys-tools-path") -@requires("local") @pytest.mark.parametrize("path_data", paths) def test_version_from_path(path_data): exec_file, version = path_data @@ -173,41 +180,102 @@ def test_version_from_path(path_data): @requires("ansys-tools-path") -@requires("local") def test_catch_version_from_path(): with pytest.raises(RuntimeError): version_from_path("mapdl", "abc") +@pytest.mark.parametrize( + "path,version,raises", + [ + ["/ansys_inc/v221/ansys/bin/ansys221", 22.1, None], + ["/ansys_inc/v222/ansys/bin/mapdl", 22.2, None], + ["/usr/ansys_inc/v231/ansys/bin/mapdl", 23.1, None], + ["/usr/ansys_inc/v232/ansys/bin/mapdl", 23.2, None], + ["/usr/ansys_inc/v241/ansys/bin/mapdl", 24.1, None], + ["/ansysinc/v242/ansys/bin/ansys2", 24.2, ValueError], + ["/ansysinc/v242/ansys/bin/mapdl", 24.2, ValueError], + ], +) @requires("ansys-tools-path") -@requires("local") -@requires("linux") -def test_find_mapdl_linux(): - # assuming ansys is installed, should be able to find it on linux - # without env var +def test_find_mapdl_linux(my_fs, path, version, raises): + my_fs.os = OSType.LINUX + my_fs.create_file(path) + bin_file, ver = pymapdl.launcher.find_mapdl() - assert os.path.isfile(bin_file) - assert isinstance(ver, float) + + if raises: + assert not bin_file + assert not ver + + else: + assert bin_file.startswith(path.replace("mapdl", "")) + assert isinstance(ver, float) + assert ver == version @requires("ansys-tools-path") -@requires("local") -def test_invalid_mode(mapdl, cleared): +@patch("psutil.cpu_count", lambda *args, **kwargs: 2) +@patch("ansys.mapdl.core.launcher._is_ubuntu", lambda *args, **kwargs: True) +@patch("ansys.mapdl.core.launcher.get_process_at_port", lambda *args, **kwargs: None) +def test_invalid_mode(mapdl, my_fs, cleared, monkeypatch): + monkeypatch.delenv("PYMAPDL_START_INSTANCE", False) + monkeypatch.delenv("PYMAPDL_IP", False) + monkeypatch.delenv("PYMAPDL_PORT", False) + + my_fs.create_file("/ansys_inc/v241/ansys/bin/ansys241") with pytest.raises(ValueError): - exec_file = find_mapdl(installed_mapdl_versions[0])[0] + exec_file = find_mapdl()[0] pymapdl.launch_mapdl( exec_file, port=mapdl.port + 1, mode="notamode", start_timeout=start_timeout ) @requires("ansys-tools-path") -@requires("local") -@pytest.mark.skipif(not os.path.isfile(V150_EXEC), reason="Requires v150") -def test_old_version(mapdl, cleared): - exec_file = find_mapdl("150")[0] - with pytest.raises(ValueError): +@pytest.mark.parametrize("version", [120, 170, 190]) +@patch("psutil.cpu_count", lambda *args, **kwargs: 2) +@patch("ansys.mapdl.core.launcher._is_ubuntu", lambda *args, **kwargs: True) +@patch("ansys.mapdl.core.launcher.get_process_at_port", lambda *args, **kwargs: None) +def test_old_version_not_version(mapdl, my_fs, cleared, monkeypatch, version): + monkeypatch.delenv("PYMAPDL_START_INSTANCE", False) + monkeypatch.delenv("PYMAPDL_IP", False) + monkeypatch.delenv("PYMAPDL_PORT", False) + + exec_file = f"/ansys_inc/v{version}/ansys/bin/ansys{version}" + my_fs.create_file(exec_file) + assert exec_file == find_mapdl()[0] + + with pytest.raises( + ValueError, match="The MAPDL gRPC interface requires MAPDL 20.2 or later" + ): + pymapdl.launch_mapdl( + exec_file=exec_file, + port=mapdl.port + 1, + mode="grpc", + start_timeout=start_timeout, + ) + + +@requires("ansys-tools-path") +@pytest.mark.parametrize("version", [203, 213, 351]) +@patch("psutil.cpu_count", lambda *args, **kwargs: 2) +@patch("ansys.mapdl.core.launcher._is_ubuntu", lambda *args, **kwargs: True) +@patch("ansys.mapdl.core.launcher.get_process_at_port", lambda *args, **kwargs: None) +def test_not_valid_versions(mapdl, my_fs, cleared, monkeypatch, version): + monkeypatch.delenv("PYMAPDL_START_INSTANCE", False) + monkeypatch.delenv("PYMAPDL_IP", False) + monkeypatch.delenv("PYMAPDL_PORT", False) + + exec_file = f"/ansys_inc/v{version}/ansys/bin/ansys{version}" + my_fs.create_file(exec_file) + + assert exec_file == find_mapdl()[0] + with pytest.raises(ValueError, match="MAPDL version must be one of the following"): pymapdl.launch_mapdl( - exec_file, port=mapdl.port + 1, mode="console", start_timeout=start_timeout + exec_file=exec_file, + port=mapdl.port + 1, + mode="grpc", + start_timeout=start_timeout, ) @@ -244,7 +312,6 @@ def test_license_type_keyword_names(monkeypatch, license_name): assert f"-p {license_name}" in args["additional_switches"] -# @requires("local") @pytest.mark.parametrize("license_name", LICENSES) def test_license_type_additional_switch(license_name): args = launch_mapdl( @@ -721,7 +788,7 @@ def test_get_slurm_options(set_env_var_context, validation): ], ) def test_slurm_ram(monkeypatch, ram, expected, context): - monkeypatch.setenv("SLURM_MEM_PER_NODE", ram) + monkeypatch.setenv("SLURM_MEM_PER_NODE", str(ram)) monkeypatch.setenv("PYMAPDL_MAPDL_EXEC", "asdf/qwer/poiu") args = { @@ -1208,7 +1275,7 @@ def test_launch_grpc(tmpdir, launch_on_hpc): @pytest.mark.parametrize("env", [None, 3, 10]) def test_get_cpus(monkeypatch, arg, env): if env: - monkeypatch.setenv("PYMAPDL_NPROC", env) + monkeypatch.setenv("PYMAPDL_NPROC", str(env)) context = NullContext() 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): def test_launch_on_hpc_exception_launch_mapdl(monkeypatch): monkeypatch.delenv("PYMAPDL_START_INSTANCE", False) - exec_file = "path/to/mapdl/v242/executable/ansys242" + exec_file = "path/to/mapdl/v242/ansys/bin/executable/ansys242" process = get_fake_process("ERROR") @@ -1475,7 +1542,7 @@ def test_launch_on_hpc_exception_launch_mapdl(monkeypatch): def test_launch_on_hpc_exception_successfull_sbatch(monkeypatch): monkeypatch.delenv("PYMAPDL_START_INSTANCE", False) - exec_file = "path/to/mapdl/v242/executable/ansys242" + exec_file = "path/to/mapdl/v242/ansys/bin/executable/ansys242" def raise_exception(*args, **kwargs): raise Exception("Fake exception when launching MAPDL") @@ -1603,7 +1670,7 @@ def test_get_port(monkeypatch, port, port_envvar, start_instance, port_busy, res monkeypatch.delenv("PYMAPDL_PORT", False) if port_envvar: - monkeypatch.setenv("PYMAPDL_PORT", port_envvar) + monkeypatch.setenv("PYMAPDL_PORT", str(port_envvar)) # Testing if port_busy: @@ -1706,7 +1773,7 @@ def test_get_version_version_error(monkeypatch): @pytest.mark.parametrize("version", [211, 221, 232]) def test_get_version_env_var(monkeypatch, version): - monkeypatch.setenv("PYMAPDL_MAPDL_VERSION", version) + monkeypatch.setenv("PYMAPDL_MAPDL_VERSION", str(version)) assert version == get_version(None) assert version != get_version(241)