-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
398 lines (307 loc) · 11.9 KB
/
conftest.py
File metadata and controls
398 lines (307 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# SPDX-FileCopyrightText: Alliander N. V.
#
# SPDX-License-Identifier: Apache-2.0
"""Global pytest fixtures for ROS 2 integration testing."""
import json
import os
import signal
import subprocess
import time
from typing import Generator, Iterator
import pytest
import rclpy
from _pytest.config import Config
from _pytest.config.argparsing import Parser
from _pytest.fixtures import SubRequest
from rclpy.node import Node
from termcolor import cprint
import utils
from predefined_configurations import PredefinedConfigurations
from start import Compose
LAUNCH_TIMEOUT = 90 # seconds
COMPOSE_FILE = "/alliander_robotics/compose_pytest.yml"
HOST_COMPOSE_FILE = "/alliander_robotics/compose.yml"
MAX_ROS_DOMAIN_ID = (
232 # https://docs.ros.org/en/jazzy/Concepts/Intermediate/About-Domain-ID.html
)
class Configurations:
"""Configurations used for pytest.
Attributes:
mode (str): The mode of testing.
changed_packages (set[str]): The set of packages that have changed.
ros_domain_id (int): ROS domain ID in which the test will be executed.
"""
mode: str
changed_packages: set[str]
ros_domain_id: int = 0
def pytest_addoption(parser: Parser) -> None:
"""Add custom command line options for pytest.
Args:
parser (Parser): The pytest parser to add options to.
"""
parser.addoption("--simulation", action="store", default="True")
parser.addoption("--mode", action="store", default="changed")
@pytest.hookimpl()
def pytest_sessionstart() -> None:
"""Run before the pytest session starts."""
Configurations.changed_packages = utils.get_changed_packages(verbose=True)
print(f"\nChanged packages: {Configurations.changed_packages}\n")
@pytest.fixture(scope="session", autouse=True)
def control_session(pytestconfig: Config) -> Generator:
"""Controls setup and teardown of the pytest session.
Args:
pytestconfig (Config): The pytest configuration object.
Yields:
Generator: Yields signal control to the test session.
"""
mode = pytestconfig.getoption("mode")
mode_options = ["changed", "all"]
if mode not in mode_options:
pytest.exit(
f"Invalid mode '{mode}' specified. Please choose from {mode_options}."
)
Configurations.mode = mode
orig = signal.signal(signal.SIGTERM, signal.getsignal(signal.SIGINT))
yield
print("\n")
cprint("All tests finished, stopping Docker containers...", "yellow")
stop_containers(COMPOSE_FILE)
signal.signal(signal.SIGTERM, orig)
@pytest.fixture(scope="class", autouse=True)
def control_class(request: SubRequest) -> Generator:
"""Controls the setup and teardown of a pytest class.
Args:
request (SubRequest): The pytest request object.
Yields:
Generator: Starts and stops Docker containers for each module.
"""
Configurations.ros_domain_id = (Configurations.ros_domain_id + 1) % (
MAX_ROS_DOMAIN_ID + 1
) # Loop back to ID 0 if new ID exceeds the allowed maximum
os.environ["ROS_DOMAIN_ID"] = f"{Configurations.ros_domain_id}"
print("")
cprint(f"[{request.cls.__name__}]: started", "blue")
services = create_compose_file(request)
if Configurations.mode != "all":
skip_if_no_changes(services)
pull_missing_images()
wait_for_removal_nodes()
process = start_containers(services)
yield
stop_containers(COMPOSE_FILE)
process.wait()
cprint(f"[{request.cls.__name__}]: finished", "blue")
@pytest.fixture(scope="function", autouse=True)
def control_function(request: SubRequest) -> Generator:
"""Control the setup and teardown of an individual test function.
Args:
request (SubRequest): The pytest request object.
Yields:
Generator: Yields start and end of each test function.
"""
cprint(f"Starting test: {request.node.name}", "blue")
yield
print("")
cprint(f"Finished test: {request.node.name}", "blue")
def create_compose_file(request: SubRequest) -> list:
"""Create a compose file for the group of tests.
Args:
request (SubRequest): The pytest request object.
Returns:
list: The list of services defined in the compose file.
"""
compose = Compose(Configurations.ros_domain_id)
if os.getenv("NO_NVIDIA", default="false").lower() == "true":
compose.mode = "configuration-no-nvidia"
else:
compose.mode = "configuration"
compose.predefined_configuration = PredefinedConfigurations()
compose.visualization = False
compose.predefined_configuration.plat_conf.platforms = (
request.cls.platforms.values()
)
load_ui = os.getenv("GAZEBO_UI", default="false").lower() == "true"
world = getattr(request.cls, "world", "empty.sdf")
compose.predefined_configuration.sim_conf.load_ui = load_ui
compose.predefined_configuration.sim_conf.world = world
# Propagate dev mounts
dev_mounts = os.getenv("DEV_MOUNTS", default="false") == "true"
if dev_mounts:
compose.dev = True
host_cwd = os.getenv("HOST_CWD", default="/alliander")
home_dir = os.getenv("HOME_DIR", default="/root")
Compose.host_cwd = host_cwd
Compose.home_dir = home_dir
services = compose.create_compose(COMPOSE_FILE)
return services
def skip_if_no_changes(services: list) -> None:
"""Skip the test if no relevant packages have changed.
Args:
services (list): The list of services required for the test.
"""
required_packages = set(services)
required_packages.add("alliander_core")
if required_packages.isdisjoint(Configurations.changed_packages):
pytest.skip(reason="No relevant packages have changed.")
else:
print("\n")
print("Running test since the following relevant packages have changed:")
print(required_packages.intersection(Configurations.changed_packages))
print("\n")
def pull_missing_images() -> None:
"""Pull the missing Docker images required for the test."""
compose = json.loads(
subprocess.check_output(
f"docker compose -f {COMPOSE_FILE} config --format json".split(),
)
)
services = []
images = []
for service_name, service in compose["services"].items():
services.append(service_name)
images.append(service["image"])
# Create list of services of which the image needs to be pulled:
services_to_pull = []
for image, service in zip(images, services, strict=False):
try:
subprocess.check_output(
f"docker image inspect {image}".split(),
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
cprint(f"Image {image} is already available locally.", "green")
except subprocess.CalledProcessError:
cprint(f"Image {image} is not available locally.", "yellow")
services_to_pull.append(service)
# Pull the missing images:
cmd = f"docker compose -f {COMPOSE_FILE} pull {' '.join(services_to_pull)}"
if os.getenv("NO_NVIDIA", default="false").lower() == "true":
cmd += " --quiet"
if services_to_pull:
cprint(f"Pulling missing images: {services_to_pull}", "yellow")
subprocess.run(cmd, timeout=3600, shell=True, check=True)
def wait_for_removal_nodes() -> None:
"""Wait for all active nodes to be stopped before moving on."""
cprint("Checking ros2 node list before starting containers.", "blue")
while active_nodes := subprocess.check_output(
["ros2", "node", "list"],
stdin=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
):
cprint(
f"Waiting for the following nodes to exit: {active_nodes.decode('utf-8').strip()}",
"red",
)
time.sleep(1)
cprint("No more nodes active, moving on.", "blue")
def start_containers(services: list) -> subprocess.Popen:
"""Start the Docker containers for all services.
Args:
services (list): The list of services to start.
Returns:
subprocess.Popen: The process running the Docker containers.
"""
process = subprocess.Popen([f"docker compose -f {COMPOSE_FILE} up"], shell=True)
containers_started = False
start_time = time.time()
while not containers_started:
containers_started = check_containers_started(COMPOSE_FILE, services)
if time.time() - start_time > LAUNCH_TIMEOUT:
stop_containers(COMPOSE_FILE)
pytest.exit("Timeout waiting for containers to start. Exiting pytest.")
cprint("All containers are started, start testing!", "green")
return process
def stop_containers(compose_file: str) -> None:
"""Stop and remove Docker containers defined in the given compose file.
Args:
compose_file (str): The path to the Docker compose file.
"""
subprocess.run(
[f"docker compose -f {compose_file} down -t 1"], check=False, shell=True
)
subprocess.run(
[f"docker compose -f {compose_file} rm -fsv"], check=False, shell=True
)
def check_containers_started(compose_file: str, services: list) -> bool:
"""Check if the expected number of Docker containers are started.
Args:
compose_file (str): The path to the Docker compose file.
services (list): The list of expected running services.
Returns:
bool: True if all services are started.
"""
process = subprocess.run(
[
f"docker inspect -f='{{{{.Name}}}} {{{{.State.Health.Status}}}}' $(docker compose -f {compose_file} ps -q)"
],
check=False,
shell=True,
capture_output=True,
)
stdout = process.stdout.decode("utf-8").rstrip()
lines = stdout.split("\n")
statuses = {}
for line in lines:
try:
name, status = line.strip("/").split()
except ValueError:
continue
if name in services:
statuses[name] = status == "healthy"
if len(statuses) < len(services):
return False
return all(statuses.values())
@pytest.fixture(scope="class")
def test_node() -> Iterator[Node]:
"""Fixture to create a singleton node for testing.
This node is used to ensure that the ROS 2 context is initialized and can be used in tests.
Yields:
Node: The singleton node for testing.
"""
rclpy.init()
node = Node("test_node")
yield node
node.destroy_node()
if rclpy.ok():
rclpy.shutdown()
@pytest.fixture(scope="session")
def timeout(pytestconfig: Config) -> int:
"""Fixture to get the timeout value from pytest config return it.
Args:
pytestconfig (Config): The pytest configuration object.
Returns:
int: The timeout value in seconds.
"""
return int(int(pytestconfig.getini("timeout")) / 2)
@pytest.fixture(scope="session")
def joint_movement_tolerance() -> float:
"""Tolerance of testing joint movements.
This is the maximum allowed deviation for joint movements during tests.
Returns:
float: The tolerance value for joint movements.
"""
return 0.01
@pytest.fixture(scope="session")
def finger_joint_fault_tolerance() -> float:
"""Tolerance of testing finger joint movements.
This is the maximum allowed deviation for finger joint movements during tests.
Returns:
float: The tolerance value for finger joint movements.
"""
return 0.025
@pytest.fixture(scope="session")
def navigation_distance_tolerance() -> float:
"""Distance tolerance of testing navigation.
This is the maximum allowed deviation for navigation during tests.
Returns:
float: The tolerance value for navigation.
"""
return 0.25
@pytest.fixture(scope="session")
def navigation_degree_tolerance() -> float:
"""Latitude/Longitude degree tolerance of testing navigation.
This is the maximum allowed deviation for navigation during tests.
Returns:
float: The tolerance value for navigation.
"""
return 2.5e-6 # ~0.25 meters