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
|
|
|
# Refactor by @ariel-anieli in 2024 |
10
|
|
|
|
11
|
|
|
"""Glances unitary tests suite.""" |
12
|
|
|
|
13
|
|
|
import json |
14
|
|
|
import time |
15
|
|
|
import unittest |
16
|
|
|
from datetime import datetime |
17
|
|
|
|
18
|
|
|
from glances import __version__ |
19
|
|
|
from glances.events_list import GlancesEventsList |
20
|
|
|
from glances.filter import GlancesFilter, GlancesFilterList |
21
|
|
|
from glances.globals import LINUX, WINDOWS, pretty_date, string_value_to_float, subsample |
22
|
|
|
from glances.main import GlancesMain |
23
|
|
|
from glances.outputs.glances_bars import Bar |
24
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel |
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(args_begin_at=2) |
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) and plugin_instance.get_raw() != {}: |
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) and len(plugin_instance.get_raw()) > 0: |
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) and len(plugin_instance.get_raw()) > 0: |
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 stats history |
133
|
|
|
# Not working on WINDOWS |
134
|
|
|
if plugin_instance.history_enable() and not WINDOWS: |
135
|
|
|
if isinstance(plugin_instance.get_raw(), dict): |
136
|
|
|
first_history_field = plugin_instance.get_items_history_list()[0]['name'] |
137
|
|
|
elif isinstance(plugin_instance.get_raw(), list) and len(plugin_instance.get_raw()) > 0: |
138
|
|
|
first_history_field = '_'.join( |
139
|
|
|
[ |
140
|
|
|
plugin_instance.get_raw()[0][plugin_instance.get_key()], |
141
|
|
|
plugin_instance.get_items_history_list()[0]['name'], |
142
|
|
|
] |
143
|
|
|
) |
144
|
|
|
if len(plugin_instance.get_raw()) > 0: |
145
|
|
|
self.assertEqual(len(plugin_instance.get_raw_history(first_history_field)), 2) |
|
|
|
|
146
|
|
|
self.assertGreater( |
147
|
|
|
plugin_instance.get_raw_history(first_history_field)[1][0], |
148
|
|
|
plugin_instance.get_raw_history(first_history_field)[0][0], |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
# Update stats (add third element) |
152
|
|
|
plugin_instance.update() |
153
|
|
|
plugin_instance.update_stats_history() |
154
|
|
|
plugin_instance.update_views() |
155
|
|
|
|
156
|
|
|
if len(plugin_instance.get_raw()) > 0: |
157
|
|
|
self.assertEqual(len(plugin_instance.get_raw_history(first_history_field)), 3) |
158
|
|
|
self.assertEqual(len(plugin_instance.get_raw_history(first_history_field, 2)), 2) |
159
|
|
|
self.assertIsInstance(json.loads(plugin_instance.get_stats_history()), dict) |
160
|
|
|
|
161
|
|
|
# Check views |
162
|
|
|
self.assertIsInstance(plugin_instance.get_views(), dict) |
163
|
|
|
self.assertIsInstance(json.loads(plugin_instance.get_json_views()), dict) |
164
|
|
|
self.assertEqual(json.loads(plugin_instance.get_json_views()), plugin_instance.get_views()) |
165
|
|
|
# Check views history |
166
|
|
|
# Not working on WINDOWS |
167
|
|
|
if plugin_instance.history_enable() and not WINDOWS: |
168
|
|
|
if isinstance(plugin_instance.get_raw(), dict): |
169
|
|
|
self.assertIsInstance(plugin_instance.get_views(first_history_field), dict) |
170
|
|
|
self.assertTrue('decoration' in plugin_instance.get_views(first_history_field)) |
171
|
|
|
elif isinstance(plugin_instance.get_raw(), list) and len(plugin_instance.get_raw()) > 0: |
172
|
|
|
first_history_field = plugin_instance.get_items_history_list()[0]['name'] |
173
|
|
|
first_item = plugin_instance.get_raw()[0][plugin_instance.get_key()] |
174
|
|
|
self.assertIsInstance(plugin_instance.get_views(item=first_item, key=first_history_field), dict) |
175
|
|
|
self.assertTrue('decoration' in plugin_instance.get_views(item=first_item, key=first_history_field)) |
176
|
|
|
|
177
|
|
|
def test_000_update(self): |
178
|
|
|
"""Update stats (mandatory step for all the stats). |
179
|
|
|
|
180
|
|
|
The update is made twice (for rate computation). |
181
|
|
|
""" |
182
|
|
|
print('INFO: [TEST_000] Test the stats update function') |
183
|
|
|
try: |
184
|
|
|
stats.update() |
185
|
|
|
except Exception as e: |
186
|
|
|
print(f'ERROR: Stats update failed: {e}') |
187
|
|
|
self.assertTrue(False) |
188
|
|
|
time.sleep(1) |
189
|
|
|
try: |
190
|
|
|
stats.update() |
191
|
|
|
except Exception as e: |
192
|
|
|
print(f'ERROR: Stats update failed: {e}') |
193
|
|
|
self.assertTrue(False) |
194
|
|
|
|
195
|
|
|
self.assertTrue(True) |
196
|
|
|
|
197
|
|
|
def test_001_plugins(self): |
198
|
|
|
"""Check mandatory plugins.""" |
199
|
|
|
plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs'] |
200
|
|
|
print('INFO: [TEST_001] Check the mandatory plugins list: {}'.format(', '.join(plugins_to_check))) |
201
|
|
|
plugins_list = stats.getPluginsList() |
202
|
|
|
for plugin in plugins_to_check: |
203
|
|
|
self.assertTrue(plugin in plugins_list) |
204
|
|
|
|
205
|
|
|
def test_002_system(self): |
206
|
|
|
"""Check SYSTEM plugin.""" |
207
|
|
|
stats_to_check = ['hostname', 'os_name'] |
208
|
|
|
print('INFO: [TEST_002] Check SYSTEM stats: {}'.format(', '.join(stats_to_check))) |
209
|
|
|
stats_grab = stats.get_plugin('system').get_raw() |
210
|
|
|
for stat in stats_to_check: |
211
|
|
|
# Check that the key exist |
212
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
213
|
|
|
print(f'INFO: SYSTEM stats: {stats_grab}') |
214
|
|
|
|
215
|
|
|
def test_003_cpu(self): |
216
|
|
|
"""Check CPU plugin.""" |
217
|
|
|
stats_to_check = ['system', 'user', 'idle'] |
218
|
|
|
print('INFO: [TEST_003] Check mandatory CPU stats: {}'.format(', '.join(stats_to_check))) |
219
|
|
|
stats_grab = stats.get_plugin('cpu').get_raw() |
220
|
|
|
for stat in stats_to_check: |
221
|
|
|
# Check that the key exist |
222
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
223
|
|
|
# Check that % is > 0 and < 100 |
224
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
225
|
|
|
self.assertLessEqual(stats_grab[stat], 100) |
226
|
|
|
print(f'INFO: CPU stats: {stats_grab}') |
227
|
|
|
|
228
|
|
|
@unittest.skipIf(WINDOWS, "Load average not available on Windows") |
229
|
|
|
def test_004_load(self): |
230
|
|
|
"""Check LOAD plugin.""" |
231
|
|
|
stats_to_check = ['cpucore', 'min1', 'min5', 'min15'] |
232
|
|
|
print('INFO: [TEST_004] Check LOAD stats: {}'.format(', '.join(stats_to_check))) |
233
|
|
|
stats_grab = stats.get_plugin('load').get_raw() |
234
|
|
|
for stat in stats_to_check: |
235
|
|
|
# Check that the key exist |
236
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
237
|
|
|
# Check that % is > 0 |
238
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
239
|
|
|
print(f'INFO: LOAD stats: {stats_grab}') |
240
|
|
|
|
241
|
|
|
def test_005_mem(self): |
242
|
|
|
"""Check MEM plugin.""" |
243
|
|
|
plugin_name = 'mem' |
244
|
|
|
stats_to_check = ['available', 'used', 'free', 'total'] |
245
|
|
|
print('INFO: [TEST_005] Check {} stats: {}'.format(plugin_name, ', '.join(stats_to_check))) |
246
|
|
|
stats_grab = stats.get_plugin('mem').get_raw() |
247
|
|
|
for stat in stats_to_check: |
248
|
|
|
# Check that the key exist |
249
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
250
|
|
|
# Check that % is > 0 |
251
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
252
|
|
|
print(f'INFO: MEM stats: {stats_grab}') |
253
|
|
|
|
254
|
|
|
def test_006_memswap(self): |
255
|
|
|
"""Check MEMSWAP plugin.""" |
256
|
|
|
stats_to_check = ['used', 'free', 'total'] |
257
|
|
|
print('INFO: [TEST_006] Check MEMSWAP stats: {}'.format(', '.join(stats_to_check))) |
258
|
|
|
stats_grab = stats.get_plugin('memswap').get_raw() |
259
|
|
|
for stat in stats_to_check: |
260
|
|
|
# Check that the key exist |
261
|
|
|
self.assertTrue(stat in stats_grab, msg=f'Cannot find key: {stat}') |
262
|
|
|
# Check that % is > 0 |
263
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0) |
264
|
|
|
print(f'INFO: MEMSWAP stats: {stats_grab}') |
265
|
|
|
|
266
|
|
|
def test_007_network(self): |
267
|
|
|
"""Check NETWORK plugin.""" |
268
|
|
|
print('INFO: [TEST_007] Check NETWORK stats') |
269
|
|
|
stats_grab = stats.get_plugin('network').get_raw() |
270
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Network stats is not a list') |
271
|
|
|
print(f'INFO: NETWORK stats: {stats_grab}') |
272
|
|
|
|
273
|
|
|
def test_008_diskio(self): |
274
|
|
|
"""Check DISKIO plugin.""" |
275
|
|
|
print('INFO: [TEST_008] Check DISKIO stats') |
276
|
|
|
stats_grab = stats.get_plugin('diskio').get_raw() |
277
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='DiskIO stats is not a list') |
278
|
|
|
print(f'INFO: diskio stats: {stats_grab}') |
279
|
|
|
|
280
|
|
|
def test_009_fs(self): |
281
|
|
|
"""Check File System plugin.""" |
282
|
|
|
# stats_to_check = [ ] |
283
|
|
|
print('INFO: [TEST_009] Check FS stats') |
284
|
|
|
stats_grab = stats.get_plugin('fs').get_raw() |
285
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='FileSystem stats is not a list') |
286
|
|
|
print(f'INFO: FS stats: {stats_grab}') |
287
|
|
|
|
288
|
|
|
def test_010_processes(self): |
289
|
|
|
"""Check Process plugin.""" |
290
|
|
|
# stats_to_check = [ ] |
291
|
|
|
print('INFO: [TEST_010] Check PROCESS stats') |
292
|
|
|
stats_grab = stats.get_plugin('processcount').get_raw() |
293
|
|
|
# total = stats_grab['total'] |
294
|
|
|
self.assertTrue(isinstance(stats_grab, dict), msg='Process count stats is not a dict') |
295
|
|
|
print(f'INFO: PROCESS count stats: {stats_grab}') |
296
|
|
|
stats_grab = stats.get_plugin('processlist').get_raw() |
297
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Process count stats is not a list') |
298
|
|
|
print(f'INFO: PROCESS list stats: {len(stats_grab)} items in the list') |
299
|
|
|
# Check if number of processes in the list equal counter |
300
|
|
|
# self.assertEqual(total, len(stats_grab)) |
301
|
|
|
|
302
|
|
|
def test_011_folders(self): |
303
|
|
|
"""Check File System plugin.""" |
304
|
|
|
# stats_to_check = [ ] |
305
|
|
|
print('INFO: [TEST_011] Check FOLDER stats') |
306
|
|
|
stats_grab = stats.get_plugin('folders').get_raw() |
307
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Folders stats is not a list') |
308
|
|
|
print(f'INFO: Folders stats: {stats_grab}') |
309
|
|
|
|
310
|
|
|
def test_012_ip(self): |
311
|
|
|
"""Check IP plugin.""" |
312
|
|
|
print('INFO: [TEST_012] Check IP stats') |
313
|
|
|
stats_grab = stats.get_plugin('ip').get_raw() |
314
|
|
|
self.assertTrue(isinstance(stats_grab, dict), msg='IP stats is not a dict') |
315
|
|
|
print(f'INFO: IP stats: {stats_grab}') |
316
|
|
|
|
317
|
|
|
@unittest.skipIf(not LINUX, "IRQs available only on Linux") |
318
|
|
|
def test_013_irq(self): |
319
|
|
|
"""Check IRQ plugin.""" |
320
|
|
|
print('INFO: [TEST_013] Check IRQ stats') |
321
|
|
|
stats_grab = stats.get_plugin('irq').get_raw() |
322
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='IRQ stats is not a list') |
323
|
|
|
print(f'INFO: IRQ stats: {stats_grab}') |
324
|
|
|
|
325
|
|
|
@unittest.skipIf(not LINUX, "GPU available only on Linux") |
326
|
|
|
def test_014_gpu(self): |
327
|
|
|
"""Check GPU plugin.""" |
328
|
|
|
print('INFO: [TEST_014] Check GPU stats') |
329
|
|
|
stats_grab = stats.get_plugin('gpu').get_raw() |
330
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='GPU stats is not a list') |
331
|
|
|
print(f'INFO: GPU stats: {stats_grab}') |
332
|
|
|
|
333
|
|
|
def test_015_sorted_stats(self): |
334
|
|
|
"""Check sorted stats method.""" |
335
|
|
|
print('INFO: [TEST_015] Check sorted stats method') |
336
|
|
|
aliases = { |
337
|
|
|
"key2": "alias11", |
338
|
|
|
"key5": "alias2", |
339
|
|
|
} |
340
|
|
|
unsorted_stats = [ |
341
|
|
|
{"key": "key4"}, |
342
|
|
|
{"key": "key2"}, |
343
|
|
|
{"key": "key5"}, |
344
|
|
|
{"key": "key21"}, |
345
|
|
|
{"key": "key3"}, |
346
|
|
|
] |
347
|
|
|
|
348
|
|
|
gp = GlancesPluginModel() |
349
|
|
|
gp.get_key = lambda: "key" |
350
|
|
|
gp.has_alias = aliases.get |
351
|
|
|
gp.stats = unsorted_stats |
352
|
|
|
|
353
|
|
|
sorted_stats = gp.sorted_stats() |
354
|
|
|
self.assertEqual(len(sorted_stats), 5) |
355
|
|
|
self.assertEqual(sorted_stats[0]["key"], "key5") |
356
|
|
|
self.assertEqual(sorted_stats[1]["key"], "key2") |
357
|
|
|
self.assertEqual(sorted_stats[2]["key"], "key3") |
358
|
|
|
self.assertEqual(sorted_stats[3]["key"], "key4") |
359
|
|
|
self.assertEqual(sorted_stats[4]["key"], "key21") |
360
|
|
|
|
361
|
|
|
def test_016_subsample(self): |
362
|
|
|
"""Test subsampling function.""" |
363
|
|
|
print('INFO: [TEST_016] Subsampling') |
364
|
|
|
for l_test in [ |
365
|
|
|
([1, 2, 3], 4), |
366
|
|
|
([1, 2, 3, 4], 4), |
367
|
|
|
([1, 2, 3, 4, 5, 6, 7], 4), |
368
|
|
|
([1, 2, 3, 4, 5, 6, 7, 8], 4), |
369
|
|
|
(list(range(1, 800)), 4), |
370
|
|
|
(list(range(1, 8000)), 800), |
371
|
|
|
]: |
372
|
|
|
l_subsample = subsample(l_test[0], l_test[1]) |
373
|
|
|
self.assertLessEqual(len(l_subsample), l_test[1]) |
374
|
|
|
|
375
|
|
|
def test_017_hddsmart(self): |
376
|
|
|
"""Check hard disk SMART data plugin.""" |
377
|
|
|
try: |
378
|
|
|
from glances.globals import is_admin |
379
|
|
|
except ImportError: |
380
|
|
|
print("INFO: [TEST_017] pySMART not found, not running SMART plugin test") |
381
|
|
|
return |
382
|
|
|
|
383
|
|
|
stat = 'DeviceName' |
384
|
|
|
print(f'INFO: [TEST_017] Check SMART stats: {stat}') |
385
|
|
|
stats_grab = stats.get_plugin('smart').get_raw() |
386
|
|
|
if not is_admin(): |
387
|
|
|
print("INFO: Not admin, SMART list should be empty") |
388
|
|
|
assert len(stats_grab) == 0 |
389
|
|
|
elif stats_grab == {}: |
390
|
|
|
print("INFO: Admin but SMART list is empty") |
391
|
|
|
assert len(stats_grab) == 0 |
392
|
|
|
else: |
393
|
|
|
print(stats_grab) |
394
|
|
|
self.assertTrue(stat in stats_grab[0].keys(), msg=f'Cannot find key: {stat}') |
395
|
|
|
|
396
|
|
|
print(f'INFO: SMART stats: {stats_grab}') |
397
|
|
|
|
398
|
|
|
def test_017_programs(self): |
399
|
|
|
"""Check Programs plugin.""" |
400
|
|
|
# stats_to_check = [ ] |
401
|
|
|
print('INFO: [TEST_022] Check PROGRAMS stats') |
402
|
|
|
stats_grab = stats.get_plugin('programlist').get_raw() |
403
|
|
|
self.assertTrue(isinstance(stats_grab, list), msg='Programs stats is not a list') |
404
|
|
|
if stats_grab: |
405
|
|
|
self.assertTrue(isinstance(stats_grab[0], dict), msg='Programs stats is not a list of dict') |
406
|
|
|
self.assertTrue('nprocs' in stats_grab[0], msg='No nprocs') |
407
|
|
|
|
408
|
|
|
def test_018_string_value_to_float(self): |
409
|
|
|
"""Check string_value_to_float function""" |
410
|
|
|
print('INFO: [TEST_018] Check string_value_to_float function') |
411
|
|
|
self.assertEqual(string_value_to_float('32kB'), 32000.0) |
412
|
|
|
self.assertEqual(string_value_to_float('32 KB'), 32000.0) |
413
|
|
|
self.assertEqual(string_value_to_float('15.5MB'), 15500000.0) |
414
|
|
|
self.assertEqual(string_value_to_float('25.9'), 25.9) |
415
|
|
|
self.assertEqual(string_value_to_float('12'), 12) |
416
|
|
|
self.assertEqual(string_value_to_float('--'), None) |
417
|
|
|
|
418
|
|
|
def test_019_events(self): |
419
|
|
|
"""Test events class""" |
420
|
|
|
print('INFO: [TEST_019] Test events') |
421
|
|
|
# Init events |
422
|
|
|
events = GlancesEventsList(max_events=5, min_duration=1, min_interval=3) |
423
|
|
|
# Minimal event duration not reached |
424
|
|
|
events.add('WARNING', 'LOAD', 4) |
425
|
|
|
events.add('CRITICAL', 'LOAD', 5) |
426
|
|
|
events.add('OK', 'LOAD', 1) |
427
|
|
|
self.assertEqual(len(events.get()), 0) |
428
|
|
|
# Minimal event duration LOAD reached |
429
|
|
|
events.add('WARNING', 'LOAD', 4) |
430
|
|
|
time.sleep(1) |
431
|
|
|
events.add('CRITICAL', 'LOAD', 5) |
432
|
|
|
time.sleep(1) |
433
|
|
|
events.add('OK', 'LOAD', 1) |
434
|
|
|
self.assertEqual(len(events.get()), 1) |
435
|
|
|
self.assertEqual(events.get()[0]['type'], 'LOAD') |
436
|
|
|
self.assertEqual(events.get()[0]['state'], 'CRITICAL') |
437
|
|
|
self.assertEqual(events.get()[0]['max'], 5) |
438
|
|
|
# Minimal event duration CPU reached |
439
|
|
|
events.add('WARNING', 'CPU', 60) |
440
|
|
|
time.sleep(1) |
441
|
|
|
events.add('WARNING', 'CPU', 70) |
442
|
|
|
time.sleep(1) |
443
|
|
|
events.add('OK', 'CPU', 10) |
444
|
|
|
self.assertEqual(len(events.get()), 2) |
445
|
|
|
self.assertEqual(events.get()[0]['type'], 'CPU') |
446
|
|
|
self.assertEqual(events.get()[0]['state'], 'WARNING') |
447
|
|
|
self.assertEqual(events.get()[0]['min'], 60) |
448
|
|
|
self.assertEqual(events.get()[0]['max'], 70) |
449
|
|
|
self.assertEqual(events.get()[0]['count'], 2) |
450
|
|
|
# Minimal event duration CPU reached (again) |
451
|
|
|
# but time between two events (min_interval) is too short |
452
|
|
|
# a merge will be done |
453
|
|
|
time.sleep(0.5) |
454
|
|
|
events.add('WARNING', 'CPU', 60) |
455
|
|
|
time.sleep(1) |
456
|
|
|
events.add('WARNING', 'CPU', 80) |
457
|
|
|
time.sleep(1) |
458
|
|
|
events.add('OK', 'CPU', 10) |
459
|
|
|
self.assertEqual(len(events.get()), 2) |
460
|
|
|
self.assertEqual(events.get()[0]['type'], 'CPU') |
461
|
|
|
self.assertEqual(events.get()[0]['state'], 'WARNING') |
462
|
|
|
self.assertEqual(events.get()[0]['min'], 60) |
463
|
|
|
self.assertEqual(events.get()[0]['max'], 80) |
464
|
|
|
self.assertEqual(events.get()[0]['count'], 4) |
465
|
|
|
# Clean WARNING events |
466
|
|
|
events.clean() |
467
|
|
|
self.assertEqual(len(events.get()), 1) |
468
|
|
|
|
469
|
|
|
def test_020_filter(self): |
470
|
|
|
"""Test filter classes""" |
471
|
|
|
print('INFO: [TEST_020] Test filter') |
472
|
|
|
gf = GlancesFilter() |
473
|
|
|
gf.filter = '.*python.*' |
474
|
|
|
self.assertEqual(gf.filter, '.*python.*') |
475
|
|
|
self.assertEqual(gf.filter_key, None) |
476
|
|
|
self.assertTrue(gf.is_filtered({'name': 'python'})) |
477
|
|
|
self.assertTrue(gf.is_filtered({'name': '/usr/bin/python -m glances'})) |
478
|
|
|
self.assertFalse(gf.is_filtered({'noname': 'python'})) |
479
|
|
|
self.assertFalse(gf.is_filtered({'name': 'snake'})) |
480
|
|
|
gf.filter = 'username:nicolargo' |
481
|
|
|
self.assertEqual(gf.filter, 'nicolargo') |
482
|
|
|
self.assertEqual(gf.filter_key, 'username') |
483
|
|
|
self.assertTrue(gf.is_filtered({'username': 'nicolargo'})) |
484
|
|
|
self.assertFalse(gf.is_filtered({'username': 'notme'})) |
485
|
|
|
self.assertFalse(gf.is_filtered({'notuser': 'nicolargo'})) |
486
|
|
|
gfl = GlancesFilterList() |
487
|
|
|
gfl.filter = '.*python.*,username:nicolargo' |
488
|
|
|
self.assertTrue(gfl.is_filtered({'name': 'python is in the place'})) |
489
|
|
|
self.assertFalse(gfl.is_filtered({'name': 'snake is in the place'})) |
490
|
|
|
self.assertTrue(gfl.is_filtered({'name': 'snake is in the place', 'username': 'nicolargo'})) |
491
|
|
|
self.assertFalse(gfl.is_filtered({'name': 'snake is in the place', 'username': 'notme'})) |
492
|
|
|
|
493
|
|
|
def test_021_pretty_date(self): |
494
|
|
|
"""Test pretty_date""" |
495
|
|
|
print('INFO: [TEST_021] pretty_date') |
496
|
|
|
self.assertEqual(pretty_date(datetime(2024, 1, 1, 12, 0), datetime(2024, 1, 1, 12, 0)), 'just now') |
497
|
|
|
self.assertEqual(pretty_date(datetime(2024, 1, 1, 11, 59), datetime(2024, 1, 1, 12, 0)), 'a min') |
498
|
|
|
self.assertEqual(pretty_date(datetime(2024, 1, 1, 11, 55), datetime(2024, 1, 1, 12, 0)), '5 mins') |
499
|
|
|
self.assertEqual(pretty_date(datetime(2024, 1, 1, 11, 0), datetime(2024, 1, 1, 12, 0)), 'an hour') |
500
|
|
|
self.assertEqual(pretty_date(datetime(2024, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '12 hours') |
501
|
|
|
self.assertEqual(pretty_date(datetime(2023, 12, 20, 0, 0), datetime(2024, 1, 1, 12, 0)), 'a week') |
502
|
|
|
self.assertEqual(pretty_date(datetime(2023, 12, 5, 0, 0), datetime(2024, 1, 1, 12, 0)), '3 weeks') |
503
|
|
|
self.assertEqual(pretty_date(datetime(2023, 12, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), 'a month') |
504
|
|
|
self.assertEqual(pretty_date(datetime(2023, 6, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '7 months') |
505
|
|
|
self.assertEqual(pretty_date(datetime(2023, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), 'an year') |
506
|
|
|
self.assertEqual(pretty_date(datetime(2020, 1, 1, 0, 0), datetime(2024, 1, 1, 12, 0)), '4 years') |
507
|
|
|
|
508
|
|
|
def test_094_thresholds(self): |
509
|
|
|
"""Test thresholds classes""" |
510
|
|
|
print('INFO: [TEST_094] Thresholds') |
511
|
|
|
ok = GlancesThresholdOk() |
512
|
|
|
careful = GlancesThresholdCareful() |
513
|
|
|
warning = GlancesThresholdWarning() |
514
|
|
|
critical = GlancesThresholdCritical() |
515
|
|
|
self.assertTrue(ok < careful) |
516
|
|
|
self.assertTrue(careful < warning) |
517
|
|
|
self.assertTrue(warning < critical) |
518
|
|
|
self.assertFalse(ok > careful) |
519
|
|
|
self.assertEqual(ok, ok) |
520
|
|
|
self.assertEqual(str(ok), 'OK') |
521
|
|
|
thresholds = GlancesThresholds() |
522
|
|
|
thresholds.add('cpu_percent', 'OK') |
523
|
|
|
self.assertEqual(thresholds.get(stat_name='cpu_percent').description(), 'OK') |
524
|
|
|
|
525
|
|
|
def test_095_methods(self): |
526
|
|
|
"""Test mandatories methods""" |
527
|
|
|
print('INFO: [TEST_095] Mandatories methods') |
528
|
|
|
mandatories_methods = ['reset', 'update'] |
529
|
|
|
plugins_list = stats.getPluginsList() |
530
|
|
|
for plugin in plugins_list: |
531
|
|
|
for method in mandatories_methods: |
532
|
|
|
self.assertTrue(hasattr(stats.get_plugin(plugin), method), msg=f'{plugin} has no method {method}()') |
533
|
|
|
|
534
|
|
|
def test_096_views(self): |
535
|
|
|
"""Test get_views method""" |
536
|
|
|
print('INFO: [TEST_096] Test views') |
537
|
|
|
plugins_list = stats.getPluginsList() |
538
|
|
|
for plugin in plugins_list: |
539
|
|
|
stats.get_plugin(plugin).get_raw() |
540
|
|
|
views_grab = stats.get_plugin(plugin).get_views() |
541
|
|
|
self.assertTrue(isinstance(views_grab, dict), msg=f'{plugin} view is not a dict') |
542
|
|
|
|
543
|
|
|
def test_097_attribute(self): |
544
|
|
|
"""Test GlancesAttribute classes""" |
545
|
|
|
print('INFO: [TEST_097] Test attribute') |
546
|
|
|
# GlancesAttribute |
547
|
|
|
from glances.attribute import GlancesAttribute |
548
|
|
|
|
549
|
|
|
a = GlancesAttribute('a', description='ad', history_max_size=3) |
550
|
|
|
self.assertEqual(a.name, 'a') |
551
|
|
|
self.assertEqual(a.description, 'ad') |
552
|
|
|
a.description = 'adn' |
553
|
|
|
self.assertEqual(a.description, 'adn') |
554
|
|
|
a.value = 1 |
555
|
|
|
a.value = 2 |
556
|
|
|
self.assertEqual(len(a.history), 2) |
557
|
|
|
a.value = 3 |
558
|
|
|
self.assertEqual(len(a.history), 3) |
559
|
|
|
a.value = 4 |
560
|
|
|
# Check if history_max_size=3 is OK |
561
|
|
|
self.assertEqual(len(a.history), 3) |
562
|
|
|
self.assertEqual(a.history_size(), 3) |
563
|
|
|
self.assertEqual(a.history_len(), 3) |
564
|
|
|
self.assertEqual(a.history_value()[1], 4) |
565
|
|
|
self.assertEqual(a.history_mean(nb=3), 4.5) |
566
|
|
|
|
567
|
|
|
def test_098_history(self): |
568
|
|
|
"""Test GlancesHistory classes""" |
569
|
|
|
print('INFO: [TEST_098] Test history') |
570
|
|
|
# GlancesHistory |
571
|
|
|
from glances.history import GlancesHistory |
572
|
|
|
|
573
|
|
|
h = GlancesHistory() |
574
|
|
|
h.add('a', 1, history_max_size=100) |
575
|
|
|
h.add('a', 2, history_max_size=100) |
576
|
|
|
h.add('a', 3, history_max_size=100) |
577
|
|
|
h.add('b', 10, history_max_size=100) |
578
|
|
|
h.add('b', 20, history_max_size=100) |
579
|
|
|
h.add('b', 30, history_max_size=100) |
580
|
|
|
self.assertEqual(len(h.get()), 2) |
581
|
|
|
self.assertEqual(len(h.get()['a']), 3) |
582
|
|
|
h.reset() |
583
|
|
|
self.assertEqual(len(h.get()), 2) |
584
|
|
|
self.assertEqual(len(h.get()['a']), 0) |
585
|
|
|
|
586
|
|
|
def test_099_output_bars(self): |
587
|
|
|
"""Test quick look plugin. |
588
|
|
|
|
589
|
|
|
> bar.min_value |
590
|
|
|
0 |
591
|
|
|
> bar.max_value |
592
|
|
|
100 |
593
|
|
|
> bar.percent = -1 |
594
|
|
|
> bar.percent |
595
|
|
|
0 |
596
|
|
|
""" |
597
|
|
|
print('INFO: [TEST_099] Test progress bar') |
598
|
|
|
|
599
|
|
|
bar = Bar(size=1) |
600
|
|
|
# Percent value can not be lower than min_value |
601
|
|
|
bar.percent = -1 |
602
|
|
|
self.assertLessEqual(bar.percent, bar.min_value) |
603
|
|
|
# but... percent value can be higher than max_value |
604
|
|
|
bar.percent = 101 |
605
|
|
|
self.assertLessEqual(bar.percent, 101) |
606
|
|
|
|
607
|
|
|
# Test display |
608
|
|
|
bar = Bar(size=50) |
609
|
|
|
bar.percent = 0 |
610
|
|
|
self.assertEqual(bar.get(), ' 0.0%') |
611
|
|
|
bar.percent = 70 |
612
|
|
|
self.assertEqual(bar.get(), '||||||||||||||||||||||||||||||| 70.0%') |
613
|
|
|
bar.percent = 100 |
614
|
|
|
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| 100%') |
615
|
|
|
bar.percent = 110 |
616
|
|
|
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%') |
617
|
|
|
|
618
|
|
|
# Error in Github Action. Do not remove the comment. |
619
|
|
|
# def test_100_system_plugin_method(self): |
620
|
|
|
# """Test system plugin methods""" |
621
|
|
|
# print('INFO: [TEST_100] Test system plugin methods') |
622
|
|
|
# self._common_plugin_tests('system') |
623
|
|
|
|
624
|
|
|
def test_101_cpu_plugin_method(self): |
625
|
|
|
"""Test cpu plugin methods""" |
626
|
|
|
print('INFO: [TEST_100] Test cpu plugin methods') |
627
|
|
|
self._common_plugin_tests('cpu') |
628
|
|
|
|
629
|
|
|
@unittest.skipIf(WINDOWS, "Load average not available on Windows") |
630
|
|
|
def test_102_load_plugin_method(self): |
631
|
|
|
"""Test load plugin methods""" |
632
|
|
|
print('INFO: [TEST_102] Test load plugin methods') |
633
|
|
|
self._common_plugin_tests('load') |
634
|
|
|
|
635
|
|
|
def test_103_mem_plugin_method(self): |
636
|
|
|
"""Test mem plugin methods""" |
637
|
|
|
print('INFO: [TEST_103] Test mem plugin methods') |
638
|
|
|
self._common_plugin_tests('mem') |
639
|
|
|
|
640
|
|
|
def test_104_memswap_plugin_method(self): |
641
|
|
|
"""Test memswap plugin methods""" |
642
|
|
|
print('INFO: [TEST_104] Test memswap plugin methods') |
643
|
|
|
self._common_plugin_tests('memswap') |
644
|
|
|
|
645
|
|
|
def test_105_network_plugin_method(self): |
646
|
|
|
"""Test network plugin methods""" |
647
|
|
|
print('INFO: [TEST_105] Test network plugin methods') |
648
|
|
|
self._common_plugin_tests('network') |
649
|
|
|
|
650
|
|
|
# Error in Github Action. Do not remove the comment. |
651
|
|
|
# def test_106_diskio_plugin_method(self): |
652
|
|
|
# """Test diskio plugin methods""" |
653
|
|
|
# print('INFO: [TEST_106] Test diskio plugin methods') |
654
|
|
|
# self._common_plugin_tests('diskio') |
655
|
|
|
|
656
|
|
|
def test_107_fs_plugin_method(self): |
657
|
|
|
"""Test fs plugin methods""" |
658
|
|
|
print('INFO: [TEST_107] Test fs plugin methods') |
659
|
|
|
self._common_plugin_tests('fs') |
660
|
|
|
|
661
|
|
|
def test_200_views_hidden(self): |
662
|
|
|
"""Test hide feature""" |
663
|
|
|
print('INFO: [TEST_200] Test views hidden feature') |
664
|
|
|
# Test will be done with the diskio plugin, first available interface (read_bytes fields) |
665
|
|
|
plugin = 'diskio' |
666
|
|
|
field = 'read_bytes_rate_per_sec' |
667
|
|
|
plugin_instance = stats.get_plugin(plugin) |
668
|
|
|
if len(plugin_instance.get_views()) == 0 or not test_config.get_bool_value(plugin, 'hide_zero', False): |
669
|
|
|
# No diskIO interface, test can not be done |
670
|
|
|
return |
671
|
|
|
# Get first disk interface |
672
|
|
|
key = list(plugin_instance.get_views().keys())[0] |
673
|
|
|
# Test |
674
|
|
|
###### |
675
|
|
|
# Init the stats |
676
|
|
|
plugin_instance.update() |
677
|
|
|
raw_stats = plugin_instance.get_raw() |
678
|
|
|
# Reset the views |
679
|
|
|
plugin_instance.set_views({}) |
680
|
|
|
# Set field to 0 (should be hidden) |
681
|
|
|
raw_stats[0][field] = 0 |
682
|
|
|
plugin_instance.set_stats(raw_stats) |
683
|
|
|
self.assertEqual(plugin_instance.get_raw()[0][field], 0) |
684
|
|
|
plugin_instance.update_views() |
685
|
|
|
self.assertTrue(plugin_instance.get_views()[key][field]['hidden']) |
686
|
|
|
# Set field to 0 (should be hidden) |
687
|
|
|
raw_stats[0][field] = 0 |
688
|
|
|
plugin_instance.set_stats(raw_stats) |
689
|
|
|
self.assertEqual(plugin_instance.get_raw()[0][field], 0) |
690
|
|
|
plugin_instance.update_views() |
691
|
|
|
self.assertTrue(plugin_instance.get_views()[key][field]['hidden']) |
692
|
|
|
# Set field to 1 (should not be hidden) |
693
|
|
|
raw_stats[0][field] = 1 |
694
|
|
|
plugin_instance.set_stats(raw_stats) |
695
|
|
|
self.assertEqual(plugin_instance.get_raw()[0][field], 1) |
696
|
|
|
plugin_instance.update_views() |
697
|
|
|
self.assertFalse(plugin_instance.get_views()[key][field]['hidden']) |
698
|
|
|
# Set field back to 0 (should not be hidden) |
699
|
|
|
raw_stats[0][field] = 0 |
700
|
|
|
plugin_instance.set_stats(raw_stats) |
701
|
|
|
self.assertEqual(plugin_instance.get_raw()[0][field], 0) |
702
|
|
|
plugin_instance.update_views() |
703
|
|
|
self.assertFalse(plugin_instance.get_views()[key][field]['hidden']) |
704
|
|
|
|
705
|
|
|
# def test_700_secure(self): |
706
|
|
|
# """Test secure functions""" |
707
|
|
|
# print('INFO: [TEST_700] Secure functions') |
708
|
|
|
|
709
|
|
|
# if WINDOWS: |
710
|
|
|
# self.assertIn(secure_popen('echo TEST'), ['TEST\n', 'TEST\r\n']) |
711
|
|
|
# self.assertIn(secure_popen('echo TEST1 && echo TEST2'), ['TEST1\nTEST2\n', 'TEST1\r\nTEST2\r\n']) |
712
|
|
|
# else: |
713
|
|
|
# self.assertEqual(secure_popen('echo -n TEST'), 'TEST') |
714
|
|
|
# self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2') |
715
|
|
|
# # Make the test failed on Github (AssertionError: '' != 'FOO\n') |
716
|
|
|
# # but not on my localLinux computer... |
717
|
|
|
# # self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n') |
718
|
|
|
|
719
|
|
|
def test_999_the_end(self): |
720
|
|
|
"""Free all the stats""" |
721
|
|
|
print('INFO: [TEST_999] Free the stats') |
722
|
|
|
stats.end() |
723
|
|
|
self.assertTrue(True) |
724
|
|
|
|
725
|
|
|
|
726
|
|
|
if __name__ == '__main__': |
727
|
|
|
unittest.main() |
728
|
|
|
|