|
1
|
|
|
import sys |
|
2
|
|
|
import math |
|
|
|
|
|
|
3
|
|
|
import time |
|
4
|
|
|
import socket |
|
5
|
|
|
import os.path |
|
6
|
|
|
import unittest |
|
7
|
|
|
from threading import Event |
|
8
|
|
|
from collections import deque |
|
9
|
|
|
|
|
10
|
|
|
MAX_DIFF = 8192 |
|
11
|
|
|
FILE_PATH = os.path.realpath(__file__) |
|
12
|
|
|
PATH = os.path.dirname(FILE_PATH) |
|
13
|
|
|
DAT_PATH = os.path.join(PATH, "dat") |
|
14
|
|
|
sys.path.insert(0, os.path.realpath(os.path.join(PATH, "../../../"))) |
|
15
|
|
|
import plumd.util |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
class TestPlumdUtils(unittest.TestCase): |
|
19
|
|
|
|
|
20
|
|
|
def test_get_hostname(self): |
|
21
|
|
|
"""ensure hostname properly generated""" |
|
22
|
|
|
hostname = socket.gethostname().split(".")[0] |
|
23
|
|
|
self.assertEqual(plumd.util.get_hostname(), hostname) |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
def test_get_file_map_normal(self): |
|
27
|
|
|
"""ensure get_file_map returns expected mappings""" |
|
28
|
|
|
self.maxDiff = MAX_DIFF |
|
29
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/normal1.dat") |
|
30
|
|
|
check = plumd.util.get_file_map(fname, 2, 0) |
|
31
|
|
|
expected = { |
|
32
|
|
|
'vda': deque(['253','0','12985','19498','566600','80185','8755', |
|
|
|
|
|
|
33
|
|
|
'25098','270730','344158','0','179702','424339']), |
|
|
|
|
|
|
34
|
|
|
'vda1': deque(['253','1','112','1231','2702','80','13','4','34', |
|
|
|
|
|
|
35
|
|
|
'79','0','120','159']), |
|
|
|
|
|
|
36
|
|
|
'vda2': deque(['253','2','12864','18251','563698','80078','8742', |
|
|
|
|
|
|
37
|
|
|
'25094','270696','344079','0','179608','424153']) |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
self.assertEqual(check, expected) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_get_file_map_short1(self): |
|
43
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
44
|
|
|
self.maxDiff = MAX_DIFF |
|
45
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/short1.dat") |
|
46
|
|
|
check = plumd.util.get_file_map(fname, 2, 0) |
|
47
|
|
|
expected = { |
|
48
|
|
|
'vda': deque(['253','0','12985','19498','566600','80185','8755', |
|
|
|
|
|
|
49
|
|
|
'25098','270730','344158','0','179702','424339']), |
|
|
|
|
|
|
50
|
|
|
'vda1': deque(['253','1','112','1231','2702','80','13','4','34', |
|
|
|
|
|
|
51
|
|
|
'79','0','120','159']) |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
self.assertEqual(check, expected) |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
def test_get_file_map_short2(self): |
|
57
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
58
|
|
|
self.maxDiff = MAX_DIFF |
|
59
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/short2.dat") |
|
60
|
|
|
check = plumd.util.get_file_map(fname, 2, 0) |
|
61
|
|
|
expected = { |
|
62
|
|
|
'vda': deque(['253','0','12985','19498','566600','80185','8755', |
|
|
|
|
|
|
63
|
|
|
'25098','270730','344158','0','179702','424339']), |
|
|
|
|
|
|
64
|
|
|
'vda1': deque(['253','1','112','1231','2702','80','13','4','34', |
|
|
|
|
|
|
65
|
|
|
'79','0','120','159']) |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
self.assertEqual(check, expected) |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def test_get_file_map_empty(self): |
|
71
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
72
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/empty.dat") |
|
73
|
|
|
check = plumd.util.get_file_map(fname, 2, 0) |
|
74
|
|
|
expected = {} |
|
75
|
|
|
self.assertEqual(check, expected) |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def test_get_file_map_list_normal(self): |
|
79
|
|
|
"""ensure get_file_map returns expected mappings""" |
|
80
|
|
|
self.maxDiff = MAX_DIFF |
|
81
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/normal1.dat") |
|
82
|
|
|
check = plumd.util.get_file_map_list(fname, 2, 0) |
|
83
|
|
|
expected = { |
|
84
|
|
|
'vda': ['253','0','12985','19498','566600','80185','8755', |
|
|
|
|
|
|
85
|
|
|
'25098','270730','344158','0','179702','424339'], |
|
|
|
|
|
|
86
|
|
|
'vda1': ['253','1','112','1231','2702','80','13','4','34', |
|
|
|
|
|
|
87
|
|
|
'79','0','120','159'], |
|
|
|
|
|
|
88
|
|
|
'vda2': ['253','2','12864','18251','563698','80078','8742', |
|
|
|
|
|
|
89
|
|
|
'25094','270696','344079','0','179608','424153'] |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
self.assertEqual(check, expected) |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
def test_get_file_map_list_short1(self): |
|
95
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
96
|
|
|
self.maxDiff = MAX_DIFF |
|
97
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/short1.dat") |
|
98
|
|
|
check = plumd.util.get_file_map_list(fname, 2, 0) |
|
99
|
|
|
expected = { |
|
100
|
|
|
'vda': ['253','0','12985','19498','566600','80185','8755', |
|
|
|
|
|
|
101
|
|
|
'25098','270730','344158','0','179702','424339'], |
|
|
|
|
|
|
102
|
|
|
'vda1': ['253','1','112','1231','2702','80','13','4','34', |
|
|
|
|
|
|
103
|
|
|
'79','0','120','159'] |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
self.assertEqual(check, expected) |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
def test_get_file_map_list_short2(self): |
|
109
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
110
|
|
|
self.maxDiff = MAX_DIFF |
|
111
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/short2.dat") |
|
112
|
|
|
check = plumd.util.get_file_map_list(fname, 2, 0) |
|
113
|
|
|
expected = { |
|
114
|
|
|
'vda': ['253','0','12985','19498','566600','80185','8755', |
|
|
|
|
|
|
115
|
|
|
'25098','270730','344158','0','179702','424339'], |
|
|
|
|
|
|
116
|
|
|
'vda1': ['253','1','112','1231','2702','80','13','4','34', |
|
|
|
|
|
|
117
|
|
|
'79','0','120','159'] |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
self.assertEqual(check, expected) |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
def test_get_file_map_list_empty(self): |
|
123
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
124
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/empty.dat") |
|
125
|
|
|
check = plumd.util.get_file_map_list(fname, 2, 0) |
|
126
|
|
|
expected = {} |
|
127
|
|
|
self.assertEqual(check, expected) |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
def test_get_file_map_list_nonexistent(self): |
|
131
|
|
|
"""ensure get_file_map properly handles invalid/short input""" |
|
132
|
|
|
fname = os.path.join(DAT_PATH, "get_file_map/nonexistantfile") |
|
133
|
|
|
check = plumd.util.get_file_map_list(fname, 2, 0) |
|
134
|
|
|
expected = {} |
|
135
|
|
|
self.assertEqual(check, expected) |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
def test_get_file_list_normal(self): |
|
139
|
|
|
"""ensure get_file_list properly handles normal inpurt""" |
|
140
|
|
|
fname = os.path.join(DAT_PATH, "get_file_list/normal1.dat") |
|
141
|
|
|
check = plumd.util.get_file_list(fname) |
|
142
|
|
|
evals = ['rootfs / rootfs rw 0 0', |
|
143
|
|
|
'sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0', |
|
144
|
|
|
'proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0'] |
|
145
|
|
|
expected = deque(evals) |
|
|
|
|
|
|
146
|
|
|
# issues with line endings |
|
147
|
|
|
for exval in evals: |
|
148
|
|
|
vval = check.popleft().rstrip('\r\n') |
|
149
|
|
|
self.assertEqual(vval, exval) |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
def test_get_file_list_empty(self): |
|
153
|
|
|
"""ensure get_file_list properly handles empty inpurt""" |
|
154
|
|
|
fname = os.path.join(DAT_PATH, "get_file_list/empty.dat") |
|
155
|
|
|
check = plumd.util.get_file_list(fname) |
|
156
|
|
|
expected = deque(['']) |
|
157
|
|
|
self.assertEqual(check, expected) |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
def test_get_file_list_nonexistent(self): |
|
161
|
|
|
"""ensure get_file_list properly handles empty inpurt""" |
|
162
|
|
|
fname = os.path.join(DAT_PATH, "get_file_list/nonexistantfile") |
|
163
|
|
|
check = plumd.util.get_file_list(fname) |
|
164
|
|
|
expected = deque(['']) |
|
165
|
|
|
self.assertEqual(check, expected) |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
def test_get_file_normal1(self): |
|
169
|
|
|
"""ensure get_file properly handles normal inpurt""" |
|
170
|
|
|
fname = os.path.join(DAT_PATH, "get_file/normal1.dat") |
|
171
|
|
|
check = plumd.util.get_file(fname) |
|
172
|
|
|
expected = "get_file Normal1" |
|
173
|
|
|
self.assertEqual(check, expected) |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
def test_get_file_normal1(self): |
|
|
|
|
|
|
177
|
|
|
"""ensure get_file properly handles empty inpurt""" |
|
178
|
|
|
fname = os.path.join(DAT_PATH, "get_file/nonexistantfile") |
|
179
|
|
|
check = plumd.util.get_file(fname) |
|
180
|
|
|
expected = "" |
|
181
|
|
|
self.assertEqual(check, expected) |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
def test_interval_normal1(self): |
|
185
|
|
|
"""ensure interval class functioning normally""" |
|
186
|
|
|
interval = 0.1 |
|
187
|
|
|
start = time.time() |
|
188
|
|
|
with plumd.util.Interval(interval): |
|
189
|
|
|
pass |
|
190
|
|
|
end = time.time() |
|
191
|
|
|
# should be roughly 0.1 seconds |
|
192
|
|
|
took = end - start |
|
193
|
|
|
if took > interval: |
|
194
|
|
|
diff = took - interval |
|
195
|
|
|
else: |
|
196
|
|
|
diff = interval - took |
|
197
|
|
|
# expect a small difference |
|
198
|
|
|
msg = "expected interval of {0} s, difference was: {1}" |
|
199
|
|
|
self.assertTrue(interval/100 > diff, msg=msg.format(interval, diff)) |
|
200
|
|
|
|
|
201
|
|
|
|
|
202
|
|
|
def test_interval_invalid1(self): |
|
203
|
|
|
"""ensure interval class handling invalid interval""" |
|
204
|
|
|
interval = -0.1 |
|
205
|
|
|
start = time.time() |
|
|
|
|
|
|
206
|
|
|
self.assertRaises(ValueError, plumd.util.Interval, interval) |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
def test_interval_eventnotset(self): |
|
210
|
|
|
"""ensure interval class handling events properly""" |
|
211
|
|
|
interval = 0.01 |
|
212
|
|
|
evt = Event() |
|
213
|
|
|
start = time.time() |
|
214
|
|
|
with plumd.util.Interval(interval, evt): |
|
215
|
|
|
pass |
|
216
|
|
|
end = time.time() |
|
217
|
|
|
took = end - start |
|
218
|
|
|
# should be roughly 0.1 seconds |
|
219
|
|
|
if took > interval: |
|
220
|
|
|
diff = took - interval |
|
221
|
|
|
else: |
|
222
|
|
|
diff = interval - took |
|
223
|
|
|
expected = interval/10 |
|
224
|
|
|
# expect a very very small difference - note: heavily loaded systems may trigger failure |
|
225
|
|
|
msg = "expected interval of {0} s, difference was: {1}" |
|
226
|
|
|
self.assertTrue(diff < expected, msg=msg.format(diff, diff)) |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
def test_interval_eventset(self): |
|
230
|
|
|
"""ensure interval class handling events properly""" |
|
231
|
|
|
interval = 0.01 |
|
232
|
|
|
evt = Event() |
|
233
|
|
|
start = time.time() |
|
234
|
|
|
with plumd.util.Interval(interval, evt): |
|
235
|
|
|
evt.set() |
|
236
|
|
|
end = time.time() |
|
237
|
|
|
diff = end - start |
|
238
|
|
|
# in this case the loop is expected to complete early |
|
239
|
|
|
# if we get here the test passes if it didn't take longer |
|
240
|
|
|
# than interval |
|
241
|
|
|
# running on heavily loaded systems could fail this though.. |
|
242
|
|
|
self.assertTrue(interval > diff) |
|
243
|
|
|
|
|
244
|
|
|
if __name__ == '__main__': |
|
245
|
|
|
unittest.main() |
|
246
|
|
|
|