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
|
||||
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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"""
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"""
|
||||
|
|
|
|||
Loading…
Reference in New Issue