mirror of
https://github.com/nicolargo/glances.git
synced 2026-09-16 07:13:25 -04:00
WifiPlugin.get_alert chained its three comparisons, so the first undefined level was compared against None, raised TypeError, and the blanket handler dropped the result to DEFAULT. Because 'critical' is tested first, leaving it out silenced the other two: a config defining only wifi_careful produced no alert at all, and even a strong signal came back undecorated instead of OK. GlancesPluginModel.get_alert already guards each level separately, and the sensors plugin has tests pinning that partial thresholds work. This brings wifi in line. `is not None` rather than truthiness because these limits are negative dBm values, where `if threshold` would discard a 0. The TypeError handler is kept for a non-numeric signal level (issue #1373) but narrowed to that case, which is now the only way it can fire.
69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
#!/usr/bin/env python
|
|
#
|
|
# Glances - An eye on your system
|
|
#
|
|
# SPDX-FileCopyrightText: 2026 Nicolas Hennion <nicolas@nicolargo.com>
|
|
#
|
|
# SPDX-License-Identifier: LGPL-3.0-only
|
|
#
|
|
|
|
"""Tests for the Wifi plugin."""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from glances.plugins.wifi import WifiPlugin
|
|
|
|
# Signal quality is reported in dBm, so the thresholds are negative and lower is worse.
|
|
ALL_LEVELS = {'wifi_careful': -65, 'wifi_warning': -75, 'wifi_critical': -85}
|
|
|
|
|
|
def decoration(limits, value):
|
|
plugin = WifiPlugin(args=MagicMock(), config=None)
|
|
plugin._limits = dict(limits)
|
|
return plugin.get_alert(value)
|
|
|
|
|
|
class TestWifiAlertThresholds:
|
|
"""get_alert must honour whichever levels the config defines."""
|
|
|
|
@pytest.mark.parametrize(
|
|
('value', 'expected'),
|
|
[(-50, 'OK'), (-70, 'CAREFUL'), (-80, 'WARNING'), (-90, 'CRITICAL')],
|
|
)
|
|
def test_all_levels_configured(self, value, expected):
|
|
assert decoration(ALL_LEVELS, value) == expected
|
|
|
|
def test_only_careful_still_alerts(self):
|
|
# Chaining the comparisons meant the undefined 'critical' level was compared
|
|
# against None first, raising TypeError and dropping the result to DEFAULT —
|
|
# so a config defining only 'careful' never produced an alert at all.
|
|
assert decoration({'wifi_careful': -65}, -70) == 'CAREFUL'
|
|
|
|
def test_only_warning_still_alerts(self):
|
|
assert decoration({'wifi_warning': -75}, -80) == 'WARNING'
|
|
|
|
def test_only_critical_still_alerts(self):
|
|
assert decoration({'wifi_critical': -85}, -90) == 'CRITICAL'
|
|
|
|
@pytest.mark.parametrize(
|
|
'limits',
|
|
[{'wifi_careful': -65}, {'wifi_warning': -75}, {'wifi_critical': -85}],
|
|
)
|
|
def test_a_good_signal_is_ok_under_any_partial_config(self, limits):
|
|
# A signal above every defined threshold is OK, not undecorated.
|
|
assert decoration(limits, -50) == 'OK'
|
|
|
|
def test_the_most_severe_level_wins(self):
|
|
assert decoration(ALL_LEVELS, -90) == 'CRITICAL'
|
|
assert decoration({'wifi_warning': -75, 'wifi_critical': -85}, -90) == 'CRITICAL'
|
|
|
|
def test_no_threshold_at_all_is_not_decorated(self):
|
|
assert decoration({}, -90) == 'DEFAULT'
|
|
|
|
def test_a_non_numeric_level_is_not_decorated(self):
|
|
# Regression guard for issue #1373: an unparsed signal level is not comparable.
|
|
assert decoration(ALL_LEVELS, None) == 'DEFAULT'
|
|
assert decoration(ALL_LEVELS, 'n/a') == 'DEFAULT'
|