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