3030import warnings
3131
3232import psutil
33+ from pyfakefs .fake_filesystem import OSType
3334import pytest
3435
3536from ansys .mapdl import core as pymapdl
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 = ""
10198except :
10299 from conftest import MAPDL_VERSION
103100
104101 installed_mapdl_versions = [MAPDL_VERSION ]
105- V150_EXEC = ""
102+
106103
107104from 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
140143def 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" )
153155def 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 )
170177def 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" )
177183def 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 )
249316def 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)
723790def 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 ])
12091276def 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
14411508def 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
14761543def 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 ])
17081775def 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