Commit | Line | Data |
---|---|---|
d907165c SM |
1 | # Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com> |
2 | # | |
3 | # This program is free software; you can redistribute it and/or | |
4 | # modify it under the terms of the GNU General Public License | |
5 | # as published by the Free Software Foundation; only version 2 | |
6 | # of the License. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program; if not, write to the Free Software | |
15 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
16 | ||
17 | import unittest | |
18 | import bt2 | |
19 | import os | |
a38d7650 | 20 | import re |
f2bad367 | 21 | from pathlib import PureWindowsPath, PurePosixPath |
d907165c SM |
22 | |
23 | ||
bbff0ab4 | 24 | test_ctf_traces_path = os.environ['BT_CTF_TRACES_PATH'] |
d907165c SM |
25 | |
26 | ||
1d4ac4b6 FD |
27 | # Key to sort streams in a predictable order. |
28 | def sort_predictably(stream): | |
29 | if 'range-ns' in stream: | |
30 | return stream['range-ns']['begin'] | |
31 | else: | |
32 | return stream['paths'][0] | |
d907165c SM |
33 | |
34 | ||
35 | class QueryTraceInfoClockOffsetTestCase(unittest.TestCase): | |
d907165c SM |
36 | def setUp(self): |
37 | ctf = bt2.find_plugin('ctf') | |
38 | self._fs = ctf.source_component_classes['fs'] | |
39 | ||
73760435 | 40 | self._inputs = [ |
cfbd7cf3 FD |
41 | os.path.join(test_ctf_traces_path, 'intersection', '3eventsintersect') |
42 | ] | |
d907165c SM |
43 | |
44 | def _check(self, trace, offset): | |
45 | self.assertEqual(trace['range-ns']['begin'], 13515309000000000 + offset) | |
46 | self.assertEqual(trace['range-ns']['end'], 13515309000000120 + offset) | |
cfbd7cf3 FD |
47 | self.assertEqual( |
48 | trace['intersection-range-ns']['begin'], 13515309000000070 + offset | |
49 | ) | |
50 | self.assertEqual( | |
51 | trace['intersection-range-ns']['end'], 13515309000000100 + offset | |
52 | ) | |
d907165c | 53 | |
1d4ac4b6 | 54 | streams = sorted(trace['streams'], key=sort_predictably) |
d907165c SM |
55 | self.assertEqual(streams[0]['range-ns']['begin'], 13515309000000000 + offset) |
56 | self.assertEqual(streams[0]['range-ns']['end'], 13515309000000100 + offset) | |
57 | self.assertEqual(streams[1]['range-ns']['begin'], 13515309000000070 + offset) | |
58 | self.assertEqual(streams[1]['range-ns']['end'], 13515309000000120 + offset) | |
59 | ||
60 | # Test various cominations of the clock-class-offset-s and | |
1a29b831 | 61 | # clock-class-offset-ns parameters to babeltrace.trace-info queries. |
d907165c SM |
62 | |
63 | # Without clock class offset | |
64 | ||
65 | def test_no_clock_class_offset(self): | |
3c729b9a | 66 | res = bt2.QueryExecutor( |
1a29b831 | 67 | self._fs, 'babeltrace.trace-info', {'inputs': self._inputs} |
3c729b9a | 68 | ).query() |
d907165c SM |
69 | trace = res[0] |
70 | self._check(trace, 0) | |
71 | ||
72 | # With clock-class-offset-s | |
73 | ||
74 | def test_clock_class_offset_s(self): | |
3c729b9a | 75 | res = bt2.QueryExecutor( |
1a29b831 PP |
76 | self._fs, |
77 | 'babeltrace.trace-info', | |
78 | {'inputs': self._inputs, 'clock-class-offset-s': 2}, | |
3c729b9a | 79 | ).query() |
d907165c SM |
80 | trace = res[0] |
81 | self._check(trace, 2000000000) | |
82 | ||
83 | # With clock-class-offset-ns | |
84 | ||
85 | def test_clock_class_offset_ns(self): | |
3c729b9a | 86 | res = bt2.QueryExecutor( |
1a29b831 PP |
87 | self._fs, |
88 | 'babeltrace.trace-info', | |
89 | {'inputs': self._inputs, 'clock-class-offset-ns': 2}, | |
3c729b9a | 90 | ).query() |
d907165c SM |
91 | trace = res[0] |
92 | self._check(trace, 2) | |
93 | ||
94 | # With both, negative | |
95 | ||
96 | def test_clock_class_offset_both(self): | |
3c729b9a | 97 | res = bt2.QueryExecutor( |
cfbd7cf3 | 98 | self._fs, |
1a29b831 | 99 | 'babeltrace.trace-info', |
cfbd7cf3 | 100 | { |
73760435 | 101 | 'inputs': self._inputs, |
cfbd7cf3 FD |
102 | 'clock-class-offset-s': -2, |
103 | 'clock-class-offset-ns': -2, | |
104 | }, | |
3c729b9a | 105 | ).query() |
d907165c SM |
106 | trace = res[0] |
107 | self._check(trace, -2000000002) | |
108 | ||
109 | def test_clock_class_offset_s_wrong_type(self): | |
a635e507 | 110 | with self.assertRaises(bt2._Error): |
3c729b9a | 111 | bt2.QueryExecutor( |
cfbd7cf3 | 112 | self._fs, |
1a29b831 | 113 | 'babeltrace.trace-info', |
73760435 | 114 | {'inputs': self._inputs, 'clock-class-offset-s': "2"}, |
3c729b9a | 115 | ).query() |
d907165c SM |
116 | |
117 | def test_clock_class_offset_s_wrong_type_none(self): | |
a635e507 | 118 | with self.assertRaises(bt2._Error): |
3c729b9a | 119 | bt2.QueryExecutor( |
cfbd7cf3 | 120 | self._fs, |
1a29b831 | 121 | 'babeltrace.trace-info', |
73760435 | 122 | {'inputs': self._inputs, 'clock-class-offset-s': None}, |
3c729b9a | 123 | ).query() |
d907165c SM |
124 | |
125 | def test_clock_class_offset_ns_wrong_type(self): | |
a635e507 | 126 | with self.assertRaises(bt2._Error): |
3c729b9a | 127 | bt2.QueryExecutor( |
cfbd7cf3 | 128 | self._fs, |
1a29b831 | 129 | 'babeltrace.trace-info', |
73760435 | 130 | {'inputs': self._inputs, 'clock-class-offset-ns': "2"}, |
3c729b9a | 131 | ).query() |
d907165c SM |
132 | |
133 | def test_clock_class_offset_ns_wrong_type_none(self): | |
a635e507 | 134 | with self.assertRaises(bt2._Error): |
3c729b9a | 135 | bt2.QueryExecutor( |
cfbd7cf3 | 136 | self._fs, |
1a29b831 | 137 | 'babeltrace.trace-info', |
73760435 | 138 | {'inputs': self._inputs, 'clock-class-offset-ns': None}, |
3c729b9a | 139 | ).query() |
d907165c | 140 | |
a38d7650 SM |
141 | |
142 | class QueryTraceInfoPortNameTestCase(unittest.TestCase): | |
143 | def setUp(self): | |
144 | ctf = bt2.find_plugin("ctf") | |
145 | self._fs = ctf.source_component_classes["fs"] | |
146 | ||
a38d7650 | 147 | def test_trace_uuid_stream_class_id_no_stream_id(self): |
3c729b9a | 148 | res = bt2.QueryExecutor( |
a38d7650 | 149 | self._fs, |
1a29b831 | 150 | "babeltrace.trace-info", |
a38d7650 | 151 | { |
73760435 | 152 | "inputs": [ |
a38d7650 SM |
153 | os.path.join( |
154 | test_ctf_traces_path, "intersection", "3eventsintersect" | |
155 | ) | |
156 | ] | |
157 | }, | |
3c729b9a | 158 | ).query() |
f2bad367 JR |
159 | |
160 | os_stream_path = PurePosixPath( | |
161 | '/tests/data/ctf-traces/intersection/3eventsintersect/' | |
162 | ) | |
163 | if os.environ['BT_OS_TYPE'] == 'mingw': | |
164 | os_stream_path = PureWindowsPath(os_stream_path) | |
165 | ||
a38d7650 SM |
166 | self.assertEqual(len(res), 1) |
167 | trace = res[0] | |
1d4ac4b6 | 168 | streams = sorted(trace["streams"], key=sort_predictably) |
a38d7650 SM |
169 | self.assertEqual(len(streams), 2) |
170 | self.assertRegexpMatches( | |
171 | str(streams[0]["port-name"]), | |
f2bad367 JR |
172 | r"^7afe8fbe-79b8-4f6a-bbc7-d0c782e7ddaf \| 0 \| .*" |
173 | + re.escape(str(os_stream_path / "test_stream_0")) | |
174 | + r"$", | |
a38d7650 SM |
175 | ) |
176 | self.assertRegexpMatches( | |
177 | str(streams[1]["port-name"]), | |
f2bad367 JR |
178 | r"^7afe8fbe-79b8-4f6a-bbc7-d0c782e7ddaf \| 0 \| .*" |
179 | + re.escape(str(os_stream_path / "test_stream_1")) | |
180 | + r"$", | |
a38d7650 SM |
181 | ) |
182 | ||
183 | def test_trace_uuid_no_stream_class_id_no_stream_id(self): | |
3c729b9a | 184 | res = bt2.QueryExecutor( |
a38d7650 | 185 | self._fs, |
1a29b831 | 186 | "babeltrace.trace-info", |
73760435 | 187 | {"inputs": [os.path.join(test_ctf_traces_path, "succeed", "succeed1")]}, |
3c729b9a | 188 | ).query() |
f2bad367 JR |
189 | |
190 | os_stream_path = PurePosixPath( | |
191 | '/tests/data/ctf-traces/succeed/succeed1/dummystream' | |
192 | ) | |
193 | if os.environ['BT_OS_TYPE'] == 'mingw': | |
194 | os_stream_path = PureWindowsPath(os_stream_path) | |
195 | ||
a38d7650 SM |
196 | self.assertEqual(len(res), 1) |
197 | trace = res[0] | |
1d4ac4b6 | 198 | streams = sorted(trace["streams"], key=sort_predictably) |
a38d7650 SM |
199 | self.assertEqual(len(streams), 1) |
200 | self.assertRegexpMatches( | |
201 | str(streams[0]["port-name"]), | |
f2bad367 JR |
202 | r"^2a6422d0-6cee-11e0-8c08-cb07d7b3a564 \| .*" |
203 | + re.escape(str(os_stream_path)) | |
204 | + r"$", | |
a38d7650 SM |
205 | ) |
206 | ||
207 | ||
1d4ac4b6 FD |
208 | class QueryTraceInfoRangeTestCase(unittest.TestCase): |
209 | def setUp(self): | |
210 | ctf = bt2.find_plugin("ctf") | |
211 | self._fs = ctf.source_component_classes["fs"] | |
212 | ||
1d4ac4b6 | 213 | def test_trace_no_range(self): |
1a29b831 PP |
214 | # This trace has no `timestamp_begin` and `timestamp_end` in its |
215 | # packet context. The `babeltrace.trace-info` query should omit | |
216 | # the `range-ns` fields in the `trace` and `stream` data | |
217 | # structures. | |
1d4ac4b6 | 218 | |
3c729b9a | 219 | res = bt2.QueryExecutor( |
1d4ac4b6 | 220 | self._fs, |
1a29b831 | 221 | "babeltrace.trace-info", |
73760435 | 222 | {"inputs": [os.path.join(test_ctf_traces_path, "succeed", "succeed1")]}, |
3c729b9a | 223 | ).query() |
1d4ac4b6 FD |
224 | |
225 | self.assertEqual(len(res), 1) | |
226 | trace = res[0] | |
227 | streams = trace["streams"] | |
228 | self.assertEqual(len(streams), 1) | |
229 | ||
230 | self.assertRaises(KeyError, lambda: trace['range-ns']) | |
231 | self.assertRaises(KeyError, lambda: streams[0]['range-ns']) | |
232 | ||
233 | ||
d907165c SM |
234 | if __name__ == '__main__': |
235 | unittest.main() |