Skip to content

Commit ca1d252

Browse files
committed
nimble_volumes: Add unit tests
Jira: CMK-37374 Change-Id: Id6bff98e3182a66d7b5b3448688b51ea64ecfec3
1 parent 5fac528 commit ca1d252

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

cmk/plugins/nimble/agent_based/nimble_volumes.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# mypy: disable-error-code="explicit-any"
77

88
import time
9-
from collections.abc import Mapping
9+
from collections.abc import Mapping, MutableMapping
1010
from typing import Any
1111

1212
from cmk.agent_based.v2 import (
@@ -32,6 +32,16 @@ def discover_nimble_volumes(section: StringTable) -> DiscoveryResult:
3232

3333

3434
def check_nimble_volumes(item: str, params: Mapping[str, Any], section: StringTable) -> CheckResult:
35+
yield from _check_nimble_volumes(item, params, section, time.time(), get_value_store())
36+
37+
38+
def _check_nimble_volumes(
39+
item: str,
40+
params: Mapping[str, Any],
41+
section: StringTable,
42+
now: float,
43+
value_store: MutableMapping[str, object],
44+
) -> CheckResult:
3545
for line in section:
3646
if line[1] == item:
3747
if line[4] == "0":
@@ -40,11 +50,11 @@ def check_nimble_volumes(item: str, params: Mapping[str, Any], section: StringTa
4050
total = int(line[2])
4151
free = total - int(line[3])
4252
yield from df_check_filesystem_list(
43-
get_value_store(),
53+
value_store,
4454
item,
4555
params,
4656
[(item, total, free, 0)],
47-
this_time=time.time(),
57+
this_time=now,
4858
)
4959

5060

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# Copyright (C) 2026 Checkmk GmbH - License: GNU General Public License v2
3+
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
4+
# conditions defined in the file COPYING, which is part of this source code package.
5+
6+
import pytest
7+
8+
from cmk.agent_based.v2 import Metric, Result, Service, State
9+
from cmk.plugins.lib.df import FILESYSTEM_DEFAULT_PARAMS
10+
from cmk.plugins.nimble.agent_based.nimble_volumes import (
11+
_check_nimble_volumes,
12+
discover_nimble_volumes,
13+
parse_nimble_volumes,
14+
)
15+
16+
STRING_TABLE = [
17+
["1", "vol-ok", "1073741824", "536870912", "1"],
18+
["", "vol-empty-size", "", "", ""],
19+
]
20+
21+
22+
def test_discovery_skips_volumes_without_size_data() -> None:
23+
section = parse_nimble_volumes(STRING_TABLE)
24+
assert list(discover_nimble_volumes(section)) == [Service(item="vol-ok")]
25+
26+
27+
def test_check_ok_volume() -> None:
28+
section = parse_nimble_volumes(STRING_TABLE)
29+
results = list(
30+
_check_nimble_volumes(
31+
"vol-ok", FILESYSTEM_DEFAULT_PARAMS, section, 60.0, {"vol-ok.delta": (0, 0)}
32+
)
33+
)
34+
assert Metric("fs_size", 1073741824.0, boundaries=(0.0, None)) in results
35+
assert Result(state=State.OK, summary="Used: 50.00% - 512 TiB of 1.00 PiB") in results
36+
37+
38+
@pytest.mark.xfail(strict=True, reason="CMK-37374")
39+
def test_check_does_not_crash_on_empty_size_values() -> None:
40+
section = parse_nimble_volumes(STRING_TABLE)
41+
assert not list(
42+
_check_nimble_volumes("vol-empty-size", FILESYSTEM_DEFAULT_PARAMS, section, 60.0, {})
43+
)

0 commit comments

Comments
 (0)