def test_query_not_implemented(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass with self.assertRaises(bt2.UnknownObject): bt2.QueryExecutor(MySink, 'obj', 23).query()
def test_query(self): res = bt2.QueryExecutor(self._comp_cls, 'an object', { 'yes': 'no', 'book': -17 }).query() expected = ['an object', {'yes': 'no', 'book': -17}, 23] self.assertEqual(res, expected)
def _compute_stream_intersections(self): # Pre-compute the trimmer range to use for each port in the graph, when # stream intersection mode is enabled. self._stream_inter_port_to_range = {} for src_comp_and_spec in self._src_comps_and_specs: # Query the port's component for the `babeltrace.trace-infos` # object which contains the range for each stream, from which we can # compute the intersection of the streams in each trace. query_exec = bt2.QueryExecutor( src_comp_and_spec.spec.component_class, 'babeltrace.trace-infos', src_comp_and_spec.spec.params, ) trace_infos = query_exec.query() for trace_info in trace_infos: begin = max([ stream['range-ns']['begin'] for stream in trace_info['stream-infos'] ]) end = min([ stream['range-ns']['end'] for stream in trace_info['stream-infos'] ]) # Each port associated to this trace will have this computed # range. for stream in trace_info['stream-infos']: # A port name is unique within a component, but not # necessarily across all components. Use a component # and port name pair to make it unique across the graph. port_name = str(stream['port-name']) key = (src_comp_and_spec.comp.addr, port_name) self._stream_inter_port_to_range[key] = (begin, end)
def test_query(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): nonlocal query_params query_params = params return {'null': None, 'bt2': 'BT2'} query_params = None params = { 'array': ['coucou', 23, None], 'other_map': { 'yes': 'yeah', '19': 19, 'minus 1.5': -1.5 }, 'null': None, } res = bt2.QueryExecutor(MySink, 'obj', params).query() self.assertIs(type(res), bt2._MapValueConst) self.assertIs(type(res['bt2']), bt2._StringValueConst) self.assertEqual(query_params, params) self.assertEqual(res, {'null': None, 'bt2': 'BT2'}) del query_params
def test_trace_uuid_stream_class_id_no_stream_id(self): res = bt2.QueryExecutor( self._fs, "babeltrace.trace-infos", { "inputs": [ os.path.join(test_ctf_traces_path, "intersection", "3eventsintersect") ] }, ).query() if os.environ['BT_OS_TYPE'] == 'mingw': os_stream_path = ( '\\tests\\data\\ctf-traces\\intersection\\3eventsintersect\\') else: os_stream_path = '/tests/data/ctf-traces/intersection/3eventsintersect/' self.assertEqual(len(res), 1) trace = res[0] streams = sorted(trace["stream-infos"], key=sort_predictably) self.assertEqual(len(streams), 2) self.assertRegex( str(streams[0]["port-name"]), r"^7afe8fbe-79b8-4f6a-bbc7-d0c782e7ddaf \| 0 \| .*" + re.escape(os_stream_path + "test_stream_0") + r"$", ) self.assertRegex( str(streams[1]["port-name"]), r"^7afe8fbe-79b8-4f6a-bbc7-d0c782e7ddaf \| 0 \| .*" + re.escape(os_stream_path + "test_stream_1") + r"$", )
def test_query(self): class MySink(bt2._UserSinkComponent): def _consume(self): pass @classmethod def _query(cls, query_exec, obj, params): nonlocal query_params query_params = params return { 'null': None, 'bt2': 'BT2', } query_params = None params = { 'array': ['coucou', 23, None], 'other_map': { 'yes': 'yeah', '19': 19, 'minus 1.5': -1.5, }, 'null': None, } res = bt2.QueryExecutor().query(MySink, 'obj', params) self.assertEqual(query_params, params) self.assertEqual(res, { 'null': None, 'bt2': 'BT2', }) del query_params
def test_query_not_implemented(self): class MySink(bt2._UserSinkComponent): def _consume(self): pass with self.assertRaises(bt2.Error): bt2.QueryExecutor().query(MySink, 'obj', 23)
def do_one_query(input, expected_group): qe = bt2.QueryExecutor(fs, 'babeltrace.support-info', { 'input': input, 'type': 'directory' }) result = qe.query() self.assertEqual(result['group'], expected_group)
def test_default_interrupter(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass query_exec = bt2.QueryExecutor(MySink, 'obj') interrupter = query_exec.default_interrupter self.assertIs(type(interrupter), bt2.Interrupter)
def setUp(self): ctf = bt2.find_plugin('ctf') self._fs = ctf.source_component_classes['fs'] self._paths = [ os.path.join(test_ctf_traces_path, 'intersection', '3eventsintersect') ] self._executor = bt2.QueryExecutor()
def test_clock_class_offset_s_wrong_type(self): with self.assertRaises(bt2._Error): bt2.QueryExecutor( self._fs, 'babeltrace.trace-infos', { 'inputs': self._inputs, 'clock-class-offset-s': "2" }, ).query()
def _query_trace_info(self): try: result = bt2.QueryExecutor().query( self._trace_collection._fs_comp_cls, 'trace-info', {'path': self._path}) except: raise ValueError assert (len(result) == 1) return result
def test_query_try_again(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): raise bt2.TryAgain with self.assertRaises(bt2.TryAgain): bt2.QueryExecutor(MySink, 'obj', [17, 23]).query()
def test_query_gen_error(self): class MySink(bt2._UserSinkComponent): def _consume(self): pass @classmethod def _query(cls, query_exec, obj, params): raise ValueError with self.assertRaises(bt2.Error): res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
def test_clock_class_offset_ns(self): res = bt2.QueryExecutor( self._fs, 'babeltrace.trace-infos', { 'inputs': self._inputs, 'clock-class-offset-ns': 2 }, ).query() trace = res[0] self._check(trace, 2)
def test_query_unknown_object(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): raise bt2.UnknownObject with self.assertRaises(bt2.UnknownObject): bt2.QueryExecutor(MySink, 'obj', [17, 23]).query()
def test_query_returns_none(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @staticmethod def _user_query(priv_query_exec, obj, params, method_obj): return res = bt2.QueryExecutor(MySink, 'obj', None).query() self.assertIsNone(res)
def test_query_raises(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params): raise ValueError with self.assertRaises(bt2._Error): bt2.QueryExecutor(MySink, 'obj', 23).query()
def test_query_wrong_return_type(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): return ... with self.assertRaises(bt2._Error): bt2.QueryExecutor(MySink, 'obj', 23).query()
def test_query_with_method_obj_non_python_comp_cls(self): plugin = bt2.find_plugin('text', find_in_user_dir=False, find_in_sys_dir=False) assert plugin is not None cc = plugin.source_component_classes['dmesg'] assert cc is not None with self.assertRaisesRegex( ValueError, re.escape(r'cannot pass a Python object to a non-Python component class'), ): bt2.QueryExecutor(cc, 'obj', method_obj=object()).query()
def _test_lttng_quirks(self, trace_name): res = bt2.QueryExecutor( self._fs, "babeltrace.trace-infos", { "inputs": [os.path.join(self._path, trace_name)] }, ).query() self.assertEqual(len(res), 1) return res[0]
def test_query_invalid_params(self): class MySink(bt2._UserSinkComponent): def _consume(self): pass @classmethod def _query(cls, query_exec, obj, params): raise bt2.InvalidQueryParams with self.assertRaises(bt2.InvalidQueryParams): res = bt2.QueryExecutor().query(MySink, 'obj', [17, 23])
def test_query_logging_level_invalid_value(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): pass query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) with self.assertRaises(ValueError): query_exec.logging_level = 12345
def test_query_with_none_method_obj(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): nonlocal query_method_obj query_method_obj = method_obj query_method_obj = object() bt2.QueryExecutor(MySink, 'obj').query() self.assertIsNone(query_method_obj) del query_method_obj
def test_query_canceled(self): class MySink(bt2._UserSinkComponent): def _consume(self): pass @classmethod def _query(cls, query_exec, obj, params): raise bt2.TryAgain query_exec = bt2.QueryExecutor() query_exec.cancel() with self.assertRaises(bt2.QueryExecutorCanceled): res = query_exec.query(MySink, 'obj', [17, 23])
def test_query_interrupt(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): test_self.assertFalse(query_exec.is_interrupted) query_exec.default_interrupter.set() test_self.assertTrue(query_exec.is_interrupted) test_self = self query_exec = bt2.QueryExecutor(MySink, 'obj', [17, 23]) query_exec.query()
def test_query_params_none(self): class MySink(bt2._UserSinkComponent): def _consume(self): pass @classmethod def _query(cls, query_exec, obj, params): nonlocal query_params query_params = params query_params = 23 res = bt2.QueryExecutor().query(MySink, 'obj', None) self.assertEqual(query_params, None) del query_params
def test_query_no_params(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): nonlocal query_params query_params = params query_params = 23 bt2.QueryExecutor(MySink, 'obj').query() self.assertIs(query_params, None) del query_params
def test_query_logging_level(self): class MySink(bt2._UserSinkComponent): def _user_consume(self): pass @classmethod def _user_query(cls, priv_query_exec, obj, params, method_obj): nonlocal query_log_level query_log_level = priv_query_exec.logging_level query_log_level = None query_exec = bt2.QueryExecutor(MySink, 'obj', None) query_exec.logging_level = bt2.LoggingLevel.INFO query_exec.query() self.assertEqual(query_log_level, bt2.LoggingLevel.INFO) del query_log_level
def test_component_class_error_cause(self): q = bt2.QueryExecutor(SinkWithFailingQuery, 'hello') with self.assertRaises(bt2._Error) as ctx: q.query() cause = ctx.exception[0] self.assertIs(type(cause), bt2._ComponentClassErrorCause) self._common_cause_tests(cause) self.assertIn('Query is failing', cause.message) self.assertEqual(cause.component_class_type, bt2.ComponentClassType.SINK) self.assertEqual(cause.component_class_name, 'SinkWithFailingQuery') self.assertIsNone(cause.plugin_name)