-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ed17a1
commit afb6496
Showing
3 changed files
with
273 additions
and
44 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
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
192 changes: 192 additions & 0 deletions
192
roles/openshift_health_checker/test/search_journalctl_test.py
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,192 @@ | ||
import pytest | ||
import search_journalctl | ||
from time import time | ||
|
||
|
||
SAVED_TIMESTAMPS = list() | ||
|
||
|
||
def canned_search_journalctl(get_log_input=None): | ||
"""Create a Curator check object with canned get_log_input method""" | ||
module = search_journalctl # fails if a module is actually invoked | ||
if get_log_input: | ||
module.get_log_input = get_log_input | ||
return module | ||
|
||
|
||
def get_timestamp_ms(modifier=0, saveto=None): | ||
t = str(time() + modifier).split(".")[0] + "000000" | ||
if type(saveto) is list: | ||
saveto.append(t) | ||
return t | ||
|
||
|
||
def create_test_log_object(stamp, msg): | ||
return '{{"__REALTIME_TIMESTAMP": "{}", "MESSAGE": "{}"}}'.format(stamp, msg) | ||
|
||
|
||
@pytest.mark.parametrize('stamp,time_limit,is_too_old', [ | ||
("1496341364241717", 60 * 60, True), | ||
(time() * 1000000, 60 * 60, False), | ||
(time() * 500000, 10, True), | ||
]) | ||
def test_stamp_too_old(stamp, time_limit, is_too_old): | ||
assert search_journalctl._stamp_too_old(stamp, time_limit) == is_too_old | ||
|
||
|
||
@pytest.mark.parametrize('matcher,missing_fields', [ | ||
( | ||
{ | ||
"start_regexp": r"sample start regexp", | ||
"regexp": r"sample regexp", | ||
}, | ||
["unit"], | ||
), | ||
( | ||
{ | ||
"regexp": r"sample regexp", | ||
}, | ||
["unit", "start_regexp"], | ||
), | ||
( | ||
{}, | ||
["unit", "start_regexp", "regexp"], | ||
), | ||
]) | ||
def test_ensure_matcher_fields(matcher, missing_fields): | ||
with pytest.raises(search_journalctl.InvalidMatcherException) as e: | ||
search_journalctl._ensure_matcher_fields(matcher) | ||
assert set(missing_fields) == set(e.value.missing_fields) | ||
assert "missing required fields" in str(e.value) | ||
|
||
|
||
@pytest.mark.parametrize('matchers,log_input,expected_matches,expected_errors', [ | ||
( | ||
[ | ||
{ | ||
"start_regexp": r"Sample Logs Beginning", | ||
"regexp": r"test log message", | ||
"unit": "test", | ||
}, | ||
], | ||
[ | ||
create_test_log_object(get_timestamp_ms(), "test log message"), | ||
create_test_log_object(get_timestamp_ms(), "Sample Logs Beginning"), | ||
], | ||
["test log message"], | ||
[], | ||
), | ||
# test with invalid json in log input | ||
( | ||
[ | ||
{ | ||
"start_regexp": r"Sample Logs Beginning", | ||
"regexp": r"test log message", | ||
"unit": "test", | ||
}, | ||
], | ||
[ | ||
'{__REALTIME_TIMESTAMP: ' + str(get_timestamp_ms()) + ', "MESSAGE": "test log message"}', | ||
], | ||
[], | ||
["Expecting property name"], | ||
), | ||
# test with invalid regexp | ||
( | ||
[ | ||
{ | ||
"start_regexp": r"Sample Logs Beginning", | ||
"regexp": r"test [ log message", | ||
"unit": "test", | ||
}, | ||
], | ||
[ | ||
create_test_log_object(get_timestamp_ms(), "test log message"), | ||
create_test_log_object(get_timestamp_ms(), "sample log message"), | ||
create_test_log_object(get_timestamp_ms(), "fake log message"), | ||
create_test_log_object(get_timestamp_ms(), "dummy log message"), | ||
create_test_log_object(get_timestamp_ms(), "Sample Logs Beginning"), | ||
], | ||
[], | ||
["unexpected end of regular expression"], | ||
), | ||
]) | ||
def test_get_log_matches(matchers, log_input, expected_matches, expected_errors): | ||
def get_log_input(matcher): | ||
return log_input, None | ||
|
||
module = canned_search_journalctl(get_log_input) | ||
matched_regexp, errors = module._get_log_matches(matchers, 500, 60 * 60) | ||
|
||
assert set(matched_regexp) == set(expected_matches) | ||
for partial_err_msg in expected_errors: | ||
for actual_err_msg in errors: | ||
assert partial_err_msg in actual_err_msg | ||
|
||
|
||
@pytest.mark.parametrize('matcher,log_count_limit,time_limit,log_input,expected_match,expected_error', [ | ||
# test with matching log message, but out of bounds of log_count_limit | ||
# expect zero matches and no errors | ||
( | ||
{ | ||
"start_regexp": r"Sample Logs Beginning", | ||
"regexp": r"dummy log message", | ||
"unit": "test", | ||
}, | ||
3, | ||
100 * 60 * 60, | ||
[ | ||
create_test_log_object(get_timestamp_ms(), "test log message"), | ||
create_test_log_object(get_timestamp_ms(), "sample log message"), | ||
create_test_log_object(get_timestamp_ms(), "fake log message"), | ||
create_test_log_object(get_timestamp_ms(), "dummy log message"), | ||
create_test_log_object(get_timestamp_ms(), "Sample Logs Beginning"), | ||
], | ||
None, | ||
None, | ||
), | ||
# test with matching log message, but with timestamp too old | ||
# expect zero matches and no errors | ||
( | ||
{ | ||
"start_regexp": r"Sample Logs Beginning", | ||
"regexp": r"dummy log message", | ||
"unit": "test", | ||
}, | ||
100, | ||
10, | ||
[ | ||
create_test_log_object(get_timestamp_ms(), "test log message"), | ||
create_test_log_object(get_timestamp_ms(), "sample log message"), | ||
create_test_log_object(get_timestamp_ms(), "fake log message"), | ||
create_test_log_object(get_timestamp_ms(-1000), "dummy log message"), | ||
create_test_log_object(get_timestamp_ms(-1000), "Sample Logs Beginning"), | ||
], | ||
None, | ||
None, | ||
), | ||
# test with matching log message, and timestamp within time limit | ||
( | ||
{ | ||
"start_regexp": r"Sample Logs Beginning", | ||
"regexp": r"dummy log message", | ||
"unit": "test", | ||
}, | ||
100, | ||
1010, | ||
[ | ||
create_test_log_object(get_timestamp_ms(), "test log message"), | ||
create_test_log_object(get_timestamp_ms(), "sample log message"), | ||
create_test_log_object(get_timestamp_ms(), "fake log message"), | ||
create_test_log_object(get_timestamp_ms(-1000, SAVED_TIMESTAMPS), "dummy log message"), | ||
create_test_log_object(get_timestamp_ms(-1000), "Sample Logs Beginning"), | ||
], | ||
create_test_log_object(SAVED_TIMESTAMPS[-1], "dummy log message"), | ||
None, | ||
), | ||
]) | ||
def test_find_matches_skips_logs(matcher, log_count_limit, time_limit, log_input, expected_match, expected_error): | ||
match, error = search_journalctl.find_matches(log_input, matcher, log_count_limit, time_limit) | ||
|
||
assert match == expected_match | ||
assert error == expected_error |