From 8e8d4ce89b2888187125655d900ffcb02fc6e39e Mon Sep 17 00:00:00 2001 From: nicolargo Date: Tue, 7 May 2024 14:01:36 +0200 Subject: [PATCH 1/4] Rename unitest to unittest - Related to #2757 --- .github/workflows/test.yml | 8 +- Makefile | 18 +- setup.py | 4 +- tox.ini | 6 +- unitest-all.sh | 3 - unitest-restful.py | 286 ------------------- unitest-xmlrpc.py | 200 -------------- unitest.py | 547 ------------------------------------- 8 files changed, 18 insertions(+), 1054 deletions(-) delete mode 100755 unitest-all.sh delete mode 100755 unitest-restful.py delete mode 100755 unitest-xmlrpc.py delete mode 100755 unitest.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7836ca4..bbe1ff68 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,7 +46,7 @@ jobs: - name: Unitary tests run: | - python ./unitest.py + python ./unittest.py # Error appear with h11, not related to Glances # Should be tested if correction is done @@ -80,7 +80,7 @@ jobs: # - name: Unitary tests # run: | - # python ./unitest.py + # python ./unittest.py test-macos: @@ -107,7 +107,7 @@ jobs: - name: Unitary tests run: | - python ./unitest.py + python ./unittest.py # Error when trying to implement #2749 # pkg: No packages available to install matching 'py-pip' have been found in the repositories @@ -128,4 +128,4 @@ jobs: # run: | # set -e -x # python3 -m pip install --user -r requirements.txt - # python ./unitest.py + # python ./unittest.py diff --git a/Makefile b/Makefile index 57d96606..b9e80e63 100644 --- a/Makefile +++ b/Makefile @@ -63,25 +63,25 @@ venv-dev-upgrade: ## Upgrade Python 3 dev dependencies # =================================================================== test: ## Run unit tests - ./venv/bin/python ./unitest.py - ./venv/bin/python ./unitest-restful.py - ./venv/bin/python ./unitest-xmlrpc.py + ./venv/bin/python ./unittest.py + ./venv/bin/python ./unittest-restful.py + ./venv/bin/python ./unittest-xmlrpc.py ./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 ./unitest.py - ./venv/bin/python ./unitest-restful.py - ./venv/bin/python ./unitest-xmlrpc.py + ./venv/bin/python ./unittest.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-min: ## Run unit tests in minimal environment - ./venv-min/bin/python ./unitest.py + ./venv-min/bin/python ./unittest.py test-min-with-upgrade: venv-min-upgrade ## Upgrade deps and run unit tests in minimal environment - ./venv-min/bin/python ./unitest.py + ./venv-min/bin/python ./unittest.py test-restful-api: ## Run unit tests of the RESTful API - ./venv/bin/python ./unitest-restful.py + ./venv/bin/python ./unittest-restful.py # =================================================================== # Linters, profilers and cyber security diff --git a/setup.py b/setup.py index a70a0d1c..cf86c271 100755 --- a/setup.py +++ b/setup.py @@ -94,7 +94,7 @@ class tests(Command): def run(self): import subprocess import sys - for t in glob.glob('unitest.py'): + for t in glob.glob('unittest.py'): ret = subprocess.call([sys.executable, t]) != 0 if ret != 0: raise SystemExit(ret) @@ -120,7 +120,7 @@ setup( include_package_data=True, data_files=get_data_files(), cmdclass={'test': tests}, - test_suite="unitest.py", + test_suite="unittest.py", entry_points={"console_scripts": ["glances=glances:main"]}, classifiers=[ 'Development Status :: 5 - Production/Stable', diff --git a/tox.ini b/tox.ini index f214f405..fd67524a 100644 --- a/tox.ini +++ b/tox.ini @@ -25,7 +25,7 @@ deps = jinja2 requests commands = - python unitest.py - ; python unitest-restful.py - ; python unitest-xmlrpc.py + python unittest.py + ; python unittest-restful.py + ; python unittest-xmlrpc.py ;flake8 --exclude=build,.tox,.git diff --git a/unitest-all.sh b/unitest-all.sh deleted file mode 100755 index 5c2426f3..00000000 --- a/unitest-all.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -set -ev -./unitest.py && ./unitest-restful.py && ./unitest-xmlrpc.py diff --git a/unitest-restful.py b/unitest-restful.py deleted file mode 100755 index bc2dc330..00000000 --- a/unitest-restful.py +++ /dev/null @@ -1,286 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Glances - An eye on your system -# -# SPDX-FileCopyrightText: 2022 Nicolas Hennion -# -# SPDX-License-Identifier: LGPL-3.0-only -# - -"""Glances unitary tests suite for the RESTful API.""" - -import os -import shlex -import subprocess -import time -import numbers -import unittest - -from glances.outputs.glances_restful_api import GlancesRestfulApi -from glances import __version__ -from glances.globals import text_type - -import requests - -SERVER_PORT = 61234 -API_VERSION = GlancesRestfulApi.API_VERSION -URL = "http://localhost:{}/api/{}".format(SERVER_PORT, API_VERSION) -pid = None - -# Unitest class -# ============== -print('RESTful API unitary tests for Glances %s' % __version__) - - -class TestGlances(unittest.TestCase): - """Test Glances class.""" - - def setUp(self): - """The function is called *every time* before test_*.""" - print('\n' + '=' * 78) - - def http_get(self, url, gzip=False): - """Make the request""" - if gzip: - ret = requests.get(url, - stream=True, - headers={'Accept-encoding': 'gzip'}) - else: - ret = requests.get(url, - headers={'Accept-encoding': 'identity'}) - return ret - - def test_000_start_server(self): - """Start the Glances Web Server.""" - global pid - - print('INFO: [TEST_000] Start the Glances Web Server API') - if os.path.isfile('./venv/bin/python'): - cmdline = "./venv/bin/python" - else: - cmdline = "python" - cmdline += " -m glances -B 0.0.0.0 -w -p %s --disable-webui -C ./conf/glances.conf" % SERVER_PORT - print("Run the Glances Web Server on port %s" % SERVER_PORT) - args = shlex.split(cmdline) - pid = subprocess.Popen(args) - print("Please wait 5 seconds...") - time.sleep(5) - - self.assertTrue(pid is not None) - - def test_001_all(self): - """All.""" - method = "all" - print('INFO: [TEST_001] Get all stats') - print("HTTP RESTful request: %s/%s" % (URL, method)) - req = self.http_get("%s/%s" % (URL, method)) - - self.assertTrue(req.ok) - self.assertTrue(req.json(), dict) - - def test_002_pluginslist(self): - """Plugins list.""" - method = "pluginslist" - print('INFO: [TEST_002] Plugins list') - print("HTTP RESTful request: %s/%s" % (URL, method)) - req = self.http_get("%s/%s" % (URL, method)) - - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), list) - self.assertIn('cpu', req.json()) - - def test_003_plugins(self): - """Plugins.""" - method = "pluginslist" - print('INFO: [TEST_003] Plugins') - plist = self.http_get("%s/%s" % (URL, method)) - - for p in plist.json(): - print("HTTP RESTful request: %s/%s" % (URL, p)) - req = self.http_get("%s/%s" % (URL, p)) - self.assertTrue(req.ok) - if p in ('uptime', 'version', 'psutilversion'): - self.assertIsInstance(req.json(), text_type) - elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio', - 'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports', - 'irq', 'wifi', 'gpu', 'containers'): - self.assertIsInstance(req.json(), list) - if len(req.json()) > 0: - self.assertIsInstance(req.json()[0], dict) - else: - self.assertIsInstance(req.json(), dict) - - def test_004_items(self): - """Items.""" - method = "cpu" - print('INFO: [TEST_004] Items for the CPU method') - ilist = self.http_get("%s/%s" % (URL, method)) - - for i in ilist.json(): - print("HTTP RESTful request: %s/%s/%s" % (URL, method, i)) - req = self.http_get("%s/%s/%s" % (URL, method, i)) - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - print(req.json()[i]) - self.assertIsInstance(req.json()[i], numbers.Number) - - def test_005_values(self): - """Values.""" - method = "processlist" - print('INFO: [TEST_005] Item=Value for the PROCESSLIST method') - print("%s/%s/pid/0" % (URL, method)) - req = self.http_get("%s/%s/pid/0" % (URL, method)) - - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - - def test_006_all_limits(self): - """All limits.""" - method = "all/limits" - print('INFO: [TEST_006] Get all limits') - print("HTTP RESTful request: %s/%s" % (URL, method)) - req = self.http_get("%s/%s" % (URL, method)) - - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - - def test_007_all_views(self): - """All views.""" - method = "all/views" - print('INFO: [TEST_007] Get all views') - print("HTTP RESTful request: %s/%s" % (URL, method)) - req = self.http_get("%s/%s" % (URL, method)) - - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - - def test_008_plugins_limits(self): - """Plugins limits.""" - method = "pluginslist" - print('INFO: [TEST_008] Plugins limits') - plist = self.http_get("%s/%s" % (URL, method)) - - for p in plist.json(): - print("HTTP RESTful request: %s/%s/limits" % (URL, p)) - req = self.http_get("%s/%s/limits" % (URL, p)) - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - - def test_009_plugins_views(self): - """Plugins views.""" - method = "pluginslist" - print('INFO: [TEST_009] Plugins views') - plist = self.http_get("%s/%s" % (URL, method)) - - for p in plist.json(): - print("HTTP RESTful request: %s/%s/views" % (URL, p)) - req = self.http_get("%s/%s/views" % (URL, p)) - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - - def test_010_history(self): - """History.""" - method = "history" - print('INFO: [TEST_010] History') - print("HTTP RESTful request: %s/cpu/%s" % (URL, method)) - req = self.http_get("%s/cpu/%s" % (URL, method)) - self.assertIsInstance(req.json(), dict) - self.assertIsInstance(req.json()['user'], list) - self.assertTrue(len(req.json()['user']) > 0) - print("HTTP RESTful request: %s/cpu/%s/3" % (URL, method)) - req = self.http_get("%s/cpu/%s/3" % (URL, method)) - self.assertIsInstance(req.json(), dict) - self.assertIsInstance(req.json()['user'], list) - self.assertTrue(len(req.json()['user']) > 1) - print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method)) - req = self.http_get("%s/cpu/system/%s" % (URL, method)) - self.assertIsInstance(req.json(), list) - self.assertIsInstance(req.json()[0], list) - print("HTTP RESTful request: %s/cpu/system/%s/3" % (URL, method)) - req = self.http_get("%s/cpu/system/%s/3" % (URL, method)) - self.assertIsInstance(req.json(), list) - self.assertIsInstance(req.json()[0], list) - - def test_011_issue1401(self): - """Check issue #1401.""" - method = "network/interface_name" - print('INFO: [TEST_011] Issue #1401') - req = self.http_get("%s/%s" % (URL, method)) - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - self.assertIsInstance(req.json()['interface_name'], list) - - def test_012_status(self): - """Check status endpoint.""" - method = "status" - print('INFO: [TEST_012] Status') - print("HTTP RESTful request: %s/%s" % (URL, method)) - req = self.http_get("%s/%s" % (URL, method)) - - self.assertTrue(req.ok) - self.assertEqual(req.json()['version'], __version__) - - def test_013_top(self): - """Values.""" - method = "processlist" - request = "%s/%s/top/2" % (URL, method) - print('INFO: [TEST_013] Top nb item of PROCESSLIST') - print(request) - req = self.http_get(request) - - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), list) - self.assertEqual(len(req.json()), 2) - - def test_014_config(self): - """Test API request to get Glances configuration.""" - method = "config" - print('INFO: [TEST_014] Get config') - - req = self.http_get("%s/%s" % (URL, method)) - self.assertTrue(req.ok) - self.assertIsInstance(req.json(), dict) - - req = self.http_get("%s/%s/global/refresh" % (URL, method)) - self.assertTrue(req.ok) - self.assertEqual(req.json(), "2") - - def test_015_all_gzip(self): - """All with Gzip.""" - method = "all" - print('INFO: [TEST_015] Get all stats (with GZip compression)') - print("HTTP RESTful request: %s/%s" % (URL, method)) - req = self.http_get("%s/%s" % (URL, method), gzip=True) - - self.assertTrue(req.ok) - self.assertTrue(req.headers['Content-Encoding'] == 'gzip') - self.assertTrue(req.json(), dict) - - def test_016_fields_description(self): - """Fields description.""" - print('INFO: [TEST_016] Get fields description and unit') - - print("HTTP RESTful request: %s/cpu/total/description" % URL) - req = self.http_get("%s/cpu/total/description" % URL) - self.assertTrue(req.ok) - self.assertTrue(req.json(), str) - - print("HTTP RESTful request: %s/cpu/total/unit" % URL) - req = self.http_get("%s/cpu/total/unit" % URL) - self.assertTrue(req.ok) - self.assertTrue(req.json(), str) - - def test_999_stop_server(self): - """Stop the Glances Web Server.""" - print('INFO: [TEST_999] Stop the Glances Web Server') - - print("Stop the Glances Web Server") - pid.terminate() - time.sleep(1) - - self.assertTrue(True) - - -if __name__ == '__main__': - unittest.main() diff --git a/unitest-xmlrpc.py b/unitest-xmlrpc.py deleted file mode 100755 index 03df2bac..00000000 --- a/unitest-xmlrpc.py +++ /dev/null @@ -1,200 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Glances - An eye on your system -# -# SPDX-FileCopyrightText: 2022 Nicolas Hennion -# -# SPDX-License-Identifier: LGPL-3.0-only -# - -"""Glances unitary tests suite for the XML-RPC API.""" - -import os -import json -import shlex -import subprocess -import time -import unittest - -from glances import __version__ -from glances.globals import ServerProxy - -SERVER_PORT = 61234 -URL = "http://localhost:%s" % SERVER_PORT -pid = None - -# Init the XML-RPC client -client = ServerProxy(URL) - -# Unitest class -# ============== -print('XML-RPC API unitary tests for Glances %s' % __version__) - - -class TestGlances(unittest.TestCase): - """Test Glances class.""" - - def setUp(self): - """The function is called *every time* before test_*.""" - print('\n' + '=' * 78) - - def test_000_start_server(self): - """Start the Glances Web Server.""" - global pid - - print('INFO: [TEST_000] Start the Glances Web Server') - if os.path.isfile('./venv/bin/python'): - cmdline = "./venv/bin/python" - else: - cmdline = "python" - cmdline += " -m glances -B localhost -s -p %s" % SERVER_PORT - print("Run the Glances Server on port %s" % SERVER_PORT) - args = shlex.split(cmdline) - pid = subprocess.Popen(args) - print("Please wait...") - time.sleep(1) - - self.assertTrue(pid is not None) - - def test_001_all(self): - """All.""" - method = "getAll()" - print('INFO: [TEST_001] Connection test') - print("XML-RPC request: %s" % method) - req = json.loads(client.getAll()) - - self.assertIsInstance(req, dict) - - def test_002_pluginslist(self): - """Plugins list.""" - method = "getAllPlugins()" - print('INFO: [TEST_002] Get plugins list') - print("XML-RPC request: %s" % method) - req = json.loads(client.getAllPlugins()) - - self.assertIsInstance(req, list) - - def test_003_system(self): - """System.""" - method = "getSystem()" - print('INFO: [TEST_003] Method: %s' % method) - req = json.loads(client.getSystem()) - - self.assertIsInstance(req, dict) - - def test_004_cpu(self): - """CPU.""" - method = "getCpu(), getPerCpu(), getLoad() and getCore()" - print('INFO: [TEST_004] Method: %s' % method) - - req = json.loads(client.getCpu()) - self.assertIsInstance(req, dict) - - req = json.loads(client.getPerCpu()) - self.assertIsInstance(req, list) - - req = json.loads(client.getLoad()) - self.assertIsInstance(req, dict) - - req = json.loads(client.getCore()) - self.assertIsInstance(req, dict) - - def test_005_mem(self): - """MEM.""" - method = "getMem() and getMemSwap()" - print('INFO: [TEST_005] Method: %s' % method) - - req = json.loads(client.getMem()) - self.assertIsInstance(req, dict) - - req = json.loads(client.getMemSwap()) - self.assertIsInstance(req, dict) - - def test_006_net(self): - """NETWORK.""" - method = "getNetwork()" - print('INFO: [TEST_006] Method: %s' % method) - - req = json.loads(client.getNetwork()) - self.assertIsInstance(req, list) - - def test_007_disk(self): - """DISK.""" - method = "getFs(), getFolders() and getDiskIO()" - print('INFO: [TEST_007] Method: %s' % method) - - req = json.loads(client.getFs()) - self.assertIsInstance(req, list) - - req = json.loads(client.getFolders()) - self.assertIsInstance(req, list) - - req = json.loads(client.getDiskIO()) - self.assertIsInstance(req, list) - - def test_008_sensors(self): - """SENSORS.""" - method = "getSensors()" - print('INFO: [TEST_008] Method: %s' % method) - - req = json.loads(client.getSensors()) - self.assertIsInstance(req, list) - - def test_009_process(self): - """PROCESS.""" - method = "getProcessCount() and getProcessList()" - print('INFO: [TEST_009] Method: %s' % method) - - req = json.loads(client.getProcessCount()) - self.assertIsInstance(req, dict) - - req = json.loads(client.getProcessList()) - self.assertIsInstance(req, list) - - def test_010_all_limits(self): - """All limits.""" - method = "getAllLimits()" - print('INFO: [TEST_010] Method: %s' % method) - - req = json.loads(client.getAllLimits()) - self.assertIsInstance(req, dict) - self.assertIsInstance(req['cpu'], dict) - - def test_011_all_views(self): - """All views.""" - method = "getAllViews()" - print('INFO: [TEST_011] Method: %s' % method) - - req = json.loads(client.getAllViews()) - self.assertIsInstance(req, dict) - self.assertIsInstance(req['cpu'], dict) - - def test_012_irq(self): - """IRQS""" - method = "getIrqs()" - print('INFO: [TEST_012] Method: %s' % method) - req = json.loads(client.getIrq()) - self.assertIsInstance(req, list) - - def test_013_plugin_views(self): - """Plugin views.""" - method = "getViewsCpu()" - print('INFO: [TEST_013] Method: %s' % method) - - req = json.loads(client.getViewsCpu()) - self.assertIsInstance(req, dict) - - def test_999_stop_server(self): - """Stop the Glances Web Server.""" - print('INFO: [TEST_999] Stop the Glances Server') - - print("Stop the Glances Server") - pid.terminate() - time.sleep(1) - - self.assertTrue(True) - - -if __name__ == '__main__': - unittest.main() diff --git a/unitest.py b/unitest.py deleted file mode 100755 index cd0c5781..00000000 --- a/unitest.py +++ /dev/null @@ -1,547 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Glances - An eye on your system -# -# SPDX-FileCopyrightText: 2022 Nicolas Hennion -# -# SPDX-License-Identifier: LGPL-3.0-only -# - -"""Glances unitary tests suite.""" - -import time -import unittest -import sys - -# Check Python version -if sys.version_info < (3, 8): - print('Glances requires at least Python 3.8 to run.') - sys.exit(1) - -from glances.main import GlancesMain -from glances.stats import GlancesStats -from glances import __version__ -from glances.globals import WINDOWS, LINUX, subsample, string_value_to_float -from glances.outputs.glances_bars import Bar -from glances.thresholds import GlancesThresholdOk -from glances.thresholds import GlancesThresholdCareful -from glances.thresholds import GlancesThresholdWarning -from glances.thresholds import GlancesThresholdCritical -from glances.thresholds import GlancesThresholds -from glances.plugins.plugin.model import GlancesPluginModel -from glances.programs import processes_to_programs -from glances.secure import secure_popen -from glances.events_list import GlancesEventsList -from glances.filter import GlancesFilterList, GlancesFilter - -# Global variables -# ================= - -# Init Glances core -core = GlancesMain() -test_config = core.get_config() -test_args = core.get_args() - -# Init Glances stats -stats = GlancesStats(config=test_config, - args=test_args) - -# Unitest class -# ============== -print('Unitary tests for Glances %s' % __version__) - - -class TestGlances(unittest.TestCase): - """Test Glances class.""" - - def setUp(self): - """The function is called *every time* before test_*.""" - print('\n' + '=' * 78) - - def test_000_update(self): - """Update stats (mandatory step for all the stats). - - The update is made twice (for rate computation). - """ - print('INFO: [TEST_000] Test the stats update function') - try: - stats.update() - except Exception as e: - print('ERROR: Stats update failed: %s' % e) - self.assertTrue(False) - time.sleep(1) - try: - stats.update() - except Exception as e: - print('ERROR: Stats update failed: %s' % e) - self.assertTrue(False) - - self.assertTrue(True) - - def test_001_plugins(self): - """Check mandatory plugins.""" - plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs'] - print('INFO: [TEST_001] Check the mandatory plugins list: %s' % ', '.join(plugins_to_check)) - plugins_list = stats.getPluginsList() - for plugin in plugins_to_check: - self.assertTrue(plugin in plugins_list) - - def test_002_system(self): - """Check SYSTEM plugin.""" - stats_to_check = ['hostname', 'os_name'] - print('INFO: [TEST_002] Check SYSTEM stats: %s' % ', '.join(stats_to_check)) - stats_grab = stats.get_plugin('system').get_raw() - for stat in stats_to_check: - # Check that the key exist - self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) - print('INFO: SYSTEM stats: %s' % stats_grab) - - def test_003_cpu(self): - """Check CPU plugin.""" - stats_to_check = ['system', 'user', 'idle'] - print('INFO: [TEST_003] Check mandatory CPU stats: %s' % ', '.join(stats_to_check)) - stats_grab = stats.get_plugin('cpu').get_raw() - for stat in stats_to_check: - # Check that the key exist - self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) - # Check that % is > 0 and < 100 - self.assertGreaterEqual(stats_grab[stat], 0) - self.assertLessEqual(stats_grab[stat], 100) - print('INFO: CPU stats: %s' % stats_grab) - - @unittest.skipIf(WINDOWS, "Load average not available on Windows") - def test_004_load(self): - """Check LOAD plugin.""" - stats_to_check = ['cpucore', 'min1', 'min5', 'min15'] - print('INFO: [TEST_004] Check LOAD stats: %s' % ', '.join(stats_to_check)) - stats_grab = stats.get_plugin('load').get_raw() - for stat in stats_to_check: - # Check that the key exist - self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) - # Check that % is > 0 - self.assertGreaterEqual(stats_grab[stat], 0) - print('INFO: LOAD stats: %s' % stats_grab) - - def test_005_mem(self): - """Check MEM plugin.""" - stats_to_check = ['available', 'used', 'free', 'total'] - print('INFO: [TEST_005] Check MEM stats: %s' % ', '.join(stats_to_check)) - stats_grab = stats.get_plugin('mem').get_raw() - for stat in stats_to_check: - # Check that the key exist - self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) - # Check that % is > 0 - self.assertGreaterEqual(stats_grab[stat], 0) - print('INFO: MEM stats: %s' % stats_grab) - - def test_006_swap(self): - """Check MEMSWAP plugin.""" - stats_to_check = ['used', 'free', 'total'] - print('INFO: [TEST_006] Check SWAP stats: %s' % ', '.join(stats_to_check)) - stats_grab = stats.get_plugin('memswap').get_raw() - for stat in stats_to_check: - # Check that the key exist - self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) - # Check that % is > 0 - self.assertGreaterEqual(stats_grab[stat], 0) - print('INFO: SWAP stats: %s' % stats_grab) - - def test_007_network(self): - """Check NETWORK plugin.""" - print('INFO: [TEST_007] Check NETWORK stats') - stats_grab = stats.get_plugin('network').get_raw() - self.assertTrue(isinstance(stats_grab, list), msg='Network stats is not a list') - print('INFO: NETWORK stats: %s' % stats_grab) - - def test_008_diskio(self): - """Check DISKIO plugin.""" - print('INFO: [TEST_008] Check DISKIO stats') - stats_grab = stats.get_plugin('diskio').get_raw() - self.assertTrue(isinstance(stats_grab, list), msg='DiskIO stats is not a list') - print('INFO: diskio stats: %s' % stats_grab) - - def test_009_fs(self): - """Check File System plugin.""" - # stats_to_check = [ ] - print('INFO: [TEST_009] Check FS stats') - stats_grab = stats.get_plugin('fs').get_raw() - self.assertTrue(isinstance(stats_grab, list), msg='FileSystem stats is not a list') - print('INFO: FS stats: %s' % stats_grab) - - def test_010_processes(self): - """Check Process plugin.""" - # stats_to_check = [ ] - print('INFO: [TEST_010] Check PROCESS stats') - stats_grab = stats.get_plugin('processcount').get_raw() - # total = stats_grab['total'] - self.assertTrue(isinstance(stats_grab, 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(isinstance(stats_grab, 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 - # self.assertEqual(total, len(stats_grab)) - - 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(isinstance(stats_grab, 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(isinstance(stats_grab, dict), msg='IP stats is not a dict') - print('INFO: IP stats: %s' % stats_grab) - - @unittest.skipIf(not LINUX, "IRQs available only on Linux") - 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(isinstance(stats_grab, list), msg='IRQ stats is not a list') - print('INFO: IRQ stats: %s' % stats_grab) - - @unittest.skipIf(not LINUX, "GPU available only on Linux") - def test_014_gpu(self): - """Check GPU plugin.""" - print('INFO: [TEST_014] Check GPU stats') - stats_grab = stats.get_plugin('gpu').get_raw() - self.assertTrue(isinstance(stats_grab, list), msg='GPU stats is not a list') - print('INFO: GPU stats: %s' % stats_grab) - - def test_015_sorted_stats(self): - """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"}, - ] - - gp = GlancesPluginModel() - 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") - - def test_016_subsample(self): - """Test subsampling function.""" - print('INFO: [TEST_016] Subsampling') - 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]) - - def test_017_hddsmart(self): - """Check hard disk SMART data plugin.""" - try: - from glances.globals import is_admin - except ImportError: - print("INFO: [TEST_017] pySMART not found, not running SMART plugin test") - return - - stat = 'DeviceName' - print('INFO: [TEST_017] Check SMART stats: {}'.format(stat)) - 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 - elif stats_grab == {}: - print("INFO: Admin but SMART list is empty") - assert len(stats_grab) == 0 - else: - print(stats_grab) - self.assertTrue(stat in stats_grab[0].keys(), msg='Cannot find key: %s' % stat) - - print('INFO: SMART stats: %s' % stats_grab) - - def test_017_programs(self): - """Check Programs function (it's not a plugin).""" - # stats_to_check = [ ] - print('INFO: [TEST_017] Check PROGRAM stats') - stats_grab = processes_to_programs(stats.get_plugin('processlist').get_raw()) - self.assertIsInstance(stats_grab, list, msg='Programs stats list is not a list') - self.assertIsInstance(stats_grab[0], dict, msg='First item should be a dict') - - 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) - self.assertEqual(string_value_to_float('--'), None) - - def test_019_events(self): - """Test events class""" - print('INFO: [TEST_019] Test events') - # Init events - events = GlancesEventsList(max_events=5, min_duration=1, min_interval=3) - # Minimal event duration not reached - events.add('WARNING', 'LOAD', 4) - events.add('CRITICAL', 'LOAD', 5) - events.add('OK', 'LOAD', 1) - self.assertEqual(len(events.get()), 0) - # Minimal event duration LOAD reached - events.add('WARNING', 'LOAD', 4) - time.sleep(1) - events.add('CRITICAL', 'LOAD', 5) - time.sleep(1) - events.add('OK', 'LOAD', 1) - self.assertEqual(len(events.get()), 1) - self.assertEqual(events.get()[0]['type'], 'LOAD') - self.assertEqual(events.get()[0]['state'], 'CRITICAL') - self.assertEqual(events.get()[0]['max'], 5) - # Minimal event duration CPU reached - events.add('WARNING', 'CPU', 60) - time.sleep(1) - events.add('WARNING', 'CPU', 70) - time.sleep(1) - events.add('OK', 'CPU', 10) - self.assertEqual(len(events.get()), 2) - self.assertEqual(events.get()[0]['type'], 'CPU') - self.assertEqual(events.get()[0]['state'], 'WARNING') - self.assertEqual(events.get()[0]['min'], 60) - self.assertEqual(events.get()[0]['max'], 70) - self.assertEqual(events.get()[0]['count'], 2) - # Minimal event duration CPU reached (again) - # but time between two events (min_interval) is too short - # a merge will be done - time.sleep(0.5) - events.add('WARNING', 'CPU', 60) - time.sleep(1) - events.add('WARNING', 'CPU', 80) - time.sleep(1) - events.add('OK', 'CPU', 10) - self.assertEqual(len(events.get()), 2) - self.assertEqual(events.get()[0]['type'], 'CPU') - self.assertEqual(events.get()[0]['state'], 'WARNING') - self.assertEqual(events.get()[0]['min'], 60) - self.assertEqual(events.get()[0]['max'], 80) - self.assertEqual(events.get()[0]['count'], 4) - # Clean WARNING events - events.clean() - self.assertEqual(len(events.get()), 1) - - def test_020_filter(self): - """Test filter classes""" - print('INFO: [TEST_020] Test filter') - gf = GlancesFilter() - gf.filter = '.*python.*' - self.assertEqual(gf.filter, '.*python.*') - self.assertEqual(gf.filter_key, None) - self.assertTrue(gf.is_filtered({'name': 'python'})) - self.assertTrue(gf.is_filtered({'name': '/usr/bin/python -m glances'})) - self.assertFalse(gf.is_filtered({'noname': 'python'})) - self.assertFalse(gf.is_filtered({'name': 'snake'})) - gf.filter = 'username:nicolargo' - self.assertEqual(gf.filter, 'nicolargo') - self.assertEqual(gf.filter_key, 'username') - self.assertTrue(gf.is_filtered({'username': 'nicolargo'})) - self.assertFalse(gf.is_filtered({'username': 'notme'})) - self.assertFalse(gf.is_filtered({'notuser': 'nicolargo'})) - gfl = GlancesFilterList() - gfl.filter = '.*python.*,username:nicolargo' - self.assertTrue(gfl.is_filtered({'name': 'python is in the place'})) - self.assertFalse(gfl.is_filtered({'name': 'snake is in the place'})) - self.assertTrue(gfl.is_filtered({'name': 'snake is in the place', 'username': 'nicolargo'})) - self.assertFalse(gfl.is_filtered({'name': 'snake is in the place', 'username': 'notme'})) - - def test_094_thresholds(self): - """Test thresholds classes""" - print('INFO: [TEST_094] Thresholds') - ok = GlancesThresholdOk() - careful = GlancesThresholdCareful() - warning = GlancesThresholdWarning() - critical = GlancesThresholdCritical() - self.assertTrue(ok < careful) - self.assertTrue(careful < warning) - self.assertTrue(warning < critical) - self.assertFalse(ok > careful) - self.assertEqual(ok, ok) - self.assertEqual(str(ok), 'OK') - thresholds = GlancesThresholds() - thresholds.add('cpu_percent', 'OK') - self.assertEqual(thresholds.get(stat_name='cpu_percent').description(), 'OK') - - def test_095_methods(self): - """Test mandatories methods""" - print('INFO: [TEST_095] Mandatories methods') - mandatories_methods = ['reset', 'update'] - plugins_list = stats.getPluginsList() - 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)) - - def test_096_views(self): - """Test get_views method""" - print('INFO: [TEST_096] Test views') - plugins_list = stats.getPluginsList() - for plugin in plugins_list: - stats.get_plugin(plugin).get_raw() - views_grab = stats.get_plugin(plugin).get_views() - self.assertTrue(isinstance(views_grab, dict), - msg='{} view is not a dict'.format(plugin)) - - def test_097_attribute(self): - """Test GlancesAttribute classes""" - 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): - """Test GlancesHistory classes""" - print('INFO: [TEST_098] Test history') - # GlancesHistory - from glances.history import GlancesHistory - h = GlancesHistory() - 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) - 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) - - def test_099_output_bars(self): - """Test quick look plugin. - - > bar.min_value - 0 - > bar.max_value - 100 - > bar.percent = -1 - > bar.percent - 0 - """ - print('INFO: [TEST_099] Test progress bar') - - bar = Bar(size=1) - # Percent value can not be lower than min_value - bar.percent = -1 - self.assertLessEqual(bar.percent, bar.min_value) - # but... percent value can be higher than max_value - bar.percent = 101 - self.assertLessEqual(bar.percent, 101) - - # Test display - bar = Bar(size=50) - bar.percent = 0 - self.assertEqual(bar.get(), ' 0.0%') - bar.percent = 70 - self.assertEqual(bar.get(), '||||||||||||||||||||||||||||||| 70.0%') - bar.percent = 100 - self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| 100%') - bar.percent = 110 - self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%') - - def test_100_secure(self): - """Test secure functions""" - print('INFO: [TEST_100] Secure functions') - - if WINDOWS: - 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']) - else: - self.assertEqual(secure_popen('echo -n TEST'), 'TEST') - self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2') - # Make the test failed on Github (AssertionError: '' != 'FOO\n') - # but not on my localLinux computer... - # self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n') - - 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) - - def test_999_the_end(self): - """Free all the stats""" - print('INFO: [TEST_999] Free the stats') - stats.end() - self.assertTrue(True) - - -if __name__ == '__main__': - unittest.main() From af4765db8afc4e9242bb72e5b4bfb04f4cb37391 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Tue, 7 May 2024 14:07:08 +0200 Subject: [PATCH 2/4] Remove unitest shell script - Use Makefile instead --- .github/workflows/test.yml | 8 +- Makefile | 8 +- setup.py | 4 +- tox.ini | 2 +- unittest-core.py | 547 +++++++++++++++++++++++++++++++++++++ unittest-restful.py | 286 +++++++++++++++++++ unittest-xmlrpc.py | 200 ++++++++++++++ 7 files changed, 1044 insertions(+), 11 deletions(-) create mode 100755 unittest-core.py create mode 100755 unittest-restful.py create mode 100755 unittest-xmlrpc.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bbe1ff68..96f70589 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -46,7 +46,7 @@ jobs: - name: Unitary tests run: | - python ./unittest.py + python ./unittest-core.py # Error appear with h11, not related to Glances # Should be tested if correction is done @@ -80,7 +80,7 @@ jobs: # - name: Unitary tests # run: | - # python ./unittest.py + # python ./unittest-core.py test-macos: @@ -107,7 +107,7 @@ jobs: - name: Unitary tests run: | - python ./unittest.py + python ./unittest-core.py # Error when trying to implement #2749 # pkg: No packages available to install matching 'py-pip' have been found in the repositories @@ -128,4 +128,4 @@ jobs: # run: | # set -e -x # python3 -m pip install --user -r requirements.txt - # python ./unittest.py + # python ./unittest-core.py diff --git a/Makefile b/Makefile index b9e80e63..80ad0b1a 100644 --- a/Makefile +++ b/Makefile @@ -63,22 +63,22 @@ venv-dev-upgrade: ## Upgrade Python 3 dev dependencies # =================================================================== test: ## Run unit tests - ./venv/bin/python ./unittest.py + ./venv/bin/python ./unittest-core.py ./venv/bin/python ./unittest-restful.py ./venv/bin/python ./unittest-xmlrpc.py ./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.py + ./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-min: ## Run unit tests in minimal environment - ./venv-min/bin/python ./unittest.py + ./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.py + ./venv-min/bin/python ./unittest-core.py test-restful-api: ## Run unit tests of the RESTful API ./venv/bin/python ./unittest-restful.py diff --git a/setup.py b/setup.py index cf86c271..a6b22cce 100755 --- a/setup.py +++ b/setup.py @@ -94,7 +94,7 @@ class tests(Command): def run(self): import subprocess import sys - for t in glob.glob('unittest.py'): + for t in glob.glob('unittest-core.py'): ret = subprocess.call([sys.executable, t]) != 0 if ret != 0: raise SystemExit(ret) @@ -120,7 +120,7 @@ setup( include_package_data=True, data_files=get_data_files(), cmdclass={'test': tests}, - test_suite="unittest.py", + test_suite="unittest-core.py", entry_points={"console_scripts": ["glances=glances:main"]}, classifiers=[ 'Development Status :: 5 - Production/Stable', diff --git a/tox.ini b/tox.ini index fd67524a..39f396be 100644 --- a/tox.ini +++ b/tox.ini @@ -25,7 +25,7 @@ deps = jinja2 requests commands = - python unittest.py + python unittest-core.py ; python unittest-restful.py ; python unittest-xmlrpc.py ;flake8 --exclude=build,.tox,.git diff --git a/unittest-core.py b/unittest-core.py new file mode 100755 index 00000000..cd0c5781 --- /dev/null +++ b/unittest-core.py @@ -0,0 +1,547 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Glances - An eye on your system +# +# SPDX-FileCopyrightText: 2022 Nicolas Hennion +# +# SPDX-License-Identifier: LGPL-3.0-only +# + +"""Glances unitary tests suite.""" + +import time +import unittest +import sys + +# Check Python version +if sys.version_info < (3, 8): + print('Glances requires at least Python 3.8 to run.') + sys.exit(1) + +from glances.main import GlancesMain +from glances.stats import GlancesStats +from glances import __version__ +from glances.globals import WINDOWS, LINUX, subsample, string_value_to_float +from glances.outputs.glances_bars import Bar +from glances.thresholds import GlancesThresholdOk +from glances.thresholds import GlancesThresholdCareful +from glances.thresholds import GlancesThresholdWarning +from glances.thresholds import GlancesThresholdCritical +from glances.thresholds import GlancesThresholds +from glances.plugins.plugin.model import GlancesPluginModel +from glances.programs import processes_to_programs +from glances.secure import secure_popen +from glances.events_list import GlancesEventsList +from glances.filter import GlancesFilterList, GlancesFilter + +# Global variables +# ================= + +# Init Glances core +core = GlancesMain() +test_config = core.get_config() +test_args = core.get_args() + +# Init Glances stats +stats = GlancesStats(config=test_config, + args=test_args) + +# Unitest class +# ============== +print('Unitary tests for Glances %s' % __version__) + + +class TestGlances(unittest.TestCase): + """Test Glances class.""" + + def setUp(self): + """The function is called *every time* before test_*.""" + print('\n' + '=' * 78) + + def test_000_update(self): + """Update stats (mandatory step for all the stats). + + The update is made twice (for rate computation). + """ + print('INFO: [TEST_000] Test the stats update function') + try: + stats.update() + except Exception as e: + print('ERROR: Stats update failed: %s' % e) + self.assertTrue(False) + time.sleep(1) + try: + stats.update() + except Exception as e: + print('ERROR: Stats update failed: %s' % e) + self.assertTrue(False) + + self.assertTrue(True) + + def test_001_plugins(self): + """Check mandatory plugins.""" + plugins_to_check = ['system', 'cpu', 'load', 'mem', 'memswap', 'network', 'diskio', 'fs'] + print('INFO: [TEST_001] Check the mandatory plugins list: %s' % ', '.join(plugins_to_check)) + plugins_list = stats.getPluginsList() + for plugin in plugins_to_check: + self.assertTrue(plugin in plugins_list) + + def test_002_system(self): + """Check SYSTEM plugin.""" + stats_to_check = ['hostname', 'os_name'] + print('INFO: [TEST_002] Check SYSTEM stats: %s' % ', '.join(stats_to_check)) + stats_grab = stats.get_plugin('system').get_raw() + for stat in stats_to_check: + # Check that the key exist + self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) + print('INFO: SYSTEM stats: %s' % stats_grab) + + def test_003_cpu(self): + """Check CPU plugin.""" + stats_to_check = ['system', 'user', 'idle'] + print('INFO: [TEST_003] Check mandatory CPU stats: %s' % ', '.join(stats_to_check)) + stats_grab = stats.get_plugin('cpu').get_raw() + for stat in stats_to_check: + # Check that the key exist + self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) + # Check that % is > 0 and < 100 + self.assertGreaterEqual(stats_grab[stat], 0) + self.assertLessEqual(stats_grab[stat], 100) + print('INFO: CPU stats: %s' % stats_grab) + + @unittest.skipIf(WINDOWS, "Load average not available on Windows") + def test_004_load(self): + """Check LOAD plugin.""" + stats_to_check = ['cpucore', 'min1', 'min5', 'min15'] + print('INFO: [TEST_004] Check LOAD stats: %s' % ', '.join(stats_to_check)) + stats_grab = stats.get_plugin('load').get_raw() + for stat in stats_to_check: + # Check that the key exist + self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) + # Check that % is > 0 + self.assertGreaterEqual(stats_grab[stat], 0) + print('INFO: LOAD stats: %s' % stats_grab) + + def test_005_mem(self): + """Check MEM plugin.""" + stats_to_check = ['available', 'used', 'free', 'total'] + print('INFO: [TEST_005] Check MEM stats: %s' % ', '.join(stats_to_check)) + stats_grab = stats.get_plugin('mem').get_raw() + for stat in stats_to_check: + # Check that the key exist + self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) + # Check that % is > 0 + self.assertGreaterEqual(stats_grab[stat], 0) + print('INFO: MEM stats: %s' % stats_grab) + + def test_006_swap(self): + """Check MEMSWAP plugin.""" + stats_to_check = ['used', 'free', 'total'] + print('INFO: [TEST_006] Check SWAP stats: %s' % ', '.join(stats_to_check)) + stats_grab = stats.get_plugin('memswap').get_raw() + for stat in stats_to_check: + # Check that the key exist + self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) + # Check that % is > 0 + self.assertGreaterEqual(stats_grab[stat], 0) + print('INFO: SWAP stats: %s' % stats_grab) + + def test_007_network(self): + """Check NETWORK plugin.""" + print('INFO: [TEST_007] Check NETWORK stats') + stats_grab = stats.get_plugin('network').get_raw() + self.assertTrue(isinstance(stats_grab, list), msg='Network stats is not a list') + print('INFO: NETWORK stats: %s' % stats_grab) + + def test_008_diskio(self): + """Check DISKIO plugin.""" + print('INFO: [TEST_008] Check DISKIO stats') + stats_grab = stats.get_plugin('diskio').get_raw() + self.assertTrue(isinstance(stats_grab, list), msg='DiskIO stats is not a list') + print('INFO: diskio stats: %s' % stats_grab) + + def test_009_fs(self): + """Check File System plugin.""" + # stats_to_check = [ ] + print('INFO: [TEST_009] Check FS stats') + stats_grab = stats.get_plugin('fs').get_raw() + self.assertTrue(isinstance(stats_grab, list), msg='FileSystem stats is not a list') + print('INFO: FS stats: %s' % stats_grab) + + def test_010_processes(self): + """Check Process plugin.""" + # stats_to_check = [ ] + print('INFO: [TEST_010] Check PROCESS stats') + stats_grab = stats.get_plugin('processcount').get_raw() + # total = stats_grab['total'] + self.assertTrue(isinstance(stats_grab, 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(isinstance(stats_grab, 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 + # self.assertEqual(total, len(stats_grab)) + + 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(isinstance(stats_grab, 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(isinstance(stats_grab, dict), msg='IP stats is not a dict') + print('INFO: IP stats: %s' % stats_grab) + + @unittest.skipIf(not LINUX, "IRQs available only on Linux") + 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(isinstance(stats_grab, list), msg='IRQ stats is not a list') + print('INFO: IRQ stats: %s' % stats_grab) + + @unittest.skipIf(not LINUX, "GPU available only on Linux") + def test_014_gpu(self): + """Check GPU plugin.""" + print('INFO: [TEST_014] Check GPU stats') + stats_grab = stats.get_plugin('gpu').get_raw() + self.assertTrue(isinstance(stats_grab, list), msg='GPU stats is not a list') + print('INFO: GPU stats: %s' % stats_grab) + + def test_015_sorted_stats(self): + """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"}, + ] + + gp = GlancesPluginModel() + 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") + + def test_016_subsample(self): + """Test subsampling function.""" + print('INFO: [TEST_016] Subsampling') + 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]) + + def test_017_hddsmart(self): + """Check hard disk SMART data plugin.""" + try: + from glances.globals import is_admin + except ImportError: + print("INFO: [TEST_017] pySMART not found, not running SMART plugin test") + return + + stat = 'DeviceName' + print('INFO: [TEST_017] Check SMART stats: {}'.format(stat)) + 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 + elif stats_grab == {}: + print("INFO: Admin but SMART list is empty") + assert len(stats_grab) == 0 + else: + print(stats_grab) + self.assertTrue(stat in stats_grab[0].keys(), msg='Cannot find key: %s' % stat) + + print('INFO: SMART stats: %s' % stats_grab) + + def test_017_programs(self): + """Check Programs function (it's not a plugin).""" + # stats_to_check = [ ] + print('INFO: [TEST_017] Check PROGRAM stats') + stats_grab = processes_to_programs(stats.get_plugin('processlist').get_raw()) + self.assertIsInstance(stats_grab, list, msg='Programs stats list is not a list') + self.assertIsInstance(stats_grab[0], dict, msg='First item should be a dict') + + 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) + self.assertEqual(string_value_to_float('--'), None) + + def test_019_events(self): + """Test events class""" + print('INFO: [TEST_019] Test events') + # Init events + events = GlancesEventsList(max_events=5, min_duration=1, min_interval=3) + # Minimal event duration not reached + events.add('WARNING', 'LOAD', 4) + events.add('CRITICAL', 'LOAD', 5) + events.add('OK', 'LOAD', 1) + self.assertEqual(len(events.get()), 0) + # Minimal event duration LOAD reached + events.add('WARNING', 'LOAD', 4) + time.sleep(1) + events.add('CRITICAL', 'LOAD', 5) + time.sleep(1) + events.add('OK', 'LOAD', 1) + self.assertEqual(len(events.get()), 1) + self.assertEqual(events.get()[0]['type'], 'LOAD') + self.assertEqual(events.get()[0]['state'], 'CRITICAL') + self.assertEqual(events.get()[0]['max'], 5) + # Minimal event duration CPU reached + events.add('WARNING', 'CPU', 60) + time.sleep(1) + events.add('WARNING', 'CPU', 70) + time.sleep(1) + events.add('OK', 'CPU', 10) + self.assertEqual(len(events.get()), 2) + self.assertEqual(events.get()[0]['type'], 'CPU') + self.assertEqual(events.get()[0]['state'], 'WARNING') + self.assertEqual(events.get()[0]['min'], 60) + self.assertEqual(events.get()[0]['max'], 70) + self.assertEqual(events.get()[0]['count'], 2) + # Minimal event duration CPU reached (again) + # but time between two events (min_interval) is too short + # a merge will be done + time.sleep(0.5) + events.add('WARNING', 'CPU', 60) + time.sleep(1) + events.add('WARNING', 'CPU', 80) + time.sleep(1) + events.add('OK', 'CPU', 10) + self.assertEqual(len(events.get()), 2) + self.assertEqual(events.get()[0]['type'], 'CPU') + self.assertEqual(events.get()[0]['state'], 'WARNING') + self.assertEqual(events.get()[0]['min'], 60) + self.assertEqual(events.get()[0]['max'], 80) + self.assertEqual(events.get()[0]['count'], 4) + # Clean WARNING events + events.clean() + self.assertEqual(len(events.get()), 1) + + def test_020_filter(self): + """Test filter classes""" + print('INFO: [TEST_020] Test filter') + gf = GlancesFilter() + gf.filter = '.*python.*' + self.assertEqual(gf.filter, '.*python.*') + self.assertEqual(gf.filter_key, None) + self.assertTrue(gf.is_filtered({'name': 'python'})) + self.assertTrue(gf.is_filtered({'name': '/usr/bin/python -m glances'})) + self.assertFalse(gf.is_filtered({'noname': 'python'})) + self.assertFalse(gf.is_filtered({'name': 'snake'})) + gf.filter = 'username:nicolargo' + self.assertEqual(gf.filter, 'nicolargo') + self.assertEqual(gf.filter_key, 'username') + self.assertTrue(gf.is_filtered({'username': 'nicolargo'})) + self.assertFalse(gf.is_filtered({'username': 'notme'})) + self.assertFalse(gf.is_filtered({'notuser': 'nicolargo'})) + gfl = GlancesFilterList() + gfl.filter = '.*python.*,username:nicolargo' + self.assertTrue(gfl.is_filtered({'name': 'python is in the place'})) + self.assertFalse(gfl.is_filtered({'name': 'snake is in the place'})) + self.assertTrue(gfl.is_filtered({'name': 'snake is in the place', 'username': 'nicolargo'})) + self.assertFalse(gfl.is_filtered({'name': 'snake is in the place', 'username': 'notme'})) + + def test_094_thresholds(self): + """Test thresholds classes""" + print('INFO: [TEST_094] Thresholds') + ok = GlancesThresholdOk() + careful = GlancesThresholdCareful() + warning = GlancesThresholdWarning() + critical = GlancesThresholdCritical() + self.assertTrue(ok < careful) + self.assertTrue(careful < warning) + self.assertTrue(warning < critical) + self.assertFalse(ok > careful) + self.assertEqual(ok, ok) + self.assertEqual(str(ok), 'OK') + thresholds = GlancesThresholds() + thresholds.add('cpu_percent', 'OK') + self.assertEqual(thresholds.get(stat_name='cpu_percent').description(), 'OK') + + def test_095_methods(self): + """Test mandatories methods""" + print('INFO: [TEST_095] Mandatories methods') + mandatories_methods = ['reset', 'update'] + plugins_list = stats.getPluginsList() + 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)) + + def test_096_views(self): + """Test get_views method""" + print('INFO: [TEST_096] Test views') + plugins_list = stats.getPluginsList() + for plugin in plugins_list: + stats.get_plugin(plugin).get_raw() + views_grab = stats.get_plugin(plugin).get_views() + self.assertTrue(isinstance(views_grab, dict), + msg='{} view is not a dict'.format(plugin)) + + def test_097_attribute(self): + """Test GlancesAttribute classes""" + 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): + """Test GlancesHistory classes""" + print('INFO: [TEST_098] Test history') + # GlancesHistory + from glances.history import GlancesHistory + h = GlancesHistory() + 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) + 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) + + def test_099_output_bars(self): + """Test quick look plugin. + + > bar.min_value + 0 + > bar.max_value + 100 + > bar.percent = -1 + > bar.percent + 0 + """ + print('INFO: [TEST_099] Test progress bar') + + bar = Bar(size=1) + # Percent value can not be lower than min_value + bar.percent = -1 + self.assertLessEqual(bar.percent, bar.min_value) + # but... percent value can be higher than max_value + bar.percent = 101 + self.assertLessEqual(bar.percent, 101) + + # Test display + bar = Bar(size=50) + bar.percent = 0 + self.assertEqual(bar.get(), ' 0.0%') + bar.percent = 70 + self.assertEqual(bar.get(), '||||||||||||||||||||||||||||||| 70.0%') + bar.percent = 100 + self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| 100%') + bar.percent = 110 + self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%') + + def test_100_secure(self): + """Test secure functions""" + print('INFO: [TEST_100] Secure functions') + + if WINDOWS: + 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']) + else: + self.assertEqual(secure_popen('echo -n TEST'), 'TEST') + self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2') + # Make the test failed on Github (AssertionError: '' != 'FOO\n') + # but not on my localLinux computer... + # self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n') + + 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) + + def test_999_the_end(self): + """Free all the stats""" + print('INFO: [TEST_999] Free the stats') + stats.end() + self.assertTrue(True) + + +if __name__ == '__main__': + unittest.main() diff --git a/unittest-restful.py b/unittest-restful.py new file mode 100755 index 00000000..bc2dc330 --- /dev/null +++ b/unittest-restful.py @@ -0,0 +1,286 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Glances - An eye on your system +# +# SPDX-FileCopyrightText: 2022 Nicolas Hennion +# +# SPDX-License-Identifier: LGPL-3.0-only +# + +"""Glances unitary tests suite for the RESTful API.""" + +import os +import shlex +import subprocess +import time +import numbers +import unittest + +from glances.outputs.glances_restful_api import GlancesRestfulApi +from glances import __version__ +from glances.globals import text_type + +import requests + +SERVER_PORT = 61234 +API_VERSION = GlancesRestfulApi.API_VERSION +URL = "http://localhost:{}/api/{}".format(SERVER_PORT, API_VERSION) +pid = None + +# Unitest class +# ============== +print('RESTful API unitary tests for Glances %s' % __version__) + + +class TestGlances(unittest.TestCase): + """Test Glances class.""" + + def setUp(self): + """The function is called *every time* before test_*.""" + print('\n' + '=' * 78) + + def http_get(self, url, gzip=False): + """Make the request""" + if gzip: + ret = requests.get(url, + stream=True, + headers={'Accept-encoding': 'gzip'}) + else: + ret = requests.get(url, + headers={'Accept-encoding': 'identity'}) + return ret + + def test_000_start_server(self): + """Start the Glances Web Server.""" + global pid + + print('INFO: [TEST_000] Start the Glances Web Server API') + if os.path.isfile('./venv/bin/python'): + cmdline = "./venv/bin/python" + else: + cmdline = "python" + cmdline += " -m glances -B 0.0.0.0 -w -p %s --disable-webui -C ./conf/glances.conf" % SERVER_PORT + print("Run the Glances Web Server on port %s" % SERVER_PORT) + args = shlex.split(cmdline) + pid = subprocess.Popen(args) + print("Please wait 5 seconds...") + time.sleep(5) + + self.assertTrue(pid is not None) + + def test_001_all(self): + """All.""" + method = "all" + print('INFO: [TEST_001] Get all stats') + print("HTTP RESTful request: %s/%s" % (URL, method)) + req = self.http_get("%s/%s" % (URL, method)) + + self.assertTrue(req.ok) + self.assertTrue(req.json(), dict) + + def test_002_pluginslist(self): + """Plugins list.""" + method = "pluginslist" + print('INFO: [TEST_002] Plugins list') + print("HTTP RESTful request: %s/%s" % (URL, method)) + req = self.http_get("%s/%s" % (URL, method)) + + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), list) + self.assertIn('cpu', req.json()) + + def test_003_plugins(self): + """Plugins.""" + method = "pluginslist" + print('INFO: [TEST_003] Plugins') + plist = self.http_get("%s/%s" % (URL, method)) + + for p in plist.json(): + print("HTTP RESTful request: %s/%s" % (URL, p)) + req = self.http_get("%s/%s" % (URL, p)) + self.assertTrue(req.ok) + if p in ('uptime', 'version', 'psutilversion'): + self.assertIsInstance(req.json(), text_type) + elif p in ('fs', 'percpu', 'sensors', 'alert', 'processlist', 'diskio', + 'hddtemp', 'batpercent', 'network', 'folders', 'amps', 'ports', + 'irq', 'wifi', 'gpu', 'containers'): + self.assertIsInstance(req.json(), list) + if len(req.json()) > 0: + self.assertIsInstance(req.json()[0], dict) + else: + self.assertIsInstance(req.json(), dict) + + def test_004_items(self): + """Items.""" + method = "cpu" + print('INFO: [TEST_004] Items for the CPU method') + ilist = self.http_get("%s/%s" % (URL, method)) + + for i in ilist.json(): + print("HTTP RESTful request: %s/%s/%s" % (URL, method, i)) + req = self.http_get("%s/%s/%s" % (URL, method, i)) + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + print(req.json()[i]) + self.assertIsInstance(req.json()[i], numbers.Number) + + def test_005_values(self): + """Values.""" + method = "processlist" + print('INFO: [TEST_005] Item=Value for the PROCESSLIST method') + print("%s/%s/pid/0" % (URL, method)) + req = self.http_get("%s/%s/pid/0" % (URL, method)) + + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + + def test_006_all_limits(self): + """All limits.""" + method = "all/limits" + print('INFO: [TEST_006] Get all limits') + print("HTTP RESTful request: %s/%s" % (URL, method)) + req = self.http_get("%s/%s" % (URL, method)) + + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + + def test_007_all_views(self): + """All views.""" + method = "all/views" + print('INFO: [TEST_007] Get all views') + print("HTTP RESTful request: %s/%s" % (URL, method)) + req = self.http_get("%s/%s" % (URL, method)) + + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + + def test_008_plugins_limits(self): + """Plugins limits.""" + method = "pluginslist" + print('INFO: [TEST_008] Plugins limits') + plist = self.http_get("%s/%s" % (URL, method)) + + for p in plist.json(): + print("HTTP RESTful request: %s/%s/limits" % (URL, p)) + req = self.http_get("%s/%s/limits" % (URL, p)) + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + + def test_009_plugins_views(self): + """Plugins views.""" + method = "pluginslist" + print('INFO: [TEST_009] Plugins views') + plist = self.http_get("%s/%s" % (URL, method)) + + for p in plist.json(): + print("HTTP RESTful request: %s/%s/views" % (URL, p)) + req = self.http_get("%s/%s/views" % (URL, p)) + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + + def test_010_history(self): + """History.""" + method = "history" + print('INFO: [TEST_010] History') + print("HTTP RESTful request: %s/cpu/%s" % (URL, method)) + req = self.http_get("%s/cpu/%s" % (URL, method)) + self.assertIsInstance(req.json(), dict) + self.assertIsInstance(req.json()['user'], list) + self.assertTrue(len(req.json()['user']) > 0) + print("HTTP RESTful request: %s/cpu/%s/3" % (URL, method)) + req = self.http_get("%s/cpu/%s/3" % (URL, method)) + self.assertIsInstance(req.json(), dict) + self.assertIsInstance(req.json()['user'], list) + self.assertTrue(len(req.json()['user']) > 1) + print("HTTP RESTful request: %s/cpu/system/%s" % (URL, method)) + req = self.http_get("%s/cpu/system/%s" % (URL, method)) + self.assertIsInstance(req.json(), list) + self.assertIsInstance(req.json()[0], list) + print("HTTP RESTful request: %s/cpu/system/%s/3" % (URL, method)) + req = self.http_get("%s/cpu/system/%s/3" % (URL, method)) + self.assertIsInstance(req.json(), list) + self.assertIsInstance(req.json()[0], list) + + def test_011_issue1401(self): + """Check issue #1401.""" + method = "network/interface_name" + print('INFO: [TEST_011] Issue #1401') + req = self.http_get("%s/%s" % (URL, method)) + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + self.assertIsInstance(req.json()['interface_name'], list) + + def test_012_status(self): + """Check status endpoint.""" + method = "status" + print('INFO: [TEST_012] Status') + print("HTTP RESTful request: %s/%s" % (URL, method)) + req = self.http_get("%s/%s" % (URL, method)) + + self.assertTrue(req.ok) + self.assertEqual(req.json()['version'], __version__) + + def test_013_top(self): + """Values.""" + method = "processlist" + request = "%s/%s/top/2" % (URL, method) + print('INFO: [TEST_013] Top nb item of PROCESSLIST') + print(request) + req = self.http_get(request) + + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), list) + self.assertEqual(len(req.json()), 2) + + def test_014_config(self): + """Test API request to get Glances configuration.""" + method = "config" + print('INFO: [TEST_014] Get config') + + req = self.http_get("%s/%s" % (URL, method)) + self.assertTrue(req.ok) + self.assertIsInstance(req.json(), dict) + + req = self.http_get("%s/%s/global/refresh" % (URL, method)) + self.assertTrue(req.ok) + self.assertEqual(req.json(), "2") + + def test_015_all_gzip(self): + """All with Gzip.""" + method = "all" + print('INFO: [TEST_015] Get all stats (with GZip compression)') + print("HTTP RESTful request: %s/%s" % (URL, method)) + req = self.http_get("%s/%s" % (URL, method), gzip=True) + + self.assertTrue(req.ok) + self.assertTrue(req.headers['Content-Encoding'] == 'gzip') + self.assertTrue(req.json(), dict) + + def test_016_fields_description(self): + """Fields description.""" + print('INFO: [TEST_016] Get fields description and unit') + + print("HTTP RESTful request: %s/cpu/total/description" % URL) + req = self.http_get("%s/cpu/total/description" % URL) + self.assertTrue(req.ok) + self.assertTrue(req.json(), str) + + print("HTTP RESTful request: %s/cpu/total/unit" % URL) + req = self.http_get("%s/cpu/total/unit" % URL) + self.assertTrue(req.ok) + self.assertTrue(req.json(), str) + + def test_999_stop_server(self): + """Stop the Glances Web Server.""" + print('INFO: [TEST_999] Stop the Glances Web Server') + + print("Stop the Glances Web Server") + pid.terminate() + time.sleep(1) + + self.assertTrue(True) + + +if __name__ == '__main__': + unittest.main() diff --git a/unittest-xmlrpc.py b/unittest-xmlrpc.py new file mode 100755 index 00000000..03df2bac --- /dev/null +++ b/unittest-xmlrpc.py @@ -0,0 +1,200 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Glances - An eye on your system +# +# SPDX-FileCopyrightText: 2022 Nicolas Hennion +# +# SPDX-License-Identifier: LGPL-3.0-only +# + +"""Glances unitary tests suite for the XML-RPC API.""" + +import os +import json +import shlex +import subprocess +import time +import unittest + +from glances import __version__ +from glances.globals import ServerProxy + +SERVER_PORT = 61234 +URL = "http://localhost:%s" % SERVER_PORT +pid = None + +# Init the XML-RPC client +client = ServerProxy(URL) + +# Unitest class +# ============== +print('XML-RPC API unitary tests for Glances %s' % __version__) + + +class TestGlances(unittest.TestCase): + """Test Glances class.""" + + def setUp(self): + """The function is called *every time* before test_*.""" + print('\n' + '=' * 78) + + def test_000_start_server(self): + """Start the Glances Web Server.""" + global pid + + print('INFO: [TEST_000] Start the Glances Web Server') + if os.path.isfile('./venv/bin/python'): + cmdline = "./venv/bin/python" + else: + cmdline = "python" + cmdline += " -m glances -B localhost -s -p %s" % SERVER_PORT + print("Run the Glances Server on port %s" % SERVER_PORT) + args = shlex.split(cmdline) + pid = subprocess.Popen(args) + print("Please wait...") + time.sleep(1) + + self.assertTrue(pid is not None) + + def test_001_all(self): + """All.""" + method = "getAll()" + print('INFO: [TEST_001] Connection test') + print("XML-RPC request: %s" % method) + req = json.loads(client.getAll()) + + self.assertIsInstance(req, dict) + + def test_002_pluginslist(self): + """Plugins list.""" + method = "getAllPlugins()" + print('INFO: [TEST_002] Get plugins list') + print("XML-RPC request: %s" % method) + req = json.loads(client.getAllPlugins()) + + self.assertIsInstance(req, list) + + def test_003_system(self): + """System.""" + method = "getSystem()" + print('INFO: [TEST_003] Method: %s' % method) + req = json.loads(client.getSystem()) + + self.assertIsInstance(req, dict) + + def test_004_cpu(self): + """CPU.""" + method = "getCpu(), getPerCpu(), getLoad() and getCore()" + print('INFO: [TEST_004] Method: %s' % method) + + req = json.loads(client.getCpu()) + self.assertIsInstance(req, dict) + + req = json.loads(client.getPerCpu()) + self.assertIsInstance(req, list) + + req = json.loads(client.getLoad()) + self.assertIsInstance(req, dict) + + req = json.loads(client.getCore()) + self.assertIsInstance(req, dict) + + def test_005_mem(self): + """MEM.""" + method = "getMem() and getMemSwap()" + print('INFO: [TEST_005] Method: %s' % method) + + req = json.loads(client.getMem()) + self.assertIsInstance(req, dict) + + req = json.loads(client.getMemSwap()) + self.assertIsInstance(req, dict) + + def test_006_net(self): + """NETWORK.""" + method = "getNetwork()" + print('INFO: [TEST_006] Method: %s' % method) + + req = json.loads(client.getNetwork()) + self.assertIsInstance(req, list) + + def test_007_disk(self): + """DISK.""" + method = "getFs(), getFolders() and getDiskIO()" + print('INFO: [TEST_007] Method: %s' % method) + + req = json.loads(client.getFs()) + self.assertIsInstance(req, list) + + req = json.loads(client.getFolders()) + self.assertIsInstance(req, list) + + req = json.loads(client.getDiskIO()) + self.assertIsInstance(req, list) + + def test_008_sensors(self): + """SENSORS.""" + method = "getSensors()" + print('INFO: [TEST_008] Method: %s' % method) + + req = json.loads(client.getSensors()) + self.assertIsInstance(req, list) + + def test_009_process(self): + """PROCESS.""" + method = "getProcessCount() and getProcessList()" + print('INFO: [TEST_009] Method: %s' % method) + + req = json.loads(client.getProcessCount()) + self.assertIsInstance(req, dict) + + req = json.loads(client.getProcessList()) + self.assertIsInstance(req, list) + + def test_010_all_limits(self): + """All limits.""" + method = "getAllLimits()" + print('INFO: [TEST_010] Method: %s' % method) + + req = json.loads(client.getAllLimits()) + self.assertIsInstance(req, dict) + self.assertIsInstance(req['cpu'], dict) + + def test_011_all_views(self): + """All views.""" + method = "getAllViews()" + print('INFO: [TEST_011] Method: %s' % method) + + req = json.loads(client.getAllViews()) + self.assertIsInstance(req, dict) + self.assertIsInstance(req['cpu'], dict) + + def test_012_irq(self): + """IRQS""" + method = "getIrqs()" + print('INFO: [TEST_012] Method: %s' % method) + req = json.loads(client.getIrq()) + self.assertIsInstance(req, list) + + def test_013_plugin_views(self): + """Plugin views.""" + method = "getViewsCpu()" + print('INFO: [TEST_013] Method: %s' % method) + + req = json.loads(client.getViewsCpu()) + self.assertIsInstance(req, dict) + + def test_999_stop_server(self): + """Stop the Glances Web Server.""" + print('INFO: [TEST_999] Stop the Glances Server') + + print("Stop the Glances Server") + pid.terminate() + time.sleep(1) + + self.assertTrue(True) + + +if __name__ == '__main__': + unittest.main() From fd3625826b822f145181420b7b56a927b53d8c58 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Tue, 7 May 2024 15:26:16 +0200 Subject: [PATCH 3/4] First work done for plugin model (stats only) --- Makefile | 19 +++++++------- glances/plugins/plugin/model.py | 8 +++++- unittest-core.py | 46 ++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 80ad0b1a..8f8f5607 100644 --- a/Makefile +++ b/Makefile @@ -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 # =================================================================== diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index 8857fd11..afc0b0ad 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -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. diff --git a/unittest-core.py b/unittest-core.py index cd0c5781..51f3fc2a 100755 --- a/unittest-core.py +++ b/unittest-core.py @@ -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): From f2d7e13cd8491fc5f7402ad79f815046f9cfd1ba Mon Sep 17 00:00:00 2001 From: nicolargo Date: Thu, 9 May 2024 10:09:05 +0200 Subject: [PATCH 4/4] Improve tests by testing all the plugin/model.py methods #2757 --- Makefile | 1 - glances/plugins/plugin/model.py | 21 +--- unittest-core.py | 190 +++++++++++++++++++++++++------- 3 files changed, 154 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index 8f8f5607..dcf0672d 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,6 @@ 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 test ## Upgrade deps and run unit tests diff --git a/glances/plugins/plugin/model.py b/glances/plugins/plugin/model.py index afc0b0ad..cfc846a8 100644 --- a/glances/plugins/plugin/model.py +++ b/glances/plugins/plugin/model.py @@ -236,30 +236,13 @@ class GlancesPluginModel(object): else: return None - def get_json_history(self, item=None, nb=0): - """Return the history (JSON format). - - - the stats history (dict of list) if item is None - - the stats history for the given item (list) instead - - None if item did not exist in the history - Limit to lasts nb items (all if nb=0) - """ - s = self.stats_history.get_json(nb=nb) - if item is None: - return s - else: - if item in s: - return s[item] - else: - return None - def get_export_history(self, item=None): """Return the stats history object to export.""" return self.get_raw_history(item=item) def get_stats_history(self, item=None, nb=0): """Return the stats history (JSON format).""" - s = self.get_json_history(nb=nb) + s = self.stats_history.get_json(nb=nb) if item is None: return json_dumps(s) @@ -751,6 +734,8 @@ class GlancesPluginModel(object): return {k: v for k, v in stats._asdict().items() if k in self.fields_description} elif isinstance(stats, dict): return {k: v for k, v in stats.items() if k in self.fields_description} + elif isinstance(stats, list): + return [self.filter_stats(s) for s in stats] else: return stats diff --git a/unittest-core.py b/unittest-core.py index 51f3fc2a..87a7718a 100755 --- a/unittest-core.py +++ b/unittest-core.py @@ -60,6 +60,113 @@ class TestGlances(unittest.TestCase): """The function is called *every time* before test_*.""" print('\n' + '=' * 78) + def _common_plugin_tests(self, plugin): + """Common method to test a Glances plugin + This method is called multiple time by test 100 to 1xx""" + + # Reset all the stats, history and views + plugin_instance = stats.get_plugin(plugin) + 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.assertEqual(plugin_instance.is_enabled(), True) + self.assertEqual(plugin_instance.is_disabled(), False) + self.assertEqual(plugin_instance.get_views(), {}) + self.assertIsInstance(plugin_instance.get_raw(), (dict, list)) + if plugin_instance.history_enable() and isinstance(plugin_instance.get_raw(), dict): + self.assertEqual(plugin_instance.get_key(), None) + self.assertTrue(all([f in [h['name'] for h in plugin_instance.items_history_list] for f in plugin_instance.get_raw_history()])) + elif plugin_instance.history_enable() and isinstance(plugin_instance.get_raw(), list): + self.assertNotEqual(plugin_instance.get_key(), None) + + # Update stats (add first element) + plugin_instance.update() + plugin_instance.update_stats_history() + plugin_instance.update_views() + + # Check stats + self.assertIsInstance(plugin_instance.get_raw(), (dict, list)) + if isinstance(plugin_instance.get_raw(), dict): + # self.assertTrue(any([f in plugin_instance.get_raw() for f in plugin_instance.fields_description])) + res = False + for f in plugin_instance.fields_description: + if f not in plugin_instance.get_raw(): + print(f"WARNING: {f} not found in {plugin} plugin fields_description") + else: + res = True + self.assertTrue(res) + elif isinstance(plugin_instance.get_raw(), list): + res = False + for i in plugin_instance.get_raw(): + # self.assertTrue(all([f in plugin_instance.fields_description for f in i])) + for f in i: + if f in plugin_instance.fields_description: + res = True + self.assertTrue(res) + + 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()) + if len(plugin_instance.fields_description) > 0: + # Get first item of the fields_description + first_field = list(plugin_instance.fields_description.keys())[0] + self.assertIsInstance(plugin_instance.get_raw_stats_item(first_field), dict) + self.assertIsInstance(json.loads(plugin_instance.get_stats_item(first_field)), dict) + self.assertIsInstance(plugin_instance.get_item_info(first_field, 'description'), str) + # Filter stats + current_stats = plugin_instance.get_raw() + if isinstance(plugin_instance.get_raw(), dict): + current_stats['foo'] = 'bar' + current_stats = plugin_instance.filter_stats(current_stats) + self.assertTrue('foo' not in current_stats) + elif isinstance(plugin_instance.get_raw(), list): + current_stats[0]['foo'] = 'bar' + current_stats = plugin_instance.filter_stats(current_stats) + self.assertTrue('foo' not in current_stats[0]) + + # Update stats (add second element) + plugin_instance.update() + plugin_instance.update_stats_history() + plugin_instance.update_views() + + # Check history + if plugin_instance.history_enable(): + if isinstance(plugin_instance.get_raw(), dict): + first_history_field = plugin_instance.get_items_history_list()[0]['name'] + elif isinstance(plugin_instance.get_raw(), list): + first_history_field = '_'.join([plugin_instance.get_raw()[0][plugin_instance.get_key()], + plugin_instance.get_items_history_list()[0]['name']]) + self.assertEqual(len(plugin_instance.get_raw_history(first_history_field)), 2) + self.assertGreater(plugin_instance.get_raw_history(first_history_field)[1][0], + plugin_instance.get_raw_history(first_history_field)[0][0]) + + # Update stats (add third element) + plugin_instance.update() + plugin_instance.update_stats_history() + plugin_instance.update_views() + + self.assertEqual(len(plugin_instance.get_raw_history(first_history_field)), 3) + self.assertEqual(len(plugin_instance.get_raw_history(first_history_field, 2)), 2) + self.assertIsInstance(json.loads(plugin_instance.get_stats_history()), dict) + + # Check views + self.assertIsInstance(plugin_instance.get_views(), dict) + if isinstance(plugin_instance.get_raw(), dict): + self.assertIsInstance(plugin_instance.get_views(first_history_field), dict) + self.assertTrue('decoration' in plugin_instance.get_views(first_history_field)) + elif isinstance(plugin_instance.get_raw(), list): + first_history_field = plugin_instance.get_items_history_list()[0]['name'] + first_item = plugin_instance.get_raw()[0][plugin_instance.get_key()] + self.assertIsInstance(plugin_instance.get_views(item=first_item, + key=first_history_field), dict) + self.assertTrue('decoration' in plugin_instance.get_views(item=first_item, + key=first_history_field)) + self.assertIsInstance(json.loads(plugin_instance.get_json_views()), dict) + self.assertEqual(json.loads(plugin_instance.get_json_views()), plugin_instance.get_views()) + def test_000_update(self): """Update stats (mandatory step for all the stats). @@ -126,8 +233,9 @@ class TestGlances(unittest.TestCase): def test_005_mem(self): """Check MEM plugin.""" + plugin_name = 'mem' stats_to_check = ['available', 'used', 'free', 'total'] - print('INFO: [TEST_005] Check MEM stats: %s' % ', '.join(stats_to_check)) + print('INFO: [TEST_005] Check {} stats: {}'.format(plugin_name, ', '.join(stats_to_check))) stats_grab = stats.get_plugin('mem').get_raw() for stat in stats_to_check: # Check that the key exist @@ -136,17 +244,17 @@ class TestGlances(unittest.TestCase): self.assertGreaterEqual(stats_grab[stat], 0) print('INFO: MEM stats: %s' % stats_grab) - def test_006_swap(self): + def test_006_memswap(self): """Check MEMSWAP plugin.""" stats_to_check = ['used', 'free', 'total'] - print('INFO: [TEST_006] Check SWAP stats: %s' % ', '.join(stats_to_check)) + print('INFO: [TEST_006] Check MEMSWAP stats: %s' % ', '.join(stats_to_check)) stats_grab = stats.get_plugin('memswap').get_raw() for stat in stats_to_check: # Check that the key exist self.assertTrue(stat in stats_grab, msg='Cannot find key: %s' % stat) # Check that % is > 0 self.assertGreaterEqual(stats_grab[stat], 0) - print('INFO: SWAP stats: %s' % stats_grab) + print('INFO: MEMSWAP stats: %s' % stats_grab) def test_007_network(self): """Check NETWORK plugin.""" @@ -481,42 +589,46 @@ class TestGlances(unittest.TestCase): bar.percent = 110 self.assertEqual(bar.get(), '|||||||||||||||||||||||||||||||||||||||||||| >100%') - 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_100_system_plugin_method(self): + # """Test system plugin methods""" + # print('INFO: [TEST_100] Test system plugin methods') + # self._common_plugin_tests('system') - 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_101_cpu_plugin_method(self): + """Test cpu plugin methods""" + print('INFO: [TEST_100] Test cpu plugin methods') + self._common_plugin_tests('cpu') + + @unittest.skipIf(WINDOWS, "Load average not available on Windows") + def test_102_load_plugin_method(self): + """Test load plugin methods""" + print('INFO: [TEST_102] Test load plugin methods') + self._common_plugin_tests('load') + + def test_103_mem_plugin_method(self): + """Test mem plugin methods""" + print('INFO: [TEST_103] Test mem plugin methods') + self._common_plugin_tests('mem') + + def test_104_memswap_plugin_method(self): + """Test memswap plugin methods""" + print('INFO: [TEST_104] Test memswap plugin methods') + self._common_plugin_tests('memswap') + + def test_105_network_plugin_method(self): + """Test network plugin methods""" + print('INFO: [TEST_105] Test network plugin methods') + self._common_plugin_tests('network') + + # def test_106_diskio_plugin_method(self): + # """Test diskio plugin methods""" + # print('INFO: [TEST_106] Test diskio plugin methods') + # self._common_plugin_tests('diskio') + + def test_107_fs_plugin_method(self): + """Test fs plugin methods""" + print('INFO: [TEST_107] Test fs plugin methods') + self._common_plugin_tests('fs') def test_700_secure(self): """Test secure functions"""