1
1
"""Testing module for the local launcher."""
2
2
3
+ import re
4
+
3
5
import pytest
4
6
5
7
from ansys .geometry .core import Modeler
6
8
from ansys .geometry .core .connection import LocalDockerInstance , launch_local_modeler
7
9
8
10
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
+
9
29
def test_if_docker_is_installed ():
10
30
"""Simple test to check if Docker is installed in the machine."""
11
31
assert LocalDockerInstance .is_docker_installed ()
12
32
13
33
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
+ ):
15
37
"""Checking connection to existing service using launch modeler."""
38
+ if not docker_instance :
39
+ pytest .skip ("Docker local launcher tests are not runnable." )
40
+
16
41
# Get the existing target
17
42
target = modeler .client .target ().split (":" )
18
43
port = int (target [1 ])
@@ -25,20 +50,23 @@ def test_local_launcher_connect(modeler: Modeler, caplog: pytest.LogCaptureFixtu
25
50
local_modeler = launch_local_modeler (
26
51
port = port , connect_to_existing_service = True , restart_if_existing_service = False
27
52
)
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 ()
31
55
32
56
# Try to close it... this will throw a warning
33
57
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 ()
37
60
38
61
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
+ ):
40
65
"""Checking connection to existing service using launch modeler and
41
66
restarting existing service."""
67
+ if not docker_instance :
68
+ pytest .skip ("Docker local launcher tests are not runnable." )
69
+
42
70
# Get the existing target
43
71
target = modeler .client .target ().split (":" )
44
72
port = int (target [1 ])
@@ -50,8 +78,7 @@ def test_local_launcher_connect_with_restart(modeler: Modeler, caplog: pytest.Lo
50
78
)
51
79
52
80
# 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
55
82
caplog .clear ()
56
83
57
84
# Connect to the previous modeler and restart it
@@ -60,30 +87,30 @@ def test_local_launcher_connect_with_restart(modeler: Modeler, caplog: pytest.Lo
60
87
)
61
88
62
89
# 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
67
92
caplog .clear ()
68
93
69
94
# Try to close the new_modeler_restarted... this will throw a warning
70
95
new_modeler_restarted .client .close ()
71
96
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
74
98
caplog .clear ()
75
99
76
100
# And now try to close the new_modeler... this will NOT throw a warning
77
101
new_modeler .client .close ()
78
102
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
81
104
caplog .clear ()
82
105
83
106
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
+ ):
85
110
"""Checks that an error is raised when trying to deploy a container
86
111
with a name that already exists."""
112
+ if not docker_instance :
113
+ pytest .skip ("Docker local launcher tests are not runnable." )
87
114
88
115
# Get the existing target
89
116
target = modeler .client .target ().split (":" )
@@ -101,8 +128,7 @@ def test_try_deploying_container_with_same_name(modeler: Modeler, caplog: pytest
101
128
)
102
129
103
130
# 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
106
132
caplog .clear ()
107
133
108
134
# 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
119
145
120
146
# And now try to close the new_modeler... this will NOT throw a warning
121
147
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
125
149
caplog .clear ()
0 commit comments