mirror of
https://github.com/nicolargo/glances.git
synced 2026-08-02 16:27:01 -04:00
149 lines
4.0 KiB
Python
149 lines
4.0 KiB
Python
#
|
|
# Glances - An eye on your system
|
|
#
|
|
# SPDX-FileCopyrightText: 2024 Nicolas Hennion <nicolas@nicolargo.com>
|
|
#
|
|
# SPDX-License-Identifier: LGPL-3.0-only
|
|
#
|
|
|
|
"""Glances unitary tests suite fixtures.
|
|
|
|
This module provides pytest fixtures for testing Glances plugins and components.
|
|
|
|
For WebUI tests, the following are required:
|
|
- chromedriver command line (example on Ubuntu system):
|
|
$ sudo apt install chromium-chromedriver
|
|
- The chromedriver command line should be in your path (/usr/bin)
|
|
"""
|
|
|
|
import logging
|
|
import shlex
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
from glances.config_v5 import GlancesConfigV5
|
|
from glances.main import GlancesMain
|
|
from glances.stats import GlancesStats
|
|
from glances.stats_store_v5 import StatsStoreV5
|
|
|
|
# Optional imports for WebUI testing
|
|
try:
|
|
from selenium import webdriver
|
|
from selenium.webdriver import ChromeOptions
|
|
from selenium.webdriver.chrome.service import Service as ChromeService
|
|
from webdriver_manager.chrome import ChromeDriverManager
|
|
|
|
SELENIUM_AVAILABLE = True
|
|
except ImportError:
|
|
SELENIUM_AVAILABLE = False
|
|
|
|
SERVER_PORT = 61234
|
|
URL = f"http://localhost:{SERVER_PORT}"
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def logger():
|
|
return logging.getLogger(__name__)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def glances_stats():
|
|
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()
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def glances_stats_no_history():
|
|
testargs = ["glances", "-C", "./conf/glances.conf"]
|
|
with patch('sys.argv', testargs):
|
|
core = GlancesMain()
|
|
args = core.get_args()
|
|
args.time = 1
|
|
args.cached_time = 1
|
|
args.disable_history = True
|
|
stats = GlancesStats(config=core.get_config(), args=args)
|
|
yield stats
|
|
stats.end()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def glances_webserver():
|
|
cmdline = sys.executable
|
|
cmdline += f" -m glances -B 0.0.0.0 -w --browser -p {SERVER_PORT} -C ./conf/glances.conf"
|
|
args = shlex.split(cmdline)
|
|
pid = subprocess.Popen(args)
|
|
time.sleep(3)
|
|
yield pid
|
|
pid.terminate()
|
|
time.sleep(1)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def web_browser():
|
|
"""Init Chrome browser for WebUI testing.
|
|
|
|
Requires selenium and webdriver-manager packages.
|
|
"""
|
|
if not SELENIUM_AVAILABLE:
|
|
pytest.skip("selenium not installed - skipping WebUI tests")
|
|
|
|
opt = ChromeOptions()
|
|
opt.add_argument("--headless")
|
|
opt.add_argument("--start-maximized")
|
|
srv = ChromeService(ChromeDriverManager().install())
|
|
driver = webdriver.Chrome(options=opt, service=srv)
|
|
|
|
# Yield the WebDriver instance
|
|
driver.implicitly_wait(10)
|
|
yield driver
|
|
|
|
# Close the WebDriver instance
|
|
driver.quit()
|
|
|
|
|
|
@pytest.fixture
|
|
def store_with():
|
|
"""Factory fixture — returns a callable producing a fresh StatsStoreV5.
|
|
|
|
Shared across v5 plugin model tests (see
|
|
tests/test_plugin_base_v5_stop_and_threshold_field.py, the origin of
|
|
this fixture).
|
|
"""
|
|
|
|
def _factory() -> StatsStoreV5:
|
|
return StatsStoreV5()
|
|
|
|
return _factory
|
|
|
|
|
|
@pytest.fixture
|
|
def config_with(tmp_path: Path, monkeypatch):
|
|
"""Factory fixture — returns a callable that builds a real GlancesConfigV5
|
|
from a ``{section: {key: value}}`` dict, backed by a temp glances.conf.
|
|
"""
|
|
monkeypatch.setattr(GlancesConfigV5, "SYSTEM_CONFIG_PATH", tmp_path / "etc" / "glances.conf")
|
|
xdg = tmp_path / "xdg"
|
|
cfg_dir = xdg / "glances"
|
|
cfg_dir.mkdir(parents=True)
|
|
monkeypatch.setenv("XDG_CONFIG_HOME", str(xdg))
|
|
|
|
def _factory(sections: dict[str, dict[str, str]]) -> GlancesConfigV5:
|
|
body = ""
|
|
for section, options in sections.items():
|
|
body += f"[{section}]\n"
|
|
for key, value in options.items():
|
|
body += f"{key}={value}\n"
|
|
(cfg_dir / "glances.conf").write_text(body)
|
|
return GlancesConfigV5()
|
|
|
|
return _factory
|