mirror of https://github.com/nicolargo/glances.git
Unittest now take into account the default glances.conf file
This commit is contained in:
parent
d9abfca7fb
commit
c0dad78189
|
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from fastapi.openapi.utils import get_openapi
|
from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
|
|
@ -8,7 +9,9 @@ from glances.main import GlancesMain
|
||||||
from glances.outputs.glances_restful_api import GlancesRestfulApi
|
from glances.outputs.glances_restful_api import GlancesRestfulApi
|
||||||
|
|
||||||
# Init Glances core
|
# 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_config = core.get_config()
|
||||||
test_args = core.get_args()
|
test_args = core.get_args()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,10 +21,10 @@ plugin_dependencies_tree = {
|
||||||
class GlancesAPI:
|
class GlancesAPI:
|
||||||
ttl = 2.0 # Default cache TTL in seconds
|
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
|
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.args = args if args is not None else core.get_args()
|
||||||
self.config = config if config is not None else core.get_config()
|
self.config = config if config is not None else core.get_config()
|
||||||
self._stats = GlancesStats(config=self.config, args=self.args)
|
self._stats = GlancesStats(config=self.config, args=self.args)
|
||||||
|
|
|
||||||
|
|
@ -103,14 +103,14 @@ Examples of use:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, args_begin_at=1):
|
def __init__(self):
|
||||||
"""Manage the command line arguments."""
|
"""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."""
|
"""Main method to init Glances."""
|
||||||
# Read the command line arguments or parse the one given in parameter (parser)
|
# 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
|
# Load the configuration file, if it exists
|
||||||
# This function should be called after the parse_args
|
# This function should be called after the parse_args
|
||||||
|
|
@ -846,11 +846,9 @@ Examples of use:
|
||||||
if args.disable_history:
|
if args.disable_history:
|
||||||
logger.info("Stats history is disabled")
|
logger.info("Stats history is disabled")
|
||||||
|
|
||||||
def parse_args(self, args_begin_at):
|
def parse_args(self):
|
||||||
"""Parse command line arguments.
|
"""Parse command line arguments."""
|
||||||
Glances args start at position args_begin_at.
|
return self.init_args().parse_args(sys.argv[1:])
|
||||||
"""
|
|
||||||
return self.init_args().parse_args(sys.argv[args_begin_at:])
|
|
||||||
|
|
||||||
def check_mode_compatibility(self):
|
def check_mode_compatibility(self):
|
||||||
"""Check mode compatibility"""
|
"""Check mode compatibility"""
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import os
|
||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
|
|
@ -41,7 +42,9 @@ def logger():
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="session")
|
||||||
def glances_stats():
|
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())
|
stats = GlancesStats(config=core.get_config(), args=core.get_args())
|
||||||
yield stats
|
yield stats
|
||||||
stats.end()
|
stats.end()
|
||||||
|
|
@ -49,7 +52,9 @@ def glances_stats():
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
@pytest.fixture(scope="module")
|
||||||
def glances_stats_no_history():
|
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 = core.get_args()
|
||||||
args.time = 1
|
args.time = 1
|
||||||
args.cached_time = 1
|
args.cached_time = 1
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@
|
||||||
|
|
||||||
"""Glances API unitary tests suite."""
|
"""Glances API unitary tests suite."""
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
from glances import __version__, api
|
from glances import __version__, api
|
||||||
|
|
||||||
# Global variables
|
# Global variables
|
||||||
|
|
@ -17,7 +19,10 @@ from glances import __version__, api
|
||||||
# Init Glances API
|
# Init Glances API
|
||||||
# test_config = core.get_config()
|
# test_config = core.get_config()
|
||||||
# test_args = core.get_args()
|
# 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
|
# Pytest functions to test the Glances API version
|
||||||
|
|
@ -82,3 +87,4 @@ def test_glances_api_plugin_process():
|
||||||
|
|
||||||
def test_glances_api_limits():
|
def test_glances_api_limits():
|
||||||
assert isinstance(gl.cpu.limits, dict)
|
assert isinstance(gl.cpu.limits, dict)
|
||||||
|
assert isinstance(gl.cpu.limits, dict)
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import multiprocessing
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
# Ugly hack waiting for Python 3.10 deprecation
|
# Ugly hack waiting for Python 3.10 deprecation
|
||||||
try:
|
try:
|
||||||
|
|
@ -52,12 +53,12 @@ else:
|
||||||
# =================
|
# =================
|
||||||
|
|
||||||
# Init Glances core
|
# 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_config = core.get_config()
|
||||||
test_args = core.get_args()
|
test_args = core.get_args()
|
||||||
|
|
||||||
test_args.conf_file = './conf/glances.conf'
|
|
||||||
|
|
||||||
# Init Glances stats
|
# Init Glances stats
|
||||||
stats = GlancesStats(config=test_config, args=test_args)
|
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('quicklook'), ['quicklook', 'fs', 'core', 'load', 'alert'])
|
||||||
self.assertEqual(get_plugin_dependencies('vms'), ['vms', 'processcount', 'alert'])
|
self.assertEqual(get_plugin_dependencies('vms'), ['vms', 'processcount', 'alert'])
|
||||||
|
|
||||||
# def test_023_get_alert(self):
|
def test_023_get_alert(self):
|
||||||
# """Test get_alert function"""
|
"""Test get_alert function"""
|
||||||
# print('INFO: [TEST_023] get_alert')
|
print('INFO: [TEST_023] get_alert')
|
||||||
# self.assertEqual(stats.get_plugin('cpu').get_alert(10, minimum=0, maximum=100, header='total'),
|
self.assertEqual(stats.get_plugin('cpu').get_alert(10, minimum=0, maximum=100, header='total'), 'OK_LOG')
|
||||||
# '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(65, minimum=0, maximum=100, header='total'),
|
self.assertEqual(stats.get_plugin('cpu').get_alert(75, minimum=0, maximum=100, header='total'), 'WARNING_LOG')
|
||||||
# 'CAREFUL_LOG')
|
self.assertEqual(stats.get_plugin('cpu').get_alert(85, minimum=0, maximum=100, header='total'), 'CRITICAL_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):
|
def test_093_auto_unit(self):
|
||||||
"""Test auto_unit classe"""
|
"""Test auto_unit classe"""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue