First work done for plugin model (stats only)

This commit is contained in:
nicolargo 2024-05-07 15:26:16 +02:00
parent af4765db8a
commit fd3625826b
3 changed files with 58 additions and 15 deletions

View File

@ -62,27 +62,26 @@ venv-dev-upgrade: ## Upgrade Python 3 dev dependencies
# Tests
# ===================================================================
test: ## Run unit tests
test-core: ## Run core unit tests
./venv/bin/python ./unittest-core.py
test-restful: ## Run Restful unit tests
./venv/bin/python ./unittest-restful.py
test-xmlrpc: ## Run XMLRPC unit tests
./venv/bin/python ./unittest-xmlrpc.py
test: test-core test-restful test-xmlrpc ## Run unit tests
./venv-dev/bin/python -m black ./glances --check --exclude outputs/static
test-with-upgrade: venv-upgrade venv-dev-upgrade ## Upgrade deps and run unit tests
./venv/bin/python ./unittest-core.py
./venv/bin/python ./unittest-restful.py
./venv/bin/python ./unittest-xmlrpc.py
./venv/bin-dev/python -m black ./glances --check --exclude outputs/static
test-with-upgrade: venv-upgrade venv-dev-upgrade test ## Upgrade deps and run unit tests
test-min: ## Run unit tests in minimal environment
test-min: ## Run core unit tests in minimal environment
./venv-min/bin/python ./unittest-core.py
test-min-with-upgrade: venv-min-upgrade ## Upgrade deps and run unit tests in minimal environment
./venv-min/bin/python ./unittest-core.py
test-restful-api: ## Run unit tests of the RESTful API
./venv/bin/python ./unittest-restful.py
# ===================================================================
# Linters, profilers and cyber security
# ===================================================================

View File

@ -129,7 +129,7 @@ class GlancesPluginModel(object):
def __repr__(self):
"""Return the raw stats."""
return self.stats
return str(self.stats)
def __str__(self):
"""Return the human-readable stats."""
@ -301,6 +301,8 @@ class GlancesPluginModel(object):
def sorted_stats(self):
"""Get the stats sorted by an alias (if present) or key."""
key = self.get_key()
if key is None:
return self.stats
try:
return sorted(
self.stats,
@ -551,6 +553,10 @@ class GlancesPluginModel(object):
"""Set the views to input_views."""
self.views = input_views
def reset_views(self):
"""Reset the views to input_views."""
self.views = dict()
def get_views(self, item=None, key=None, option=None):
"""Return the views object.

View File

@ -13,6 +13,7 @@
import time
import unittest
import sys
import json
# Check Python version
if sys.version_info < (3, 8):
@ -480,9 +481,46 @@ class TestGlances(unittest.TestCase):
bar.percent = 110
self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%')
def test_100_secure(self):
def test_100_plugin_model_dict(self):
"""Test a standard plugin returning a dict"""
print('INFO: [TEST_100] Test standard plugin returning a dict')
plugin_instance = stats.get_plugin('mem')
plugin_instance.reset() # reset stats
plugin_instance.reset_views() # reset views
plugin_instance.reset_stats_history() # reset history
# Check before update
self.assertEqual(plugin_instance.get_raw(), plugin_instance.stats_init_value)
self.assertIsInstance(plugin_instance.get_raw(), dict)
self.assertEqual(plugin_instance.get_key(), None)
self.assertEqual(plugin_instance.is_enabled(), True)
self.assertEqual(plugin_instance.is_disabled(), False)
self.assertEqual(plugin_instance.history_enable(), True)
self.assertTrue(all([f in [h['name'] for h in plugin_instance.items_history_list] for f in plugin_instance.get_raw_history()]))
self.assertEqual(plugin_instance.get_views(), {})
# Update stats
plugin_instance.update()
plugin_instance.update_stats_history()
plugin_instance.update_views()
# Check stats
self.assertIsInstance(plugin_instance.get_raw(), dict)
self.assertTrue(all([f in plugin_instance.fields_description for f in plugin_instance.get_raw()]))
self.assertEqual(plugin_instance.get_raw(), plugin_instance.get_export())
self.assertEqual(plugin_instance.get_stats(), plugin_instance.get_json())
self.assertEqual(json.loads(plugin_instance.get_stats()), plugin_instance.get_raw())
# Check history
pass
# Check views
pass
def test_110_plugin_model_list(self):
"""Test a standard plugin returning a list"""
print('INFO: [TEST_110] Test standard plugin returning a list')
plugin_instance = stats.get_plugin('network')
# + sorted_stats
def test_700_secure(self):
"""Test secure functions"""
print('INFO: [TEST_100] Secure functions')
print('INFO: [TEST_700] Secure functions')
if WINDOWS:
self.assertIn(secure_popen('echo TEST'), ['TEST\n',
@ -496,10 +534,10 @@ class TestGlances(unittest.TestCase):
# but not on my localLinux computer...
# self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n')
def test_200_memory_leak(self):
def test_800_memory_leak(self):
"""Memory leak check"""
import tracemalloc
print('INFO: [TEST_200] Memory leak check')
print('INFO: [TEST_800] Memory leak check')
tracemalloc.start()
# 3 iterations just to init the stats and fill the memory
for _ in range(3):