Skip to content

Commit 3d560f4

Browse files
authored
Add a file.exists() check for the server executable. (#261)
* Add a file.exists() check for the server executable. * Resolve flake8 * Adding a test * Exclude Linux lines from coverage for now * Trying out actually running the test_launcher.py tests for server version 2.0 and above * Fixed the test_start_local
1 parent b305f87 commit 3d560f4

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

ansys/dpf/core/server.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ def check_version(self, required_version, msg=None):
549549

550550
return server_meet_version_and_raise(required_version, self, msg)
551551

552+
552553
def _find_port_available_for_docker_bind(port):
553554
run_cmd = "docker ps --all"
554555
process = subprocess.Popen(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
@@ -562,6 +563,7 @@ def _find_port_available_for_docker_bind(port):
562563
port += 1
563564
return port
564565

566+
565567
def _run_launch_server_process(ansys_path, ip, port, docker_name):
566568
if docker_name:
567569
docker_server_port = int(os.environ.get("DOCKER_SERVER_PORT", port))
@@ -585,11 +587,13 @@ def _run_launch_server_process(ansys_path, ip, port, docker_name):
585587
docker_name]
586588
else:
587589
if os.name == "nt":
588-
run_cmd = f"Ans.Dpf.Grpc.bat --address {ip} --port {port}"
590+
executable = "Ans.Dpf.Grpc.bat"
591+
run_cmd = f"{executable} --address {ip} --port {port}"
589592
path_in_install = "aisol/bin/winx64"
590593
else:
591-
run_cmd = ["./Ans.Dpf.Grpc.sh", f"--address {ip}", f"--port {port}"]
592-
path_in_install = "aisol/bin/linx64"
594+
executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover
595+
run_cmd = [executable, f"--address {ip}", f"--port {port}"] # pragma: no cover
596+
path_in_install = "aisol/bin/linx64" # pragma: no cover
593597

594598
# verify ansys path is valid
595599
if os.path.isdir(f"{str(ansys_path)}/{path_in_install}"):
@@ -600,15 +604,20 @@ def _run_launch_server_process(ansys_path, ip, port, docker_name):
600604
raise NotADirectoryError(
601605
f'Invalid ansys path at "{str(ansys_path)}". '
602606
"Unable to locate the directory containing DPF at "
603-
f'"{dpf_run_dir}"'
604-
)
607+
f'"{dpf_run_dir}"')
608+
else:
609+
if not os.path.exists(os.path.join(dpf_run_dir, executable)):
610+
raise FileNotFoundError(
611+
f'DPF executable not found at "{dpf_run_dir}". '
612+
f'Unable to locate the executable "{executable}"')
605613

606614
old_dir = os.getcwd()
607615
os.chdir(dpf_run_dir)
608616
process = subprocess.Popen(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
609617
os.chdir(old_dir)
610618
return process
611619

620+
612621
def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10., docker_name=None):
613622
"""Launch Ansys DPF.
614623

tests/test_launcher.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
1+
import os
12
import pytest
2-
33
from ansys.dpf import core
4-
from ansys.dpf.core.misc import is_ubuntu
5-
6-
ansys_path = core.misc.find_ansys()
7-
8-
invalid_version = None
9-
if ansys_path is not None:
10-
try:
11-
invalid_version = int(ansys_path[-3:]) < 211
12-
except:
13-
invalid_version = True
14-
15-
# skip unless ansys v212 is installed
16-
if ansys_path is None or invalid_version or is_ubuntu():
17-
pytestmark = pytest.mark.skip("Requires local install of ANSYS 2021R2")
184

195

206
def test_start_local():
217
if not core.SERVER:
228
core.start_local_server()
239
starting_server = id(core.SERVER)
2410
n_init = len(core._server_instances)
25-
server = core.start_local_server(as_global=False, ansys_path=core.SERVER.ansys_path)
11+
server = core.start_local_server(as_global=False)
2612
assert len(core._server_instances) == n_init + 1
27-
core._server_instances[-1]().shutdown()
28-
13+
server.shutdown()
2914
# ensure global channel didn't change
3015
assert starting_server == id(core.SERVER)
3116

@@ -35,12 +20,21 @@ def test_start_local_failed():
3520
core.start_local_server(ansys_path="", use_docker_by_default=False)
3621

3722

23+
def test_start_local_failed_executable():
24+
from ansys.dpf.core._version import __ansys_version__
25+
from ansys.dpf.core.server import find_ansys
26+
from pathlib import Path
27+
with pytest.raises(FileNotFoundError):
28+
path = Path(os.environ.get("AWP_ROOT" + __ansys_version__,
29+
find_ansys())).parent.absolute()
30+
core.start_local_server(ansys_path=path)
31+
32+
3833
def test_server_ip():
39-
assert core.SERVER.ip != None
40-
assert core.SERVER.port != None
41-
assert core.SERVER.version != None
42-
43-
assert core.SERVER.info["server_process_id"] != None
44-
assert core.SERVER.info["server_ip"] != None
45-
assert core.SERVER.info["server_port"] != None
46-
assert core.SERVER.info["server_version"] != None
34+
assert core.SERVER.ip is not None
35+
assert core.SERVER.port is not None
36+
assert core.SERVER.version is not None
37+
assert core.SERVER.info["server_process_id"] is not None
38+
assert core.SERVER.info["server_ip"] is not None
39+
assert core.SERVER.info["server_port"] is not None
40+
assert core.SERVER.info["server_version"] is not None

0 commit comments

Comments
 (0)