File: test_trace_collection_message_iterator.py

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (740 lines) | stat: -rw-r--r-- 27,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2019 EfficiOS Inc.
#

import os
import os.path
import datetime
import unittest

import bt2

_BT_TESTS_DATADIR = os.environ["BT_TESTS_DATADIR"]
_BT_CTF_TRACES_PATH = os.environ["BT_CTF_TRACES_PATH"]
_3EVENTS_INTERSECT_TRACE_PATH = os.path.join(
    _BT_CTF_TRACES_PATH, "1", "intersection", "3eventsintersect"
)
_NOINTERSECT_TRACE_PATH = os.path.join(
    _BT_CTF_TRACES_PATH, "1", "intersection", "nointersect"
)
_SEQUENCE_TRACE_PATH = os.path.join(_BT_CTF_TRACES_PATH, "1", "succeed", "sequence")
_AUTO_SOURCE_DISCOVERY_GROUPING_PATH = os.path.join(
    _BT_TESTS_DATADIR, "auto-source-discovery", "grouping"
)
_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH = os.path.join(
    _BT_TESTS_DATADIR, "auto-source-discovery", "params-log-level"
)

_METADATA_SYNTAX_ERROR_TRACE_PATH = os.path.join(
    _BT_CTF_TRACES_PATH, "1", "fail", "metadata-syntax-error"
)
_BT_ENABLE_PYTHON_PLUGINS = os.environ["BT_TESTS_ENABLE_PYTHON_PLUGINS"] == "1"


class _SomeSource(
    bt2._UserSourceComponent, message_iterator_class=bt2._UserMessageIterator
):
    pass


class _SomeFilter(
    bt2._UserFilterComponent, message_iterator_class=bt2._UserMessageIterator
):
    pass


class _SomeSink(bt2._UserSinkComponent):
    def _user_consume(self):
        pass


class ComponentSpecTestCase(unittest.TestCase):
    def setUp(self):
        # A source CC from a plugin.
        self._dmesg_cc = bt2.find_plugin("text").source_component_classes["dmesg"]
        assert self._dmesg_cc is not None

        # A filter CC from a plugin.
        self._muxer_cc = bt2.find_plugin("utils").filter_component_classes["muxer"]
        assert self._muxer_cc is not None

        # A sink CC from a plugin.
        self._pretty_cc = bt2.find_plugin("text").sink_component_classes["pretty"]
        assert self._pretty_cc is not None

    def test_create_source_from_name(self):
        spec = bt2.ComponentSpec.from_named_plugin_and_component_class("text", "dmesg")
        self.assertEqual(spec.component_class.name, "dmesg")

    def test_create_source_from_plugin(self):
        spec = bt2.ComponentSpec(self._dmesg_cc)
        self.assertEqual(spec.component_class.name, "dmesg")

    def test_create_source_from_user(self):
        spec = bt2.ComponentSpec(_SomeSource)
        self.assertEqual(spec.component_class.name, "_SomeSource")

    def test_create_filter_from_name(self):
        spec = bt2.ComponentSpec.from_named_plugin_and_component_class("utils", "muxer")
        self.assertEqual(spec.component_class.name, "muxer")

    def test_create_filter_from_object(self):
        spec = bt2.ComponentSpec(self._muxer_cc)
        self.assertEqual(spec.component_class.name, "muxer")

    def test_create_sink_from_name(self):
        with self.assertRaisesRegex(
            KeyError,
            "source or filter component class `pretty` not found in plugin `text`",
        ):
            bt2.ComponentSpec.from_named_plugin_and_component_class("text", "pretty")

    def test_create_sink_from_object(self):
        with self.assertRaisesRegex(
            TypeError,
            "'_SinkComponentClassConst' is not a source or filter component class",
        ):
            bt2.ComponentSpec(self._pretty_cc)

    def test_create_from_object_with_params(self):
        spec = bt2.ComponentSpec(self._dmesg_cc, {"salut": 23})
        self.assertEqual(spec.params["salut"], 23)

    def test_create_from_name_with_params(self):
        spec = bt2.ComponentSpec.from_named_plugin_and_component_class(
            "text", "dmesg", {"salut": 23}
        )
        self.assertEqual(spec.params["salut"], 23)

    def test_create_from_object_with_path_params(self):
        spec = spec = bt2.ComponentSpec(self._dmesg_cc, "a path")
        self.assertEqual(spec.params["inputs"], ["a path"])

    def test_create_from_name_with_path_params(self):
        spec = spec = bt2.ComponentSpec.from_named_plugin_and_component_class(
            "text", "dmesg", "a path"
        )
        self.assertEqual(spec.params["inputs"], ["a path"])

    def test_create_wrong_comp_class_type(self):
        with self.assertRaisesRegex(
            TypeError, "'int' is not a source or filter component class"
        ):
            bt2.ComponentSpec(18)

    def test_create_from_name_wrong_plugin_name_type(self):
        with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"):
            bt2.ComponentSpec.from_named_plugin_and_component_class(23, "compcls")

    def test_create_from_name_non_existent_plugin(self):
        with self.assertRaisesRegex(
            ValueError, "no such plugin: this_plugin_does_not_exist"
        ):
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "this_plugin_does_not_exist", "compcls"
            )

    def test_create_from_name_wrong_component_class_name_type(self):
        with self.assertRaisesRegex(TypeError, "'int' is not a 'str' object"):
            bt2.ComponentSpec.from_named_plugin_and_component_class("utils", 190)

    def test_create_wrong_params_type(self):
        with self.assertRaisesRegex(
            TypeError, "cannot create value object from 'datetime' object"
        ):
            bt2.ComponentSpec(self._dmesg_cc, params=datetime.datetime.now())

    def test_create_from_name_wrong_params_type(self):
        with self.assertRaisesRegex(
            TypeError, "cannot create value object from 'datetime' object"
        ):
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "text", "dmesg", datetime.datetime.now()
            )

    def test_create_wrong_log_level_type(self):
        with self.assertRaisesRegex(
            TypeError, "'str' is not a '<enum 'LoggingLevel'>' object"
        ):
            bt2.ComponentSpec(self._dmesg_cc, logging_level="banane")

    def test_create_from_name_wrong_log_level_type(self):
        with self.assertRaisesRegex(
            TypeError, "'str' is not a '<enum 'LoggingLevel'>' object"
        ):
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "text", "dmesg", logging_level="banane"
            )


# Return a map, msg type -> number of messages of this type.


def _count_msgs_by_type(msgs):
    res = {}

    for msg in msgs:
        t = type(msg)
        n = res.get(t, 0)
        res[t] = n + 1

    return res


class TraceCollectionMessageIteratorTestCase(unittest.TestCase):
    def test_create_wrong_stream_intersection_mode_type(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]

        with self.assertRaises(TypeError):
            bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=23)

    def test_create_wrong_begin_type(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]

        with self.assertRaises(TypeError):
            bt2.TraceCollectionMessageIterator(specs, begin="hi")

    def test_create_wrong_end_type(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]

        with self.assertRaises(TypeError):
            bt2.TraceCollectionMessageIterator(specs, begin="lel")

    def test_create_begin_s(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        bt2.TraceCollectionMessageIterator(specs, begin=19457.918232)

    def test_create_end_s(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        bt2.TraceCollectionMessageIterator(specs, end=123.12312)

    def test_create_begin_datetime(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        bt2.TraceCollectionMessageIterator(specs, begin=datetime.datetime.now())

    def test_create_end_datetime(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        bt2.TraceCollectionMessageIterator(specs, end=datetime.datetime.now())

    def test_iter_no_intersection(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        msg_iter = bt2.TraceCollectionMessageIterator(specs)
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 28)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 8)

    # Same as the above, but we pass a single spec instead of a spec list.
    def test_iter_specs_not_list(self):
        spec = bt2.ComponentSpec.from_named_plugin_and_component_class(
            "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
        )
        msg_iter = bt2.TraceCollectionMessageIterator(spec)
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 28)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 8)

    def test_iter_custom_filter(self):
        src_spec = bt2.ComponentSpec.from_named_plugin_and_component_class(
            "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
        )
        flt_spec = bt2.ComponentSpec.from_named_plugin_and_component_class(
            "utils", "trimmer", {"end": "13515309.000000075"}
        )
        msg_iter = bt2.TraceCollectionMessageIterator(src_spec, flt_spec)
        hist = _count_msgs_by_type(msg_iter)
        self.assertEqual(hist[bt2._EventMessageConst], 5)

    def test_iter_intersection(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        msg_iter = bt2.TraceCollectionMessageIterator(
            specs, stream_intersection_mode=True
        )
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 15)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 3)

    def test_iter_intersection_params(self):
        # Check that all params used to create the source component are passed
        # to the `babeltrace.trace-infos` query.
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf",
                "fs",
                {
                    "inputs": [_3EVENTS_INTERSECT_TRACE_PATH],
                    "clock-class-offset-s": 1000,
                },
            )
        ]

        msg_iter = bt2.TraceCollectionMessageIterator(
            specs, stream_intersection_mode=True
        )

        event_msgs = [x for x in msg_iter if type(x) is bt2._EventMessageConst]
        self.assertEqual(len(event_msgs), 3)
        self.assertEqual(
            event_msgs[0].default_clock_snapshot.ns_from_origin, 13516309000000071
        )
        self.assertEqual(
            event_msgs[1].default_clock_snapshot.ns_from_origin, 13516309000000072
        )
        self.assertEqual(
            event_msgs[2].default_clock_snapshot.ns_from_origin, 13516309000000082
        )

    def test_iter_no_intersection_two_traces(self):
        spec = bt2.ComponentSpec.from_named_plugin_and_component_class(
            "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
        )
        specs = [spec, spec]
        msg_iter = bt2.TraceCollectionMessageIterator(specs)
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 56)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 16)

    def test_iter_no_intersection_begin(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        msg_iter = bt2.TraceCollectionMessageIterator(specs, begin=13515309.000000023)
        hist = _count_msgs_by_type(msg_iter)
        self.assertEqual(hist[bt2._EventMessageConst], 6)

    def test_iter_no_intersection_end(self):
        specs = [
            bt2.ComponentSpec.from_named_plugin_and_component_class(
                "ctf", "fs", _3EVENTS_INTERSECT_TRACE_PATH
            )
        ]
        msg_iter = bt2.TraceCollectionMessageIterator(specs, end=13515309.000000075)
        hist = _count_msgs_by_type(msg_iter)
        self.assertEqual(hist[bt2._EventMessageConst], 5)

    def test_iter_auto_source_component_spec(self):
        specs = [bt2.AutoSourceComponentSpec(_3EVENTS_INTERSECT_TRACE_PATH)]
        msg_iter = bt2.TraceCollectionMessageIterator(specs)
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 28)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 8)

    def test_iter_auto_source_component_spec_list_of_strings(self):
        msg_iter = bt2.TraceCollectionMessageIterator([_3EVENTS_INTERSECT_TRACE_PATH])
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 28)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 8)

    def test_iter_auto_source_component_spec_string(self):
        msg_iter = bt2.TraceCollectionMessageIterator(_3EVENTS_INTERSECT_TRACE_PATH)
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 28)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 8)

    def test_iter_mixed_inputs(self):
        msg_iter = bt2.TraceCollectionMessageIterator(
            [
                _3EVENTS_INTERSECT_TRACE_PATH,
                bt2.AutoSourceComponentSpec(_SEQUENCE_TRACE_PATH),
                bt2.ComponentSpec.from_named_plugin_and_component_class(
                    "ctf", "fs", _NOINTERSECT_TRACE_PATH
                ),
            ]
        )
        msgs = list(msg_iter)
        self.assertEqual(len(msgs), 76)
        hist = _count_msgs_by_type(msgs)
        self.assertEqual(hist[bt2._EventMessageConst], 24)

    def test_auto_source_component_non_existent(self):
        with self.assertRaisesRegex(
            RuntimeError,
            "Some auto source component specs did not produce any component",
        ):
            # Test with one path known to contain a trace and one path known
            # to not contain any trace.
            bt2.TraceCollectionMessageIterator(
                [_SEQUENCE_TRACE_PATH, "/this/path/better/not/exist"]
            )


class _TestAutoDiscoverSourceComponentSpecs(unittest.TestCase):
    def setUp(self):
        self._saved_babeltrace_plugin_path = os.environ["BABELTRACE_PLUGIN_PATH"]
        os.environ["BABELTRACE_PLUGIN_PATH"] += os.pathsep + self._plugin_path

    def tearDown(self):
        os.environ["BABELTRACE_PLUGIN_PATH"] = self._saved_babeltrace_plugin_path


@unittest.skipUnless(
    _BT_ENABLE_PYTHON_PLUGINS,
    "Support for Python plugins is disabled",
)
class TestAutoDiscoverSourceComponentSpecsGrouping(
    _TestAutoDiscoverSourceComponentSpecs
):
    _plugin_path = _AUTO_SOURCE_DISCOVERY_GROUPING_PATH

    def test_grouping(self):
        specs = [
            bt2.AutoSourceComponentSpec("ABCDE"),
            bt2.AutoSourceComponentSpec(_AUTO_SOURCE_DISCOVERY_GROUPING_PATH),
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 8)

        self.assertEqual(msgs[0].stream.name, "TestSourceABCDE: ABCDE")
        self.assertEqual(msgs[1].stream.name, "TestSourceExt: aaa1, aaa2, aaa3")
        self.assertEqual(msgs[2].stream.name, "TestSourceExt: bbb1, bbb2")
        self.assertEqual(msgs[3].stream.name, "TestSourceExt: ccc1")
        self.assertEqual(msgs[4].stream.name, "TestSourceExt: ccc2")
        self.assertEqual(msgs[5].stream.name, "TestSourceExt: ccc3")
        self.assertEqual(msgs[6].stream.name, "TestSourceExt: ccc4")
        self.assertEqual(msgs[7].stream.name, "TestSourceSomeDir: some-dir")


@unittest.skipUnless(
    _BT_ENABLE_PYTHON_PLUGINS,
    "Support for Python plugins is disabled",
)
class TestAutoDiscoverSourceComponentSpecsParamsObjLogLevel(
    _TestAutoDiscoverSourceComponentSpecs
):
    _plugin_path = _AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH

    _dir_a = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, "dir-a")
    _dir_b = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, "dir-b")
    _dir_ab = os.path.join(_AUTO_SOURCE_DISCOVERY_PARAMS_LOG_LEVEL_PATH, "dir-ab")

    def _test_two_comps_from_one_spec(self, params, obj=None, logging_level=None):
        specs = [
            bt2.AutoSourceComponentSpec(
                self._dir_ab, params=params, obj=obj, logging_level=logging_level
            )
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 2)

        return msgs

    def test_params_two_comps_from_one_spec(self):
        msgs = self._test_two_comps_from_one_spec(
            params={"test-allo": "madame", "what": "test-params"}
        )

        self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: ('test-allo', 'madame')")

    def test_obj_two_comps_from_one_spec(self):
        msgs = self._test_two_comps_from_one_spec(
            params={"what": "python-obj"}, obj="deore"
        )

        self.assertEqual(msgs[0].stream.name, "TestSourceA: deore")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")

    def test_log_level_two_comps_from_one_spec(self):
        msgs = self._test_two_comps_from_one_spec(
            params={"what": "log-level"}, logging_level=bt2.LoggingLevel.DEBUG
        )

        self.assertEqual(
            msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG)
        )
        self.assertEqual(
            msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG)
        )

    def _test_two_comps_from_two_specs(
        self,
        params_a=None,
        params_b=None,
        obj_a=None,
        obj_b=None,
        logging_level_a=None,
        logging_level_b=None,
    ):
        specs = [
            bt2.AutoSourceComponentSpec(
                self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a
            ),
            bt2.AutoSourceComponentSpec(
                self._dir_b, params=params_b, obj=obj_b, logging_level=logging_level_b
            ),
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 2)

        return msgs

    def test_params_two_comps_from_two_specs(self):
        msgs = self._test_two_comps_from_two_specs(
            params_a={"test-allo": "madame", "what": "test-params"},
            params_b={"test-bonjour": "monsieur", "what": "test-params"},
        )

        self.assertEqual(msgs[0].stream.name, "TestSourceA: ('test-allo', 'madame')")
        self.assertEqual(
            msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')"
        )

    def test_obj_two_comps_from_two_specs(self):
        msgs = self._test_two_comps_from_two_specs(
            params_a={"what": "python-obj"},
            params_b={"what": "python-obj"},
            obj_a="deore",
            obj_b="alivio",
        )

        self.assertEqual(msgs[0].stream.name, "TestSourceA: deore")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio")

    def test_log_level_two_comps_from_two_specs(self):
        msgs = self._test_two_comps_from_two_specs(
            params_a={"what": "log-level"},
            params_b={"what": "log-level"},
            logging_level_a=bt2.LoggingLevel.DEBUG,
            logging_level_b=bt2.LoggingLevel.TRACE,
        )

        self.assertEqual(
            msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.DEBUG)
        )
        self.assertEqual(
            msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE)
        )

    def _test_one_comp_from_one_spec_one_comp_from_both_1(
        self,
        params_a=None,
        params_ab=None,
        obj_a=None,
        obj_ab=None,
        logging_level_a=None,
        logging_level_ab=None,
    ):
        specs = [
            bt2.AutoSourceComponentSpec(
                self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a
            ),
            bt2.AutoSourceComponentSpec(
                self._dir_ab,
                params=params_ab,
                obj=obj_ab,
                logging_level=logging_level_ab,
            ),
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 2)

        return msgs

    def test_params_one_comp_from_one_spec_one_comp_from_both_1(self):
        msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1(
            params_a={"test-allo": "madame", "what": "test-params"},
            params_ab={"test-bonjour": "monsieur", "what": "test-params"},
        )

        self.assertEqual(
            msgs[0].stream.name,
            "TestSourceA: ('test-allo', 'madame'), ('test-bonjour', 'monsieur')",
        )
        self.assertEqual(
            msgs[1].stream.name, "TestSourceB: ('test-bonjour', 'monsieur')"
        )

    def test_obj_one_comp_from_one_spec_one_comp_from_both_1(self):
        msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1(
            params_a={"what": "python-obj"},
            params_ab={"what": "python-obj"},
            obj_a="deore",
            obj_ab="alivio",
        )

        self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: alivio")

    def test_log_level_one_comp_from_one_spec_one_comp_from_both_1(self):
        msgs = self._test_one_comp_from_one_spec_one_comp_from_both_1(
            params_a={"what": "log-level"},
            params_ab={"what": "log-level"},
            logging_level_a=bt2.LoggingLevel.DEBUG,
            logging_level_ab=bt2.LoggingLevel.TRACE,
        )

        self.assertEqual(
            msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE)
        )
        self.assertEqual(
            msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.TRACE)
        )

    def _test_one_comp_from_one_spec_one_comp_from_both_2(
        self,
        params_ab=None,
        params_a=None,
        obj_ab=None,
        obj_a=None,
        logging_level_ab=None,
        logging_level_a=None,
    ):
        specs = [
            bt2.AutoSourceComponentSpec(
                self._dir_ab,
                params=params_ab,
                obj=obj_ab,
                logging_level=logging_level_ab,
            ),
            bt2.AutoSourceComponentSpec(
                self._dir_a, params=params_a, obj=obj_a, logging_level=logging_level_a
            ),
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 2)

        return msgs

    def test_params_one_comp_from_one_spec_one_comp_from_both_2(self):
        msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2(
            params_ab={
                "test-bonjour": "madame",
                "test-salut": "les amis",
                "what": "test-params",
            },
            params_a={"test-bonjour": "monsieur", "what": "test-params"},
        )

        self.assertEqual(
            msgs[0].stream.name,
            "TestSourceA: ('test-bonjour', 'monsieur'), ('test-salut', 'les amis')",
        )
        self.assertEqual(
            msgs[1].stream.name,
            "TestSourceB: ('test-bonjour', 'madame'), ('test-salut', 'les amis')",
        )

    def test_obj_one_comp_from_one_spec_one_comp_from_both_2(self):
        msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2(
            params_ab={"what": "python-obj"},
            params_a={"what": "python-obj"},
            obj_ab="deore",
            obj_a="alivio",
        )

        self.assertEqual(msgs[0].stream.name, "TestSourceA: alivio")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")

    def test_log_level_one_comp_from_one_spec_one_comp_from_both_2(self):
        msgs = self._test_one_comp_from_one_spec_one_comp_from_both_2(
            params_ab={"what": "log-level"},
            params_a={"what": "log-level"},
            logging_level_ab=bt2.LoggingLevel.DEBUG,
            logging_level_a=bt2.LoggingLevel.TRACE,
        )

        self.assertEqual(
            msgs[0].stream.name, "TestSourceA: {}".format(bt2.LoggingLevel.TRACE)
        )
        self.assertEqual(
            msgs[1].stream.name, "TestSourceB: {}".format(bt2.LoggingLevel.DEBUG)
        )

    def test_obj_override_with_none(self):
        specs = [
            bt2.AutoSourceComponentSpec(
                self._dir_ab, params={"what": "python-obj"}, obj="deore"
            ),
            bt2.AutoSourceComponentSpec(
                self._dir_a, params={"what": "python-obj"}, obj=None
            ),
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 2)
        self.assertEqual(msgs[0].stream.name, "TestSourceA: None")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")

    def test_obj_no_override_with_no_obj(self):
        specs = [
            bt2.AutoSourceComponentSpec(
                self._dir_ab, params={"what": "python-obj"}, obj="deore"
            ),
            bt2.AutoSourceComponentSpec(self._dir_a, params={"what": "python-obj"}),
        ]
        it = bt2.TraceCollectionMessageIterator(specs)
        msgs = [x for x in it if type(x) is bt2._StreamBeginningMessageConst]

        self.assertEqual(len(msgs), 2)
        self.assertEqual(msgs[0].stream.name, "TestSourceA: deore")
        self.assertEqual(msgs[1].stream.name, "TestSourceB: deore")


class TestAutoDiscoverFailures(unittest.TestCase):
    def test_metadata_syntax_error(self):
        with self.assertRaisesRegex(
            bt2._Error,
            'At line 3 in metadata stream: syntax error, unexpected CTF_RSBRAC: token="]"',
        ):
            specs = [bt2.AutoSourceComponentSpec(_METADATA_SYNTAX_ERROR_TRACE_PATH)]
            bt2.TraceCollectionMessageIterator(specs)


if __name__ == "__main__":
    unittest.main()