forked from ansible/django-ansible-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test module for excess runtime util
- Loading branch information
1 parent
1bdfd0d
commit 173dc9b
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
import time | ||
|
||
from ansible_base.lib.logging.runtime import log_excess_runtime | ||
|
||
logger = logging.getLogger('test_app.tests.lib.logging') | ||
|
||
|
||
def sleep_for(delta): | ||
time.sleep(delta) | ||
|
||
|
||
def test_no_log_needed(caplog): | ||
df = log_excess_runtime(logger)(sleep_for) | ||
df(0) | ||
assert caplog.text == '' | ||
|
||
|
||
def test_debug_log(caplog): | ||
df = log_excess_runtime(logger, debug_cutoff=0.0)(sleep_for) | ||
with caplog.at_level(logging.DEBUG): | ||
df(0) | ||
assert "Running 'sleep_for' took " in caplog.text | ||
|
||
|
||
def test_info_log(caplog): | ||
df = log_excess_runtime(logger, debug_cutoff=0.0, cutoff=0.05)(sleep_for) | ||
with caplog.at_level(logging.INFO): | ||
df(0.1) | ||
assert "Running 'sleep_for' took " in caplog.text | ||
|
||
|
||
def extra_message(log_data): | ||
log_data['foo'] = 'bar' | ||
|
||
|
||
def test_extra_msg_and_data(caplog): | ||
df = log_excess_runtime(logger, debug_cutoff=0.0, add_log_data=True, msg='extra_message log foo={foo}')(extra_message) | ||
with caplog.at_level(logging.DEBUG): | ||
df() | ||
assert "extra_message log foo=bar" in caplog.text |