Skip to content

Commit

Permalink
Adding signal handler module
Browse files Browse the repository at this point in the history
Signal handler function is added for graceful exiting of
the test framework.

Fixes: gluster#208
Signed-off-by: Nishith Vihar Sakinala <nsakinal@redhat.com>
  • Loading branch information
nishith-vihar committed Apr 27, 2021
1 parent ca587b0 commit db98800
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 3 deletions.
4 changes: 4 additions & 0 deletions core/environ.py
Original file line number Diff line number Diff line change
@@ -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:
"""
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion core/parsing/params_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
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:
"""
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):
"""
Expand Down
6 changes: 6 additions & 0 deletions core/parsing/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion core/redant_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions core/result_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
20 changes: 20 additions & 0 deletions core/signal_handler.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions core/test_list_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion core/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tools/signal_handler.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit db98800

Please sign in to comment.