Skip to content

Commit ec68013

Browse files
committed
bug fix + utest updates
1 parent b089cff commit ec68013

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

nodescraper/plugins/inband/storage/storage_analyzer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ def analyze_data(
100100
passing_devices = []
101101
failing_devices = []
102102
for device_name, device_data in data.storage_data.items():
103-
if args.check_devices and not self._matches_device_filter(
104-
device_name, args.check_devices, args.regex_match
105-
):
106-
continue
107-
elif args.ignore_devices and self._matches_device_filter(
108-
device_name, args.ignore_devices, args.regex_match
109-
):
110-
continue
103+
if args.check_devices:
104+
if not self._matches_device_filter(
105+
device_name, args.check_devices, args.regex_match
106+
):
107+
continue
108+
elif args.ignore_devices:
109+
if self._matches_device_filter(device_name, args.ignore_devices, args.regex_match):
110+
continue
111111

112112
condition = False
113113
if args.min_required_free_space_abs:

test/unit/plugin/test_storage_analyzer.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,63 @@ def model_obj():
5151
)
5252

5353

54+
@pytest.fixture
55+
def multiple_dev():
56+
return StorageDataModel(
57+
storage_data={
58+
"dev1": DeviceStorageData(total=100, free=90, used=10, percent=10),
59+
"dev2": DeviceStorageData(total=100, free=5, used=95, percent=95),
60+
}
61+
)
62+
63+
5464
@pytest.fixture
5565
def analyzer(system_info):
5666
return StorageAnalyzer(system_info=system_info)
5767

5868

69+
def test_filter_exact_match(analyzer):
70+
assert analyzer._matches_device_filter("foo", ["foo", "bar"], regex_match=False)
71+
assert not analyzer._matches_device_filter("baz", ["foo", "bar"], regex_match=False)
72+
73+
74+
def test_filter_regex_match(analyzer):
75+
assert analyzer._matches_device_filter("disk0", [r"disk\d"], regex_match=True)
76+
assert not analyzer._matches_device_filter("diskA", [r"disk\d"], regex_match=True)
77+
78+
79+
def test_filter_invalid_regex(analyzer, monkeypatch):
80+
calls = []
81+
monkeypatch.setattr(analyzer, "_log_event", lambda **kw: calls.append(kw))
82+
assert not analyzer._matches_device_filter("somestring", ["[invalid"], regex_match=True)
83+
assert len(calls) == 1
84+
event = calls[0]
85+
assert event["category"] == EventCategory.STORAGE
86+
assert event["priority"] == EventPriority.ERROR
87+
assert "Invalid regex pattern" in event["description"]
88+
89+
90+
def test_check_devices_only(analyzer, multiple_dev):
91+
args = StorageAnalyzerArgs(min_required_free_space_prct=50, check_devices=["dev1"])
92+
res = analyzer.analyze_data(multiple_dev, args)
93+
assert res.status == ExecutionStatus.OK
94+
95+
96+
def test_ignore_devices(analyzer, multiple_dev):
97+
args = StorageAnalyzerArgs(min_required_free_space_prct=50, ignore_devices=["dev2"])
98+
res = analyzer.analyze_data(multiple_dev, args)
99+
assert res.status == ExecutionStatus.OK
100+
101+
102+
def test_check_overrides_ignore(analyzer, multiple_dev):
103+
args = StorageAnalyzerArgs(
104+
min_required_free_space_prct=50, check_devices=["dev2"], ignore_devices=["dev2", "dev1"]
105+
)
106+
res = analyzer.analyze_data(multiple_dev, args)
107+
assert res.status == ExecutionStatus.ERROR
108+
assert any(e.category == EventCategory.STORAGE.value for e in res.events)
109+
110+
59111
def test_only_absolute_threshold_fails(analyzer, model_obj):
60112
args = StorageAnalyzerArgs(min_required_free_space_abs="800GB")
61113
result = analyzer.analyze_data(model_obj, args)

0 commit comments

Comments
 (0)