Commit | Line | Data |
---|---|---|
32d2d479 MJ |
1 | # |
2 | # Copyright (C) 2019 EfficiOS Inc. | |
3 | # | |
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; only version 2 | |
7 | # of the License. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | # | |
18 | ||
cc261e29 PP |
19 | import unittest |
20 | import datetime | |
cc261e29 PP |
21 | import bt2 |
22 | import os | |
23 | import os.path | |
24 | ||
25 | ||
94e72386 | 26 | _BT_TESTS_DATADIR = os.environ['BT_TESTS_DATADIR'] |
b7c5d194 | 27 | _BT_CTF_TRACES_PATH = os.environ['BT_CTF_TRACES_PATH'] |
61d96b89 FD |
28 | _3EVENTS_INTERSECT_TRACE_PATH = os.path.join( |
29 | _BT_CTF_TRACES_PATH, 'intersection', '3eventsintersect' | |
30 | ) | |
94e72386 SM |
31 | _NOINTERSECT_TRACE_PATH = os.path.join( |
32 | _BT_CTF_TRACES_PATH, 'intersection', 'nointersect' | |
33 | ) | |
34 | _SEQUENCE_TRACE_PATH = os.path.join(_BT_CTF_TRACES_PATH, 'succeed', 'sequence') | |
35 | _AUTO_SOURCE_DISCOVERY_GROUPING_PATH = os.path.join( | |
36 | _BT_TESTS_DATADIR, 'auto-source-discovery', 'grouping' | |
37 | ) | |
38 | _AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH = os.path.join( | |
39 | _BT_TESTS_DATADIR, 'auto-source-discovery', 'params-log-level' | |
40 | ) | |
cc261e29 | 41 | |
a25d8f4c SM |
42 | _METADATA_SYNTAX_ERROR_TRACE_PATH = os.path.join( |
43 | _BT_CTF_TRACES_PATH, "fail", "metadata-syntax-error" | |
44 | ) | |
45 | ||
cc261e29 | 46 | |
3bd6bc48 SM |
47 | class _SomeSource( |
48 | bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator | |
49 | ): | |
50 | pass | |
51 | ||
52 | ||
53 | class _SomeFilter( | |
54 | bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator | |
55 | ): | |
56 | pass | |
57 | ||
58 | ||
59 | class _SomeSink(bt2._UserSinkComponent): | |
60 | def _user_consume(self): | |
61 | pass | |
62 | ||
63 | ||
88fdcc33 | 64 | class ComponentSpecTestCase(unittest.TestCase): |
3bd6bc48 SM |
65 | def setUp(self): |
66 | # A source CC from a plugin. | |
67 | self._dmesg_cc = bt2.find_plugin('text').source_component_classes['dmesg'] | |
68 | assert self._dmesg_cc is not None | |
69 | ||
70 | # A filter CC from a plugin. | |
71 | self._muxer_cc = bt2.find_plugin('utils').filter_component_classes['muxer'] | |
72 | assert self._muxer_cc is not None | |
73 | ||
74 | # A sink CC from a plugin. | |
75 | self._pretty_cc = bt2.find_plugin('text').sink_component_classes['pretty'] | |
76 | assert self._pretty_cc is not None | |
77 | ||
78 | def test_create_source_from_name(self): | |
79 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class('text', 'dmesg') | |
80 | self.assertEqual(spec.component_class.name, 'dmesg') | |
81 | ||
82 | def test_create_source_from_plugin(self): | |
83 | spec = bt2.ComponentSpec(self._dmesg_cc) | |
84 | self.assertEqual(spec.component_class.name, 'dmesg') | |
85 | ||
86 | def test_create_source_from_user(self): | |
87 | spec = bt2.ComponentSpec(_SomeSource) | |
88 | self.assertEqual(spec.component_class.name, '_SomeSource') | |
89 | ||
90 | def test_create_filter_from_name(self): | |
91 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class('utils', 'muxer') | |
92 | self.assertEqual(spec.component_class.name, 'muxer') | |
93 | ||
94 | def test_create_filter_from_object(self): | |
95 | spec = bt2.ComponentSpec(self._muxer_cc) | |
96 | self.assertEqual(spec.component_class.name, 'muxer') | |
97 | ||
98 | def test_create_sink_from_name(self): | |
99 | with self.assertRaisesRegex( | |
100 | KeyError, | |
101 | 'source or filter component class `pretty` not found in plugin `text`', | |
102 | ): | |
103 | bt2.ComponentSpec.from_named_plugin_and_component_class('text', 'pretty') | |
104 | ||
105 | def test_create_sink_from_object(self): | |
106 | with self.assertRaisesRegex( | |
5265f5e3 FD |
107 | TypeError, |
108 | "'_SinkComponentClassConst' is not a source or filter component class", | |
3bd6bc48 SM |
109 | ): |
110 | bt2.ComponentSpec(self._pretty_cc) | |
111 | ||
112 | def test_create_from_object_with_params(self): | |
113 | spec = bt2.ComponentSpec(self._dmesg_cc, {'salut': 23}) | |
114 | self.assertEqual(spec.params['salut'], 23) | |
115 | ||
116 | def test_create_from_name_with_params(self): | |
117 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class( | |
118 | 'text', 'dmesg', {'salut': 23} | |
119 | ) | |
120 | self.assertEqual(spec.params['salut'], 23) | |
cc261e29 | 121 | |
3bd6bc48 SM |
122 | def test_create_from_object_with_path_params(self): |
123 | spec = spec = bt2.ComponentSpec(self._dmesg_cc, 'a path') | |
124 | self.assertEqual(spec.params['inputs'], ['a path']) | |
cc261e29 | 125 | |
3bd6bc48 SM |
126 | def test_create_from_name_with_path_params(self): |
127 | spec = spec = bt2.ComponentSpec.from_named_plugin_and_component_class( | |
128 | 'text', 'dmesg', 'a path' | |
129 | ) | |
a1040187 | 130 | self.assertEqual(spec.params['inputs'], ['a path']) |
cc261e29 | 131 | |
3bd6bc48 SM |
132 | def test_create_wrong_comp_class_type(self): |
133 | with self.assertRaisesRegex( | |
134 | TypeError, "'int' is not a source or filter component class" | |
135 | ): | |
136 | bt2.ComponentSpec(18) | |
cc261e29 | 137 | |
3bd6bc48 SM |
138 | def test_create_from_name_wrong_plugin_name_type(self): |
139 | with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"): | |
140 | bt2.ComponentSpec.from_named_plugin_and_component_class(23, 'compcls') | |
141 | ||
142 | def test_create_from_name_non_existent_plugin(self): | |
143 | with self.assertRaisesRegex( | |
144 | ValueError, "no such plugin: this_plugin_does_not_exist" | |
145 | ): | |
146 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
147 | 'this_plugin_does_not_exist', 'compcls' | |
148 | ) | |
149 | ||
150 | def test_create_from_name_wrong_component_class_name_type(self): | |
151 | with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"): | |
152 | bt2.ComponentSpec.from_named_plugin_and_component_class('utils', 190) | |
cc261e29 PP |
153 | |
154 | def test_create_wrong_params_type(self): | |
3bd6bc48 SM |
155 | with self.assertRaisesRegex( |
156 | TypeError, "cannot create value object from 'datetime' object" | |
157 | ): | |
158 | bt2.ComponentSpec(self._dmesg_cc, params=datetime.datetime.now()) | |
159 | ||
160 | def test_create_from_name_wrong_params_type(self): | |
161 | with self.assertRaisesRegex( | |
162 | TypeError, "cannot create value object from 'datetime' object" | |
163 | ): | |
164 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
165 | 'text', 'dmesg', datetime.datetime.now() | |
166 | ) | |
167 | ||
168 | def test_create_wrong_log_level_type(self): | |
169 | with self.assertRaisesRegex(TypeError, "'str' is not an 'int' object"): | |
170 | bt2.ComponentSpec(self._dmesg_cc, logging_level='banane') | |
171 | ||
172 | def test_create_from_name_wrong_log_level_type(self): | |
173 | with self.assertRaisesRegex(TypeError, "'str' is not an 'int' object"): | |
174 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
175 | 'text', 'dmesg', logging_level='banane' | |
176 | ) | |
da35796c SM |
177 | |
178 | ||
179 | # Return a map, msg type -> number of messages of this type. | |
180 | ||
61d96b89 | 181 | |
da35796c SM |
182 | def _count_msgs_by_type(msgs): |
183 | res = {} | |
184 | ||
185 | for msg in msgs: | |
186 | t = type(msg) | |
187 | n = res.get(t, 0) | |
188 | res[t] = n + 1 | |
189 | ||
190 | return res | |
cc261e29 PP |
191 | |
192 | ||
fa4c33e3 | 193 | class TraceCollectionMessageIteratorTestCase(unittest.TestCase): |
cc261e29 | 194 | def test_create_wrong_stream_intersection_mode_type(self): |
3bd6bc48 SM |
195 | specs = [ |
196 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
197 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
198 | ) | |
199 | ] | |
cc261e29 PP |
200 | |
201 | with self.assertRaises(TypeError): | |
da35796c | 202 | bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=23) |
cc261e29 PP |
203 | |
204 | def test_create_wrong_begin_type(self): | |
3bd6bc48 SM |
205 | specs = [ |
206 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
207 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
208 | ) | |
209 | ] | |
cc261e29 PP |
210 | |
211 | with self.assertRaises(TypeError): | |
da35796c | 212 | bt2.TraceCollectionMessageIterator(specs, begin='hi') |
cc261e29 PP |
213 | |
214 | def test_create_wrong_end_type(self): | |
3bd6bc48 SM |
215 | specs = [ |
216 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
217 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
218 | ) | |
219 | ] | |
cc261e29 PP |
220 | |
221 | with self.assertRaises(TypeError): | |
da35796c | 222 | bt2.TraceCollectionMessageIterator(specs, begin='lel') |
cc261e29 | 223 | |
cc261e29 | 224 | def test_create_begin_s(self): |
3bd6bc48 SM |
225 | specs = [ |
226 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
227 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
228 | ) | |
229 | ] | |
da35796c | 230 | bt2.TraceCollectionMessageIterator(specs, begin=19457.918232) |
cc261e29 PP |
231 | |
232 | def test_create_end_s(self): | |
3bd6bc48 SM |
233 | specs = [ |
234 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
235 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
236 | ) | |
237 | ] | |
da35796c | 238 | bt2.TraceCollectionMessageIterator(specs, end=123.12312) |
cc261e29 PP |
239 | |
240 | def test_create_begin_datetime(self): | |
3bd6bc48 SM |
241 | specs = [ |
242 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
243 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
244 | ) | |
245 | ] | |
da35796c | 246 | bt2.TraceCollectionMessageIterator(specs, begin=datetime.datetime.now()) |
cc261e29 PP |
247 | |
248 | def test_create_end_datetime(self): | |
3bd6bc48 SM |
249 | specs = [ |
250 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
251 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
252 | ) | |
253 | ] | |
da35796c | 254 | bt2.TraceCollectionMessageIterator(specs, end=datetime.datetime.now()) |
cc261e29 PP |
255 | |
256 | def test_iter_no_intersection(self): | |
3bd6bc48 SM |
257 | specs = [ |
258 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
259 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
260 | ) | |
261 | ] | |
fa4c33e3 | 262 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
da35796c | 263 | msgs = list(msg_iter) |
b7cbc799 | 264 | self.assertEqual(len(msgs), 28) |
da35796c | 265 | hist = _count_msgs_by_type(msgs) |
9cbe0c59 | 266 | self.assertEqual(hist[bt2._EventMessageConst], 8) |
cc261e29 | 267 | |
da35796c | 268 | # Same as the above, but we pass a single spec instead of a spec list. |
88fdcc33 | 269 | def test_iter_specs_not_list(self): |
3bd6bc48 SM |
270 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class( |
271 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
272 | ) | |
da35796c SM |
273 | msg_iter = bt2.TraceCollectionMessageIterator(spec) |
274 | msgs = list(msg_iter) | |
b7cbc799 | 275 | self.assertEqual(len(msgs), 28) |
da35796c | 276 | hist = _count_msgs_by_type(msgs) |
9cbe0c59 | 277 | self.assertEqual(hist[bt2._EventMessageConst], 8) |
88fdcc33 PP |
278 | |
279 | def test_iter_custom_filter(self): | |
3bd6bc48 SM |
280 | src_spec = bt2.ComponentSpec.from_named_plugin_and_component_class( |
281 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
282 | ) | |
283 | flt_spec = bt2.ComponentSpec.from_named_plugin_and_component_class( | |
284 | 'utils', 'trimmer', {'end': '13515309.000000075'} | |
285 | ) | |
da35796c SM |
286 | msg_iter = bt2.TraceCollectionMessageIterator(src_spec, flt_spec) |
287 | hist = _count_msgs_by_type(msg_iter) | |
9cbe0c59 | 288 | self.assertEqual(hist[bt2._EventMessageConst], 5) |
88fdcc33 | 289 | |
cc261e29 | 290 | def test_iter_intersection(self): |
3bd6bc48 SM |
291 | specs = [ |
292 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
293 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
294 | ) | |
295 | ] | |
61d96b89 FD |
296 | msg_iter = bt2.TraceCollectionMessageIterator( |
297 | specs, stream_intersection_mode=True | |
298 | ) | |
da35796c | 299 | msgs = list(msg_iter) |
b7cbc799 | 300 | self.assertEqual(len(msgs), 15) |
da35796c | 301 | hist = _count_msgs_by_type(msgs) |
9cbe0c59 | 302 | self.assertEqual(hist[bt2._EventMessageConst], 3) |
cc261e29 | 303 | |
8762085c SM |
304 | def test_iter_intersection_params(self): |
305 | # Check that all params used to create the source component are passed | |
9db4399f | 306 | # to the `babeltrace.trace-infos` query. |
3bd6bc48 SM |
307 | specs = [ |
308 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
8762085c SM |
309 | 'ctf', |
310 | 'fs', | |
311 | { | |
312 | 'inputs': [_3EVENTS_INTERSECT_TRACE_PATH], | |
313 | 'clock-class-offset-s': 1000, | |
314 | }, | |
3bd6bc48 SM |
315 | ) |
316 | ] | |
cc261e29 | 317 | |
8762085c SM |
318 | msg_iter = bt2.TraceCollectionMessageIterator( |
319 | specs, stream_intersection_mode=True | |
320 | ) | |
321 | ||
9cbe0c59 | 322 | event_msgs = [x for x in msg_iter if type(x) is bt2._EventMessageConst] |
8762085c SM |
323 | self.assertEqual(len(event_msgs), 3) |
324 | self.assertEqual( | |
325 | event_msgs[0].default_clock_snapshot.ns_from_origin, 13516309000000071 | |
326 | ) | |
327 | self.assertEqual( | |
328 | event_msgs[1].default_clock_snapshot.ns_from_origin, 13516309000000072 | |
329 | ) | |
330 | self.assertEqual( | |
331 | event_msgs[2].default_clock_snapshot.ns_from_origin, 13516309000000082 | |
332 | ) | |
cc261e29 PP |
333 | |
334 | def test_iter_no_intersection_two_traces(self): | |
3bd6bc48 SM |
335 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class( |
336 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
337 | ) | |
cc261e29 | 338 | specs = [spec, spec] |
fa4c33e3 | 339 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
da35796c | 340 | msgs = list(msg_iter) |
b7cbc799 | 341 | self.assertEqual(len(msgs), 56) |
da35796c | 342 | hist = _count_msgs_by_type(msgs) |
9cbe0c59 | 343 | self.assertEqual(hist[bt2._EventMessageConst], 16) |
cc261e29 PP |
344 | |
345 | def test_iter_no_intersection_begin(self): | |
3bd6bc48 SM |
346 | specs = [ |
347 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
348 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
349 | ) | |
350 | ] | |
da35796c SM |
351 | msg_iter = bt2.TraceCollectionMessageIterator(specs, begin=13515309.000000023) |
352 | hist = _count_msgs_by_type(msg_iter) | |
9cbe0c59 | 353 | self.assertEqual(hist[bt2._EventMessageConst], 6) |
cc261e29 PP |
354 | |
355 | def test_iter_no_intersection_end(self): | |
3bd6bc48 SM |
356 | specs = [ |
357 | bt2.ComponentSpec.from_named_plugin_and_component_class( | |
358 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH | |
359 | ) | |
360 | ] | |
da35796c SM |
361 | msg_iter = bt2.TraceCollectionMessageIterator(specs, end=13515309.000000075) |
362 | hist = _count_msgs_by_type(msg_iter) | |
9cbe0c59 | 363 | self.assertEqual(hist[bt2._EventMessageConst], 5) |
94e72386 SM |
364 | |
365 | def test_iter_auto_source_component_spec(self): | |
366 | specs = [bt2.AutoSourceComponentSpec(_3EVENTS_INTERSECT_TRACE_PATH)] | |
367 | msg_iter = bt2.TraceCollectionMessageIterator(specs) | |
368 | msgs = list(msg_iter) | |
369 | self.assertEqual(len(msgs), 28) | |
370 | hist = _count_msgs_by_type(msgs) | |
9cbe0c59 | 371 | self.assertEqual(hist[bt2._EventMessageConst], 8) |
94e72386 SM |
372 | |
373 | def test_iter_auto_source_component_spec_list_of_strings(self): | |
374 | msg_iter = bt2.TraceCollectionMessageIterator([_3EVENTS_INTERSECT_TRACE_PATH]) | |
375 | msgs = list(msg_iter) | |
376 | self.assertEqual(len(msgs), 28) | |
377 | hist = _count_msgs_by_type(msgs) | |
9cbe0c59 | 378 | self.assertEqual(hist[bt2._EventMessageConst], 8) |
94e72386 SM |
379 | |
380 | def test_iter_auto_source_component_spec_string(self): | |
381 | msg_iter = bt2.TraceCollectionMessageIterator(_3EVENTS_INTERSECT_TRACE_PATH) | |
382 | msgs = list(msg_iter) | |
383 | self.assertEqual(len(msgs), 28) | |
384 | hist = _count_msgs_by_type(msgs) | |
9cbe0c59 | 385 | self.assertEqual(hist[bt2._EventMessageConst], 8) |
94e72386 SM |
386 | |
387 | def test_iter_mixed_inputs(self): | |
388 | msg_iter = bt2.TraceCollectionMessageIterator( | |
389 | [ | |
390 | _3EVENTS_INTERSECT_TRACE_PATH, | |
391 | bt2.AutoSourceComponentSpec(_SEQUENCE_TRACE_PATH), | |
3bd6bc48 SM |
392 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
393 | 'ctf', 'fs', _NOINTERSECT_TRACE_PATH | |
394 | ), | |
94e72386 SM |
395 | ] |
396 | ) | |
397 | msgs = list(msg_iter) | |
398 | self.assertEqual(len(msgs), 76) | |
399 | hist = _count_msgs_by_type(msgs) | |
9cbe0c59 | 400 | self.assertEqual(hist[bt2._EventMessageConst], 24) |
94e72386 | 401 | |
5f7f0be0 SM |
402 | def test_auto_source_component_non_existent(self): |
403 | with self.assertRaisesRegex( | |
404 | RuntimeError, | |
405 | 'Some auto source component specs did not produce any component', | |
406 | ): | |
407 | # Test with one path known to contain a trace and one path known | |
408 | # to not contain any trace. | |
409 | bt2.TraceCollectionMessageIterator( | |
410 | [_SEQUENCE_TRACE_PATH, '/this/path/better/not/exist'] | |
411 | ) | |
412 | ||
94e72386 SM |
413 | |
414 | class _TestAutoDiscoverSourceComponentSpecs(unittest.TestCase): | |
415 | def setUp(self): | |
416 | self._saved_babeltrace_plugin_path = os.environ['BABELTRACE_PLUGIN_PATH'] | |
d479cff8 | 417 | os.environ['BABELTRACE_PLUGIN_PATH'] += os.pathsep + self._plugin_path |
94e72386 SM |
418 | |
419 | def tearDown(self): | |
420 | os.environ['BABELTRACE_PLUGIN_PATH'] = self._saved_babeltrace_plugin_path | |
421 | ||
422 | ||
423 | class TestAutoDiscoverSourceComponentSpecsGrouping( | |
424 | _TestAutoDiscoverSourceComponentSpecs | |
425 | ): | |
426 | _plugin_path = _AUTO_SOURCE_DISCOVERY_GROUPING_PATH | |
427 | ||
428 | def test_grouping(self): | |
429 | specs = [ | |
430 | bt2.AutoSourceComponentSpec('ABCDE'), | |
431 | bt2.AutoSourceComponentSpec(_AUTO_SOURCE_DISCOVERY_GROUPING_PATH), | |
94e72386 SM |
432 | ] |
433 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 434 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
435 | |
436 | self.assertEqual(len(msgs), 8) | |
437 | ||
438 | self.assertEqual(msgs[0].stream.name, 'TestSourceABCDE: ABCDE') | |
439 | self.assertEqual(msgs[1].stream.name, 'TestSourceExt: aaa1, aaa2, aaa3') | |
440 | self.assertEqual(msgs[2].stream.name, 'TestSourceExt: bbb1, bbb2') | |
441 | self.assertEqual(msgs[3].stream.name, 'TestSourceExt: ccc1') | |
442 | self.assertEqual(msgs[4].stream.name, 'TestSourceExt: ccc2') | |
443 | self.assertEqual(msgs[5].stream.name, 'TestSourceExt: ccc3') | |
444 | self.assertEqual(msgs[6].stream.name, 'TestSourceExt: ccc4') | |
445 | self.assertEqual(msgs[7].stream.name, 'TestSourceSomeDir: some-dir') | |
446 | ||
447 | ||
448 | class TestAutoDiscoverSourceComponentSpecsParamsObjLogLevel( | |
449 | _TestAutoDiscoverSourceComponentSpecs | |
450 | ): | |
451 | _plugin_path = _AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH | |
452 | ||
453 | _dir_a = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-a') | |
454 | _dir_b = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-b') | |
455 | _dir_ab = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-ab') | |
456 | ||
457 | def _test_two_comps_from_one_spec(self, params, obj=None, logging_level=None): | |
458 | specs = [ | |
459 | bt2.AutoSourceComponentSpec( | |
460 | self._dir_ab, params=params, obj=obj, logging_level=logging_level | |
461 | ) | |
462 | ] | |
463 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 464 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
465 | |
466 | self.assertEqual(len(msgs), 2) | |
467 | ||
468 | return msgs | |
469 | ||
470 | def test_params_two_comps_from_one_spec(self): | |
471 | msgs = self._test_two_comps_from_one_spec( | |
472 | params={'test-allo': 'madame', 'what': 'test-params'} | |
473 | ) | |
474 | ||
475 | self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')") | |
476 | self.assertEqual(msgs[1].stream.name, "TestSourceB: ('test-allo', 'madame')") | |
477 | ||
478 | def test_obj_two_comps_from_one_spec(self): | |
479 | msgs = self._test_two_comps_from_one_spec( | |
480 | params={'what': 'python-obj'}, obj='deore' | |
481 | ) | |
482 | ||
483 | self.assertEqual(msgs[0].stream.name, "TestSourceA: deore") | |
484 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") | |
485 | ||
486 | def test_log_level_two_comps_from_one_spec(self): | |
487 | msgs = self._test_two_comps_from_one_spec( | |
488 | params={'what': 'log-level'}, logging_level=bt2.LoggingLevel.DEBUG | |
489 | ) | |
490 | ||
491 | self.assertEqual( | |
492 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG) | |
493 | ) | |
494 | self.assertEqual( | |
495 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG) | |
496 | ) | |
497 | ||
498 | def _test_two_comps_from_two_specs( | |
499 | self, | |
500 | params_a=None, | |
501 | params_b=None, | |
502 | obj_a=None, | |
503 | obj_b=None, | |
504 | logging_level_a=None, | |
505 | logging_level_b=None, | |
506 | ): | |
507 | specs = [ | |
508 | bt2.AutoSourceComponentSpec( | |
509 | self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a | |
510 | ), | |
511 | bt2.AutoSourceComponentSpec( | |
512 | self._dir_b, params=params_b, obj=obj_b, logging_level=logging_level_b | |
513 | ), | |
514 | ] | |
515 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 516 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
517 | |
518 | self.assertEqual(len(msgs), 2) | |
519 | ||
520 | return msgs | |
521 | ||
522 | def test_params_two_comps_from_two_specs(self): | |
523 | msgs = self._test_two_comps_from_two_specs( | |
524 | params_a={'test-allo': 'madame', 'what': 'test-params'}, | |
525 | params_b={'test-bonjour': 'monsieur', 'what': 'test-params'}, | |
526 | ) | |
527 | ||
528 | self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')") | |
529 | self.assertEqual( | |
530 | msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')" | |
531 | ) | |
532 | ||
533 | def test_obj_two_comps_from_two_specs(self): | |
534 | msgs = self._test_two_comps_from_two_specs( | |
535 | params_a={'what': 'python-obj'}, | |
536 | params_b={'what': 'python-obj'}, | |
537 | obj_a='deore', | |
538 | obj_b='alivio', | |
539 | ) | |
540 | ||
541 | self.assertEqual(msgs[0].stream.name, "TestSourceA: deore") | |
542 | self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio") | |
543 | ||
544 | def test_log_level_two_comps_from_two_specs(self): | |
545 | msgs = self._test_two_comps_from_two_specs( | |
546 | params_a={'what': 'log-level'}, | |
547 | params_b={'what': 'log-level'}, | |
548 | logging_level_a=bt2.LoggingLevel.DEBUG, | |
549 | logging_level_b=bt2.LoggingLevel.TRACE, | |
550 | ) | |
551 | ||
552 | self.assertEqual( | |
553 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG) | |
554 | ) | |
555 | self.assertEqual( | |
556 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE) | |
557 | ) | |
558 | ||
559 | def _test_one_comp_from_one_spec_one_comp_from_both_1( | |
560 | self, | |
561 | params_a=None, | |
562 | params_ab=None, | |
563 | obj_a=None, | |
564 | obj_ab=None, | |
565 | logging_level_a=None, | |
566 | logging_level_ab=None, | |
567 | ): | |
568 | specs = [ | |
569 | bt2.AutoSourceComponentSpec( | |
570 | self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a | |
571 | ), | |
572 | bt2.AutoSourceComponentSpec( | |
573 | self._dir_ab, | |
574 | params=params_ab, | |
575 | obj=obj_ab, | |
576 | logging_level=logging_level_ab, | |
577 | ), | |
578 | ] | |
579 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 580 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
581 | |
582 | self.assertEqual(len(msgs), 2) | |
583 | ||
584 | return msgs | |
585 | ||
586 | def test_params_one_comp_from_one_spec_one_comp_from_both_1(self): | |
587 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1( | |
588 | params_a={'test-allo': 'madame', 'what': 'test-params'}, | |
589 | params_ab={'test-bonjour': 'monsieur', 'what': 'test-params'}, | |
590 | ) | |
591 | ||
592 | self.assertEqual( | |
593 | msgs[0].stream.name, | |
594 | "TestSourceA: ('test-allo', 'madame'), ('test-bonjour', 'monsieur')", | |
595 | ) | |
596 | self.assertEqual( | |
597 | msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')" | |
598 | ) | |
599 | ||
600 | def test_obj_one_comp_from_one_spec_one_comp_from_both_1(self): | |
601 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1( | |
602 | params_a={'what': 'python-obj'}, | |
603 | params_ab={'what': 'python-obj'}, | |
604 | obj_a='deore', | |
605 | obj_ab='alivio', | |
606 | ) | |
607 | ||
608 | self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio") | |
609 | self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio") | |
610 | ||
611 | def test_log_level_one_comp_from_one_spec_one_comp_from_both_1(self): | |
612 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1( | |
613 | params_a={'what': 'log-level'}, | |
614 | params_ab={'what': 'log-level'}, | |
615 | logging_level_a=bt2.LoggingLevel.DEBUG, | |
616 | logging_level_ab=bt2.LoggingLevel.TRACE, | |
617 | ) | |
618 | ||
619 | self.assertEqual( | |
620 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE) | |
621 | ) | |
622 | self.assertEqual( | |
623 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE) | |
624 | ) | |
625 | ||
626 | def _test_one_comp_from_one_spec_one_comp_from_both_2( | |
627 | self, | |
628 | params_ab=None, | |
629 | params_a=None, | |
630 | obj_ab=None, | |
631 | obj_a=None, | |
632 | logging_level_ab=None, | |
633 | logging_level_a=None, | |
634 | ): | |
635 | specs = [ | |
636 | bt2.AutoSourceComponentSpec( | |
637 | self._dir_ab, | |
638 | params=params_ab, | |
639 | obj=obj_ab, | |
640 | logging_level=logging_level_ab, | |
641 | ), | |
642 | bt2.AutoSourceComponentSpec( | |
643 | self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a | |
644 | ), | |
645 | ] | |
646 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 647 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
648 | |
649 | self.assertEqual(len(msgs), 2) | |
650 | ||
651 | return msgs | |
652 | ||
653 | def test_params_one_comp_from_one_spec_one_comp_from_both_2(self): | |
654 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2( | |
655 | params_ab={ | |
656 | 'test-bonjour': 'madame', | |
657 | 'test-salut': 'les amis', | |
658 | 'what': 'test-params', | |
659 | }, | |
660 | params_a={'test-bonjour': 'monsieur', 'what': 'test-params'}, | |
661 | ) | |
662 | ||
663 | self.assertEqual( | |
664 | msgs[0].stream.name, | |
665 | "TestSourceA: ('test-bonjour', 'monsieur'), ('test-salut', 'les amis')", | |
666 | ) | |
667 | self.assertEqual( | |
668 | msgs[1].stream.name, | |
669 | "TestSourceB: ('test-bonjour', 'madame'), ('test-salut', 'les amis')", | |
670 | ) | |
671 | ||
672 | def test_obj_one_comp_from_one_spec_one_comp_from_both_2(self): | |
673 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2( | |
674 | params_ab={'what': 'python-obj'}, | |
675 | params_a={'what': 'python-obj'}, | |
676 | obj_ab='deore', | |
677 | obj_a='alivio', | |
678 | ) | |
679 | ||
680 | self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio") | |
681 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") | |
682 | ||
683 | def test_log_level_one_comp_from_one_spec_one_comp_from_both_2(self): | |
684 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2( | |
685 | params_ab={'what': 'log-level'}, | |
686 | params_a={'what': 'log-level'}, | |
687 | logging_level_ab=bt2.LoggingLevel.DEBUG, | |
688 | logging_level_a=bt2.LoggingLevel.TRACE, | |
689 | ) | |
690 | ||
691 | self.assertEqual( | |
692 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE) | |
693 | ) | |
694 | self.assertEqual( | |
695 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG) | |
696 | ) | |
697 | ||
698 | def test_obj_override_with_none(self): | |
699 | specs = [ | |
700 | bt2.AutoSourceComponentSpec( | |
701 | self._dir_ab, params={'what': 'python-obj'}, obj='deore' | |
702 | ), | |
703 | bt2.AutoSourceComponentSpec( | |
704 | self._dir_a, params={'what': 'python-obj'}, obj=None | |
705 | ), | |
706 | ] | |
707 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 708 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
709 | |
710 | self.assertEqual(len(msgs), 2) | |
711 | self.assertEqual(msgs[0].stream.name, "TestSourceA: None") | |
712 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") | |
713 | ||
714 | def test_obj_no_override_with_no_obj(self): | |
715 | specs = [ | |
716 | bt2.AutoSourceComponentSpec( | |
717 | self._dir_ab, params={'what': 'python-obj'}, obj='deore' | |
718 | ), | |
719 | bt2.AutoSourceComponentSpec(self._dir_a, params={'what': 'python-obj'}), | |
720 | ] | |
721 | it = bt2.TraceCollectionMessageIterator(specs) | |
9cbe0c59 | 722 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst] |
94e72386 SM |
723 | |
724 | self.assertEqual(len(msgs), 2) | |
725 | self.assertEqual(msgs[0].stream.name, "TestSourceA: deore") | |
726 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") | |
727 | ||
728 | ||
a25d8f4c SM |
729 | class TestAutoDiscoverFailures(unittest.TestCase): |
730 | def test_metadata_syntax_error(self): | |
731 | with self.assertRaisesRegex( | |
732 | bt2._Error, | |
733 | 'Component class\'s "query" method failed', | |
734 | ): | |
735 | specs = [bt2.AutoSourceComponentSpec(_METADATA_SYNTAX_ERROR_TRACE_PATH)] | |
736 | bt2.TraceCollectionMessageIterator(specs) | |
737 | ||
738 | ||
94e72386 SM |
739 | if __name__ == '__main__': |
740 | unittest.main() |