-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathtest_record_qos_profiles.py
150 lines (133 loc) · 6.49 KB
/
test_record_qos_profiles.py
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
# Copyright 2020 Amazon.com, Inc. or its affiliates. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
from pathlib import Path
import re
import sys
import tempfile
import time
import unittest
from launch import LaunchDescription
from launch.actions import ExecuteProcess
import launch_testing
import launch_testing.actions
import launch_testing.asserts
import launch_testing.markers
import launch_testing.tools
import pytest
PROFILE_PATH = Path(__file__).parent / 'resources'
TEST_NODE = 'ros2bag_record_qos_profile_test_node'
TEST_NAMESPACE = 'ros2bag_record_qos_profile'
ERROR_STRING_MSG = 'ros2bag CLI did not produce the expected output'\
'\n Expected output pattern: {}\n Actual output: {}'
OUTPUT_WAIT_TIMEOUT = 10
SHUTDOWN_TIMEOUT = 5
@pytest.mark.rostest
@launch_testing.markers.keep_alive
def generate_test_description():
return LaunchDescription([launch_testing.actions.ReadyToTest()])
class TestRos2BagRecord(unittest.TestCase):
@classmethod
def setUpClass(cls, launch_service, proc_info, proc_output):
@contextlib.contextmanager
def launch_bag_command(self, arguments, **kwargs):
pkg_command_action = ExecuteProcess(
cmd=['ros2', 'bag', *arguments],
additional_env={'PYTHONUNBUFFERED': '1'},
name='ros2bag-cli',
output='screen',
**kwargs
)
with launch_testing.tools.launch_process(
launch_service, pkg_command_action, proc_info, proc_output
) as pkg_command:
yield pkg_command
cls.launch_bag_command = launch_bag_command
cls.tmpdir = tempfile.TemporaryDirectory()
@classmethod
def tearDownClass(cls):
try:
cls.tmpdir.cleanup()
except OSError:
if sys.platform != 'win32':
raise
# HACK to allow Windows to close pending file handles
time.sleep(3)
cls.tmpdir.cleanup()
def test_qos_simple(self):
profile_path = PROFILE_PATH / 'qos_profile.yaml'
output_path = Path(self.tmpdir.name) / 'ros2bag_test_basic'
arguments = ['record', '-a', '--qos-profile-overrides-path', profile_path.as_posix(),
'--output', output_path.as_posix()]
expected_string_regex = re.compile(
r'\[rosbag2_storage]: Opened database .* for READ_WRITE')
with self.launch_bag_command(arguments=arguments) as bag_command:
bag_command.wait_for_output(
condition=lambda output: expected_string_regex.search(output) is not None,
timeout=OUTPUT_WAIT_TIMEOUT)
bag_command.wait_for_shutdown(timeout=SHUTDOWN_TIMEOUT)
assert bag_command.terminated
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
def test_incomplete_qos_profile(self):
profile_path = PROFILE_PATH / 'incomplete_qos_profile.yaml'
output_path = Path(self.tmpdir.name) / 'ros2bag_test_incomplete'
arguments = ['record', '-a', '--qos-profile-overrides-path', profile_path.as_posix(),
'--output', output_path.as_posix()]
expected_string_regex = re.compile(
r'\[rosbag2_storage]: Opened database .* for READ_WRITE')
with self.launch_bag_command(arguments=arguments) as bag_command:
bag_command.wait_for_output(
condition=lambda output: expected_string_regex.search(output) is not None,
timeout=OUTPUT_WAIT_TIMEOUT)
bag_command.wait_for_shutdown(timeout=SHUTDOWN_TIMEOUT)
assert bag_command.terminated
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
def test_incomplete_qos_duration(self):
profile_path = PROFILE_PATH / 'incomplete_qos_duration.yaml'
output_path = Path(self.tmpdir.name) / 'ros2bag_test_incomplete_duration'
arguments = ['record', '-a', '--qos-profile-overrides-path', profile_path.as_posix(),
'--output', output_path.as_posix()]
expected_string_regex = re.compile(
r'\[ERROR] \[ros2bag]: Time overrides must include both')
with self.launch_bag_command(arguments=arguments) as bag_command:
bag_command.wait_for_output(
condition=lambda output: expected_string_regex.search(output) is not None,
timeout=OUTPUT_WAIT_TIMEOUT)
bag_command.wait_for_shutdown(timeout=SHUTDOWN_TIMEOUT)
assert bag_command.terminated
assert bag_command.exit_code != launch_testing.asserts.EXIT_OK
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))
def test_nonexistent_qos_profile(self):
profile_path = PROFILE_PATH / 'foobar.yaml'
output_path = Path(self.tmpdir.name) / 'ros2bag_test_nonexistent'
arguments = ['record', '-a', '--qos-profile-overrides-path', profile_path.as_posix(),
'--output', output_path.as_posix()]
expected_string_regex = re.compile(
r'ros2 bag record: error: argument --qos-profile-overrides-path: can\'t open')
with self.launch_bag_command(arguments=arguments) as bag_command:
bag_command.wait_for_output(
condition=lambda output: expected_string_regex.search(output) is not None,
timeout=OUTPUT_WAIT_TIMEOUT)
bag_command.wait_for_shutdown(timeout=SHUTDOWN_TIMEOUT)
assert bag_command.terminated
assert bag_command.exit_code != launch_testing.asserts.EXIT_OK
matches = expected_string_regex.search(bag_command.output)
assert matches, print(
ERROR_STRING_MSG.format(expected_string_regex.pattern, bag_command.output))