From db988008979334ee39bc1b78f3f6269b53181c8d Mon Sep 17 00:00:00 2001 From: Nishith Vihar Sakinala Date: Tue, 27 Apr 2021 13:37:36 +0530 Subject: [PATCH] Adding signal handler module Signal handler function is added for graceful exiting of the test framework. Fixes: #208 Signed-off-by: Nishith Vihar Sakinala --- core/environ.py | 4 ++++ core/parsing/params_handler.py | 7 ++++++- core/parsing/test_parser.py | 6 ++++++ core/redant_main.py | 5 ++++- core/result_handler.py | 5 +++++ core/signal_handler.py | 20 ++++++++++++++++++++ core/test_list_builder.py | 4 ++++ core/test_runner.py | 5 ++++- tools/signal_handler.py | 21 +++++++++++++++++++++ 9 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 core/signal_handler.py create mode 100644 tools/signal_handler.py diff --git a/core/environ.py b/core/environ.py index d099fec30..8d2e6dc65 100644 --- a/core/environ.py +++ b/core/environ.py @@ -1,6 +1,8 @@ import sys sys.path.insert(1, ".") from common.mixin import RedantMixin +from signal_handler import signal_handler +import signal class environ: """ @@ -13,6 +15,8 @@ def __init__(self, config_hashm : dict, log_path : str, log_level : str): Redant mixin obj to be used for server setup and teardown operations has to be created. """ + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) self.server_details = config_hashm['servers_info'] machine_details = {**self.server_details} self.redant = RedantMixin(machine_details) diff --git a/core/parsing/params_handler.py b/core/parsing/params_handler.py index 582100bd5..d11d26f0b 100644 --- a/core/parsing/params_handler.py +++ b/core/parsing/params_handler.py @@ -3,7 +3,10 @@ which contains APIs for configuration parameter parsing. """ +import sys from parsing.test_parser import Parser +from core.signal_handler import signal_handler +import signal class ParamsHandler: @@ -11,7 +14,9 @@ class ParamsHandler: This class contains all the APIs required for fetching the values of all the configuration parameters. """ - + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) + @classmethod def set_config_hashmap(cls, filepath: str): """ diff --git a/core/parsing/test_parser.py b/core/parsing/test_parser.py index 7e131f650..495c35a36 100644 --- a/core/parsing/test_parser.py +++ b/core/parsing/test_parser.py @@ -2,7 +2,11 @@ This module consists a single class - Parser,which parses the configuration file given the filepath. """ +import sys import yaml +sys.path.insert(1,".") +from core.signal_handler import signal_handler +import signal class Parser(): @@ -11,6 +15,8 @@ class Parser(): configuration file from the filepath. The API is called from the ParamsHandler module. """ + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) @staticmethod def generate_config_hashmap(filepath: str) -> dict: diff --git a/core/redant_main.py b/core/redant_main.py index 7fcc2e8cf..2b07edf7d 100644 --- a/core/redant_main.py +++ b/core/redant_main.py @@ -14,6 +14,8 @@ from test_runner import TestRunner from result_handler import ResultHandler from environ import environ +from signal_handler import signal_handler +import signal def pars_args(): """ @@ -58,7 +60,8 @@ def main(): start = time.time() args = pars_args() - + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) ParamsHandler.set_config_hashmap(args.config_file) # Obtain the client and server dict. diff --git a/core/result_handler.py b/core/result_handler.py index 8d508186d..636da4f68 100644 --- a/core/result_handler.py +++ b/core/result_handler.py @@ -7,9 +7,14 @@ """ from prettytable import PrettyTable from colorama import Fore, Style +from signal_handler import signal_handler +import signal class ResultHandler: + + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) @classmethod def _get_output(cls, test_results: dict, colorify: bool): diff --git a/core/signal_handler.py b/core/signal_handler.py new file mode 100644 index 000000000..0fd38c42d --- /dev/null +++ b/core/signal_handler.py @@ -0,0 +1,20 @@ +""" +This file consists of functions for handling +signals. Signal handling is required for the +graceful exit of the test framework +""" + +import signal + +def signal_handler(signalNumber, frame): + """ + Function for handling signal and raising the + SystemExit call for graceful exit of the test + framework + Args: + signalNumber (int): The signal number of the signal caught + frame: current stack frame, None or stack frame object. + """ + print("Signal Received",signalNumber) + raise SystemExit('Exiting...') + return diff --git a/core/test_list_builder.py b/core/test_list_builder.py index c4cb3e925..55de281b9 100644 --- a/core/test_list_builder.py +++ b/core/test_list_builder.py @@ -13,6 +13,8 @@ import copy import sys from comment_parser.comment_parser import extract_comments +from signal_handler import signal_handler +import signal class TestListBuilder: @@ -27,6 +29,8 @@ class TestListBuilder: tests_component_dir: dict = {"functional": set([]), "performance": set([]), "example": set([])} + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) @classmethod def create_test_dict(cls, path: str, single_tc: bool = False) -> tuple: diff --git a/core/test_runner.py b/core/test_runner.py index 2911cdad1..97fa7450d 100644 --- a/core/test_runner.py +++ b/core/test_runner.py @@ -8,7 +8,8 @@ from threading import Thread, Semaphore from colorama import Fore, Style from runner_thread import RunnerThread - +from signal_handler import signal_handler +import signal class TestRunner: """ @@ -20,6 +21,8 @@ class TestRunner: @classmethod def init(cls, test_run_dict: dict, config_hashmap: dict, base_log_path: str, log_level: str, semaphore_count: int): + signal.signal(signal.SIGINT, signal_handler) + signal.signal(signal.SIGTSTP, signal_handler) cls.config_hashmap = config_hashmap cls.semaphore = Semaphore(semaphore_count) cls.base_log_path = base_log_path diff --git a/tools/signal_handler.py b/tools/signal_handler.py new file mode 100644 index 000000000..d1ba18548 --- /dev/null +++ b/tools/signal_handler.py @@ -0,0 +1,21 @@ +""" +This file contains a class named SignalHandler +which contains different signal handler methods +for different signals. Signal Handling is required +for the graceful exiting of the test framework. +""" + +import signal + +def signal_handler(signalNumber, frame): + """ + Function catches the signal given and raises SystemExit + call to exit the framework gracefully + Args: + signalNumber: The signal number of the signal caught according + to POSIX standard + frame: current stack frame, either None or frame object + """ + print("Signal Received",signalNumber) + raise SystemExit('Exiting...') + return