Apply black code formatter on all Python code
[babeltrace.git] / tests / plugins / src.ctf.fs / query / test_query_trace_info.py
CommitLineData
d907165c
SM
1# Copyright (C) 2019 Simon Marchi <simon.marchi@efficios.com>
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; only version 2
6# of the License.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17import unittest
18import bt2
19import os
a38d7650 20import re
d907165c
SM
21
22
bbff0ab4 23test_ctf_traces_path = os.environ['BT_CTF_TRACES_PATH']
d907165c
SM
24
25
1d4ac4b6
FD
26# Key to sort streams in a predictable order.
27def sort_predictably(stream):
28 if 'range-ns' in stream:
29 return stream['range-ns']['begin']
30 else:
31 return stream['paths'][0]
d907165c
SM
32
33
34class QueryTraceInfoClockOffsetTestCase(unittest.TestCase):
d907165c
SM
35 def setUp(self):
36 ctf = bt2.find_plugin('ctf')
37 self._fs = ctf.source_component_classes['fs']
38
cfbd7cf3
FD
39 self._paths = [
40 os.path.join(test_ctf_traces_path, 'intersection', '3eventsintersect')
41 ]
d907165c
SM
42 self._executor = bt2.QueryExecutor()
43
44 def _check(self, trace, offset):
45 self.assertEqual(trace['range-ns']['begin'], 13515309000000000 + offset)
46 self.assertEqual(trace['range-ns']['end'], 13515309000000120 + offset)
cfbd7cf3
FD
47 self.assertEqual(
48 trace['intersection-range-ns']['begin'], 13515309000000070 + offset
49 )
50 self.assertEqual(
51 trace['intersection-range-ns']['end'], 13515309000000100 + offset
52 )
d907165c 53
1d4ac4b6 54 streams = sorted(trace['streams'], key=sort_predictably)
d907165c
SM
55 self.assertEqual(streams[0]['range-ns']['begin'], 13515309000000000 + offset)
56 self.assertEqual(streams[0]['range-ns']['end'], 13515309000000100 + offset)
57 self.assertEqual(streams[1]['range-ns']['begin'], 13515309000000070 + offset)
58 self.assertEqual(streams[1]['range-ns']['end'], 13515309000000120 + offset)
59
60 # Test various cominations of the clock-class-offset-s and
61 # clock-class-offset-ns parameters to trace-info queries.
62
63 # Without clock class offset
64
65 def test_no_clock_class_offset(self):
cfbd7cf3 66 res = self._executor.query(self._fs, 'trace-info', {'paths': self._paths})
d907165c
SM
67 trace = res[0]
68 self._check(trace, 0)
69
70 # With clock-class-offset-s
71
72 def test_clock_class_offset_s(self):
cfbd7cf3
FD
73 res = self._executor.query(
74 self._fs, 'trace-info', {'paths': self._paths, 'clock-class-offset-s': 2}
75 )
d907165c
SM
76 trace = res[0]
77 self._check(trace, 2000000000)
78
79 # With clock-class-offset-ns
80
81 def test_clock_class_offset_ns(self):
cfbd7cf3
FD
82 res = self._executor.query(
83 self._fs, 'trace-info', {'paths': self._paths, 'clock-class-offset-ns': 2}
84 )
d907165c
SM
85 trace = res[0]
86 self._check(trace, 2)
87
88 # With both, negative
89
90 def test_clock_class_offset_both(self):
cfbd7cf3
FD
91 res = self._executor.query(
92 self._fs,
93 'trace-info',
94 {
95 'paths': self._paths,
96 'clock-class-offset-s': -2,
97 'clock-class-offset-ns': -2,
98 },
99 )
d907165c
SM
100 trace = res[0]
101 self._check(trace, -2000000002)
102
103 def test_clock_class_offset_s_wrong_type(self):
d24d5663 104 with self.assertRaises(bt2.InvalidParams):
cfbd7cf3
FD
105 self._executor.query(
106 self._fs,
107 'trace-info',
108 {'paths': self._paths, 'clock-class-offset-s': "2"},
109 )
d907165c
SM
110
111 def test_clock_class_offset_s_wrong_type_none(self):
d24d5663 112 with self.assertRaises(bt2.InvalidParams):
cfbd7cf3
FD
113 self._executor.query(
114 self._fs,
115 'trace-info',
116 {'paths': self._paths, 'clock-class-offset-s': None},
117 )
d907165c
SM
118
119 def test_clock_class_offset_ns_wrong_type(self):
d24d5663 120 with self.assertRaises(bt2.InvalidParams):
cfbd7cf3
FD
121 self._executor.query(
122 self._fs,
123 'trace-info',
124 {'paths': self._paths, 'clock-class-offset-ns': "2"},
125 )
d907165c
SM
126
127 def test_clock_class_offset_ns_wrong_type_none(self):
d24d5663 128 with self.assertRaises(bt2.InvalidParams):
cfbd7cf3
FD
129 self._executor.query(
130 self._fs,
131 'trace-info',
132 {'paths': self._paths, 'clock-class-offset-ns': None},
133 )
d907165c 134
a38d7650
SM
135
136class QueryTraceInfoPortNameTestCase(unittest.TestCase):
137 def setUp(self):
138 ctf = bt2.find_plugin("ctf")
139 self._fs = ctf.source_component_classes["fs"]
140
141 self._executor = bt2.QueryExecutor()
142
143 def test_trace_uuid_stream_class_id_no_stream_id(self):
144 res = self._executor.query(
145 self._fs,
146 "trace-info",
147 {
148 "paths": [
149 os.path.join(
150 test_ctf_traces_path, "intersection", "3eventsintersect"
151 )
152 ]
153 },
154 )
155 self.assertEqual(len(res), 1)
156 trace = res[0]
1d4ac4b6 157 streams = sorted(trace["streams"], key=sort_predictably)
a38d7650
SM
158 self.assertEqual(len(streams), 2)
159 self.assertRegexpMatches(
160 str(streams[0]["port-name"]),
bbff0ab4 161 r"^7afe8fbe-79b8-4f6a-bbc7-d0c782e7ddaf \| 0 \| .*/tests/data/ctf-traces/intersection/3eventsintersect/test_stream_0$",
a38d7650
SM
162 )
163 self.assertRegexpMatches(
164 str(streams[1]["port-name"]),
bbff0ab4 165 r"^7afe8fbe-79b8-4f6a-bbc7-d0c782e7ddaf \| 0 \| .*/tests/data/ctf-traces/intersection/3eventsintersect/test_stream_1$",
a38d7650
SM
166 )
167
168 def test_trace_uuid_no_stream_class_id_no_stream_id(self):
169 res = self._executor.query(
170 self._fs,
171 "trace-info",
172 {"paths": [os.path.join(test_ctf_traces_path, "succeed", "succeed1")]},
173 )
174 self.assertEqual(len(res), 1)
175 trace = res[0]
1d4ac4b6 176 streams = sorted(trace["streams"], key=sort_predictably)
a38d7650
SM
177 self.assertEqual(len(streams), 1)
178 self.assertRegexpMatches(
179 str(streams[0]["port-name"]),
bbff0ab4 180 r"^2a6422d0-6cee-11e0-8c08-cb07d7b3a564 \| .*/tests/data/ctf-traces/succeed/succeed1/dummystream$",
a38d7650
SM
181 )
182
183
1d4ac4b6
FD
184class QueryTraceInfoRangeTestCase(unittest.TestCase):
185 def setUp(self):
186 ctf = bt2.find_plugin("ctf")
187 self._fs = ctf.source_component_classes["fs"]
188
189 self._executor = bt2.QueryExecutor()
190
191 def test_trace_no_range(self):
192 # This trace has no `timestamp_begin` and `timestamp_end` in its packet
193 # context. The `trace-info` query should omit the `range-ns` fields in
194 # the `trace` and `stream` data structures.
195
196 res = self._executor.query(
197 self._fs,
198 "trace-info",
199 {"paths": [os.path.join(test_ctf_traces_path, "succeed", "succeed1")]},
200 )
201
202 self.assertEqual(len(res), 1)
203 trace = res[0]
204 streams = trace["streams"]
205 self.assertEqual(len(streams), 1)
206
207 self.assertRaises(KeyError, lambda: trace['range-ns'])
208 self.assertRaises(KeyError, lambda: streams[0]['range-ns'])
209
210
d907165c
SM
211if __name__ == '__main__':
212 unittest.main()
This page took 0.040407 seconds and 4 git commands to generate.