Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: faking-v150 #3509

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b959054
fix: raising port busy when connecting
germa89 Oct 23, 2024
3d1d619
chore: adding changelog file 3507.fixed.md [dependabot-skip]
pyansys-ci-bot Oct 23, 2024
2ad60b5
refactor: tests
germa89 Oct 24, 2024
3c805d9
fix: check mode
germa89 Oct 24, 2024
6888b3a
fix: has_dependency
germa89 Oct 24, 2024
9c021f1
Merge branch 'fix/raising-busy-port' into tests/faking-v150
germa89 Oct 24, 2024
780c699
chore: adding changelog file 3509.added.md [dependabot-skip]
pyansys-ci-bot Oct 24, 2024
a061b5f
chore: update src/ansys/mapdl/core/launcher.py
germa89 Oct 25, 2024
4a01800
feat: removing memproof
germa89 Nov 21, 2024
db20658
chore: merge branch 'main' into tests/faking-v150
germa89 Nov 21, 2024
5873f26
docs: update src/ansys/mapdl/core/launcher.py
germa89 Nov 21, 2024
8b401a6
chore: merge branch 'tests/faking-v150' of https://github.com/ansys/p…
germa89 Nov 21, 2024
6d4f10c
fix: adding full directory
germa89 Nov 21, 2024
76edd83
test: testing lazy read
germa89 Nov 21, 2024
715c84a
chore: adding changelog file 3509.added.md [dependabot-skip]
pyansys-ci-bot Nov 21, 2024
77fc4e4
tests: not adding proc directory
germa89 Nov 25, 2024
5b43b45
chore: merge remote-tracking branch 'origin/main' into tests/faking-v150
germa89 Nov 25, 2024
0d9d4ff
chore: merge remote-tracking branch 'origin/main' into tests/faking-v150
germa89 Nov 25, 2024
3cb43fc
fix: test_old_version
germa89 Nov 25, 2024
dd56731
ci: add pyfakefs to minimal
germa89 Nov 25, 2024
fa46254
fix: test_invalid_mode
germa89 Nov 25, 2024
ce45e18
fix: patching psutil
germa89 Nov 25, 2024
5bcf3c7
fix: redundancy
germa89 Nov 25, 2024
66ac674
fix: tests
germa89 Nov 25, 2024
4d77bc2
fix: early exit
germa89 Nov 25, 2024
3dd0f1a
fix: tests
germa89 Nov 25, 2024
a8dec55
chore: merge remote-tracking branch 'origin/main' into tests/faking-v150
germa89 Nov 25, 2024
e1f4cdd
chore: merge remote-tracking branch 'origin/main' into tests/faking-v150
germa89 Nov 25, 2024
195b1b3
refactor: delegating to pypim first
germa89 Nov 25, 2024
816758a
fix: remove some warnings from pytest output
germa89 Nov 25, 2024
648e45a
feat: running MPI fix only if on windows
germa89 Nov 25, 2024
00a86ed
Merge branch 'main' into tests/faking-v150
germa89 Nov 26, 2024
ba3fa6b
Merge branch 'tests/faking-v150' of https://github.com/ansys/pymapdl …
germa89 Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/3509.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test: faking-v150
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ tests = [
"pyfakefs==5.7.1",
"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",
Expand Down
38 changes: 21 additions & 17 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,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.")

Expand Down Expand Up @@ -1471,20 +1489,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.
Expand Down Expand Up @@ -2230,9 +2234,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)
Expand Down
139 changes: 103 additions & 36 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import warnings

import psutil
from pyfakefs.fake_filesystem import OSType
import pytest

from ansys.mapdl import core as pymapdl
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"""
Expand All @@ -148,66 +151,131 @@ 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
version = 211
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
assert version_from_path("mapdl", exec_file) == version


@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, port=mapdl.port + 1, mode="console", start_timeout=start_timeout
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=exec_file,
port=mapdl.port + 1,
mode="grpc",
start_timeout=start_timeout,
)


Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
Loading