Skip to content

Commit 355c178

Browse files
committed
feat(tests): add E2E tests for SNMP provider to validate integration
1 parent fb7f3c4 commit 355c178

File tree

4 files changed

+380
-25
lines changed

4 files changed

+380
-25
lines changed

keep/entrypoint.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ set -x
99
# Get the directory of the current script
1010
SCRIPT_DIR=$(dirname "$0")
1111

12-
pip install pyasn1==0.4.8 pyasn1-modules==0.2.8 pysnmp-lextudio==5.0.34 --no-cache-dir
13-
1412
python "$SCRIPT_DIR/server_jobs_bg.py" &
1513

1614
# Build the providers cache

keep/providers/snmp_provider/snmp_provider.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import asyncio
66
import json
7-
import logging
87
import socket
98
import threading
109
from datetime import datetime
@@ -128,6 +127,7 @@ def start_consume(self):
128127
daemon=True
129128
)
130129
self.trap_thread.start()
130+
self.logger.info(f"SNMP trap receiver thread started successfully on {self.authentication_config.listen_address}:{self.authentication_config.port}")
131131

132132
return {"status": "SNMP trap receiver started"}
133133

@@ -152,7 +152,7 @@ def _start_trap_receiver(self):
152152
# Configure community string for SNMP v1 and v2c
153153
config.addV1System(
154154
self.snmp_engine,
155-
'my-area',
155+
'keep-snmp-security-domain',
156156
self.authentication_config.community
157157
)
158158

@@ -406,14 +406,38 @@ def get_alert_schema() -> Dict[str, Any]:
406406
}
407407

408408
def dispose(self):
409-
"""Clean up resources."""
410-
if self.running:
411-
self.logger.info("Stopping SNMP trap receiver")
412-
self.running = False
409+
"""Clean up resources and release all ports used by the SNMP trap receiver."""
410+
if not self.running:
411+
return
413412

414-
if self.snmp_engine:
415-
self.snmp_engine.transportDispatcher.jobFinished(1)
413+
self.logger.info("Stopping SNMP trap receiver")
414+
self.running = False
415+
416+
if self.snmp_engine:
417+
try:
418+
transport_dispatcher = self.snmp_engine.transportDispatcher
419+
420+
transport_dispatcher.jobFinished(1)
421+
422+
transport_dispatcher.closeDispatcher()
423+
424+
self.logger.info(f"SNMP engine transport dispatcher stopped, port {self.authentication_config.port} released")
425+
426+
except Exception as e:
427+
self.logger.error(f"Error during SNMP engine cleanup: {e}")
428+
finally:
416429
self.snmp_engine = None
430+
431+
432+
if self.trap_thread and self.trap_thread.is_alive():
433+
try:
434+
self.trap_thread.join(timeout=5.0)
435+
if self.trap_thread.is_alive():
436+
self.logger.warning("SNMP trap thread did not stop gracefully within timeout")
437+
except Exception as e:
438+
self.logger.error(f"Error joining SNMP trap thread: {e}")
439+
finally:
440+
self.trap_thread = None
417441

418442
@property
419443
def is_consumer(self) -> bool:
@@ -422,4 +446,4 @@ def is_consumer(self) -> bool:
422446

423447
def status(self) -> bool:
424448
"""Check if the SNMP trap receiver is running."""
425-
return self.running
449+
return self.running

poetry.lock

Lines changed: 75 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)