Commit b4f70c4b authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

add unit tests

parent 1c2a110f
......@@ -68,6 +68,8 @@ class OperationalLogger:
if from_address is None:
from_address = self.DEFAULT_FROM_ADDRESS
logging.getLogger(logger_name).handlers = []
logging.getLogger('_sub').handlers = []
logging.basicConfig(filename=log_filename, format=log_format_file, datefmt=date_format_file, level=logging.DEBUG)
formatter = logging.Formatter(fmt=log_format_console, datefmt=date_format_console)
......@@ -159,7 +161,7 @@ def setup(config_file=None, logger_name=None, log_filename=None, log_root_dir=No
config_file: filename to load for logging config as JSON using these keywords, overwritten by args
logger_name: name to pass to logger
log_root_dir, log_filename: log will be created as log_root_dir/log/YYYY-MM-DD/LOG_FILENAME.log
log_root_dir, log_filename: log will be created as log_root_dir/LOG_FILENAME.log
log_root_dir defaults to '.'
log_filename defaults to script name
from_address: email 'From' address for notifications
......@@ -209,12 +211,13 @@ def setup(config_file=None, logger_name=None, log_filename=None, log_root_dir=No
if config['console_level'] is None:
config['console_level'] = logging.INFO
logger = OperationalLogger(
log_filename=os.path.join(log_directory, log_filename), logger_name=logger_name,
from_address=from_address, notify=notify, log_level=log_level, console_level=console_level,
log_format_file=log_format_file, date_format_file=date_format_file,
log_format_console=log_format_console, date_format_console=date_format_console
)
# logger = OperationalLogger(
# log_filename=os.path.join(log_directory, log_filename), logger_name=logger_name,
# from_address=from_address, notify=notify, log_level=log_level, console_level=console_level,
# log_format_file=log_format_file, date_format_file=date_format_file,
# log_format_console=log_format_console, date_format_console=date_format_console
# )
logger = OperationalLogger(**config)
logging.info('Logging output from: {}'.format(' '.join(sys.argv)))
logging.info('Log writing to: %s', os.path.join(log_directory, log_filename))
logging.info('Process id {}, parent process id {}'.format(os.getpid(), os.getppid()))
......
#!/usr/bin/env python
"""Unit tests for OperationalLogging."""
import unittest
from nose.tools import raises, eq_
import tempfile
import os
import os.path
import logging
import shutil
import OperationalLogger as OpLog
import OperationalMail as OpMail
class TestAll(unittest.TestCase):
ab_diff = "1c1\n< alpha\n---\n> beta\n"
def setUp(self):
print '\n\n'
self.temp_dir = tempfile.mkdtemp()
self.log_dir = tempfile.mkdtemp()
self.file_a = os.path.join(self.temp_dir, 'a')
self.file_b = os.path.join(self.temp_dir, 'b')
with open(self.file_a, 'w') as f:
f.write('alpha\n')
with open(self.file_b, 'w') as f:
f.write('beta\n')
OpLog.setup(log_root_dir=self.log_dir)
def tearDown(self):
shutil.rmtree(self.temp_dir)
print '\n\n'
def test_mail(self):
OpMail.send_status(
subject="nosetest",
message="This is a message.",
to_list=["tcappallo@veriskclimate.com", "tcappallo@veriskclimate.com"],
from_address="nosetest@aer.com",
text_attachment="One line.\n<b>Two lines.</b>",
)
def test_log_shell_output(self):
ret = OpLog.log_shell(['diff', self.file_a, self.file_b], return_output=True)
eq_(ret, self.ab_diff)
ret = OpLog.log_shell(['diff', self.file_a, self.file_a], return_output=True)
assert (ret == "")
@raises(RuntimeError)
def test_log_shell_raise_err(self):
OpLog.log_shell(['diff', self.file_a, self.file_b], raise_err=True)
def test_log_shell_exit_code(self):
ret = OpLog.log_shell(['diff', self.file_a, self.file_b])
assert ret == 1
ret = OpLog.log_shell(['diff', self.file_a, self.file_a])
assert ret == 0
if __name__ == '__main__':
unittest.main()
#__all__ = ['OperationalLogger', 'OperationalMail']
#import OperationalLogger as OpLog
#import OperationalMail as OpMail
#import OperationalLogger as OpLog
#import OperationalMail as OpMail
[nosetests]
verbosity=3
detailed-errors=1
#with-profile=1
#debug=nose.loader
#pdb-failures=1
nocapture=1
logging-clear-handlers=1
with-coverage=1
cover-package=lib
#cover-package=OperationalLogging
......@@ -9,13 +9,21 @@ INSTALL_REQUIRES = [
'html2text',
]
TESTS_REQUIRE = [
'nose-cov',
]
setup(
name='OperationalLogging',
version='0.1.1',
version='0.4.1',
description='Logging and email notification for operational projects',
author='Verisk Climate',
author_email='tcappallo@veriskclimate.com',
install_requires=INSTALL_REQUIRES,
tests_require=TESTS_REQUIRE,
test_suite='nose.collector',
package_dir={'': 'lib'},
py_modules=['OperationalLogging', 'OperationalMail'],
#package_dir={'OperationalLogging': 'lib'},
#packages=['OperationalLogging'] #, 'OperationalLogging.Tests'],
py_modules=['OperationalLogger', 'OperationalMail', 'TestAll'],
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment