Skip to content

Commit b5106e1

Browse files
committed
tests: added unit tests for connect_to_mapdl
1 parent 9263f27 commit b5106e1

File tree

4 files changed

+104
-22
lines changed

4 files changed

+104
-22
lines changed

src/ansys/mapdl/core/launcher/remote.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,50 @@
3333
)
3434
from ansys.mapdl.core.mapdl_grpc import MapdlGrpc
3535

36+
_NON_VALID_ARGS = (
37+
"add_env_vars",
38+
"additional_switches",
39+
"exec_file",
40+
"jobname",
41+
"launch_on_hpc",
42+
"license_server_check",
43+
"license_type",
44+
"mapdl_output",
45+
"mode",
46+
"nproc",
47+
"override",
48+
"ram",
49+
"remove_temp_dir_on_exit",
50+
"replace_env_vars",
51+
"run_location",
52+
"running_on_hpc",
53+
"start_instance",
54+
"version",
55+
)
56+
57+
58+
def check_remote_args(args):
59+
for each_arg in _NON_VALID_ARGS:
60+
if each_arg in args:
61+
raise ValueError(
62+
f"'connect_to_mapdl' does not accept '{each_arg}' argument."
63+
)
64+
else:
65+
if each_arg == "mode":
66+
args[each_arg] = "grpc"
67+
elif each_arg == "start_instance":
68+
args[each_arg] = False
69+
else:
70+
args[each_arg] = None # setting everything as None.
71+
3672

3773
def connect_to_mapdl(
74+
port: Optional[int] = None,
75+
ip: Optional[str] = None,
3876
*,
3977
loglevel: str = "ERROR",
4078
start_timeout: Optional[int] = None,
41-
port: Optional[int] = None,
4279
cleanup_on_exit: bool = True,
43-
ip: Optional[str] = None,
4480
clear_on_connect: bool = True,
4581
log_apdl: Optional[Union[bool, str]] = None,
4682
print_com: bool = False,
@@ -55,31 +91,16 @@ def connect_to_mapdl(
5591

5692
check_kwargs(args) # check if passing wrong arguments
5793

58-
if args.get("start_instance"):
59-
raise ValueError(
60-
"'connect_to_mapdl' only accept 'start_instance' equals 'False'. "
61-
"If you intend to launch locally an instance use either "
62-
"'launch_mapdl_grpc' or the infamous 'launch_mapdl(start_instance=True)'."
63-
)
94+
check_remote_args(args)
6495

6596
pre_check_args(args)
6697

6798
get_ip(args)
6899

69100
args["port"] = get_port(args["port"], args["start_instance"])
70101

71-
# Check for a valid connection mode
72-
# args["mode"] = check_mode(args["mode"], args["version"])
73-
if args.get("mode", "grpc") != "grpc":
74-
raise ValueError("Only a 'grpc' instance can be connected to remotely.")
75-
76102
start_parm = generate_start_parameters(args)
77103

78-
# Early exit for debugging.
79-
if args["_debug_no_launch"]:
80-
# Early exit, just for testing
81-
return args # type: ignore
82-
83104
########################################
84105
# Connecting to a remote instance
85106
# -------------------------------
@@ -90,7 +111,7 @@ def connect_to_mapdl(
90111
start_parm["launched"] = False
91112

92113
mapdl = MapdlGrpc(
93-
cleanup_on_exit=False,
114+
cleanup_on_exit=args["cleanup_on_exit"],
94115
loglevel=args["loglevel"],
95116
set_no_abort=args["set_no_abort"],
96117
use_vtk=args["use_vtk"],

src/ansys/mapdl/core/launcher/tools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,8 +1176,6 @@ def pack_arguments(locals_):
11761176
args["_debug_no_launch"] = locals_.get(
11771177
"_debug_no_launch", locals_["kwargs"].get("_debug_no_launch", None)
11781178
)
1179-
args.setdefault("launch_on_hpc", False)
1180-
args.setdefault("ip", None)
11811179

11821180
return args
11831181

@@ -1565,6 +1563,9 @@ def pre_check_args(args: dict[str, Any]):
15651563
"the argument 'nproc' in 'launch_mapdl'."
15661564
)
15671565

1566+
args.setdefault("launch_on_hpc", False)
1567+
args.setdefault("ip", None)
1568+
15681569

15691570
def get_cpus(args: Dict[str, Any]):
15701571
"""Get number of CPUs

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,16 @@
169169
_ALLOWED_START_PARM = [
170170
"additional_switches",
171171
"check_parameter_names",
172+
"clear_on_connect",
172173
"env_vars",
173-
"launched",
174174
"exec_file",
175175
"finish_job_on_exit",
176176
"hostname",
177177
"ip",
178178
"jobid",
179179
"jobname",
180180
"launch_on_hpc",
181+
"launched",
181182
"mode",
182183
"nproc",
183184
"override",

tests/test_launcher/test_remote.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,62 @@
1919
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
22+
from unittest.mock import patch
23+
24+
import pytest
25+
26+
from ansys.mapdl.core.launcher.remote import _NON_VALID_ARGS, connect_to_mapdl
27+
28+
29+
def test_connect_to_mapdl():
30+
mapdl = connect_to_mapdl()
31+
32+
assert "PREP7" in mapdl.prep7()
33+
34+
assert not mapdl._start_instance
35+
assert not mapdl._launched
36+
37+
mapdl.exit(force=False)
38+
39+
40+
@pytest.mark.parametrize("arg", _NON_VALID_ARGS)
41+
def test_connect_to_mapdl_exceptions(arg):
42+
with pytest.raises(
43+
ValueError, match=f"'connect_to_mapdl' does not accept '{arg}' argument."
44+
):
45+
connect_to_mapdl(**{arg: True})
46+
47+
48+
_IP_TEST = "my_ip"
49+
50+
51+
@pytest.mark.parametrize(
52+
"arg,value",
53+
(
54+
("ip", _IP_TEST),
55+
("port", 50053),
56+
("loglevel", "DEBUG"),
57+
("loglevel", "ERROR"),
58+
("start_timeout", 12),
59+
("start_timeout", 15),
60+
("cleanup_on_exit", True),
61+
("cleanup_on_exit", False),
62+
("clear_on_connect", True),
63+
("clear_on_connect", False),
64+
("log_apdl", True),
65+
("log_apdl", False),
66+
("log_apdl", "log.out"),
67+
("print_com", False),
68+
),
69+
)
70+
@patch("socket.gethostbyname", lambda *args, **kwargs: _IP_TEST)
71+
@patch("socket.inet_aton", lambda *args, **kwargs: _IP_TEST)
72+
def test_connect_to_mapdl_kwargs(arg, value):
73+
with patch("ansys.mapdl.core.launcher.remote.MapdlGrpc") as mock_mg:
74+
args = {arg: value}
75+
mapdl = connect_to_mapdl(**args)
76+
77+
mock_mg.assert_called_once()
78+
kw = mock_mg.call_args_list[0].kwargs
79+
assert "ip" in kw and kw["ip"] == _IP_TEST
80+
assert arg in kw and kw[arg] == value

0 commit comments

Comments
 (0)