Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Add simple test output notifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgrady committed May 24, 2021
1 parent 68cc245 commit 63c4628
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lmtest/base/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from lmtest.base.daemon import Daemon, DaemonCommands
from lmtest.base.test_base import LmTest, LmTestFailure, LmTestWarning
from lmtest.notifications.console_notifier import ConsoleNotifier
from lmtest.notifications.log_notifier import LogNotifier


CONTROLLER_PID_FILE = os.path.join(gettempdir(), 'controller.pid')
DEFAULT_SLEEP_TIME = 10
Expand All @@ -25,6 +28,7 @@ def initialize(self):
self._success_count = 0
self._warn_count = 0
self._fail_count = 0
self.notifier = ConsoleNotifier()

# .............................
def add_tests(self, new_tests):
Expand Down Expand Up @@ -86,16 +90,27 @@ def run_test(self, test_to_run):
except LmTestWarning as lm_warn:
notify_message = ' - WARNING: {}'.format(str(lm_warn))
self._warn_count += 1
self.notifier.notifiy_warning(notify_message)
except LmTestFailure as lm_fail:
notify_message = ' - FAILURE: {}'.format(str(lm_fail))
self._fail_count += 1
if notify_message:
# Send notification
print(notify_message)
self.notifier.notify_failure(notify_message)
# if notify_message:
# # Send notification
# print(notify_message)

if test_to_run.get_new_tests():
self.add_tests(test_to_run.get_new_tests())

# .............................
def set_notifier(self, notifier):
"""Set the Controller instances notification method.
Args:
notifier (Notifier): The notifier to use for this instance.
"""
self.notifier = notifier


# .............................................................................
def main():
Expand All @@ -105,6 +120,10 @@ def main():
description='Controls a pool of Makeflow processes',
)

parser.add_argument(
'-l', '--log_file', type=str,
help='File path to log notifications to. Use console if not provided.'
)
parser.add_argument(
'cmd',
choices=[DaemonCommands.START, DaemonCommands.STOP, DaemonCommands.RESTART],
Expand All @@ -117,6 +136,9 @@ def main():

if args.cmd.lower() == DaemonCommands.START:
print('Start')
# Check to see if notifier configuration is provided
if args.log_file is not None:
controller_daemon.set_notifier(LogNotifier(args.log_file))
controller_daemon.start()
elif args.cmd.lower() == DaemonCommands.STOP:
print('Stop')
Expand Down
Binary file added lmtest/notifications/.console_notifier.py.swp
Binary file not shown.
1 change: 1 addition & 0 deletions lmtest/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""lmtest notifications package."""
26 changes: 26 additions & 0 deletions lmtest/notifications/console_notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Module containing methods for producing notifications via standard out."""


# .....................................................................................
class ConsoleNotifier:
"""Emit notifications to the console."""

# ...........................
@staticmethod
def notify_failure(message):
"""Emit a notification of failure.
Args:
message (str): A notification message to emit.
"""
print(message)

# ...........................
@staticmethod
def notify_warning(message):
"""Emit a notification of warning.
Args:
message (str): A notification message to emit.
"""
print(message)
36 changes: 36 additions & 0 deletions lmtest/notifications/log_notifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Module containing methods for producing notifications via a logger."""
import logging
from logging .handlers import RotatingFileHandler
import os


# .....................................................................................
class LogNotifier:
"""Emit notifications to a log."""
# ...........................
def __init__(self, log_filename):
"""Construct a file-based logger.
Args:
log_filename (str): File location to send notifications to.
"""
self.logger = logging.getLogger(os.path.basename(log_filename))
self.logger.addHandler(RotatingFileHandler(log_filename))

# ...........................
def notify_failure(self, message):
"""Emit a notification of failure.
Args:
message (str): A notification message to emit.
"""
self.logger.error(message)

# ...........................
def notify_warning(self, message):
"""Emit a notification of warning.
Args:
message (str): A notification message to emit.
"""
self.logger.warning(message)

0 comments on commit 63c4628

Please sign in to comment.