| 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 _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 | |
| 60 | class ComponentSpecTestCase(unittest.TestCase): |
| 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) |
| 116 | |
| 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']) |
| 120 | |
| 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 | ) |
| 125 | self.assertEqual(spec.params['inputs'], ['a path']) |
| 126 | |
| 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) |
| 132 | |
| 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) |
| 148 | |
| 149 | def test_create_wrong_params_type(self): |
| 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 | ) |
| 172 | |
| 173 | |
| 174 | # Return a map, msg type -> number of messages of this type. |
| 175 | |
| 176 | |
| 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 |
| 186 | |
| 187 | |
| 188 | class TraceCollectionMessageIteratorTestCase(unittest.TestCase): |
| 189 | def test_create_wrong_stream_intersection_mode_type(self): |
| 190 | specs = [ |
| 191 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 192 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 193 | ) |
| 194 | ] |
| 195 | |
| 196 | with self.assertRaises(TypeError): |
| 197 | bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=23) |
| 198 | |
| 199 | def test_create_wrong_begin_type(self): |
| 200 | specs = [ |
| 201 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 202 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 203 | ) |
| 204 | ] |
| 205 | |
| 206 | with self.assertRaises(TypeError): |
| 207 | bt2.TraceCollectionMessageIterator(specs, begin='hi') |
| 208 | |
| 209 | def test_create_wrong_end_type(self): |
| 210 | specs = [ |
| 211 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 212 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 213 | ) |
| 214 | ] |
| 215 | |
| 216 | with self.assertRaises(TypeError): |
| 217 | bt2.TraceCollectionMessageIterator(specs, begin='lel') |
| 218 | |
| 219 | def test_create_begin_s(self): |
| 220 | specs = [ |
| 221 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 222 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 223 | ) |
| 224 | ] |
| 225 | bt2.TraceCollectionMessageIterator(specs, begin=19457.918232) |
| 226 | |
| 227 | def test_create_end_s(self): |
| 228 | specs = [ |
| 229 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 230 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 231 | ) |
| 232 | ] |
| 233 | bt2.TraceCollectionMessageIterator(specs, end=123.12312) |
| 234 | |
| 235 | def test_create_begin_datetime(self): |
| 236 | specs = [ |
| 237 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 238 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 239 | ) |
| 240 | ] |
| 241 | bt2.TraceCollectionMessageIterator(specs, begin=datetime.datetime.now()) |
| 242 | |
| 243 | def test_create_end_datetime(self): |
| 244 | specs = [ |
| 245 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 246 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 247 | ) |
| 248 | ] |
| 249 | bt2.TraceCollectionMessageIterator(specs, end=datetime.datetime.now()) |
| 250 | |
| 251 | def test_iter_no_intersection(self): |
| 252 | specs = [ |
| 253 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 254 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 255 | ) |
| 256 | ] |
| 257 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
| 258 | msgs = list(msg_iter) |
| 259 | self.assertEqual(len(msgs), 28) |
| 260 | hist = _count_msgs_by_type(msgs) |
| 261 | self.assertEqual(hist[bt2._EventMessage], 8) |
| 262 | |
| 263 | # Same as the above, but we pass a single spec instead of a spec list. |
| 264 | def test_iter_specs_not_list(self): |
| 265 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 266 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 267 | ) |
| 268 | msg_iter = bt2.TraceCollectionMessageIterator(spec) |
| 269 | msgs = list(msg_iter) |
| 270 | self.assertEqual(len(msgs), 28) |
| 271 | hist = _count_msgs_by_type(msgs) |
| 272 | self.assertEqual(hist[bt2._EventMessage], 8) |
| 273 | |
| 274 | def test_iter_custom_filter(self): |
| 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 | ) |
| 281 | msg_iter = bt2.TraceCollectionMessageIterator(src_spec, flt_spec) |
| 282 | hist = _count_msgs_by_type(msg_iter) |
| 283 | self.assertEqual(hist[bt2._EventMessage], 5) |
| 284 | |
| 285 | def test_iter_intersection(self): |
| 286 | specs = [ |
| 287 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 288 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 289 | ) |
| 290 | ] |
| 291 | msg_iter = bt2.TraceCollectionMessageIterator( |
| 292 | specs, stream_intersection_mode=True |
| 293 | ) |
| 294 | msgs = list(msg_iter) |
| 295 | self.assertEqual(len(msgs), 15) |
| 296 | hist = _count_msgs_by_type(msgs) |
| 297 | self.assertEqual(hist[bt2._EventMessage], 3) |
| 298 | |
| 299 | def test_iter_intersection_no_inputs_param(self): |
| 300 | specs = [ |
| 301 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 302 | 'text', 'dmesg', {'read-from-stdin': True} |
| 303 | ) |
| 304 | ] |
| 305 | |
| 306 | with self.assertRaises(ValueError): |
| 307 | bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=True) |
| 308 | |
| 309 | def test_iter_no_intersection_two_traces(self): |
| 310 | spec = bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 311 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 312 | ) |
| 313 | specs = [spec, spec] |
| 314 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
| 315 | msgs = list(msg_iter) |
| 316 | self.assertEqual(len(msgs), 56) |
| 317 | hist = _count_msgs_by_type(msgs) |
| 318 | self.assertEqual(hist[bt2._EventMessage], 16) |
| 319 | |
| 320 | def test_iter_no_intersection_begin(self): |
| 321 | specs = [ |
| 322 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 323 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 324 | ) |
| 325 | ] |
| 326 | msg_iter = bt2.TraceCollectionMessageIterator(specs, begin=13515309.000000023) |
| 327 | hist = _count_msgs_by_type(msg_iter) |
| 328 | self.assertEqual(hist[bt2._EventMessage], 6) |
| 329 | |
| 330 | def test_iter_no_intersection_end(self): |
| 331 | specs = [ |
| 332 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 333 | 'ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH |
| 334 | ) |
| 335 | ] |
| 336 | msg_iter = bt2.TraceCollectionMessageIterator(specs, end=13515309.000000075) |
| 337 | hist = _count_msgs_by_type(msg_iter) |
| 338 | self.assertEqual(hist[bt2._EventMessage], 5) |
| 339 | |
| 340 | def test_iter_auto_source_component_spec(self): |
| 341 | specs = [bt2.AutoSourceComponentSpec(_3EVENTS_INTERSECT_TRACE_PATH)] |
| 342 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
| 343 | msgs = list(msg_iter) |
| 344 | self.assertEqual(len(msgs), 28) |
| 345 | hist = _count_msgs_by_type(msgs) |
| 346 | self.assertEqual(hist[bt2._EventMessage], 8) |
| 347 | |
| 348 | def test_iter_auto_source_component_spec_list_of_strings(self): |
| 349 | msg_iter = bt2.TraceCollectionMessageIterator([_3EVENTS_INTERSECT_TRACE_PATH]) |
| 350 | msgs = list(msg_iter) |
| 351 | self.assertEqual(len(msgs), 28) |
| 352 | hist = _count_msgs_by_type(msgs) |
| 353 | self.assertEqual(hist[bt2._EventMessage], 8) |
| 354 | |
| 355 | def test_iter_auto_source_component_spec_string(self): |
| 356 | msg_iter = bt2.TraceCollectionMessageIterator(_3EVENTS_INTERSECT_TRACE_PATH) |
| 357 | msgs = list(msg_iter) |
| 358 | self.assertEqual(len(msgs), 28) |
| 359 | hist = _count_msgs_by_type(msgs) |
| 360 | self.assertEqual(hist[bt2._EventMessage], 8) |
| 361 | |
| 362 | def test_iter_mixed_inputs(self): |
| 363 | msg_iter = bt2.TraceCollectionMessageIterator( |
| 364 | [ |
| 365 | _3EVENTS_INTERSECT_TRACE_PATH, |
| 366 | bt2.AutoSourceComponentSpec(_SEQUENCE_TRACE_PATH), |
| 367 | bt2.ComponentSpec.from_named_plugin_and_component_class( |
| 368 | 'ctf', 'fs', _NOINTERSECT_TRACE_PATH |
| 369 | ), |
| 370 | ] |
| 371 | ) |
| 372 | msgs = list(msg_iter) |
| 373 | self.assertEqual(len(msgs), 76) |
| 374 | hist = _count_msgs_by_type(msgs) |
| 375 | self.assertEqual(hist[bt2._EventMessage], 24) |
| 376 | |
| 377 | def test_auto_source_component_non_existent(self): |
| 378 | with self.assertRaisesRegex( |
| 379 | RuntimeError, |
| 380 | 'Some auto source component specs did not produce any component', |
| 381 | ): |
| 382 | # Test with one path known to contain a trace and one path known |
| 383 | # to not contain any trace. |
| 384 | bt2.TraceCollectionMessageIterator( |
| 385 | [_SEQUENCE_TRACE_PATH, '/this/path/better/not/exist'] |
| 386 | ) |
| 387 | |
| 388 | |
| 389 | class _TestAutoDiscoverSourceComponentSpecs(unittest.TestCase): |
| 390 | def setUp(self): |
| 391 | self._saved_babeltrace_plugin_path = os.environ['BABELTRACE_PLUGIN_PATH'] |
| 392 | os.environ['BABELTRACE_PLUGIN_PATH'] += os.pathsep + self._plugin_path |
| 393 | |
| 394 | def tearDown(self): |
| 395 | os.environ['BABELTRACE_PLUGIN_PATH'] = self._saved_babeltrace_plugin_path |
| 396 | |
| 397 | |
| 398 | class TestAutoDiscoverSourceComponentSpecsGrouping( |
| 399 | _TestAutoDiscoverSourceComponentSpecs |
| 400 | ): |
| 401 | _plugin_path = _AUTO_SOURCE_DISCOVERY_GROUPING_PATH |
| 402 | |
| 403 | def test_grouping(self): |
| 404 | specs = [ |
| 405 | bt2.AutoSourceComponentSpec('ABCDE'), |
| 406 | bt2.AutoSourceComponentSpec(_AUTO_SOURCE_DISCOVERY_GROUPING_PATH), |
| 407 | ] |
| 408 | it = bt2.TraceCollectionMessageIterator(specs) |
| 409 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 410 | |
| 411 | self.assertEqual(len(msgs), 8) |
| 412 | |
| 413 | self.assertEqual(msgs[0].stream.name, 'TestSourceABCDE: ABCDE') |
| 414 | self.assertEqual(msgs[1].stream.name, 'TestSourceExt: aaa1, aaa2, aaa3') |
| 415 | self.assertEqual(msgs[2].stream.name, 'TestSourceExt: bbb1, bbb2') |
| 416 | self.assertEqual(msgs[3].stream.name, 'TestSourceExt: ccc1') |
| 417 | self.assertEqual(msgs[4].stream.name, 'TestSourceExt: ccc2') |
| 418 | self.assertEqual(msgs[5].stream.name, 'TestSourceExt: ccc3') |
| 419 | self.assertEqual(msgs[6].stream.name, 'TestSourceExt: ccc4') |
| 420 | self.assertEqual(msgs[7].stream.name, 'TestSourceSomeDir: some-dir') |
| 421 | |
| 422 | |
| 423 | class TestAutoDiscoverSourceComponentSpecsParamsObjLogLevel( |
| 424 | _TestAutoDiscoverSourceComponentSpecs |
| 425 | ): |
| 426 | _plugin_path = _AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH |
| 427 | |
| 428 | _dir_a = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-a') |
| 429 | _dir_b = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-b') |
| 430 | _dir_ab = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, 'dir-ab') |
| 431 | |
| 432 | def _test_two_comps_from_one_spec(self, params, obj=None, logging_level=None): |
| 433 | specs = [ |
| 434 | bt2.AutoSourceComponentSpec( |
| 435 | self._dir_ab, params=params, obj=obj, logging_level=logging_level |
| 436 | ) |
| 437 | ] |
| 438 | it = bt2.TraceCollectionMessageIterator(specs) |
| 439 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 440 | |
| 441 | self.assertEqual(len(msgs), 2) |
| 442 | |
| 443 | return msgs |
| 444 | |
| 445 | def test_params_two_comps_from_one_spec(self): |
| 446 | msgs = self._test_two_comps_from_one_spec( |
| 447 | params={'test-allo': 'madame', 'what': 'test-params'} |
| 448 | ) |
| 449 | |
| 450 | self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')") |
| 451 | self.assertEqual(msgs[1].stream.name, "TestSourceB: ('test-allo', 'madame')") |
| 452 | |
| 453 | def test_obj_two_comps_from_one_spec(self): |
| 454 | msgs = self._test_two_comps_from_one_spec( |
| 455 | params={'what': 'python-obj'}, obj='deore' |
| 456 | ) |
| 457 | |
| 458 | self.assertEqual(msgs[0].stream.name, "TestSourceA: deore") |
| 459 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") |
| 460 | |
| 461 | def test_log_level_two_comps_from_one_spec(self): |
| 462 | msgs = self._test_two_comps_from_one_spec( |
| 463 | params={'what': 'log-level'}, logging_level=bt2.LoggingLevel.DEBUG |
| 464 | ) |
| 465 | |
| 466 | self.assertEqual( |
| 467 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG) |
| 468 | ) |
| 469 | self.assertEqual( |
| 470 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG) |
| 471 | ) |
| 472 | |
| 473 | def _test_two_comps_from_two_specs( |
| 474 | self, |
| 475 | params_a=None, |
| 476 | params_b=None, |
| 477 | obj_a=None, |
| 478 | obj_b=None, |
| 479 | logging_level_a=None, |
| 480 | logging_level_b=None, |
| 481 | ): |
| 482 | specs = [ |
| 483 | bt2.AutoSourceComponentSpec( |
| 484 | self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a |
| 485 | ), |
| 486 | bt2.AutoSourceComponentSpec( |
| 487 | self._dir_b, params=params_b, obj=obj_b, logging_level=logging_level_b |
| 488 | ), |
| 489 | ] |
| 490 | it = bt2.TraceCollectionMessageIterator(specs) |
| 491 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 492 | |
| 493 | self.assertEqual(len(msgs), 2) |
| 494 | |
| 495 | return msgs |
| 496 | |
| 497 | def test_params_two_comps_from_two_specs(self): |
| 498 | msgs = self._test_two_comps_from_two_specs( |
| 499 | params_a={'test-allo': 'madame', 'what': 'test-params'}, |
| 500 | params_b={'test-bonjour': 'monsieur', 'what': 'test-params'}, |
| 501 | ) |
| 502 | |
| 503 | self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')") |
| 504 | self.assertEqual( |
| 505 | msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')" |
| 506 | ) |
| 507 | |
| 508 | def test_obj_two_comps_from_two_specs(self): |
| 509 | msgs = self._test_two_comps_from_two_specs( |
| 510 | params_a={'what': 'python-obj'}, |
| 511 | params_b={'what': 'python-obj'}, |
| 512 | obj_a='deore', |
| 513 | obj_b='alivio', |
| 514 | ) |
| 515 | |
| 516 | self.assertEqual(msgs[0].stream.name, "TestSourceA: deore") |
| 517 | self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio") |
| 518 | |
| 519 | def test_log_level_two_comps_from_two_specs(self): |
| 520 | msgs = self._test_two_comps_from_two_specs( |
| 521 | params_a={'what': 'log-level'}, |
| 522 | params_b={'what': 'log-level'}, |
| 523 | logging_level_a=bt2.LoggingLevel.DEBUG, |
| 524 | logging_level_b=bt2.LoggingLevel.TRACE, |
| 525 | ) |
| 526 | |
| 527 | self.assertEqual( |
| 528 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG) |
| 529 | ) |
| 530 | self.assertEqual( |
| 531 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE) |
| 532 | ) |
| 533 | |
| 534 | def _test_one_comp_from_one_spec_one_comp_from_both_1( |
| 535 | self, |
| 536 | params_a=None, |
| 537 | params_ab=None, |
| 538 | obj_a=None, |
| 539 | obj_ab=None, |
| 540 | logging_level_a=None, |
| 541 | logging_level_ab=None, |
| 542 | ): |
| 543 | specs = [ |
| 544 | bt2.AutoSourceComponentSpec( |
| 545 | self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a |
| 546 | ), |
| 547 | bt2.AutoSourceComponentSpec( |
| 548 | self._dir_ab, |
| 549 | params=params_ab, |
| 550 | obj=obj_ab, |
| 551 | logging_level=logging_level_ab, |
| 552 | ), |
| 553 | ] |
| 554 | it = bt2.TraceCollectionMessageIterator(specs) |
| 555 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 556 | |
| 557 | self.assertEqual(len(msgs), 2) |
| 558 | |
| 559 | return msgs |
| 560 | |
| 561 | def test_params_one_comp_from_one_spec_one_comp_from_both_1(self): |
| 562 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1( |
| 563 | params_a={'test-allo': 'madame', 'what': 'test-params'}, |
| 564 | params_ab={'test-bonjour': 'monsieur', 'what': 'test-params'}, |
| 565 | ) |
| 566 | |
| 567 | self.assertEqual( |
| 568 | msgs[0].stream.name, |
| 569 | "TestSourceA: ('test-allo', 'madame'), ('test-bonjour', 'monsieur')", |
| 570 | ) |
| 571 | self.assertEqual( |
| 572 | msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')" |
| 573 | ) |
| 574 | |
| 575 | def test_obj_one_comp_from_one_spec_one_comp_from_both_1(self): |
| 576 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1( |
| 577 | params_a={'what': 'python-obj'}, |
| 578 | params_ab={'what': 'python-obj'}, |
| 579 | obj_a='deore', |
| 580 | obj_ab='alivio', |
| 581 | ) |
| 582 | |
| 583 | self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio") |
| 584 | self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio") |
| 585 | |
| 586 | def test_log_level_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={'what': 'log-level'}, |
| 589 | params_ab={'what': 'log-level'}, |
| 590 | logging_level_a=bt2.LoggingLevel.DEBUG, |
| 591 | logging_level_ab=bt2.LoggingLevel.TRACE, |
| 592 | ) |
| 593 | |
| 594 | self.assertEqual( |
| 595 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE) |
| 596 | ) |
| 597 | self.assertEqual( |
| 598 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE) |
| 599 | ) |
| 600 | |
| 601 | def _test_one_comp_from_one_spec_one_comp_from_both_2( |
| 602 | self, |
| 603 | params_ab=None, |
| 604 | params_a=None, |
| 605 | obj_ab=None, |
| 606 | obj_a=None, |
| 607 | logging_level_ab=None, |
| 608 | logging_level_a=None, |
| 609 | ): |
| 610 | specs = [ |
| 611 | bt2.AutoSourceComponentSpec( |
| 612 | self._dir_ab, |
| 613 | params=params_ab, |
| 614 | obj=obj_ab, |
| 615 | logging_level=logging_level_ab, |
| 616 | ), |
| 617 | bt2.AutoSourceComponentSpec( |
| 618 | self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a |
| 619 | ), |
| 620 | ] |
| 621 | it = bt2.TraceCollectionMessageIterator(specs) |
| 622 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 623 | |
| 624 | self.assertEqual(len(msgs), 2) |
| 625 | |
| 626 | return msgs |
| 627 | |
| 628 | def test_params_one_comp_from_one_spec_one_comp_from_both_2(self): |
| 629 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2( |
| 630 | params_ab={ |
| 631 | 'test-bonjour': 'madame', |
| 632 | 'test-salut': 'les amis', |
| 633 | 'what': 'test-params', |
| 634 | }, |
| 635 | params_a={'test-bonjour': 'monsieur', 'what': 'test-params'}, |
| 636 | ) |
| 637 | |
| 638 | self.assertEqual( |
| 639 | msgs[0].stream.name, |
| 640 | "TestSourceA: ('test-bonjour', 'monsieur'), ('test-salut', 'les amis')", |
| 641 | ) |
| 642 | self.assertEqual( |
| 643 | msgs[1].stream.name, |
| 644 | "TestSourceB: ('test-bonjour', 'madame'), ('test-salut', 'les amis')", |
| 645 | ) |
| 646 | |
| 647 | def test_obj_one_comp_from_one_spec_one_comp_from_both_2(self): |
| 648 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2( |
| 649 | params_ab={'what': 'python-obj'}, |
| 650 | params_a={'what': 'python-obj'}, |
| 651 | obj_ab='deore', |
| 652 | obj_a='alivio', |
| 653 | ) |
| 654 | |
| 655 | self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio") |
| 656 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") |
| 657 | |
| 658 | def test_log_level_one_comp_from_one_spec_one_comp_from_both_2(self): |
| 659 | msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2( |
| 660 | params_ab={'what': 'log-level'}, |
| 661 | params_a={'what': 'log-level'}, |
| 662 | logging_level_ab=bt2.LoggingLevel.DEBUG, |
| 663 | logging_level_a=bt2.LoggingLevel.TRACE, |
| 664 | ) |
| 665 | |
| 666 | self.assertEqual( |
| 667 | msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE) |
| 668 | ) |
| 669 | self.assertEqual( |
| 670 | msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG) |
| 671 | ) |
| 672 | |
| 673 | def test_obj_override_with_none(self): |
| 674 | specs = [ |
| 675 | bt2.AutoSourceComponentSpec( |
| 676 | self._dir_ab, params={'what': 'python-obj'}, obj='deore' |
| 677 | ), |
| 678 | bt2.AutoSourceComponentSpec( |
| 679 | self._dir_a, params={'what': 'python-obj'}, obj=None |
| 680 | ), |
| 681 | ] |
| 682 | it = bt2.TraceCollectionMessageIterator(specs) |
| 683 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 684 | |
| 685 | self.assertEqual(len(msgs), 2) |
| 686 | self.assertEqual(msgs[0].stream.name, "TestSourceA: None") |
| 687 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") |
| 688 | |
| 689 | def test_obj_no_override_with_no_obj(self): |
| 690 | specs = [ |
| 691 | bt2.AutoSourceComponentSpec( |
| 692 | self._dir_ab, params={'what': 'python-obj'}, obj='deore' |
| 693 | ), |
| 694 | bt2.AutoSourceComponentSpec(self._dir_a, params={'what': 'python-obj'}), |
| 695 | ] |
| 696 | it = bt2.TraceCollectionMessageIterator(specs) |
| 697 | msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] |
| 698 | |
| 699 | self.assertEqual(len(msgs), 2) |
| 700 | self.assertEqual(msgs[0].stream.name, "TestSourceA: deore") |
| 701 | self.assertEqual(msgs[1].stream.name, "TestSourceB: deore") |
| 702 | |
| 703 | |
| 704 | if __name__ == '__main__': |
| 705 | unittest.main() |