Skip to content

Commit 33f1a93

Browse files
committed
test: align stateful-cache and time-based unit tests with current lib
The apache-httpd-status, journald-query and logfile unit tests built the cache DB path themselves and missed the per-user `-uid<uid>` suffix that lib.db_sqlite now appends (apache also hardcoded /tmp instead of honouring $TMPDIR), so cache reset/seed no longer hit the file the plugin uses. Derive the temp dir from lib.disk.get_tmpdir() and mirror the uid suffix. The journald-query ack tests seeded a fixed acknowledgement date that aged out of the 30-day retention window; seed relative to now instead. Re-pin the redis-version 8.2 case, which reached EOL on 2026-05-25.
1 parent 45ab9bb commit 33f1a93

4 files changed

Lines changed: 52 additions & 16 deletions

File tree

  • check-plugins
    • apache-httpd-status/unit-test
    • journald-query/unit-test
    • logfile/unit-test
    • redis-version/unit-test

check-plugins/apache-httpd-status/unit-test/run

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,23 @@ import unittest
1414

1515
sys.path.insert(0, '..')
1616

17+
import lib.disk
1718
import lib.lftest
1819
from lib.globals import STATE_OK, STATE_WARN
1920

20-
CACHE_DB = '/tmp/linuxfabrik-monitoring-plugins-apache-httpd-status.db'
21+
22+
def _cache_db():
23+
# Mirror lib.db_sqlite's cache path so the reset below removes the exact
24+
# file the plugin uses: the same temp dir (lib.disk.get_tmpdir) plus the
25+
# per-user `-uid<uid>` suffix that isolates the cache on a shared /tmp.
26+
filename = 'linuxfabrik-monitoring-plugins-apache-httpd-status.db'
27+
if hasattr(os, 'getuid'):
28+
stem, ext = os.path.splitext(filename)
29+
filename = f'{stem}-uid{os.getuid()}{ext}'
30+
return os.path.join(lib.disk.get_tmpdir(), filename)
31+
32+
33+
CACHE_DB = _cache_db()
2134

2235

2336
# The apache-httpd-status plugin talks to Apache's `mod_status` endpoint

check-plugins/journald-query/unit-test/run

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88

99
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md
1010

11+
import datetime
1112
import hashlib
1213
import json
1314
import os
1415
import sqlite3
1516
import sys
16-
import tempfile
1717

1818
sys.path.insert(0, '..')
1919

2020
import unittest
2121

22+
import lib.disk
2223
import lib.lftest
24+
import lib.time
2325
from lib.globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN
2426

2527
TESTS = [
@@ -84,10 +86,13 @@ lib.lftest.attach_tests(TestCheck, TESTS)
8486
def _instance_db_path(payload):
8587
blob = json.dumps(payload, sort_keys=True).encode('utf-8')
8688
instance_hash = hashlib.sha256(blob).hexdigest()[:10]
87-
return os.path.join(
88-
tempfile.gettempdir(),
89-
f'linuxfabrik-monitoring-plugins-journald-query-{instance_hash}.db',
90-
)
89+
filename = f'linuxfabrik-monitoring-plugins-journald-query-{instance_hash}.db'
90+
# Mirror lib.db_sqlite's per-user `-uid<uid>` suffix so the test seeds and
91+
# removes the exact file the plugin reads/writes on a shared /tmp.
92+
if hasattr(os, 'getuid'):
93+
stem, ext = os.path.splitext(filename)
94+
filename = f'{stem}-uid{os.getuid()}{ext}'
95+
return os.path.join(lib.disk.get_tmpdir(), filename)
9196

9297

9398
def _default_instance_payload():
@@ -170,7 +175,7 @@ class TestIcingaCallback(unittest.TestCase):
170175
)
171176
conn.execute(
172177
'INSERT INTO acknowledged_events VALUES (?, ?, ?)',
173-
(event_fingerprint, 1659011388341815, '2026-04-22 12:00:00'),
178+
(event_fingerprint, 1659011388341815, _recent_ack_timestamp()),
174179
)
175180
conn.commit()
176181
finally:
@@ -232,10 +237,22 @@ def _callback_params():
232237
)
233238

234239

235-
def _seed_ack_db(db_path, fingerprints, acknowledged_at='2026-04-22 12:00:00'):
240+
def _recent_ack_timestamp():
241+
"""An acknowledgement timestamp inside the plugin's ACK_RETENTION_DAYS
242+
window, so the seeded ack is not purged before the suppression check. A
243+
hardcoded date would silently age out of the window and flip the test red.
244+
"""
245+
return (lib.time.now(as_type='datetime') - datetime.timedelta(days=1)).strftime(
246+
'%Y-%m-%d %H:%M:%S'
247+
)
248+
249+
250+
def _seed_ack_db(db_path, fingerprints, acknowledged_at=None):
236251
"""Create an ack DB with the given fingerprints pre-populated,
237252
using the exact schema the plugin expects.
238253
"""
254+
if acknowledged_at is None:
255+
acknowledged_at = _recent_ack_timestamp()
239256
conn = sqlite3.connect(db_path)
240257
try:
241258
conn.execute("""

check-plugins/logfile/unit-test/run

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@ import unittest
3131

3232
sys.path.insert(0, '..')
3333

34+
import lib.disk
3435
import lib.lftest
3536
from lib.globals import STATE_CRIT, STATE_OK, STATE_WARN
3637

3738

3839
def _db_glob(fixture_basename):
3940
return os.path.join(
40-
tempfile.gettempdir(),
41+
lib.disk.get_tmpdir(),
4142
f'linuxfabrik-monitoring-plugins-logfile-{fixture_basename}-*.db',
4243
)
4344

4445

4546
def _legacy_db_path(fixture_basename):
4647
return os.path.join(
47-
tempfile.gettempdir(),
48+
lib.disk.get_tmpdir(),
4849
f'linuxfabrik-monitoring-plugins-logfile-{fixture_basename}.db',
4950
)
5051

@@ -232,11 +233,16 @@ def _instance_hash(
232233

233234

234235
def _per_check_db_path(basename, **hash_kwargs):
235-
return os.path.join(
236-
tempfile.gettempdir(),
236+
filename = (
237237
f'linuxfabrik-monitoring-plugins-logfile-{basename}-'
238-
f'{_instance_hash(**hash_kwargs)}.db',
238+
f'{_instance_hash(**hash_kwargs)}.db'
239239
)
240+
# Mirror lib.db_sqlite's per-user `-uid<uid>` suffix so a seeded DB lands at
241+
# the exact path the plugin reads on a shared /tmp.
242+
if hasattr(os, 'getuid'):
243+
stem, ext = os.path.splitext(filename)
244+
filename = f'{stem}-uid{os.getuid()}{ext}'
245+
return os.path.join(lib.disk.get_tmpdir(), filename)
240246

241247

242248
class TestOffsetSurvivesAcrossRuns(unittest.TestCase):

check-plugins/redis-version/unit-test/run

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ TESTS = [
7676
'assert-in': ['Redis v8.0.6', 'EOL 2026-02-11', '[WARNING]'],
7777
},
7878
{
79-
'id': 'ok-redis-8-2-full-support-ended',
79+
'id': 'warn-redis-8-2-eol',
8080
'test': 'stdout/redis-server-v8.2,,0',
81-
'assert-retc': STATE_OK,
82-
'assert-in': ['Redis v8.2.5', 'full support ended on 2025-11-18'],
81+
'assert-retc': STATE_WARN,
82+
'assert-in': ['Redis v8.2.5', 'EOL 2026-05-25', '[WARNING]'],
8383
},
8484
{
8585
'id': 'ok-redis-8-4-full-support-ended',

0 commit comments

Comments
 (0)