From 39b351f91e4c70b30530b915d49b74d6004dac42 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 12 Aug 2019 14:37:58 -0400 Subject: [PATCH] bt2: raise an exception if an AutoSourceComponentSpec produces no component We don't do anything special at the moment if an AutoSourceComponentSpec passed to TraceCollectionMessageIterator produces no component. The created graph will have no source component and the message iterator will produce no message: In [5]: list(bt2.TraceCollectionMessageIterator('/yomadame')) 08-12 14:43:03.938 20284 20284 W CLI-CFG-SRC-AUTO-DISC auto_discover_source_components@autodisc.c:755 No trace was found based on input `/yomadame`. Out[5]: [] This patch changes the behavior to be a bit more strict. If any AutoSourceComponentSpec passed to TraceCollectionMessageIterator ends up producing no component, an exception is raised. In the following example, the directory `/home/smarchi/lttng-traces` does contain some valid traces, and therefore produces some components, whereas the string `/yomadame` does not produce any. In [2]: bt2.TraceCollectionMessageIterator(['/home/smarchi/lttng-traces', '/yomadame']) 08-12 14:45:06.433 24428 24428 W CLI-CFG-SRC-AUTO-DISC auto_discover_source_components@autodisc.c:755 No trace was found based on input `/yomadame`. --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) in ----> 1 bt2.TraceCollectionMessageIterator(['/home/smarchi/lttng-traces', '/yomadame']) ~/build/babeltrace/src/bindings/python/bt2/build/build_lib/bt2/trace_collection_message_iterator.py in __init__(self, source_component_specs, filter_component_specs, stream_intersection_mode, begin, end, plugin_set) 292 ] 293 self._src_comp_specs += _auto_discover_source_component_specs( --> 294 auto_src_comp_specs, plugin_set 295 ) 296 ~/build/babeltrace/src/bindings/python/bt2/build/build_lib/bt2/trace_collection_message_iterator.py in _auto_discover_source_component_specs(auto_source_comp_specs, plugin_set) 194 195 msg = 'Some auto source component specs did not produce any component: ' + ', '.join(unused_inputs) --> 196 raise RuntimeError(msg) 197 198 return comp_specs RuntimeError: Some auto source component specs did not produce any component: /yomadame Change-Id: I6c6df9fad18915e294559948812a8213596affdf Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/1885 Tested-by: jenkins Reviewed-by: Philippe Proulx --- .../bt2/bt2/trace_collection_message_iterator.py | 15 +++++++++++++++ .../bt2/test_trace_collection_message_iterator.py | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py b/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py index 56b48da9..3003c5f3 100644 --- a/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py +++ b/src/bindings/python/bt2/bt2/trace_collection_message_iterator.py @@ -130,6 +130,8 @@ def _auto_discover_source_component_specs(auto_source_comp_specs, plugin_set): comp_specs_raw = res['results'] assert type(comp_specs_raw) == bt2.ArrayValue + used_input_indices = set() + for comp_spec_raw in comp_specs_raw: assert type(comp_spec_raw) == bt2.ArrayValue assert len(comp_spec_raw) == 4 @@ -171,6 +173,8 @@ def _auto_discover_source_component_specs(auto_source_comp_specs, plugin_set): if orig_spec.obj is not AutoSourceComponentSpec._no_obj: obj = orig_spec.obj + used_input_indices.add(int(idx)) + params['inputs'] = comp_inputs comp_specs.append( @@ -183,6 +187,17 @@ def _auto_discover_source_component_specs(auto_source_comp_specs, plugin_set): ) ) + if len(used_input_indices) != len(inputs): + unused_input_indices = set(range(len(inputs))) - used_input_indices + unused_input_indices = sorted(unused_input_indices) + unused_inputs = [str(inputs[x]) for x in unused_input_indices] + + msg = ( + 'Some auto source component specs did not produce any component: ' + + ', '.join(unused_inputs) + ) + raise RuntimeError(msg) + return comp_specs diff --git a/tests/bindings/python/bt2/test_trace_collection_message_iterator.py b/tests/bindings/python/bt2/test_trace_collection_message_iterator.py index 22e12cde..16261708 100644 --- a/tests/bindings/python/bt2/test_trace_collection_message_iterator.py +++ b/tests/bindings/python/bt2/test_trace_collection_message_iterator.py @@ -215,6 +215,17 @@ class TraceCollectionMessageIteratorTestCase(unittest.TestCase): hist = _count_msgs_by_type(msgs) self.assertEqual(hist[bt2._EventMessage], 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): @@ -234,7 +245,6 @@ class TestAutoDiscoverSourceComponentSpecsGrouping( specs = [ bt2.AutoSourceComponentSpec('ABCDE'), bt2.AutoSourceComponentSpec(_AUTO_SOURCE_DISCOVERY_GROUPING_PATH), - bt2.AutoSourceComponentSpec('does-not-exist'), ] it = bt2.TraceCollectionMessageIterator(specs) msgs = [x for x in it if type(x) is bt2._StreamBeginningMessage] -- 2.34.1