Commit | Line | Data |
---|---|---|
d2d857a8 MJ |
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 | ||
704c2307 PP |
19 | import unittest |
20 | import datetime | |
704c2307 PP |
21 | import bt2 |
22 | import os | |
23 | import os.path | |
24 | ||
25 | ||
bbff0ab4 | 26 | _BT_CTF_TRACES_PATH = os.environ['BT_CTF_TRACES_PATH'] |
cfbd7cf3 FD |
27 | _3EVENTS_INTERSECT_TRACE_PATH = os.path.join( |
28 | _BT_CTF_TRACES_PATH, 'intersection', '3eventsintersect' | |
29 | ) | |
704c2307 PP |
30 | |
31 | ||
3d60267b | 32 | class ComponentSpecTestCase(unittest.TestCase): |
704c2307 | 33 | def test_create_good_no_params(self): |
907f2b70 | 34 | bt2.ComponentSpec('plugin', 'compcls') |
704c2307 PP |
35 | |
36 | def test_create_good_with_params(self): | |
907f2b70 | 37 | bt2.ComponentSpec('plugin', 'compcls', {'salut': 23}) |
704c2307 PP |
38 | |
39 | def test_create_good_with_path_params(self): | |
3d60267b | 40 | spec = bt2.ComponentSpec('plugin', 'compcls', 'a path') |
73760435 | 41 | self.assertEqual(spec.params['inputs'], ['a path']) |
704c2307 PP |
42 | |
43 | def test_create_wrong_plugin_name_type(self): | |
44 | with self.assertRaises(TypeError): | |
907f2b70 | 45 | bt2.ComponentSpec(23, 'compcls') |
704c2307 PP |
46 | |
47 | def test_create_wrong_component_class_name_type(self): | |
48 | with self.assertRaises(TypeError): | |
907f2b70 | 49 | bt2.ComponentSpec('plugin', 190) |
704c2307 PP |
50 | |
51 | def test_create_wrong_params_type(self): | |
52 | with self.assertRaises(TypeError): | |
907f2b70 SM |
53 | bt2.ComponentSpec('dwdw', 'compcls', datetime.datetime.now()) |
54 | ||
55 | ||
56 | # Return a map, msg type -> number of messages of this type. | |
57 | ||
cfbd7cf3 | 58 | |
907f2b70 SM |
59 | def _count_msgs_by_type(msgs): |
60 | res = {} | |
61 | ||
62 | for msg in msgs: | |
63 | t = type(msg) | |
64 | n = res.get(t, 0) | |
65 | res[t] = n + 1 | |
66 | ||
67 | return res | |
704c2307 PP |
68 | |
69 | ||
5602ef81 | 70 | class TraceCollectionMessageIteratorTestCase(unittest.TestCase): |
704c2307 | 71 | def test_create_wrong_stream_intersection_mode_type(self): |
3d60267b | 72 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
704c2307 PP |
73 | |
74 | with self.assertRaises(TypeError): | |
907f2b70 | 75 | bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=23) |
704c2307 PP |
76 | |
77 | def test_create_wrong_begin_type(self): | |
3d60267b | 78 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
704c2307 PP |
79 | |
80 | with self.assertRaises(TypeError): | |
907f2b70 | 81 | bt2.TraceCollectionMessageIterator(specs, begin='hi') |
704c2307 PP |
82 | |
83 | def test_create_wrong_end_type(self): | |
3d60267b | 84 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
704c2307 PP |
85 | |
86 | with self.assertRaises(TypeError): | |
907f2b70 | 87 | bt2.TraceCollectionMessageIterator(specs, begin='lel') |
704c2307 PP |
88 | |
89 | def test_create_no_such_plugin(self): | |
3d60267b | 90 | specs = [bt2.ComponentSpec('77', '101', _3EVENTS_INTERSECT_TRACE_PATH)] |
704c2307 | 91 | |
ce4923b0 | 92 | with self.assertRaises(ValueError): |
907f2b70 | 93 | bt2.TraceCollectionMessageIterator(specs) |
704c2307 PP |
94 | |
95 | def test_create_begin_s(self): | |
3d60267b | 96 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
907f2b70 | 97 | bt2.TraceCollectionMessageIterator(specs, begin=19457.918232) |
704c2307 PP |
98 | |
99 | def test_create_end_s(self): | |
3d60267b | 100 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
907f2b70 | 101 | bt2.TraceCollectionMessageIterator(specs, end=123.12312) |
704c2307 PP |
102 | |
103 | def test_create_begin_datetime(self): | |
3d60267b | 104 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
907f2b70 | 105 | bt2.TraceCollectionMessageIterator(specs, begin=datetime.datetime.now()) |
704c2307 PP |
106 | |
107 | def test_create_end_datetime(self): | |
3d60267b | 108 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
907f2b70 | 109 | bt2.TraceCollectionMessageIterator(specs, end=datetime.datetime.now()) |
704c2307 PP |
110 | |
111 | def test_iter_no_intersection(self): | |
3d60267b | 112 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
5602ef81 | 113 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
907f2b70 | 114 | msgs = list(msg_iter) |
188edac1 | 115 | self.assertEqual(len(msgs), 28) |
907f2b70 | 116 | hist = _count_msgs_by_type(msgs) |
3fb99a22 | 117 | self.assertEqual(hist[bt2._EventMessage], 8) |
704c2307 | 118 | |
907f2b70 | 119 | # Same as the above, but we pass a single spec instead of a spec list. |
3d60267b PP |
120 | def test_iter_specs_not_list(self): |
121 | spec = bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH) | |
907f2b70 SM |
122 | msg_iter = bt2.TraceCollectionMessageIterator(spec) |
123 | msgs = list(msg_iter) | |
188edac1 | 124 | self.assertEqual(len(msgs), 28) |
907f2b70 | 125 | hist = _count_msgs_by_type(msgs) |
3fb99a22 | 126 | self.assertEqual(hist[bt2._EventMessage], 8) |
3d60267b PP |
127 | |
128 | def test_iter_custom_filter(self): | |
129 | src_spec = bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH) | |
cfbd7cf3 | 130 | flt_spec = bt2.ComponentSpec('utils', 'trimmer', {'end': '13515309.000000075'}) |
907f2b70 SM |
131 | msg_iter = bt2.TraceCollectionMessageIterator(src_spec, flt_spec) |
132 | hist = _count_msgs_by_type(msg_iter) | |
3fb99a22 | 133 | self.assertEqual(hist[bt2._EventMessage], 5) |
3d60267b | 134 | |
704c2307 | 135 | def test_iter_intersection(self): |
3d60267b | 136 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
cfbd7cf3 FD |
137 | msg_iter = bt2.TraceCollectionMessageIterator( |
138 | specs, stream_intersection_mode=True | |
139 | ) | |
907f2b70 | 140 | msgs = list(msg_iter) |
188edac1 | 141 | self.assertEqual(len(msgs), 15) |
907f2b70 | 142 | hist = _count_msgs_by_type(msgs) |
3fb99a22 | 143 | self.assertEqual(hist[bt2._EventMessage], 3) |
704c2307 | 144 | |
73760435 | 145 | def test_iter_intersection_no_inputs_param(self): |
3d60267b | 146 | specs = [bt2.ComponentSpec('text', 'dmesg', {'read-from-stdin': True})] |
704c2307 | 147 | |
ce4923b0 | 148 | with self.assertRaises(ValueError): |
907f2b70 | 149 | bt2.TraceCollectionMessageIterator(specs, stream_intersection_mode=True) |
704c2307 PP |
150 | |
151 | def test_iter_no_intersection_two_traces(self): | |
3d60267b | 152 | spec = bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH) |
704c2307 | 153 | specs = [spec, spec] |
5602ef81 | 154 | msg_iter = bt2.TraceCollectionMessageIterator(specs) |
907f2b70 | 155 | msgs = list(msg_iter) |
188edac1 | 156 | self.assertEqual(len(msgs), 56) |
907f2b70 | 157 | hist = _count_msgs_by_type(msgs) |
3fb99a22 | 158 | self.assertEqual(hist[bt2._EventMessage], 16) |
704c2307 PP |
159 | |
160 | def test_iter_no_intersection_begin(self): | |
3d60267b | 161 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
907f2b70 SM |
162 | msg_iter = bt2.TraceCollectionMessageIterator(specs, begin=13515309.000000023) |
163 | hist = _count_msgs_by_type(msg_iter) | |
3fb99a22 | 164 | self.assertEqual(hist[bt2._EventMessage], 6) |
704c2307 PP |
165 | |
166 | def test_iter_no_intersection_end(self): | |
3d60267b | 167 | specs = [bt2.ComponentSpec('ctf', 'fs', _3EVENTS_INTERSECT_TRACE_PATH)] |
907f2b70 SM |
168 | msg_iter = bt2.TraceCollectionMessageIterator(specs, end=13515309.000000075) |
169 | hist = _count_msgs_by_type(msg_iter) | |
3fb99a22 | 170 | self.assertEqual(hist[bt2._EventMessage], 5) |