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