947c37b21acd412e58ed8465801c24f501942175
[babeltrace.git] / tests / bindings / python / bt2 / test_trace_collection_message_iterator.py
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
19 import unittest
20 import datetime
21 import bt2
22 import os
23 import os.path
24
25
26 _BT_TESTS_DATADIR = os.environ['BT_TESTS_DATADIR']
27 _BT_CTF_TRACES_PATH = os.environ['BT_CTF_TRACES_PATH']
28 _3EVENTS_INTERSECT_TRACE_PATH = os.path.join(
29 _BT_CTF_TRACES_PATH, 'intersection', '3eventsintersect'
30 )
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 )
41
42
43 class ComponentSpecTestCase(unittest.TestCase):
44 def test_create_good_no_params(self):
45 bt2.ComponentSpec('plugin', 'compcls')
46
47 def test_create_good_with_params(self):
48 bt2.ComponentSpec('plugin', 'compcls', {'salut': 23})
49
50 def test_create_good_with_path_params(self):
51 spec = bt2.ComponentSpec('plugin', 'compcls', 'a path')
52 self.assertEqual(spec.params['inputs'], ['a path'])
53
54 def test_create_wrong_plugin_name_type(self):
55 with self.assertRaises(TypeError):
56 bt2.ComponentSpec(23, 'compcls')
57
58 def test_create_wrong_component_class_name_type(self):
59 with self.assertRaises(TypeError):
60 bt2.ComponentSpec('plugin', 190)
61
62 def test_create_wrong_params_type(self):
63 with self.assertRaises(TypeError):
64 bt2.ComponentSpec('dwdw', 'compcls', datetime.datetime.now())
65
66
67 # Return a map, msg type -> number of messages of this type.
68
69
70 def _count_msgs_by_type(msgs):
71 res = {}
72
73 for msg in msgs:
74 t = type(msg)
75 n = res.get(t, 0)
76 res[t] = n + 1
77
78 return res
79
80
81 class TraceCollectionMessageIteratorTestCase(unittest.TestCase):
82 def test_create_wrong_stream_intersection_mode_type(self):
83 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
84
85 with self.assertRaises(TypeError):
86 bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=23)
87
88 def test_create_wrong_begin_type(self):
89 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
90
91 with self.assertRaises(TypeError):
92 bt2.TraceCollectionMessageIterator(specs, begin='hi')
93
94 def test_create_wrong_end_type(self):
95 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
96
97 with self.assertRaises(TypeError):
98 bt2.TraceCollectionMessageIterator(specs, begin='lel')
99
100 def test_create_no_such_plugin(self):
101 specs = [bt2.ComponentSpec('77', '101', _3EVENTS_INTERSECT_TRACE_PATH)]
102
103 with self.assertRaises(ValueError):
104 bt2.TraceCollectionMessageIterator(specs)
105
106 def test_create_begin_s(self):
107 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
108 bt2.TraceCollectionMessageIterator(specs, begin=19457.918232)
109
110 def test_create_end_s(self):
111 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
112 bt2.TraceCollectionMessageIterator(specs, end=123.12312)
113
114 def test_create_begin_datetime(self):
115 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
116 bt2.TraceCollectionMessageIterator(specs, begin=datetime.datetime.now())
117
118 def test_create_end_datetime(self):
119 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
120 bt2.TraceCollectionMessageIterator(specs, end=datetime.datetime.now())
121
122 def test_iter_no_intersection(self):
123 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
124 msg_iter = bt2.TraceCollectionMessageIterator(specs)
125 msgs = list(msg_iter)
126 self.assertEqual(len(msgs), 28)
127 hist = _count_msgs_by_type(msgs)
128 self.assertEqual(hist[bt2._EventMessage], 8)
129
130 # Same as the above, but we pass a single spec instead of a spec list.
131 def test_iter_specs_not_list(self):
132 spec = bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)
133 msg_iter = bt2.TraceCollectionMessageIterator(spec)
134 msgs = list(msg_iter)
135 self.assertEqual(len(msgs), 28)
136 hist = _count_msgs_by_type(msgs)
137 self.assertEqual(hist[bt2._EventMessage], 8)
138
139 def test_iter_custom_filter(self):
140 src_spec = bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)
141 flt_spec = bt2.ComponentSpec('utils', 'trimmer', {'end': '13515309.000000075'})
142 msg_iter = bt2.TraceCollectionMessageIterator(src_spec, flt_spec)
143 hist = _count_msgs_by_type(msg_iter)
144 self.assertEqual(hist[bt2._EventMessage], 5)
145
146 def test_iter_intersection(self):
147 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
148 msg_iter = bt2.TraceCollectionMessageIterator(
149 specs, stream_intersection_mode=True
150 )
151 msgs = list(msg_iter)
152 self.assertEqual(len(msgs), 15)
153 hist = _count_msgs_by_type(msgs)
154 self.assertEqual(hist[bt2._EventMessage], 3)
155
156 def test_iter_intersection_no_inputs_param(self):
157 specs = [bt2.ComponentSpec('text', 'dmesg', {'read-from-stdin': True})]
158
159 with self.assertRaises(ValueError):
160 bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=True)
161
162 def test_iter_no_intersection_two_traces(self):
163 spec = bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)
164 specs = [spec, spec]
165 msg_iter = bt2.TraceCollectionMessageIterator(specs)
166 msgs = list(msg_iter)
167 self.assertEqual(len(msgs), 56)
168 hist = _count_msgs_by_type(msgs)
169 self.assertEqual(hist[bt2._EventMessage], 16)
170
171 def test_iter_no_intersection_begin(self):
172 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
173 msg_iter = bt2.TraceCollectionMessageIterator(specs, begin=13515309.000000023)
174 hist = _count_msgs_by_type(msg_iter)
175 self.assertEqual(hist[bt2._EventMessage], 6)
176
177 def test_iter_no_intersection_end(self):
178 specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)]
179 msg_iter = bt2.TraceCollectionMessageIterator(specs, end=13515309.000000075)
180 hist = _count_msgs_by_type(msg_iter)
181 self.assertEqual(hist[bt2._EventMessage], 5)
182
183 def test_iter_auto_source_component_spec(self):
184 specs = [bt2.AutoSourceComponentSpec(_3EVENTS_INTERSECT_TRACE_PATH)]
185 msg_iter = bt2.TraceCollectionMessageIterator(specs)
186 msgs = list(msg_iter)
187 self.assertEqual(len(msgs), 28)
188 hist = _count_msgs_by_type(msgs)
189 self.assertEqual(hist[bt2._EventMessage], 8)
190
191 def test_iter_auto_source_component_spec_list_of_strings(self):
192 msg_iter = bt2.TraceCollectionMessageIterator([_3EVENTS_INTERSECT_TRACE_PATH])
193 msgs = list(msg_iter)
194 self.assertEqual(len(msgs), 28)
195 hist = _count_msgs_by_type(msgs)
196 self.assertEqual(hist[bt2._EventMessage], 8)
197
198 def test_iter_auto_source_component_spec_string(self):
199 msg_iter = bt2.TraceCollectionMessageIterator(_3EVENTS_INTERSECT_TRACE_PATH)
200 msgs = list(msg_iter)
201 self.assertEqual(len(msgs), 28)
202 hist = _count_msgs_by_type(msgs)
203 self.assertEqual(hist[bt2._EventMessage], 8)
204
205 def test_iter_mixed_inputs(self):
206 msg_iter = bt2.TraceCollectionMessageIterator(
207 [
208 _3EVENTS_INTERSECT_TRACE_PATH,
209 bt2.AutoSourceComponentSpec(_SEQUENCE_TRACE_PATH),
210 bt2.ComponentSpec('ctf', 'fs', _NOINTERSECT_TRACE_PATH),
211 ]
212 )
213 msgs = list(msg_iter)
214 self.assertEqual(len(msgs), 76)
215 hist = _count_msgs_by_type(msgs)
216 self.assertEqual(hist[bt2._EventMessage], 24)
217
218
219 class _TestAutoDiscoverSourceComponentSpecs(unittest.TestCase):
220 def setUp(self):
221 self._saved_babeltrace_plugin_path = os.environ['BABELTRACE_PLUGIN_PATH']
222 os.environ['BABELTRACE_PLUGIN_PATH'] += ':' + self._plugin_path
223
224 def tearDown(self):
225 os.environ['BABELTRACE_PLUGIN_PATH'] = self._saved_babeltrace_plugin_path
226
227
228 class TestAutoDiscoverSourceComponentSpecsGrouping(
229 _TestAutoDiscoverSourceComponentSpecs
230 ):
231 _plugin_path = _AUTO_SOURCE_DISCOVERY_GROUPING_PATH
232
233 def test_grouping(self):
234 specs = [
235 bt2.AutoSourceComponentSpec('ABCDE'),
236 bt2.AutoSourceComponentSpec(_AUTO_SOURCE_DISCOVERY_GROUPING_PATH),
237 bt2.AutoSourceComponentSpec('does-not-exist'),
238 ]
239 it = bt2.TraceCollectionMessageIterator(specs)
240 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
241
242 self.assertEqual(len(msgs), 8)
243
244 self.assertEqual(msgs[0].stream.name, 'TestSourceABCDE: ABCDE')
245 self.assertEqual(msgs[1].stream.name, 'TestSourceExt: aaa1, aaa2, aaa3')
246 self.assertEqual(msgs[2].stream.name, 'TestSourceExt: bbb1, bbb2')
247 self.assertEqual(msgs[3].stream.name, 'TestSourceExt: ccc1')
248 self.assertEqual(msgs[4].stream.name, 'TestSourceExt: ccc2')
249 self.assertEqual(msgs[5].stream.name, 'TestSourceExt: ccc3')
250 self.assertEqual(msgs[6].stream.name, 'TestSourceExt: ccc4')
251 self.assertEqual(msgs[7].stream.name, 'TestSourceSomeDir: some-dir')
252
253
254 class TestAutoDiscoverSourceComponentSpecsParamsObjLogLevel(
255 _TestAutoDiscoverSourceComponentSpecs
256 ):
257 _plugin_path = _AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH
258
259 _dir_a = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-a')
260 _dir_b = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-b')
261 _dir_ab = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-ab')
262
263 def _test_two_comps_from_one_spec(self, params, obj=None, logging_level=None):
264 specs = [
265 bt2.AutoSourceComponentSpec(
266 self._dir_ab, params=params, obj=obj, logging_level=logging_level
267 )
268 ]
269 it = bt2.TraceCollectionMessageIterator(specs)
270 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
271
272 self.assertEqual(len(msgs), 2)
273
274 return msgs
275
276 def test_params_two_comps_from_one_spec(self):
277 msgs = self._test_two_comps_from_one_spec(
278 params={'test-allo': 'madame', 'what': 'test-params'}
279 )
280
281 self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')")
282 self.assertEqual(msgs[1].stream.name, "TestSourceB: ('test-allo', 'madame')")
283
284 def test_obj_two_comps_from_one_spec(self):
285 msgs = self._test_two_comps_from_one_spec(
286 params={'what': 'python-obj'}, obj='deore'
287 )
288
289 self.assertEqual(msgs[0].stream.name, "TestSourceA: deore")
290 self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")
291
292 def test_log_level_two_comps_from_one_spec(self):
293 msgs = self._test_two_comps_from_one_spec(
294 params={'what': 'log-level'}, logging_level=bt2.LoggingLevel.DEBUG
295 )
296
297 self.assertEqual(
298 msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG)
299 )
300 self.assertEqual(
301 msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG)
302 )
303
304 def _test_two_comps_from_two_specs(
305 self,
306 params_a=None,
307 params_b=None,
308 obj_a=None,
309 obj_b=None,
310 logging_level_a=None,
311 logging_level_b=None,
312 ):
313 specs = [
314 bt2.AutoSourceComponentSpec(
315 self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a
316 ),
317 bt2.AutoSourceComponentSpec(
318 self._dir_b, params=params_b, obj=obj_b, logging_level=logging_level_b
319 ),
320 ]
321 it = bt2.TraceCollectionMessageIterator(specs)
322 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
323
324 self.assertEqual(len(msgs), 2)
325
326 return msgs
327
328 def test_params_two_comps_from_two_specs(self):
329 msgs = self._test_two_comps_from_two_specs(
330 params_a={'test-allo': 'madame', 'what': 'test-params'},
331 params_b={'test-bonjour': 'monsieur', 'what': 'test-params'},
332 )
333
334 self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')")
335 self.assertEqual(
336 msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')"
337 )
338
339 def test_obj_two_comps_from_two_specs(self):
340 msgs = self._test_two_comps_from_two_specs(
341 params_a={'what': 'python-obj'},
342 params_b={'what': 'python-obj'},
343 obj_a='deore',
344 obj_b='alivio',
345 )
346
347 self.assertEqual(msgs[0].stream.name, "TestSourceA: deore")
348 self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio")
349
350 def test_log_level_two_comps_from_two_specs(self):
351 msgs = self._test_two_comps_from_two_specs(
352 params_a={'what': 'log-level'},
353 params_b={'what': 'log-level'},
354 logging_level_a=bt2.LoggingLevel.DEBUG,
355 logging_level_b=bt2.LoggingLevel.TRACE,
356 )
357
358 self.assertEqual(
359 msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG)
360 )
361 self.assertEqual(
362 msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE)
363 )
364
365 def _test_one_comp_from_one_spec_one_comp_from_both_1(
366 self,
367 params_a=None,
368 params_ab=None,
369 obj_a=None,
370 obj_ab=None,
371 logging_level_a=None,
372 logging_level_ab=None,
373 ):
374 specs = [
375 bt2.AutoSourceComponentSpec(
376 self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a
377 ),
378 bt2.AutoSourceComponentSpec(
379 self._dir_ab,
380 params=params_ab,
381 obj=obj_ab,
382 logging_level=logging_level_ab,
383 ),
384 ]
385 it = bt2.TraceCollectionMessageIterator(specs)
386 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
387
388 self.assertEqual(len(msgs), 2)
389
390 return msgs
391
392 def test_params_one_comp_from_one_spec_one_comp_from_both_1(self):
393 msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1(
394 params_a={'test-allo': 'madame', 'what': 'test-params'},
395 params_ab={'test-bonjour': 'monsieur', 'what': 'test-params'},
396 )
397
398 self.assertEqual(
399 msgs[0].stream.name,
400 "TestSourceA: ('test-allo', 'madame'), ('test-bonjour', 'monsieur')",
401 )
402 self.assertEqual(
403 msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')"
404 )
405
406 def test_obj_one_comp_from_one_spec_one_comp_from_both_1(self):
407 msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1(
408 params_a={'what': 'python-obj'},
409 params_ab={'what': 'python-obj'},
410 obj_a='deore',
411 obj_ab='alivio',
412 )
413
414 self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio")
415 self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio")
416
417 def test_log_level_one_comp_from_one_spec_one_comp_from_both_1(self):
418 msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1(
419 params_a={'what': 'log-level'},
420 params_ab={'what': 'log-level'},
421 logging_level_a=bt2.LoggingLevel.DEBUG,
422 logging_level_ab=bt2.LoggingLevel.TRACE,
423 )
424
425 self.assertEqual(
426 msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE)
427 )
428 self.assertEqual(
429 msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE)
430 )
431
432 def _test_one_comp_from_one_spec_one_comp_from_both_2(
433 self,
434 params_ab=None,
435 params_a=None,
436 obj_ab=None,
437 obj_a=None,
438 logging_level_ab=None,
439 logging_level_a=None,
440 ):
441 specs = [
442 bt2.AutoSourceComponentSpec(
443 self._dir_ab,
444 params=params_ab,
445 obj=obj_ab,
446 logging_level=logging_level_ab,
447 ),
448 bt2.AutoSourceComponentSpec(
449 self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a
450 ),
451 ]
452 it = bt2.TraceCollectionMessageIterator(specs)
453 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
454
455 self.assertEqual(len(msgs), 2)
456
457 return msgs
458
459 def test_params_one_comp_from_one_spec_one_comp_from_both_2(self):
460 msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2(
461 params_ab={
462 'test-bonjour': 'madame',
463 'test-salut': 'les amis',
464 'what': 'test-params',
465 },
466 params_a={'test-bonjour': 'monsieur', 'what': 'test-params'},
467 )
468
469 self.assertEqual(
470 msgs[0].stream.name,
471 "TestSourceA: ('test-bonjour', 'monsieur'), ('test-salut', 'les amis')",
472 )
473 self.assertEqual(
474 msgs[1].stream.name,
475 "TestSourceB: ('test-bonjour', 'madame'), ('test-salut', 'les amis')",
476 )
477
478 def test_obj_one_comp_from_one_spec_one_comp_from_both_2(self):
479 msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2(
480 params_ab={'what': 'python-obj'},
481 params_a={'what': 'python-obj'},
482 obj_ab='deore',
483 obj_a='alivio',
484 )
485
486 self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio")
487 self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")
488
489 def test_log_level_one_comp_from_one_spec_one_comp_from_both_2(self):
490 msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2(
491 params_ab={'what': 'log-level'},
492 params_a={'what': 'log-level'},
493 logging_level_ab=bt2.LoggingLevel.DEBUG,
494 logging_level_a=bt2.LoggingLevel.TRACE,
495 )
496
497 self.assertEqual(
498 msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE)
499 )
500 self.assertEqual(
501 msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG)
502 )
503
504 def test_obj_override_with_none(self):
505 specs = [
506 bt2.AutoSourceComponentSpec(
507 self._dir_ab, params={'what': 'python-obj'}, obj='deore'
508 ),
509 bt2.AutoSourceComponentSpec(
510 self._dir_a, params={'what': 'python-obj'}, obj=None
511 ),
512 ]
513 it = bt2.TraceCollectionMessageIterator(specs)
514 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
515
516 self.assertEqual(len(msgs), 2)
517 self.assertEqual(msgs[0].stream.name, "TestSourceA: None")
518 self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")
519
520 def test_obj_no_override_with_no_obj(self):
521 specs = [
522 bt2.AutoSourceComponentSpec(
523 self._dir_ab, params={'what': 'python-obj'}, obj='deore'
524 ),
525 bt2.AutoSourceComponentSpec(self._dir_a, params={'what': 'python-obj'}),
526 ]
527 it = bt2.TraceCollectionMessageIterator(specs)
528 msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage]
529
530 self.assertEqual(len(msgs), 2)
531 self.assertEqual(msgs[0].stream.name, "TestSourceA: deore")
532 self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")
533
534
535 if __name__ == '__main__':
536 unittest.main()
This page took 0.043659 seconds and 4 git commands to generate.