1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Glances - An eye on your system |
4
|
|
|
# |
5
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <[email protected]> |
6
|
|
|
# |
7
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only |
8
|
|
|
# |
9
|
|
|
|
10
|
|
|
"""Glances unitary tests suite.""" |
11
|
|
|
|
12
|
|
|
import json |
13
|
|
|
import time |
14
|
|
|
import unittest |
15
|
|
|
|
16
|
|
|
from glances import __version__ |
17
|
|
|
from glances.events_list import GlancesEventsList |
18
|
|
|
from glances.filter import GlancesFilter, GlancesFilterList |
19
|
|
|
from glances.globals import LINUX, WINDOWS, string_value_to_float, subsample |
20
|
|
|
from glances.main import GlancesMain |
21
|
|
|
from glances.outputs.glances_bars import Bar |
22
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel |
23
|
|
|
from glances.programs import processes_to_programs |
24
|
|
|
from glances.secure import secure_popen |
25
|
|
|
from glances.stats import GlancesStats |
26
|
|
|
from glances.thresholds import ( |
27
|
|
|
GlancesThresholdCareful, |
28
|
|
|
GlancesThresholdCritical, |
29
|
|
|
GlancesThresholdOk, |
30
|
|
|
GlancesThresholds, |
31
|
|
|
GlancesThresholdWarning, |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
# Global variables |
35
|
|
|
# ================= |
36
|
|
|
|
37
|
|
|
# Init Glances core |
38
|
|
|
core = GlancesMain() |
39
|
|
|
test_config = core.get_config() |
40
|
|
|
test_args = core.get_args() |
41
|
|
|
|
42
|
|
|
# Init Glances stats |
43
|
|
|
stats = GlancesStats(config=test_config, args=test_args) |
44
|
|
|
|
45
|
|
|
# Unitest class |
46
|
|
|
# ============== |
47
|
|
|
print(f'Unitary tests for Glances {__version__}') |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class TestGlances(unittest.TestCase): |
51
|
|
|
"""Test Glances class.""" |
52
|
|
|
|
53
|
|
|
def setUp(self): |
54
|
|
|
"""The function is called *every time* before test_*.""" |
55
|
|
|
print('\n' + '=' * 78) |
56
|
|
|
|
57
|
|
|
def _common_plugin_tests(self, plugin): |
58
|
|
|
"""Common method to test a Glances plugin |
59
|
|
|
This method is called multiple time by test 100 to 1xx""" |
60
|
|
|
|
61
|
|
|
# Reset all the stats, history and views |
62
|
|
|
plugin_instance = stats.get_plugin(plugin) |
63
|
|
|
plugin_instance.reset() # reset stats |
64
|
|
|
plugin_instance.reset_views() # reset views |
65
|
|
|
plugin_instance.reset_stats_history() # reset history |
66
|
|
|
|
67
|
|
|
# Check before update |
68
|
|
|
self.assertEqual(plugin_instance.get_raw(), plugin_instance.stats_init_value) |
69
|
|
|
self.assertEqual(plugin_instance.is_enabled(), True) |
70
|
|
|
self.assertEqual(plugin_instance.is_disabled(), False) |
71
|
|
|
self.assertEqual(plugin_instance.get_views(), {}) |
72
|
|
|
self.assertIsInstance(plugin_instance.get_raw(), (dict, list)) |
73
|
|
|
if plugin_instance.history_enable() and isinstance(plugin_instance.get_raw(), dict): |
74
|
|
|
self.assertEqual(plugin_instance.get_key(), None) |
75
|
|
|
self.assertTrue( |
76
|
|
|
all( |
77
|
|
|
f in [h['name'] for h in plugin_instance.items_history_list] |
78
|
|
|
for f in plugin_instance.get_raw_history() |
79
|
|
|
) |
80
|
|
|
) |
81
|
|
|
elif plugin_instance.history_enable() and isinstance(plugin_instance.get_raw(), list): |
82
|
|
|
self.assertNotEqual(plugin_instance.get_key(), None) |
83
|
|
|
|
84
|
|
|
# Update stats (add first element) |
85
|
|
|
plugin_instance.update() |
86
|
|
|
plugin_instance.update_stats_history() |
87
|
|
|
plugin_instance.update_views() |
88
|
|
|
|
89
|
|
|
# Check stats |
90
|
|
|
self.assertIsInstance(plugin_instance.get_raw(), (dict, list)) |
91
|
|
|
if isinstance(plugin_instance.get_raw(), dict): |
92
|
|
|
res = False |
93
|
|
|
for f in plugin_instance.fields_description: |
94
|
|
|
if f not in plugin_instance.get_raw(): |
95
|
|
|
print(f"WARNING: {f} field not found in {plugin} plugin stats") |
96
|
|
|
else: |
97
|
|
|
res = True |
98
|
|
|
self.assertTrue(res) |
99
|
|
|
elif isinstance(plugin_instance.get_raw(), list): |
100
|
|
|
res = False |
101
|
|
|
for i in plugin_instance.get_raw(): |
102
|
|
|
for f in i: |
103
|
|
|
if f in plugin_instance.fields_description: |
104
|
|
|
res = True |
105
|
|
|
self.assertTrue(res) |
106
|
|
|
|
107
|
|
|
self.assertEqual(plugin_instance.get_raw(), plugin_instance.get_export()) |
108
|
|
|
self.assertEqual(plugin_instance.get_stats(), plugin_instance.get_json()) |
109
|
|
|
self.assertEqual(json.loads(plugin_instance.get_stats()), plugin_instance.get_raw()) |
110
|
|
|
if len(plugin_instance.fields_description) > 0: |
111
|
|
|
# Get first item of the fields_description |
112
|
|
|
first_field = list(plugin_instance.fields_description.keys())[0] |
113
|
|
|
self.assertIsInstance(plugin_instance.get_raw_stats_item(first_field), dict) |
114
|
|
|
self.assertIsInstance(json.loads(plugin_instance.get_stats_item(first_field)), dict) |
115
|
|
|
self.assertIsInstance(plugin_instance.get_item_info(first_field, 'description'), str) |
116
|
|
|
# Filter stats |
117
|
|
|
current_stats = plugin_instance.get_raw() |
118
|
|
|
if isinstance(plugin_instance.get_raw(), dict): |
119
|
|
|
current_stats['foo'] = 'bar' |
120
|
|
|
current_stats = plugin_instance.filter_stats(current_stats) |
121
|
|
|
self.assertTrue('foo' not in current_stats) |
122
|
|
|
elif isinstance(plugin_instance.get_raw(), list): |
123
|
|
|
current_stats[0]['foo'] = 'bar' |
124
|
|
|
current_stats = plugin_instance.filter_stats(current_stats) |
125
|
|
|
self.assertTrue('foo' not in current_stats[0]) |
126
|
|
|
|
127
|
|
|
# Update stats (add second element) |
128
|
|
|
plugin_instance.update() |
129
|
|
|
plugin_instance.update_stats_history() |
130
|
|
|
plugin_instance.update_views() |
131
|
|
|
|
132
|
|
|
# Check history |
133
|
|
|
if plugin_instance.history_enable(): |
134
|
|
|
if isinstance(plugin_instance.get_raw(), dict): |
135
|
|
|
first_history_field = plugin_instance.get_items_history_list()[0]['name'] |
136
|
|
|
elif isinstance(plugin_instance.get_raw(), list): |
137
|
|
|
first_history_field = '_'.join( |
138
|
|
|
[ |
139
|
|
|
plugin_instance.get_raw()[0][plugin_instance.get_key()], |
140
|
|
|
plugin_instance.get_items_history_list()[0]['name'], |
141
|
|
|
] |
142
|
|
|
) |
143
|
|
|
self.assertEqual(len(plugin_instance.get_raw_history(first_history_field)), 2) |
|
|
|
|
144
|
|
|
self.assertGreater( |
145
|
|
|
plugin_instance.get_raw_history(first_history_field)[1][0], |
146
|
|
|
plugin_instance.get_raw_history(first_history_field)[0][0], |
147
|
|
|
) |
148
|
|
|
|
149
|
|
|
# Update stats (add third element) |
150
|
|
|
plugin_instance.update() |
151
|
|
|
plugin_instance.update_stats_history() |
152
|
|
|
plugin_instance.update_views() |
153
|
|
|
|
154
|
|
|
self.assertEqual(len(plugin_instance.get_raw_history(first_history_field)), 3) |
155
|
|
|
self.assertEqual(len(plugin_instance.get_raw_history(first_history_field, 2)), 2) |
156
|
|
|
self.assertIsInstance(json.loads(plugin_instance.get_stats_history()), dict) |
157
|
|
|
|
158
|
|
|
# Check views |
159
|
|
|
self.assertIsInstance(plugin_instance.get_views(), dict) |
160
|
|
|
if isinstance(plugin_instance.get_raw(), dict): |
161
|
|
|
self.assertIsInstance(plugin_instance.get_views(first_history_field), dict) |
162
|
|
|
self.assertTrue('decoration' in plugin_instance.get_views(first_history_field)) |
163
|
|
|
elif isinstance(plugin_instance.get_raw(), list): |
164
|
|
|
first_history_field = plugin_instance.get_items_history_list()[0]['name'] |
165
|
|
|
first_item = plugin_instance.get_raw()[0][plugin_instance.get_key()] |
166
|
|
|
self.assertIsInstance(plugin_instance.get_views(item=first_item, key=first_history_field), dict) |
167
|
|
|
self.assertTrue('decoration' in plugin_instance.get_views(item=first_item, key=first_history_field)) |
168
|
|
|
self.assertIsInstance(json.loads(plugin_instance.get_json_views()), dict) |
169
|
|
|
self.assertEqual(json.loads(plugin_instance.get_json_views()), plugin_instance.get_views()) |
170
|
|
|
|
171
|
|
|
def test_000_update(self): |
172
|
|
|
"""Update stats (mandatory step for all the stats). |
173
|
|
|
|
174
|
|
|
The update is made twice (for rate computation). |
175
|
|
|
""" |
176
|
|
|
print('INFO: [TEST_000] Test the stats update function') |
177
|
|
|
try: |
178
|
|
|
stats.update() |
179
|
|
|
except Exception as e: |
180
|
|
|
print(f'ERROR: Stats update failed: {e}') |
181
|
|
|
self.assertTrue(False) |
182
|
|
|
time.sleep(1) |
183
|
|
|
try: |
184
|
|
|
stats.update() |
185
|
|
|
except Exception as e: |
186
|
|
|
print(f'ERROR: Stats update failed: {e}') |
187
|
|
|
self.assertTrue(False) |
188
|
|
|
|
189
|
|
|
self.assertTrue(True) |
190
|
|
|
|
191
|
|
|
def test_001_plugins(self): |
192
|
|
|
"""Check mandatory plugins.""" |
193
|
|
|
plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs'] |
194
|
|
|
print('INFO: [TEST_001] Check the mandatory plugins list: {}'.format(', '.join(plugins_to_check))) |
195
|
|
|
plugins_list = stats.getPluginsList() |
196
|
|
|
for plugin in plugins_to_check: |
197
|
|
|
self.assertTrue(plugin in plugins_list) |
198
|
|
|
|
199
|
|
|
def test_002_system(self): |
200
|
|
|
"""Check SYSTEM plugin.""" |
201
|
|
|
stats_to_check = ['hostname', 'os_name'] |
202
|
|
|
print('INFO: [TEST_002] Check SYSTEM stats: {}'.format(', '.join(stats_to_check))) |
203
|
|
|
stats_grab = stats.get_plugin('system').get_raw() |
204
|
|
|
for stat in stats_to_check: |
205
|
|
|
# Check that the key exist |
206
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
207
|
|
|
print(f'INFO: SYSTEM stats: {stats_grab}') |
208
|
|
|
|
209
|
|
|
def test_003_cpu(self): |
210
|
|
|
"""Check CPU plugin.""" |
211
|
|
|
stats_to_check = ['system', 'user', 'idle'] |
212
|
|
|
print('INFO: [TEST_003] Check mandatory CPU stats: {}'.format(', '.join(stats_to_check))) |
213
|
|
|
stats_grab = stats.get_plugin('cpu').get_raw() |
214
|
|
|
for stat in stats_to_check: |
215
|
|
|
# Check that the key exist |
216
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
217
|
|
|
# Check that % is > 0 and < 100 |
218
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
219
|
|
|
self.assertLessEqual(stats_grab[stat], 100) |
220
|
|
|
print(f'INFO: CPU stats: {stats_grab}') |
221
|
|
|
|
222
|
|
|
@unittest.skipIf(WINDOWS, "Load average not available on Windows") |
223
|
|
|
def test_004_load(self): |
224
|
|
|
"""Check LOAD plugin.""" |
225
|
|
|
stats_to_check = ['cpucore', 'min1', 'min5', 'min15'] |
226
|
|
|
print('INFO: [TEST_004] Check LOAD stats: {}'.format(', '.join(stats_to_check))) |
227
|
|
|
stats_grab = stats.get_plugin('load').get_raw() |
228
|
|
|
for stat in stats_to_check: |
229
|
|
|
# Check that the key exist |
230
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
231
|
|
|
# Check that % is > 0 |
232
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
233
|
|
|
print(f'INFO: LOAD stats: {stats_grab}') |
234
|
|
|
|
235
|
|
|
def test_005_mem(self): |
236
|
|
|
"""Check MEM plugin.""" |
237
|
|
|
plugin_name = 'mem' |
238
|
|
|
stats_to_check = ['available', 'used', 'free', 'total'] |
239
|
|
|
print('INFO: [TEST_005] Check {} stats: {}'.format(plugin_name, ', '.join(stats_to_check))) |
240
|
|
|
stats_grab = stats.get_plugin('mem').get_raw() |
241
|
|
|
for stat in stats_to_check: |
242
|
|
|
# Check that the key exist |
243
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
244
|
|
|
# Check that % is > 0 |
245
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
246
|
|
|
print(f'INFO: MEM stats: {stats_grab}') |
247
|
|
|
|
248
|
|
|
def test_006_memswap(self): |
249
|
|
|
"""Check MEMSWAP plugin.""" |
250
|
|
|
stats_to_check = ['used', 'free', 'total'] |
251
|
|
|
print('INFO: [TEST_006] Check MEMSWAP stats: {}'.format(', '.join(stats_to_check))) |
252
|
|
|
stats_grab = stats.get_plugin('memswap').get_raw() |
253
|
|
|
for stat in stats_to_check: |
254
|
|
|
# Check that the key exist |
255
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
256
|
|
|
# Check that % is > 0 |
257
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
258
|
|
|
print(f'INFO: MEMSWAP stats: {stats_grab}') |
259
|
|
|
|
260
|
|
|
def test_007_network(self): |
261
|
|
|
"""Check NETWORK plugin.""" |
262
|
|
|
print('INFO: [TEST_007] Check NETWORK stats') |
263
|
|
|
stats_grab = stats.get_plugin('network').get_raw() |
264
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Network stats is not a list') |
265
|
|
|
print(f'INFO: NETWORK stats: {stats_grab}') |
266
|
|
|
|
267
|
|
|
def test_008_diskio(self): |
268
|
|
|
"""Check DISKIO plugin.""" |
269
|
|
|
print('INFO: [TEST_008] Check DISKIO stats') |
270
|
|
|
stats_grab = stats.get_plugin('diskio').get_raw() |
271
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='DiskIO stats is not a list') |
272
|
|
|
print(f'INFO: diskio stats: {stats_grab}') |
273
|
|
|
|
274
|
|
|
def test_009_fs(self): |
275
|
|
|
"""Check File System plugin.""" |
276
|
|
|
# stats_to_check = [ ] |
277
|
|
|
print('INFO: [TEST_009] Check FS stats') |
278
|
|
|
stats_grab = stats.get_plugin('fs').get_raw() |
279
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='FileSystem stats is not a list') |
280
|
|
|
print(f'INFO: FS stats: {stats_grab}') |
281
|
|
|
|
282
|
|
|
def test_010_processes(self): |
283
|
|
|
"""Check Process plugin.""" |
284
|
|
|
# stats_to_check = [ ] |
285
|
|
|
print('INFO: [TEST_010] Check PROCESS stats') |
286
|
|
|
stats_grab = stats.get_plugin('processcount').get_raw() |
287
|
|
|
# total = stats_grab['total'] |
288
|
|
|
self.assertTrue(isinstance(stats_grab, dict), msg='Process count stats is not a dict') |
289
|
|
|
print(f'INFO: PROCESS count stats: {stats_grab}') |
290
|
|
|
stats_grab = stats.get_plugin('processlist').get_raw() |
291
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Process count stats is not a list') |
292
|
|
|
print(f'INFO: PROCESS list stats: {len(stats_grab)} items in the list') |
293
|
|
|
# Check if number of processes in the list equal counter |
294
|
|
|
# self.assertEqual(total, len(stats_grab)) |
295
|
|
|
|
296
|
|
|
def test_011_folders(self): |
297
|
|
|
"""Check File System plugin.""" |
298
|
|
|
# stats_to_check = [ ] |
299
|
|
|
print('INFO: [TEST_011] Check FOLDER stats') |
300
|
|
|
stats_grab = stats.get_plugin('folders').get_raw() |
301
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Folders stats is not a list') |
302
|
|
|
print(f'INFO: Folders stats: {stats_grab}') |
303
|
|
|
|
304
|
|
|
def test_012_ip(self): |
305
|
|
|
"""Check IP plugin.""" |
306
|
|
|
print('INFO: [TEST_012] Check IP stats') |
307
|
|
|
stats_grab = stats.get_plugin('ip').get_raw() |
308
|
|
|
self.assertTrue(isinstance(stats_grab, dict), msg='IP stats is not a dict') |
309
|
|
|
print(f'INFO: IP stats: {stats_grab}') |
310
|
|
|
|
311
|
|
|
@unittest.skipIf(not LINUX, "IRQs available only on Linux") |
312
|
|
|
def test_013_irq(self): |
313
|
|
|
"""Check IRQ plugin.""" |
314
|
|
|
print('INFO: [TEST_013] Check IRQ stats') |
315
|
|
|
stats_grab = stats.get_plugin('irq').get_raw() |
316
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='IRQ stats is not a list') |
317
|
|
|
print(f'INFO: IRQ stats: {stats_grab}') |
318
|
|
|
|
319
|
|
|
@unittest.skipIf(not LINUX, "GPU available only on Linux") |
320
|
|
|
def test_014_gpu(self): |
321
|
|
|
"""Check GPU plugin.""" |
322
|
|
|
print('INFO: [TEST_014] Check GPU stats') |
323
|
|
|
stats_grab = stats.get_plugin('gpu').get_raw() |
324
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='GPU stats is not a list') |
325
|
|
|
print(f'INFO: GPU stats: {stats_grab}') |
326
|
|
|
|
327
|
|
|
def test_015_sorted_stats(self): |
328
|
|
|
"""Check sorted stats method.""" |
329
|
|
|
print('INFO: [TEST_015] Check sorted stats method') |
330
|
|
|
aliases = { |
331
|
|
|
"key2": "alias11", |
332
|
|
|
"key5": "alias2", |
333
|
|
|
} |
334
|
|
|
unsorted_stats = [ |
335
|
|
|
{"key": "key4"}, |
336
|
|
|
{"key": "key2"}, |
337
|
|
|
{"key": "key5"}, |
338
|
|
|
{"key": "key21"}, |
339
|
|
|
{"key": "key3"}, |
340
|
|
|
] |
341
|
|
|
|
342
|
|
|
gp = GlancesPluginModel() |
343
|
|
|
gp.get_key = lambda: "key" |
344
|
|
|
gp.has_alias = aliases.get |
345
|
|
|
gp.stats = unsorted_stats |
346
|
|
|
|
347
|
|
|
sorted_stats = gp.sorted_stats() |
348
|
|
|
self.assertEqual(len(sorted_stats), 5) |
349
|
|
|
self.assertEqual(sorted_stats[0]["key"], "key5") |
350
|
|
|
self.assertEqual(sorted_stats[1]["key"], "key2") |
351
|
|
|
self.assertEqual(sorted_stats[2]["key"], "key3") |
352
|
|
|
self.assertEqual(sorted_stats[3]["key"], "key4") |
353
|
|
|
self.assertEqual(sorted_stats[4]["key"], "key21") |
354
|
|
|
|
355
|
|
|
def test_016_subsample(self): |
356
|
|
|
"""Test subsampling function.""" |
357
|
|
|
print('INFO: [TEST_016] Subsampling') |
358
|
|
|
for l_test in [ |
359
|
|
|
([1, 2, 3], 4), |
360
|
|
|
([1, 2, 3, 4], 4), |
361
|
|
|
([1, 2, 3, 4, 5, 6, 7], 4), |
362
|
|
|
([1, 2, 3, 4, 5, 6, 7, 8], 4), |
363
|
|
|
(list(range(1, 800)), 4), |
364
|
|
|
(list(range(1, 8000)), 800), |
365
|
|
|
]: |
366
|
|
|
l_subsample = subsample(l_test[0], l_test[1]) |
367
|
|
|
self.assertLessEqual(len(l_subsample), l_test[1]) |
368
|
|
|
|
369
|
|
|
def test_017_hddsmart(self): |
370
|
|
|
"""Check hard disk SMART data plugin.""" |
371
|
|
|
try: |
372
|
|
|
from glances.globals import is_admin |
373
|
|
|
except ImportError: |
374
|
|
|
print("INFO: [TEST_017] pySMART not found, not running SMART plugin test") |
375
|
|
|
return |
376
|
|
|
|
377
|
|
|
stat = 'DeviceName' |
378
|
|
|
print(f'INFO: [TEST_017] Check SMART stats: {stat}') |
379
|
|
|
stats_grab = stats.get_plugin('smart').get_raw() |
380
|
|
|
if not is_admin(): |
381
|
|
|
print("INFO: Not admin, SMART list should be empty") |
382
|
|
|
assert len(stats_grab) == 0 |
383
|
|
|
elif stats_grab == {}: |
384
|
|
|
print("INFO: Admin but SMART list is empty") |
385
|
|
|
assert len(stats_grab) == 0 |
386
|
|
|
else: |
387
|
|
|
print(stats_grab) |
388
|
|
|
self.assertTrue(stat in stats_grab[0].keys(), msg=f'Cannot find key: {stat}') |
389
|
|
|
|
390
|
|
|
print(f'INFO: SMART stats: {stats_grab}') |
391
|
|
|
|
392
|
|
|
def test_017_programs(self): |
393
|
|
|
"""Check Programs function (it's not a plugin).""" |
394
|
|
|
# stats_to_check = [ ] |
395
|
|
|
print('INFO: [TEST_017] Check PROGRAM stats') |
396
|
|
|
stats_grab = processes_to_programs(stats.get_plugin('processlist').get_raw()) |
397
|
|
|
self.assertIsInstance(stats_grab, list, msg='Programs stats list is not a list') |
398
|
|
|
self.assertIsInstance(stats_grab[0], dict, msg='First item should be a dict') |
399
|
|
|
|
400
|
|
|
def test_018_string_value_to_float(self): |
401
|
|
|
"""Check string_value_to_float function""" |
402
|
|
|
print('INFO: [TEST_018] Check string_value_to_float function') |
403
|
|
|
self.assertEqual(string_value_to_float('32kB'), 32000.0) |
404
|
|
|
self.assertEqual(string_value_to_float('32 KB'), 32000.0) |
405
|
|
|
self.assertEqual(string_value_to_float('15.5MB'), 15500000.0) |
406
|
|
|
self.assertEqual(string_value_to_float('25.9'), 25.9) |
407
|
|
|
self.assertEqual(string_value_to_float('12'), 12) |
408
|
|
|
self.assertEqual(string_value_to_float('--'), None) |
409
|
|
|
|
410
|
|
|
def test_019_events(self): |
411
|
|
|
"""Test events class""" |
412
|
|
|
print('INFO: [TEST_019] Test events') |
413
|
|
|
# Init events |
414
|
|
|
events = GlancesEventsList(max_events=5, min_duration=1, min_interval=3) |
415
|
|
|
# Minimal event duration not reached |
416
|
|
|
events.add('WARNING', 'LOAD', 4) |
417
|
|
|
events.add('CRITICAL', 'LOAD', 5) |
418
|
|
|
events.add('OK', 'LOAD', 1) |
419
|
|
|
self.assertEqual(len(events.get()), 0) |
420
|
|
|
# Minimal event duration LOAD reached |
421
|
|
|
events.add('WARNING', 'LOAD', 4) |
422
|
|
|
time.sleep(1) |
423
|
|
|
events.add('CRITICAL', 'LOAD', 5) |
424
|
|
|
time.sleep(1) |
425
|
|
|
events.add('OK', 'LOAD', 1) |
426
|
|
|
self.assertEqual(len(events.get()), 1) |
427
|
|
|
self.assertEqual(events.get()[0]['type'], 'LOAD') |
428
|
|
|
self.assertEqual(events.get()[0]['state'], 'CRITICAL') |
429
|
|
|
self.assertEqual(events.get()[0]['max'], 5) |
430
|
|
|
# Minimal event duration CPU reached |
431
|
|
|
events.add('WARNING', 'CPU', 60) |
432
|
|
|
time.sleep(1) |
433
|
|
|
events.add('WARNING', 'CPU', 70) |
434
|
|
|
time.sleep(1) |
435
|
|
|
events.add('OK', 'CPU', 10) |
436
|
|
|
self.assertEqual(len(events.get()), 2) |
437
|
|
|
self.assertEqual(events.get()[0]['type'], 'CPU') |
438
|
|
|
self.assertEqual(events.get()[0]['state'], 'WARNING') |
439
|
|
|
self.assertEqual(events.get()[0]['min'], 60) |
440
|
|
|
self.assertEqual(events.get()[0]['max'], 70) |
441
|
|
|
self.assertEqual(events.get()[0]['count'], 2) |
442
|
|
|
# Minimal event duration CPU reached (again) |
443
|
|
|
# but time between two events (min_interval) is too short |
444
|
|
|
# a merge will be done |
445
|
|
|
time.sleep(0.5) |
446
|
|
|
events.add('WARNING', 'CPU', 60) |
447
|
|
|
time.sleep(1) |
448
|
|
|
events.add('WARNING', 'CPU', 80) |
449
|
|
|
time.sleep(1) |
450
|
|
|
events.add('OK', 'CPU', 10) |
451
|
|
|
self.assertEqual(len(events.get()), 2) |
452
|
|
|
self.assertEqual(events.get()[0]['type'], 'CPU') |
453
|
|
|
self.assertEqual(events.get()[0]['state'], 'WARNING') |
454
|
|
|
self.assertEqual(events.get()[0]['min'], 60) |
455
|
|
|
self.assertEqual(events.get()[0]['max'], 80) |
456
|
|
|
self.assertEqual(events.get()[0]['count'], 4) |
457
|
|
|
# Clean WARNING events |
458
|
|
|
events.clean() |
459
|
|
|
self.assertEqual(len(events.get()), 1) |
460
|
|
|
|
461
|
|
|
def test_020_filter(self): |
462
|
|
|
"""Test filter classes""" |
463
|
|
|
print('INFO: [TEST_020] Test filter') |
464
|
|
|
gf = GlancesFilter() |
465
|
|
|
gf.filter = '.*python.*' |
466
|
|
|
self.assertEqual(gf.filter, '.*python.*') |
467
|
|
|
self.assertEqual(gf.filter_key, None) |
468
|
|
|
self.assertTrue(gf.is_filtered({'name': 'python'})) |
469
|
|
|
self.assertTrue(gf.is_filtered({'name': '/usr/bin/python -m glances'})) |
470
|
|
|
self.assertFalse(gf.is_filtered({'noname': 'python'})) |
471
|
|
|
self.assertFalse(gf.is_filtered({'name': 'snake'})) |
472
|
|
|
gf.filter = 'username:nicolargo' |
473
|
|
|
self.assertEqual(gf.filter, 'nicolargo') |
474
|
|
|
self.assertEqual(gf.filter_key, 'username') |
475
|
|
|
self.assertTrue(gf.is_filtered({'username': 'nicolargo'})) |
476
|
|
|
self.assertFalse(gf.is_filtered({'username': 'notme'})) |
477
|
|
|
self.assertFalse(gf.is_filtered({'notuser': 'nicolargo'})) |
478
|
|
|
gfl = GlancesFilterList() |
479
|
|
|
gfl.filter = '.*python.*,username:nicolargo' |
480
|
|
|
self.assertTrue(gfl.is_filtered({'name': 'python is in the place'})) |
481
|
|
|
self.assertFalse(gfl.is_filtered({'name': 'snake is in the place'})) |
482
|
|
|
self.assertTrue(gfl.is_filtered({'name': 'snake is in the place', 'username': 'nicolargo'})) |
483
|
|
|
self.assertFalse(gfl.is_filtered({'name': 'snake is in the place', 'username': 'notme'})) |
484
|
|
|
|
485
|
|
|
def test_094_thresholds(self): |
486
|
|
|
"""Test thresholds classes""" |
487
|
|
|
print('INFO: [TEST_094] Thresholds') |
488
|
|
|
ok = GlancesThresholdOk() |
489
|
|
|
careful = GlancesThresholdCareful() |
490
|
|
|
warning = GlancesThresholdWarning() |
491
|
|
|
critical = GlancesThresholdCritical() |
492
|
|
|
self.assertTrue(ok < careful) |
493
|
|
|
self.assertTrue(careful < warning) |
494
|
|
|
self.assertTrue(warning < critical) |
495
|
|
|
self.assertFalse(ok > careful) |
496
|
|
|
self.assertEqual(ok, ok) |
497
|
|
|
self.assertEqual(str(ok), 'OK') |
498
|
|
|
thresholds = GlancesThresholds() |
499
|
|
|
thresholds.add('cpu_percent', 'OK') |
500
|
|
|
self.assertEqual(thresholds.get(stat_name='cpu_percent').description(), 'OK') |
501
|
|
|
|
502
|
|
|
def test_095_methods(self): |
503
|
|
|
"""Test mandatories methods""" |
504
|
|
|
print('INFO: [TEST_095] Mandatories methods') |
505
|
|
|
mandatories_methods = ['reset', 'update'] |
506
|
|
|
plugins_list = stats.getPluginsList() |
507
|
|
|
for plugin in plugins_list: |
508
|
|
|
for method in mandatories_methods: |
509
|
|
|
self.assertTrue(hasattr(stats.get_plugin(plugin), method), msg=f'{plugin} has no method {method}()') |
510
|
|
|
|
511
|
|
|
def test_096_views(self): |
512
|
|
|
"""Test get_views method""" |
513
|
|
|
print('INFO: [TEST_096] Test views') |
514
|
|
|
plugins_list = stats.getPluginsList() |
515
|
|
|
for plugin in plugins_list: |
516
|
|
|
stats.get_plugin(plugin).get_raw() |
517
|
|
|
views_grab = stats.get_plugin(plugin).get_views() |
518
|
|
|
self.assertTrue(isinstance(views_grab, dict), msg=f'{plugin} view is not a dict') |
519
|
|
|
|
520
|
|
|
def test_097_attribute(self): |
521
|
|
|
"""Test GlancesAttribute classes""" |
522
|
|
|
print('INFO: [TEST_097] Test attribute') |
523
|
|
|
# GlancesAttribute |
524
|
|
|
from glances.attribute import GlancesAttribute |
525
|
|
|
|
526
|
|
|
a = GlancesAttribute('a', description='ad', history_max_size=3) |
527
|
|
|
self.assertEqual(a.name, 'a') |
528
|
|
|
self.assertEqual(a.description, 'ad') |
529
|
|
|
a.description = 'adn' |
530
|
|
|
self.assertEqual(a.description, 'adn') |
531
|
|
|
a.value = 1 |
532
|
|
|
a.value = 2 |
533
|
|
|
self.assertEqual(len(a.history), 2) |
534
|
|
|
a.value = 3 |
535
|
|
|
self.assertEqual(len(a.history), 3) |
536
|
|
|
a.value = 4 |
537
|
|
|
# Check if history_max_size=3 is OK |
538
|
|
|
self.assertEqual(len(a.history), 3) |
539
|
|
|
self.assertEqual(a.history_size(), 3) |
540
|
|
|
self.assertEqual(a.history_len(), 3) |
541
|
|
|
self.assertEqual(a.history_value()[1], 4) |
542
|
|
|
self.assertEqual(a.history_mean(nb=3), 4.5) |
543
|
|
|
|
544
|
|
|
def test_098_history(self): |
545
|
|
|
"""Test GlancesHistory classes""" |
546
|
|
|
print('INFO: [TEST_098] Test history') |
547
|
|
|
# GlancesHistory |
548
|
|
|
from glances.history import GlancesHistory |
549
|
|
|
|
550
|
|
|
h = GlancesHistory() |
551
|
|
|
h.add('a', 1, history_max_size=100) |
552
|
|
|
h.add('a', 2, history_max_size=100) |
553
|
|
|
h.add('a', 3, history_max_size=100) |
554
|
|
|
h.add('b', 10, history_max_size=100) |
555
|
|
|
h.add('b', 20, history_max_size=100) |
556
|
|
|
h.add('b', 30, history_max_size=100) |
557
|
|
|
self.assertEqual(len(h.get()), 2) |
558
|
|
|
self.assertEqual(len(h.get()['a']), 3) |
559
|
|
|
h.reset() |
560
|
|
|
self.assertEqual(len(h.get()), 2) |
561
|
|
|
self.assertEqual(len(h.get()['a']), 0) |
562
|
|
|
|
563
|
|
|
def test_099_output_bars(self): |
564
|
|
|
"""Test quick look plugin. |
565
|
|
|
|
566
|
|
|
> bar.min_value |
567
|
|
|
0 |
568
|
|
|
> bar.max_value |
569
|
|
|
100 |
570
|
|
|
> bar.percent = -1 |
571
|
|
|
> bar.percent |
572
|
|
|
0 |
573
|
|
|
""" |
574
|
|
|
print('INFO: [TEST_099] Test progress bar') |
575
|
|
|
|
576
|
|
|
bar = Bar(size=1) |
577
|
|
|
# Percent value can not be lower than min_value |
578
|
|
|
bar.percent = -1 |
579
|
|
|
self.assertLessEqual(bar.percent, bar.min_value) |
580
|
|
|
# but... percent value can be higher than max_value |
581
|
|
|
bar.percent = 101 |
582
|
|
|
self.assertLessEqual(bar.percent, 101) |
583
|
|
|
|
584
|
|
|
# Test display |
585
|
|
|
bar = Bar(size=50) |
586
|
|
|
bar.percent = 0 |
587
|
|
|
self.assertEqual(bar.get(), ' 0.0%') |
588
|
|
|
bar.percent = 70 |
589
|
|
|
self.assertEqual(bar.get(), '||||||||||||||||||||||||||||||| 70.0%') |
590
|
|
|
bar.percent = 100 |
591
|
|
|
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| 100%') |
592
|
|
|
bar.percent = 110 |
593
|
|
|
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%') |
594
|
|
|
|
595
|
|
|
# def test_100_system_plugin_method(self): |
596
|
|
|
# """Test system plugin methods""" |
597
|
|
|
# print('INFO: [TEST_100] Test system plugin methods') |
598
|
|
|
# self._common_plugin_tests('system') |
599
|
|
|
|
600
|
|
|
def test_101_cpu_plugin_method(self): |
601
|
|
|
"""Test cpu plugin methods""" |
602
|
|
|
print('INFO: [TEST_100] Test cpu plugin methods') |
603
|
|
|
self._common_plugin_tests('cpu') |
604
|
|
|
|
605
|
|
|
@unittest.skipIf(WINDOWS, "Load average not available on Windows") |
606
|
|
|
def test_102_load_plugin_method(self): |
607
|
|
|
"""Test load plugin methods""" |
608
|
|
|
print('INFO: [TEST_102] Test load plugin methods') |
609
|
|
|
self._common_plugin_tests('load') |
610
|
|
|
|
611
|
|
|
def test_103_mem_plugin_method(self): |
612
|
|
|
"""Test mem plugin methods""" |
613
|
|
|
print('INFO: [TEST_103] Test mem plugin methods') |
614
|
|
|
self._common_plugin_tests('mem') |
615
|
|
|
|
616
|
|
|
def test_104_memswap_plugin_method(self): |
617
|
|
|
"""Test memswap plugin methods""" |
618
|
|
|
print('INFO: [TEST_104] Test memswap plugin methods') |
619
|
|
|
self._common_plugin_tests('memswap') |
620
|
|
|
|
621
|
|
|
def test_105_network_plugin_method(self): |
622
|
|
|
"""Test network plugin methods""" |
623
|
|
|
print('INFO: [TEST_105] Test network plugin methods') |
624
|
|
|
self._common_plugin_tests('network') |
625
|
|
|
|
626
|
|
|
# def test_106_diskio_plugin_method(self): |
627
|
|
|
# """Test diskio plugin methods""" |
628
|
|
|
# print('INFO: [TEST_106] Test diskio plugin methods') |
629
|
|
|
# self._common_plugin_tests('diskio') |
630
|
|
|
|
631
|
|
|
def test_107_fs_plugin_method(self): |
632
|
|
|
"""Test fs plugin methods""" |
633
|
|
|
print('INFO: [TEST_107] Test fs plugin methods') |
634
|
|
|
self._common_plugin_tests('fs') |
635
|
|
|
|
636
|
|
|
def test_700_secure(self): |
637
|
|
|
"""Test secure functions""" |
638
|
|
|
print('INFO: [TEST_700] Secure functions') |
639
|
|
|
|
640
|
|
|
if WINDOWS: |
641
|
|
|
self.assertIn(secure_popen('echo TEST'), ['TEST\n', 'TEST\r\n']) |
642
|
|
|
self.assertIn(secure_popen('echo TEST1 && echo TEST2'), ['TEST1\nTEST2\n', 'TEST1\r\nTEST2\r\n']) |
643
|
|
|
else: |
644
|
|
|
self.assertEqual(secure_popen('echo -n TEST'), 'TEST') |
645
|
|
|
self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2') |
646
|
|
|
# Make the test failed on Github (AssertionError: '' != 'FOO\n') |
647
|
|
|
# but not on my localLinux computer... |
648
|
|
|
# self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n') |
649
|
|
|
|
650
|
|
|
def test_800_memory_leak(self): |
651
|
|
|
"""Memory leak check""" |
652
|
|
|
import tracemalloc |
653
|
|
|
|
654
|
|
|
print('INFO: [TEST_800] Memory leak check') |
655
|
|
|
tracemalloc.start() |
656
|
|
|
# 3 iterations just to init the stats and fill the memory |
657
|
|
|
for _ in range(3): |
658
|
|
|
stats.update() |
659
|
|
|
|
660
|
|
|
# Start the memory leak check |
661
|
|
|
snapshot_begin = tracemalloc.take_snapshot() |
662
|
|
|
for _ in range(3): |
663
|
|
|
stats.update() |
664
|
|
|
snapshot_end = tracemalloc.take_snapshot() |
665
|
|
|
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') |
666
|
|
|
memory_leak = sum([s.size_diff for s in snapshot_diff]) |
667
|
|
|
print(f'INFO: Memory leak: {memory_leak} bytes') |
668
|
|
|
|
669
|
|
|
# snapshot_begin = tracemalloc.take_snapshot() |
670
|
|
|
for _ in range(30): |
671
|
|
|
stats.update() |
672
|
|
|
snapshot_end = tracemalloc.take_snapshot() |
673
|
|
|
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') |
674
|
|
|
memory_leak = sum([s.size_diff for s in snapshot_diff]) |
675
|
|
|
print(f'INFO: Memory leak: {memory_leak} bytes') |
676
|
|
|
|
677
|
|
|
# snapshot_begin = tracemalloc.take_snapshot() |
678
|
|
|
for _ in range(300): |
679
|
|
|
stats.update() |
680
|
|
|
snapshot_end = tracemalloc.take_snapshot() |
681
|
|
|
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') |
682
|
|
|
memory_leak = sum([s.size_diff for s in snapshot_diff]) |
683
|
|
|
print(f'INFO: Memory leak: {memory_leak} bytes') |
684
|
|
|
snapshot_top = snapshot_end.compare_to(snapshot_begin, 'traceback') |
685
|
|
|
print("Memory consumption (top 5):") |
686
|
|
|
for stat in snapshot_top[:5]: |
687
|
|
|
print(stat) |
688
|
|
|
for line in stat.traceback.format(): |
689
|
|
|
print(line) |
690
|
|
|
|
691
|
|
|
def test_999_the_end(self): |
692
|
|
|
"""Free all the stats""" |
693
|
|
|
print('INFO: [TEST_999] Free the stats') |
694
|
|
|
stats.end() |
695
|
|
|
self.assertTrue(True) |
696
|
|
|
|
697
|
|
|
|
698
|
|
|
if __name__ == '__main__': |
699
|
|
|
unittest.main() |
700
|
|
|
|