From ed1a7c435d0d0169e1d9d61fb765b5d7dc95812c Mon Sep 17 00:00:00 2001 From: Alexander Indenbaum Date: Mon, 21 Oct 2024 04:42:36 +0000 Subject: [PATCH] test monitor client abort Signed-off-by: Alexander Indenbaum --- control/server.py | 2 +- tests/test_server.py | 50 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/control/server.py b/control/server.py index 4a2f92a0..9c832b11 100644 --- a/control/server.py +++ b/control/server.py @@ -115,7 +115,7 @@ def __enter__(self): def __exit__(self, exc_type, exc_value, traceback): """Cleans up SPDK and server instances.""" - if exc_type is not None and exc_type is not SystemExit: + if exc_type is not None: self.logger.exception("GatewayServer exception occurred:") else: self.logger.info("GatewayServer is terminating gracefully...") diff --git a/tests/test_server.py b/tests/test_server.py index 2cd63ed5..fdfd209c 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -2,23 +2,40 @@ import pytest import time import re +import signal import os import unittest from control.server import GatewayServer class TestServer(unittest.TestCase): + # Location of core files in test env. + core_dir="/tmp/coredump" + @pytest.fixture(autouse=True) def _config(self, config): self.config = config def validate_exception(self, e): - pattern = r'Gateway subprocess terminated pid=(\d+) exit_code=(\d+)' + pattern = r'Gateway subprocess terminated pid=(\d+) exit_code=(-?\d+)' m = re.match(pattern, e.code) assert(m) pid = int(m.group(1)) code = int(m.group(2)) assert(pid > 0) - assert(code == 1) + assert(code) + + def remove_core_files(self, directory_path): + # List all files starting with "core." in the core directory + files = [ + f for f in os.listdir(directory_path) + if os.path.isfile(os.path.join(directory_path, f)) and f.startswith("core.") + ] + + # Remove each matching file + for f in files: + file_path = os.path.join(directory_path, f) + os.remove(file_path) + print(f"Removed: {file_path}") def assert_no_core_files(self, directory_path): assert(os.path.exists(directory_path) and os.path.isdir(directory_path)) @@ -46,7 +63,34 @@ def test_no_coredumps_on_gracefull_shutdown(self): time.sleep(10) # exited context, sub processes should terminate gracefully time.sleep(10) # let it dump - self.assert_no_core_files("/tmp/coredump") + self.assert_no_core_files(self.core_dir) + + def test_monc_exit(self): + """Tests monitor client sub process abort.""" + config_monc_abort = copy.deepcopy(self.config) + signals = [ signal.SIGABRT, signal.SIGTERM, signal.SIGKILL ] + + for sig in signals: + with self.assertRaises(SystemExit) as cm: + with GatewayServer(config_monc_abort) as gateway: + gateway.set_group_id(0) + gateway.serve() + + # Give the gateway some time to start + time.sleep(2) + + # Send SIGABRT (abort signal) to the monitor client process + assert(gateway.monitor_client_process) + gateway.monitor_client_process.send_signal(signal.SIGABRT) + + # Block on running keep alive ping + gateway.keep_alive() + + # Assert error exit code + self.validate_exception(cm.exception) + + # Clean up monc core + self.remove_core_files(self.core_dir) def test_spdk_multi_gateway_exception(self): """Tests spdk sub process exiting with error, in multi gateway configuration."""