Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Trevor Cappallo
OperationalLogging
Commits
b4f70c4b
Commit
b4f70c4b
authored
Aug 17, 2015
by
Trevor Cappallo
Browse files
add unit tests
parent
1c2a110f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
100 additions
and
9 deletions
+100
-9
lib/OperationalLogger.py
lib/OperationalLogger.py
+10
-7
lib/TestAll.py
lib/TestAll.py
+64
-0
lib/__init__.py
lib/__init__.py
+5
-0
setup.cfg
setup.cfg
+11
-0
setup.py
setup.py
+10
-2
No files found.
lib/OperationalLogg
ing
.py
→
lib/OperationalLogg
er
.py
View file @
b4f70c4b
...
...
@@ -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
()))
...
...
lib/TestAll.py
0 → 100644
View file @
b4f70c4b
#!/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
()
lib/__init__.py
0 → 100644
View file @
b4f70c4b
#__all__ = ['OperationalLogger', 'OperationalMail']
#import OperationalLogger as OpLog
#import OperationalMail as OpMail
#import OperationalLogger as OpLog
#import OperationalMail as OpMail
setup.cfg
0 → 100644
View file @
b4f70c4b
[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
setup.py
View file @
b4f70c4b
...
...
@@ -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'
],
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment