From c0dad7818975a4ce2d7482cbfe73f1a4574a329b Mon Sep 17 00:00:00 2001 From: nicolargo Date: Mon, 10 Nov 2025 10:53:52 +0100 Subject: [PATCH] Unittest now take into account the default glances.conf file --- generate_openapi.py | 5 ++++- glances/api.py | 4 ++-- glances/main.py | 16 +++++++--------- tests/conftest.py | 9 +++++++-- tests/test_api.py | 8 +++++++- tests/test_core.py | 25 +++++++++++-------------- 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/generate_openapi.py b/generate_openapi.py index 5de43ede..27db11aa 100644 --- a/generate_openapi.py +++ b/generate_openapi.py @@ -1,4 +1,5 @@ import json +from unittest.mock import patch from fastapi.openapi.utils import get_openapi @@ -8,7 +9,9 @@ from glances.main import GlancesMain from glances.outputs.glances_restful_api import GlancesRestfulApi # Init Glances core -core = GlancesMain(args_begin_at=2) +testargs = ["glances", "-C", "./conf/glances.conf"] +with patch('sys.argv', testargs): + core = GlancesMain() test_config = core.get_config() test_args = core.get_args() diff --git a/glances/api.py b/glances/api.py index 5ef733c5..fe4a9b5f 100644 --- a/glances/api.py +++ b/glances/api.py @@ -21,10 +21,10 @@ plugin_dependencies_tree = { class GlancesAPI: ttl = 2.0 # Default cache TTL in seconds - def __init__(self, config=None, args=None, args_begin_at=1): + def __init__(self, config=None, args=None): self.__version__ = glances_version.split('.')[0] # Get the major version - core = GlancesMain(args_begin_at) + core = GlancesMain() self.args = args if args is not None else core.get_args() self.config = config if config is not None else core.get_config() self._stats = GlancesStats(config=self.config, args=self.args) diff --git a/glances/main.py b/glances/main.py index a888bb92..2db96850 100644 --- a/glances/main.py +++ b/glances/main.py @@ -103,14 +103,14 @@ Examples of use: """ - def __init__(self, args_begin_at=1): + def __init__(self): """Manage the command line arguments.""" - self.init_glances(args_begin_at) + self.init_glances() - def init_glances(self, args_begin_at): + def init_glances(self): """Main method to init Glances.""" # Read the command line arguments or parse the one given in parameter (parser) - self.args = self.parse_args(args_begin_at) + self.args = self.parse_args() # Load the configuration file, if it exists # This function should be called after the parse_args @@ -846,11 +846,9 @@ Examples of use: if args.disable_history: logger.info("Stats history is disabled") - def parse_args(self, args_begin_at): - """Parse command line arguments. - Glances args start at position args_begin_at. - """ - return self.init_args().parse_args(sys.argv[args_begin_at:]) + def parse_args(self): + """Parse command line arguments.""" + return self.init_args().parse_args(sys.argv[1:]) def check_mode_compatibility(self): """Check mode compatibility""" diff --git a/tests/conftest.py b/tests/conftest.py index b0256e23..608185df 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ import os import shlex import subprocess import time +from unittest.mock import patch import pytest from selenium import webdriver @@ -41,7 +42,9 @@ def logger(): @pytest.fixture(scope="session") def glances_stats(): - core = GlancesMain(args_begin_at=2) + testargs = ["glances", "-C", "./conf/glances.conf"] + with patch('sys.argv', testargs): + core = GlancesMain() stats = GlancesStats(config=core.get_config(), args=core.get_args()) yield stats stats.end() @@ -49,7 +52,9 @@ def glances_stats(): @pytest.fixture(scope="module") def glances_stats_no_history(): - core = GlancesMain(args_begin_at=2) + testargs = ["glances", "-C", "./conf/glances.conf"] + with patch('sys.argv', testargs): + core = GlancesMain() args = core.get_args() args.time = 1 args.cached_time = 1 diff --git a/tests/test_api.py b/tests/test_api.py index 1bc35861..4d30a369 100755 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -9,6 +9,8 @@ """Glances API unitary tests suite.""" +from unittest.mock import patch + from glances import __version__, api # Global variables @@ -17,7 +19,10 @@ from glances import __version__, api # Init Glances API # test_config = core.get_config() # test_args = core.get_args() -gl = api.GlancesAPI(args_begin_at=2) + +testargs = ["glances", "-C", "./conf/glances.conf"] +with patch('sys.argv', testargs): + gl = api.GlancesAPI() # Pytest functions to test the Glances API version @@ -82,3 +87,4 @@ def test_glances_api_plugin_process(): def test_glances_api_limits(): assert isinstance(gl.cpu.limits, dict) + assert isinstance(gl.cpu.limits, dict) diff --git a/tests/test_core.py b/tests/test_core.py index 239a0779..8081be21 100755 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -15,6 +15,7 @@ import multiprocessing import time import unittest from datetime import datetime +from unittest.mock import patch # Ugly hack waiting for Python 3.10 deprecation try: @@ -52,12 +53,12 @@ else: # ================= # Init Glances core -core = GlancesMain(args_begin_at=2) +testargs = ["glances", "-C", "./conf/glances.conf"] +with patch('sys.argv', testargs): + core = GlancesMain() test_config = core.get_config() test_args = core.get_args() -test_args.conf_file = './conf/glances.conf' - # Init Glances stats stats = GlancesStats(config=test_config, args=test_args) @@ -543,17 +544,13 @@ class TestGlances(unittest.TestCase): self.assertEqual(get_plugin_dependencies('quicklook'), ['quicklook', 'fs', 'core', 'load', 'alert']) self.assertEqual(get_plugin_dependencies('vms'), ['vms', 'processcount', 'alert']) - # def test_023_get_alert(self): - # """Test get_alert function""" - # print('INFO: [TEST_023] get_alert') - # self.assertEqual(stats.get_plugin('cpu').get_alert(10, minimum=0, maximum=100, header='total'), - # 'OK_LOG') - # self.assertEqual(stats.get_plugin('cpu').get_alert(65, minimum=0, maximum=100, header='total'), - # 'CAREFUL_LOG') - # self.assertEqual(stats.get_plugin('cpu').get_alert(75, minimum=0, maximum=100, header='total'), - # 'WARNING_LOG') - # self.assertEqual(stats.get_plugin('cpu').get_alert(85, minimum=0, maximum=100, header='total'), - # 'CRITICAL_LOG') + def test_023_get_alert(self): + """Test get_alert function""" + print('INFO: [TEST_023] get_alert') + self.assertEqual(stats.get_plugin('cpu').get_alert(10, minimum=0, maximum=100, header='total'), 'OK_LOG') + self.assertEqual(stats.get_plugin('cpu').get_alert(65, minimum=0, maximum=100, header='total'), 'CAREFUL_LOG') + self.assertEqual(stats.get_plugin('cpu').get_alert(75, minimum=0, maximum=100, header='total'), 'WARNING_LOG') + self.assertEqual(stats.get_plugin('cpu').get_alert(85, minimum=0, maximum=100, header='total'), 'CRITICAL_LOG') def test_093_auto_unit(self): """Test auto_unit classe"""