2014-05-21 15:48:07 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Glances - An eye on your system
|
|
|
|
#
|
2022-06-25 16:43:36 +03:00
|
|
|
# SPDX-FileCopyrightText: 2022 Nicolas Hennion <nicolas@nicolargo.com>
|
2014-05-21 15:48:07 +04:00
|
|
|
#
|
2022-06-25 16:43:36 +03:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-only
|
2014-05-21 15:48:07 +04:00
|
|
|
#
|
2014-06-13 21:00:00 +04:00
|
|
|
|
|
|
|
"""Glances unitary tests suite."""
|
2014-05-21 15:48:07 +04:00
|
|
|
|
2014-06-13 22:06:13 +04:00
|
|
|
import time
|
2014-05-21 15:48:07 +04:00
|
|
|
import unittest
|
2021-09-04 17:46:20 +03:00
|
|
|
import sys
|
|
|
|
|
|
|
|
# Check Python version
|
|
|
|
if sys.version_info < (3, 4):
|
|
|
|
print('Glances requires at least Python 3.4 to run.')
|
|
|
|
sys.exit(1)
|
2014-05-21 15:48:07 +04:00
|
|
|
|
2017-03-27 21:12:15 +03:00
|
|
|
from glances.main import GlancesMain
|
|
|
|
from glances.stats import GlancesStats
|
|
|
|
from glances import __version__
|
2023-05-09 22:26:16 +03:00
|
|
|
from glances.globals import WINDOWS, LINUX, subsample, string_value_to_float
|
2017-03-27 21:12:15 +03:00
|
|
|
from glances.outputs.glances_bars import Bar
|
2017-04-17 16:47:33 +03:00
|
|
|
from glances.thresholds import GlancesThresholdOk
|
|
|
|
from glances.thresholds import GlancesThresholdCareful
|
|
|
|
from glances.thresholds import GlancesThresholdWarning
|
|
|
|
from glances.thresholds import GlancesThresholdCritical
|
2017-04-17 20:24:00 +03:00
|
|
|
from glances.thresholds import GlancesThresholds
|
2021-09-26 18:47:09 +03:00
|
|
|
from glances.plugins.plugin.model import GlancesPluginModel
|
2022-03-12 17:20:57 +03:00
|
|
|
from glances.programs import processes_to_programs
|
2021-04-24 17:14:35 +03:00
|
|
|
from glances.secure import secure_popen
|
2017-03-27 21:12:15 +03:00
|
|
|
|
2014-05-21 15:48:07 +04:00
|
|
|
# Global variables
|
2014-06-13 21:00:00 +04:00
|
|
|
# =================
|
2014-05-21 15:48:07 +04:00
|
|
|
|
|
|
|
# Init Glances core
|
|
|
|
core = GlancesMain()
|
2022-02-26 17:45:39 +03:00
|
|
|
test_config = core.get_config()
|
|
|
|
test_args = core.get_args()
|
2014-05-21 15:48:07 +04:00
|
|
|
|
|
|
|
# Init Glances stats
|
2022-02-26 17:45:39 +03:00
|
|
|
stats = GlancesStats(config=test_config,
|
|
|
|
args=test_args)
|
2014-05-21 15:48:07 +04:00
|
|
|
|
|
|
|
# Unitest class
|
2014-06-13 21:00:00 +04:00
|
|
|
# ==============
|
2016-03-24 13:33:31 +03:00
|
|
|
print('Unitary tests for Glances %s' % __version__)
|
2015-09-21 16:39:39 +03:00
|
|
|
|
2014-06-13 21:00:00 +04:00
|
|
|
|
2014-06-13 22:06:13 +04:00
|
|
|
class TestGlances(unittest.TestCase):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Test Glances class."""
|
2014-05-21 15:48:07 +04:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""The function is called *every time* before test_*."""
|
2014-06-13 22:06:13 +04:00
|
|
|
print('\n' + '=' * 78)
|
2014-05-21 15:48:07 +04:00
|
|
|
|
|
|
|
def test_000_update(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Update stats (mandatory step for all the stats).
|
2014-05-21 15:48:07 +04:00
|
|
|
|
2014-06-13 21:00:00 +04:00
|
|
|
The update is made twice (for rate computation).
|
|
|
|
"""
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_000] Test the stats update function')
|
|
|
|
try:
|
|
|
|
stats.update()
|
2015-02-21 00:59:00 +03:00
|
|
|
except Exception as e:
|
2015-09-21 17:51:20 +03:00
|
|
|
print('ERROR: Stats update failed: %s' % e)
|
2014-05-21 15:48:07 +04:00
|
|
|
self.assertTrue(False)
|
|
|
|
time.sleep(1)
|
|
|
|
try:
|
|
|
|
stats.update()
|
2015-02-21 00:59:00 +03:00
|
|
|
except Exception as e:
|
2015-09-21 17:51:20 +03:00
|
|
|
print('ERROR: Stats update failed: %s' % e)
|
2014-05-21 15:48:07 +04:00
|
|
|
self.assertTrue(False)
|
2014-06-13 21:00:00 +04:00
|
|
|
|
2014-05-21 15:48:07 +04:00
|
|
|
self.assertTrue(True)
|
|
|
|
|
|
|
|
def test_001_plugins(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check mandatory plugins."""
|
2020-05-10 16:14:05 +03:00
|
|
|
plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs']
|
2014-06-13 22:06:13 +04:00
|
|
|
print('INFO: [TEST_001] Check the mandatory plugins list: %s' % ', '.join(plugins_to_check))
|
2017-12-30 12:01:50 +03:00
|
|
|
plugins_list = stats.getPluginsList()
|
2014-06-13 22:06:13 +04:00
|
|
|
for plugin in plugins_to_check:
|
|
|
|
self.assertTrue(plugin in plugins_list)
|
2014-05-21 15:48:07 +04:00
|
|
|
|
2016-10-23 22:39:53 +03:00
|
|
|
def test_002_system(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check SYSTEM plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
stats_to_check = ['hostname', 'os_name']
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_002] Check SYSTEM stats: %s' % ', '.join(stats_to_check))
|
|
|
|
stats_grab = stats.get_plugin('system').get_raw()
|
2014-06-13 22:06:13 +04:00
|
|
|
for stat in stats_to_check:
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that the key exist
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: SYSTEM stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_003_cpu(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check CPU plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
stats_to_check = ['system', 'user', 'idle']
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_003] Check mandatory CPU stats: %s' % ', '.join(stats_to_check))
|
|
|
|
stats_grab = stats.get_plugin('cpu').get_raw()
|
2014-06-13 22:06:13 +04:00
|
|
|
for stat in stats_to_check:
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that the key exist
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that % is > 0 and < 100
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0)
|
|
|
|
self.assertLessEqual(stats_grab[stat], 100)
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: CPU stats: %s' % stats_grab)
|
|
|
|
|
2015-11-29 22:50:53 +03:00
|
|
|
@unittest.skipIf(WINDOWS, "Load average not available on Windows")
|
2014-05-21 15:48:07 +04:00
|
|
|
def test_004_load(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check LOAD plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
stats_to_check = ['cpucore', 'min1', 'min5', 'min15']
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_004] Check LOAD stats: %s' % ', '.join(stats_to_check))
|
|
|
|
stats_grab = stats.get_plugin('load').get_raw()
|
2014-06-13 22:06:13 +04:00
|
|
|
for stat in stats_to_check:
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that the key exist
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that % is > 0
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0)
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: LOAD stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_005_mem(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check MEM plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
stats_to_check = ['available', 'used', 'free', 'total']
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_005] Check MEM stats: %s' % ', '.join(stats_to_check))
|
|
|
|
stats_grab = stats.get_plugin('mem').get_raw()
|
2014-06-13 22:06:13 +04:00
|
|
|
for stat in stats_to_check:
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that the key exist
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that % is > 0
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0)
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: MEM stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_006_swap(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check MEMSWAP plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
stats_to_check = ['used', 'free', 'total']
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_006] Check SWAP stats: %s' % ', '.join(stats_to_check))
|
|
|
|
stats_grab = stats.get_plugin('memswap').get_raw()
|
2014-06-13 22:06:13 +04:00
|
|
|
for stat in stats_to_check:
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that the key exist
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat)
|
2014-05-21 15:48:07 +04:00
|
|
|
# Check that % is > 0
|
2014-06-13 22:06:13 +04:00
|
|
|
self.assertGreaterEqual(stats_grab[stat], 0)
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: SWAP stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_007_network(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check NETWORK plugin."""
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_007] Check NETWORK stats')
|
|
|
|
stats_grab = stats.get_plugin('network').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='Network stats is not a list')
|
|
|
|
print('INFO: NETWORK stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_008_diskio(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check DISKIO plugin."""
|
2016-03-06 19:20:10 +03:00
|
|
|
print('INFO: [TEST_008] Check DISKIO stats')
|
2014-05-21 15:48:07 +04:00
|
|
|
stats_grab = stats.get_plugin('diskio').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='DiskIO stats is not a list')
|
|
|
|
print('INFO: diskio stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_009_fs(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check File System plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
# stats_to_check = [ ]
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_009] Check FS stats')
|
|
|
|
stats_grab = stats.get_plugin('fs').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='FileSystem stats is not a list')
|
|
|
|
print('INFO: FS stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_010_processes(self):
|
2014-06-13 21:00:00 +04:00
|
|
|
"""Check Process plugin."""
|
2014-06-13 22:06:13 +04:00
|
|
|
# stats_to_check = [ ]
|
2014-05-21 15:48:07 +04:00
|
|
|
print('INFO: [TEST_010] Check PROCESS stats')
|
|
|
|
stats_grab = stats.get_plugin('processcount').get_raw()
|
2014-06-13 22:06:13 +04:00
|
|
|
# total = stats_grab['total']
|
2014-05-21 15:48:07 +04:00
|
|
|
self.assertTrue(type(stats_grab) is dict, msg='Process count stats is not a dict')
|
|
|
|
print('INFO: PROCESS count stats: %s' % stats_grab)
|
|
|
|
stats_grab = stats.get_plugin('processlist').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='Process count stats is not a list')
|
|
|
|
print('INFO: PROCESS list stats: %s items in the list' % len(stats_grab))
|
|
|
|
# Check if number of processes in the list equal counter
|
2014-05-24 17:11:53 +04:00
|
|
|
# self.assertEqual(total, len(stats_grab))
|
2014-05-21 15:48:07 +04:00
|
|
|
|
2016-03-06 19:20:10 +03:00
|
|
|
def test_011_folders(self):
|
|
|
|
"""Check File System plugin."""
|
|
|
|
# stats_to_check = [ ]
|
|
|
|
print('INFO: [TEST_011] Check FOLDER stats')
|
|
|
|
stats_grab = stats.get_plugin('folders').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='Folders stats is not a list')
|
|
|
|
print('INFO: Folders stats: %s' % stats_grab)
|
|
|
|
|
|
|
|
def test_012_ip(self):
|
|
|
|
"""Check IP plugin."""
|
|
|
|
print('INFO: [TEST_012] Check IP stats')
|
|
|
|
stats_grab = stats.get_plugin('ip').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is dict, msg='IP stats is not a dict')
|
|
|
|
print('INFO: IP stats: %s' % stats_grab)
|
|
|
|
|
2016-08-20 19:51:48 +03:00
|
|
|
@unittest.skipIf(not LINUX, "IRQs available only on Linux")
|
2016-08-19 20:26:23 +03:00
|
|
|
def test_013_irq(self):
|
|
|
|
"""Check IRQ plugin."""
|
|
|
|
print('INFO: [TEST_013] Check IRQ stats')
|
|
|
|
stats_grab = stats.get_plugin('irq').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='IRQ stats is not a list')
|
|
|
|
print('INFO: IRQ stats: %s' % stats_grab)
|
|
|
|
|
2017-01-28 20:54:27 +03:00
|
|
|
@unittest.skipIf(not LINUX, "GPU available only on Linux")
|
2021-11-03 12:39:04 +03:00
|
|
|
def test_014_gpu(self):
|
2017-01-28 20:54:27 +03:00
|
|
|
"""Check GPU plugin."""
|
|
|
|
print('INFO: [TEST_014] Check GPU stats')
|
|
|
|
stats_grab = stats.get_plugin('gpu').get_raw()
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='GPU stats is not a list')
|
|
|
|
print('INFO: GPU stats: %s' % stats_grab)
|
|
|
|
|
2021-11-03 12:39:04 +03:00
|
|
|
def test_015_sorted_stats(self):
|
2018-02-14 23:08:44 +03:00
|
|
|
"""Check sorted stats method."""
|
|
|
|
print('INFO: [TEST_015] Check sorted stats method')
|
|
|
|
aliases = {
|
|
|
|
"key2": "alias11",
|
|
|
|
"key5": "alias2",
|
|
|
|
}
|
|
|
|
unsorted_stats = [
|
|
|
|
{"key": "key4"},
|
|
|
|
{"key": "key2"},
|
|
|
|
{"key": "key5"},
|
|
|
|
{"key": "key21"},
|
|
|
|
{"key": "key3"},
|
|
|
|
]
|
|
|
|
|
2021-11-03 12:39:04 +03:00
|
|
|
gp = GlancesPluginModel()
|
2018-02-14 23:08:44 +03:00
|
|
|
gp.get_key = lambda: "key"
|
|
|
|
gp.has_alias = aliases.get
|
|
|
|
gp.stats = unsorted_stats
|
|
|
|
|
|
|
|
sorted_stats = gp.sorted_stats()
|
|
|
|
self.assertEqual(len(sorted_stats), 5)
|
|
|
|
self.assertEqual(sorted_stats[0]["key"], "key5")
|
|
|
|
self.assertEqual(sorted_stats[1]["key"], "key2")
|
|
|
|
self.assertEqual(sorted_stats[2]["key"], "key3")
|
|
|
|
self.assertEqual(sorted_stats[3]["key"], "key4")
|
|
|
|
self.assertEqual(sorted_stats[4]["key"], "key21")
|
|
|
|
|
2021-11-03 12:39:04 +03:00
|
|
|
def test_016_subsample(self):
|
2018-04-02 14:23:06 +03:00
|
|
|
"""Test subsampling function."""
|
2021-11-03 12:39:04 +03:00
|
|
|
print('INFO: [TEST_016] Subsampling')
|
2021-04-20 19:52:02 +03:00
|
|
|
for l_test in [([1, 2, 3], 4),
|
|
|
|
([1, 2, 3, 4], 4),
|
|
|
|
([1, 2, 3, 4, 5, 6, 7], 4),
|
|
|
|
([1, 2, 3, 4, 5, 6, 7, 8], 4),
|
|
|
|
(list(range(1, 800)), 4),
|
|
|
|
(list(range(1, 8000)), 800)]:
|
|
|
|
l_subsample = subsample(l_test[0], l_test[1])
|
|
|
|
self.assertLessEqual(len(l_subsample), l_test[1])
|
2018-04-02 14:23:06 +03:00
|
|
|
|
2021-11-03 12:39:04 +03:00
|
|
|
def test_017_hddsmart(self):
|
2018-08-18 10:47:36 +03:00
|
|
|
"""Check hard disk SMART data plugin."""
|
2018-08-25 10:55:23 +03:00
|
|
|
try:
|
2021-08-28 11:56:04 +03:00
|
|
|
from glances.globals import is_admin
|
2018-08-25 10:55:23 +03:00
|
|
|
except ImportError:
|
2021-11-03 12:39:04 +03:00
|
|
|
print("INFO: [TEST_017] pySMART not found, not running SMART plugin test")
|
2018-08-25 10:55:23 +03:00
|
|
|
return
|
|
|
|
|
2018-08-18 10:47:36 +03:00
|
|
|
stat = 'DeviceName'
|
2021-11-03 12:39:04 +03:00
|
|
|
print('INFO: [TEST_017] Check SMART stats: {}'.format(stat))
|
2018-08-18 10:47:36 +03:00
|
|
|
stats_grab = stats.get_plugin('smart').get_raw()
|
|
|
|
if not is_admin():
|
|
|
|
print("INFO: Not admin, SMART list should be empty")
|
|
|
|
assert len(stats_grab) == 0
|
2019-08-12 10:57:28 +03:00
|
|
|
elif stats_grab == {}:
|
|
|
|
print("INFO: Admin but SMART list is empty")
|
|
|
|
assert len(stats_grab) == 0
|
2018-08-18 10:47:36 +03:00
|
|
|
else:
|
2019-08-12 10:57:28 +03:00
|
|
|
print(stats_grab)
|
2018-08-18 10:47:36 +03:00
|
|
|
self.assertTrue(stat in stats_grab[0].keys(), msg='Cannot find key: %s' % stat)
|
|
|
|
|
|
|
|
print('INFO: SMART stats: %s' % stats_grab)
|
|
|
|
|
2022-03-12 17:20:57 +03:00
|
|
|
def test_017_programs(self):
|
|
|
|
"""Check Programs function (it's not a plugin)."""
|
|
|
|
# stats_to_check = [ ]
|
2022-07-09 11:32:59 +03:00
|
|
|
print('INFO: [TEST_017] Check PROGRAM stats')
|
2022-03-12 17:20:57 +03:00
|
|
|
stats_grab = processes_to_programs(stats.get_plugin('processlist').get_raw())
|
|
|
|
self.assertTrue(type(stats_grab) is list, msg='Programs stats is not a list')
|
|
|
|
print('INFO: PROGRAM list stats: %s items in the list' % len(stats_grab))
|
|
|
|
# Check if number of processes in the list equal counter
|
|
|
|
# self.assertEqual(total, len(stats_grab))
|
|
|
|
|
2023-01-21 17:40:48 +03:00
|
|
|
def test_018_string_value_to_float(self):
|
|
|
|
"""Check string_value_to_float function"""
|
|
|
|
print('INFO: [TEST_018] Check string_value_to_float function')
|
|
|
|
self.assertEqual(string_value_to_float('32kB'), 32000.0)
|
|
|
|
self.assertEqual(string_value_to_float('32 KB'), 32000.0)
|
|
|
|
self.assertEqual(string_value_to_float('15.5MB'), 15500000.0)
|
|
|
|
self.assertEqual(string_value_to_float('25.9'), 25.9)
|
|
|
|
self.assertEqual(string_value_to_float('12'), 12)
|
2023-01-22 11:54:56 +03:00
|
|
|
self.assertEqual(string_value_to_float('--'), None)
|
2023-01-21 17:40:48 +03:00
|
|
|
|
2017-04-17 16:47:33 +03:00
|
|
|
def test_094_thresholds(self):
|
|
|
|
"""Test thresholds classes"""
|
2017-04-17 20:24:00 +03:00
|
|
|
print('INFO: [TEST_094] Thresholds')
|
2017-04-17 16:47:33 +03:00
|
|
|
ok = GlancesThresholdOk()
|
|
|
|
careful = GlancesThresholdCareful()
|
|
|
|
warning = GlancesThresholdWarning()
|
|
|
|
critical = GlancesThresholdCritical()
|
|
|
|
self.assertTrue(ok < careful)
|
|
|
|
self.assertTrue(careful < warning)
|
|
|
|
self.assertTrue(warning < critical)
|
|
|
|
self.assertFalse(ok > careful)
|
2018-01-29 21:19:19 +03:00
|
|
|
self.assertEqual(ok, ok)
|
|
|
|
self.assertEqual(str(ok), 'OK')
|
2017-04-17 20:24:00 +03:00
|
|
|
thresholds = GlancesThresholds()
|
|
|
|
thresholds.add('cpu_percent', 'OK')
|
2018-01-29 21:19:19 +03:00
|
|
|
self.assertEqual(thresholds.get(stat_name='cpu_percent').description(), 'OK')
|
2017-04-17 16:47:33 +03:00
|
|
|
|
2017-01-28 20:54:27 +03:00
|
|
|
def test_095_methods(self):
|
|
|
|
"""Test mandatories methods"""
|
|
|
|
print('INFO: [TEST_095] Mandatories methods')
|
|
|
|
mandatories_methods = ['reset', 'update']
|
2017-12-30 12:01:50 +03:00
|
|
|
plugins_list = stats.getPluginsList()
|
2017-01-28 20:54:27 +03:00
|
|
|
for plugin in plugins_list:
|
|
|
|
for method in mandatories_methods:
|
|
|
|
self.assertTrue(hasattr(stats.get_plugin(plugin), method),
|
|
|
|
msg='{} has no method {}()'.format(plugin, method))
|
|
|
|
|
2016-10-23 22:39:53 +03:00
|
|
|
def test_096_views(self):
|
|
|
|
"""Test get_views method"""
|
|
|
|
print('INFO: [TEST_096] Test views')
|
2017-12-30 12:01:50 +03:00
|
|
|
plugins_list = stats.getPluginsList()
|
2016-10-23 22:39:53 +03:00
|
|
|
for plugin in plugins_list:
|
2021-04-20 19:52:02 +03:00
|
|
|
stats.get_plugin(plugin).get_raw()
|
2016-10-23 22:39:53 +03:00
|
|
|
views_grab = stats.get_plugin(plugin).get_views()
|
|
|
|
self.assertTrue(type(views_grab) is dict,
|
|
|
|
msg='{} view is not a dict'.format(plugin))
|
|
|
|
|
2016-05-29 20:40:39 +03:00
|
|
|
def test_097_attribute(self):
|
2022-10-03 16:53:24 +03:00
|
|
|
"""Test GlancesAttribute classes"""
|
2016-05-29 20:40:39 +03:00
|
|
|
print('INFO: [TEST_097] Test attribute')
|
|
|
|
# GlancesAttribute
|
|
|
|
from glances.attribute import GlancesAttribute
|
|
|
|
a = GlancesAttribute('a', description='ad', history_max_size=3)
|
|
|
|
self.assertEqual(a.name, 'a')
|
|
|
|
self.assertEqual(a.description, 'ad')
|
|
|
|
a.description = 'adn'
|
|
|
|
self.assertEqual(a.description, 'adn')
|
|
|
|
a.value = 1
|
|
|
|
a.value = 2
|
|
|
|
self.assertEqual(len(a.history), 2)
|
|
|
|
a.value = 3
|
|
|
|
self.assertEqual(len(a.history), 3)
|
|
|
|
a.value = 4
|
|
|
|
# Check if history_max_size=3 is OK
|
|
|
|
self.assertEqual(len(a.history), 3)
|
|
|
|
self.assertEqual(a.history_size(), 3)
|
|
|
|
self.assertEqual(a.history_len(), 3)
|
|
|
|
self.assertEqual(a.history_value()[1], 4)
|
|
|
|
self.assertEqual(a.history_mean(nb=3), 4.5)
|
|
|
|
|
|
|
|
def test_098_history(self):
|
2022-10-03 16:53:24 +03:00
|
|
|
"""Test GlancesHistory classes"""
|
2016-05-29 20:40:39 +03:00
|
|
|
print('INFO: [TEST_098] Test history')
|
|
|
|
# GlancesHistory
|
|
|
|
from glances.history import GlancesHistory
|
|
|
|
h = GlancesHistory()
|
2021-06-11 18:12:07 +03:00
|
|
|
h.add('a', 1, history_max_size=100)
|
|
|
|
h.add('a', 2, history_max_size=100)
|
|
|
|
h.add('a', 3, history_max_size=100)
|
|
|
|
h.add('b', 10, history_max_size=100)
|
|
|
|
h.add('b', 20, history_max_size=100)
|
|
|
|
h.add('b', 30, history_max_size=100)
|
2016-05-29 20:40:39 +03:00
|
|
|
self.assertEqual(len(h.get()), 2)
|
|
|
|
self.assertEqual(len(h.get()['a']), 3)
|
|
|
|
h.reset()
|
|
|
|
self.assertEqual(len(h.get()), 2)
|
|
|
|
self.assertEqual(len(h.get()['a']), 0)
|
|
|
|
|
2016-03-06 19:20:10 +03:00
|
|
|
def test_099_output_bars_must_be_between_0_and_100_percent(self):
|
2015-11-07 00:33:11 +03:00
|
|
|
"""Test quick look plugin.
|
|
|
|
|
|
|
|
> bar.min_value
|
|
|
|
0
|
|
|
|
> bar.max_value
|
|
|
|
100
|
|
|
|
> bar.percent = -1
|
|
|
|
> bar.percent
|
|
|
|
0
|
|
|
|
> bar.percent = 101
|
|
|
|
> bar.percent
|
|
|
|
100
|
|
|
|
"""
|
2016-05-29 20:40:39 +03:00
|
|
|
print('INFO: [TEST_099] Test progress bar')
|
2015-09-20 22:44:59 +03:00
|
|
|
bar = Bar(size=1)
|
2015-11-07 00:33:11 +03:00
|
|
|
bar.percent = -1
|
|
|
|
self.assertLessEqual(bar.percent, bar.min_value)
|
|
|
|
bar.percent = 101
|
|
|
|
self.assertGreaterEqual(bar.percent, bar.max_value)
|
2015-09-20 22:44:59 +03:00
|
|
|
|
2021-04-24 17:14:35 +03:00
|
|
|
def test_100_secure(self):
|
|
|
|
"""Test secure functions"""
|
|
|
|
print('INFO: [TEST_100] Secure functions')
|
2023-10-07 09:42:22 +03:00
|
|
|
|
2023-05-08 23:22:56 +03:00
|
|
|
if WINDOWS:
|
2023-10-07 09:42:22 +03:00
|
|
|
self.assertIn(secure_popen('echo TEST'), ['TEST\n',
|
|
|
|
'TEST\r\n'])
|
|
|
|
self.assertIn(secure_popen('echo TEST1 && echo TEST2'), ['TEST1\nTEST2\n',
|
|
|
|
'TEST1\r\nTEST2\r\n'])
|
2023-05-08 23:22:56 +03:00
|
|
|
else:
|
|
|
|
self.assertEqual(secure_popen('echo -n TEST'), 'TEST')
|
2023-10-07 09:42:22 +03:00
|
|
|
self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2')
|
2023-07-14 10:12:29 +03:00
|
|
|
# Make the test failed on Github (AssertionError: '' != 'FOO\n')
|
|
|
|
# but not on my localLinux computer...
|
2023-10-07 09:42:22 +03:00
|
|
|
# self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n')
|
2021-04-24 17:14:35 +03:00
|
|
|
|
2022-02-26 17:45:39 +03:00
|
|
|
def test_200_memory_leak(self):
|
|
|
|
"""Memory leak check"""
|
|
|
|
import tracemalloc
|
|
|
|
print('INFO: [TEST_200] Memory leak check')
|
|
|
|
tracemalloc.start()
|
|
|
|
# 3 iterations just to init the stats and fill the memory
|
|
|
|
for _ in range(3):
|
|
|
|
stats.update()
|
|
|
|
|
|
|
|
# Start the memory leak check
|
|
|
|
snapshot_begin = tracemalloc.take_snapshot()
|
|
|
|
for _ in range(3):
|
|
|
|
stats.update()
|
|
|
|
snapshot_end = tracemalloc.take_snapshot()
|
|
|
|
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename')
|
|
|
|
memory_leak = sum([s.size_diff for s in snapshot_diff])
|
|
|
|
print('INFO: Memory leak: {} bytes'.format(memory_leak))
|
|
|
|
|
|
|
|
# snapshot_begin = tracemalloc.take_snapshot()
|
|
|
|
for _ in range(30):
|
|
|
|
stats.update()
|
|
|
|
snapshot_end = tracemalloc.take_snapshot()
|
|
|
|
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename')
|
|
|
|
memory_leak = sum([s.size_diff for s in snapshot_diff])
|
|
|
|
print('INFO: Memory leak: {} bytes'.format(memory_leak))
|
|
|
|
|
|
|
|
# snapshot_begin = tracemalloc.take_snapshot()
|
|
|
|
for _ in range(300):
|
|
|
|
stats.update()
|
|
|
|
snapshot_end = tracemalloc.take_snapshot()
|
|
|
|
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename')
|
|
|
|
memory_leak = sum([s.size_diff for s in snapshot_diff])
|
|
|
|
print('INFO: Memory leak: {} bytes'.format(memory_leak))
|
|
|
|
snapshot_top = snapshot_end.compare_to(snapshot_begin, 'traceback')
|
|
|
|
print("Memory consumption (top 5):")
|
|
|
|
for stat in snapshot_top[:5]:
|
|
|
|
print(stat)
|
|
|
|
for line in stat.traceback.format():
|
|
|
|
print(line)
|
|
|
|
|
2016-05-29 20:40:39 +03:00
|
|
|
def test_999_the_end(self):
|
|
|
|
"""Free all the stats"""
|
|
|
|
print('INFO: [TEST_999] Free the stats')
|
|
|
|
stats.end()
|
|
|
|
self.assertTrue(True)
|
|
|
|
|
2017-04-17 20:24:00 +03:00
|
|
|
|
2014-05-21 15:48:07 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|