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