11"""Testing module for the local launcher."""
22
3+ import re
4+
35import pytest
46
57from ansys .geometry .core import Modeler
68from ansys .geometry .core .connection import LocalDockerInstance , launch_local_modeler
79
810
11+ def _check_no_shutdown_warning (port : int , log : str ) -> bool :
12+ msg = f"WARNING localhost:{ port } :client\.py:[0-9]+ Geometry Service will not be shutdown since it was already running\.\.\." # noqa : E501
13+ pattern = re .compile (msg )
14+ return True if pattern .search (log ) else False
15+
16+
17+ def _check_service_already_running (port : int , log : str ) -> bool :
18+ msg = f"WARNING PyGeometry_global:localinstance\.py:[0-9]+ Service already running at port { port } \.\.\." # noqa : E501
19+ pattern = re .compile (msg )
20+ return True if pattern .search (log ) else False
21+
22+
23+ def _check_restarting_service (port : int , log : str ) -> bool :
24+ msg = f"WARNING PyGeometry_global:localinstance\.py:124 Restarting service already running at port { port } \.\.\." # noqa : E501
25+ pattern = re .compile (msg )
26+ return True if pattern .search (log ) else False
27+
28+
929def test_if_docker_is_installed ():
1030 """Simple test to check if Docker is installed in the machine."""
1131 assert LocalDockerInstance .is_docker_installed ()
1232
1333
14- def test_local_launcher_connect (modeler : Modeler , caplog : pytest .LogCaptureFixture ):
34+ def test_local_launcher_connect (
35+ modeler : Modeler , caplog : pytest .LogCaptureFixture , docker_instance : LocalDockerInstance
36+ ):
1537 """Checking connection to existing service using launch modeler."""
38+ if not docker_instance :
39+ pytest .skip ("Docker local launcher tests are not runnable." )
40+
1641 # Get the existing target
1742 target = modeler .client .target ().split (":" )
1843 port = int (target [1 ])
@@ -25,20 +50,23 @@ def test_local_launcher_connect(modeler: Modeler, caplog: pytest.LogCaptureFixtu
2550 local_modeler = launch_local_modeler (
2651 port = port , connect_to_existing_service = True , restart_if_existing_service = False
2752 )
28-
29- msg = f"WARNING PyGeometry_global:localinstance.py:155 Service already running at port { port } ...\n " # noqa : E501
30- assert msg in caplog .text
53+ assert _check_service_already_running (port , caplog .text ) is True
54+ caplog .clear ()
3155
3256 # Try to close it... this will throw a warning
3357 local_modeler .client .close ()
34-
35- msg = f"WARNING localhost:{ port } :client.py:182 Geometry Service will not be shutdown since it was already running...\n " # noqa : E501
36- assert msg in caplog .text
58+ assert _check_no_shutdown_warning (port , caplog .text ) is True
59+ caplog .clear ()
3760
3861
39- def test_local_launcher_connect_with_restart (modeler : Modeler , caplog : pytest .LogCaptureFixture ):
62+ def test_local_launcher_connect_with_restart (
63+ modeler : Modeler , caplog : pytest .LogCaptureFixture , docker_instance : LocalDockerInstance
64+ ):
4065 """Checking connection to existing service using launch modeler and
4166 restarting existing service."""
67+ if not docker_instance :
68+ pytest .skip ("Docker local launcher tests are not runnable." )
69+
4270 # Get the existing target
4371 target = modeler .client .target ().split (":" )
4472 port = int (target [1 ])
@@ -50,8 +78,7 @@ def test_local_launcher_connect_with_restart(modeler: Modeler, caplog: pytest.Lo
5078 )
5179
5280 # Check that the warning is NOT raised
53- msg = f"WARNING PyGeometry_global:localinstance.py:155 Service already running at port { new_port } ...\n " # noqa : E501
54- assert msg not in caplog .text
81+ assert _check_service_already_running (new_port , caplog .text ) is False
5582 caplog .clear ()
5683
5784 # Connect to the previous modeler and restart it
@@ -60,30 +87,30 @@ def test_local_launcher_connect_with_restart(modeler: Modeler, caplog: pytest.Lo
6087 )
6188
6289 # Check that the warnings are raised
63- msg = f"WARNING PyGeometry_global:localinstance.py:155 Service already running at port { new_port } ...\n " # noqa : E501
64- assert msg in caplog .text
65- msg = f"WARNING PyGeometry_global:localinstance.py:122 Restarting service already running at port { new_port } ...\n " # noqa : E501
66- assert msg in caplog .text
90+ assert _check_service_already_running (new_port , caplog .text ) is True
91+ assert _check_restarting_service (new_port , caplog .text ) is True
6792 caplog .clear ()
6893
6994 # Try to close the new_modeler_restarted... this will throw a warning
7095 new_modeler_restarted .client .close ()
7196
72- msg = f"WARNING localhost:{ new_port } :client.py:182 Geometry Service will not be shutdown since it was already running...\n " # noqa : E501
73- assert msg in caplog .text
97+ assert _check_no_shutdown_warning (new_port , caplog .text ) is True
7498 caplog .clear ()
7599
76100 # And now try to close the new_modeler... this will NOT throw a warning
77101 new_modeler .client .close ()
78102
79- msg = f"WARNING localhost:{ new_port } :client.py:182 Geometry Service will not be shutdown since it was already running...\n " # noqa : E501
80- assert msg not in caplog .text
103+ assert _check_no_shutdown_warning (new_port , caplog .text ) is False
81104 caplog .clear ()
82105
83106
84- def test_try_deploying_container_with_same_name (modeler : Modeler , caplog : pytest .LogCaptureFixture ):
107+ def test_try_deploying_container_with_same_name (
108+ modeler : Modeler , caplog : pytest .LogCaptureFixture , docker_instance : LocalDockerInstance
109+ ):
85110 """Checks that an error is raised when trying to deploy a container
86111 with a name that already exists."""
112+ if not docker_instance :
113+ pytest .skip ("Docker local launcher tests are not runnable." )
87114
88115 # Get the existing target
89116 target = modeler .client .target ().split (":" )
@@ -101,8 +128,7 @@ def test_try_deploying_container_with_same_name(modeler: Modeler, caplog: pytest
101128 )
102129
103130 # Check that the warning is NOT raised
104- msg = f"WARNING PyGeometry_global:localinstance.py:155 Service already running at port { new_port } ...\n " # noqa : E501
105- assert msg not in caplog .text
131+ assert _check_service_already_running (new_port , caplog .text ) is False
106132 caplog .clear ()
107133
108134 # Now try launching a service in a new port (where no service is available) but
@@ -119,7 +145,5 @@ def test_try_deploying_container_with_same_name(modeler: Modeler, caplog: pytest
119145
120146 # And now try to close the new_modeler... this will NOT throw a warning
121147 new_modeler .client .close ()
122-
123- msg = f"WARNING localhost:{ new_port } :client.py:182 Geometry Service will not be shutdown since it was already running...\n " # noqa : E501
124- assert msg not in caplog .text
148+ assert _check_no_shutdown_warning (new_port , caplog .text ) is False
125149 caplog .clear ()
0 commit comments