Skip to content

Commit

Permalink
[fix] Fixed backward compatibility issue with custom health status la…
Browse files Browse the repository at this point in the history
…bels

When using customized OPENWISP_MONITORING_HEALTH_STATUS_LABELS,
forgetting to add the new deactivated status label after upgrading
would cause a crash of the application during startup.

This fix provides a set of valid defaults which is always present
and gets overridden with the custom labels.
  • Loading branch information
nemesifier committed Nov 26, 2024
1 parent cbcbae5 commit 32ffd04
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
30 changes: 11 additions & 19 deletions openwisp_monitoring/device/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,19 @@ def get_critical_device_metrics():


def get_health_status_labels():
labels = get_settings_value(
default_labels = {
'unknown': 'unknown',
'ok': 'ok',
'problem': 'problem',
'critical': 'critical',
'deactivated': 'deactivated',
}
labels = default_labels.copy()
configured_labels = get_settings_value(
'HEALTH_STATUS_LABELS',
{
'unknown': 'unknown',
'ok': 'ok',
'problem': 'problem',
'critical': 'critical',
'deactivated': 'deactivated',
},
default_labels,
)
try:
assert 'unknown' in labels
assert 'ok' in labels
assert 'problem' in labels
assert 'critical' in labels
assert 'deactivated' in labels
except AssertionError as e: # pragma: no cover
raise ImproperlyConfigured(
'OPENWISP_MONITORING_HEALTH_STATUS_LABELS must contain the following '
'keys: unknown, ok, problem, critical'
) from e
labels.update(configured_labels)
return labels


Expand Down
9 changes: 0 additions & 9 deletions openwisp_monitoring/device/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,3 @@ def test_invalid_critical_device_metrics_setting(self):
from ..settings import get_critical_device_metrics

get_critical_device_metrics()

@patch(
'django.conf.settings.OPENWISP_MONITORING_HEALTH_STATUS_LABELS', {}, create=True
)
def test_invalid_health_status_setting(self):
with self.assertRaises(ImproperlyConfigured):
from ..settings import get_health_status_labels

get_health_status_labels()

0 comments on commit 32ffd04

Please sign in to comment.