Add log level and test for functions
This commit is contained in:
parent
7d441d6386
commit
733e73ae58
5 changed files with 179 additions and 37 deletions
101
src/tests/test_functions.py
Normal file
101
src/tests/test_functions.py
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
import json
|
||||
import pytest
|
||||
import os
|
||||
import re
|
||||
import random
|
||||
import string
|
||||
from functions import Functions
|
||||
|
||||
def test_functions_check_local_level():
|
||||
assert Functions.skip_log('CERTBOT', Functions.INFO) == False
|
||||
assert Functions.skip_log('HAPOROXY', Functions.INFO) == False
|
||||
assert Functions.skip_log('EASYHAPROXY', Functions.INFO) == False
|
||||
|
||||
os.environ['CERTBOT_LOG_LEVEL'] = 'warn'
|
||||
assert Functions.skip_log('CERTBOT', Functions.INFO) == True
|
||||
os.environ['CERTBOT_LOG_LEVEL'] = ''
|
||||
|
||||
os.environ['HAPROXY_LOG_LEVEL'] = 'warn'
|
||||
assert Functions.skip_log('HAPROXY', Functions.INFO) == True
|
||||
os.environ['HAPROXY_LOG_LEVEL'] = ''
|
||||
|
||||
os.environ['EASYHAPROXY_LOG_LEVEL'] = 'warn'
|
||||
assert Functions.skip_log('EASYHAPROXY', Functions.INFO) == True
|
||||
os.environ['EASYHAPROXY_LOG_LEVEL'] = ''
|
||||
|
||||
def test_function_load_and_save():
|
||||
filename = '/tmp/x.txt'
|
||||
try:
|
||||
assert os.path.exists(filename) == False
|
||||
text = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(50))
|
||||
Functions.save(filename, text)
|
||||
assert os.path.exists(filename) == True
|
||||
assert Functions.load(filename) == text
|
||||
finally:
|
||||
os.unlink(filename)
|
||||
|
||||
def test_functions_check_log_sanity():
|
||||
print()
|
||||
Functions.log(Functions.EASYHAPROXY_LOG, Functions.INFO, "Test 1")
|
||||
assert Functions.debug_log is None
|
||||
|
||||
Functions.debug_log = []
|
||||
try:
|
||||
Functions.log(Functions.EASYHAPROXY_LOG, Functions.INFO, "Test 2")
|
||||
assert len(Functions.debug_log) == 1
|
||||
assert re.match("\[EASYHAPROXY\] .* \[INFO\]: Test 2", Functions.debug_log[0])
|
||||
|
||||
os.environ['CERTBOT_LOG_LEVEL'] = 'DEBUG'
|
||||
Functions.log(Functions.EASYHAPROXY_LOG, Functions.INFO, "Test 3")
|
||||
assert len(Functions.debug_log) == 2
|
||||
assert re.match("\[EASYHAPROXY\] .* \[INFO\]: Test 3", Functions.debug_log[1])
|
||||
|
||||
os.environ['EASYHAPROXY_LOG_LEVEL'] = 'warn'
|
||||
Functions.log(Functions.EASYHAPROXY_LOG, Functions.INFO, "Test 4") # Should not log to debug
|
||||
assert len(Functions.debug_log) == 2
|
||||
|
||||
finally:
|
||||
os.environ['EASYHAPROXY_LOG_LEVEL'] = ''
|
||||
Functions.debug_log = None
|
||||
|
||||
def test_functions_run_bash_log_output():
|
||||
print()
|
||||
Functions.debug_log = []
|
||||
try:
|
||||
result = Functions.run_bash(Functions.EASYHAPROXY_LOG, "echo 'test run 1'", log_output=True, return_result=False)
|
||||
assert result == []
|
||||
assert len(Functions.debug_log) == 1
|
||||
assert re.match("\[EASYHAPROXY\] .* \[INFO\]: test run 1", Functions.debug_log[0])
|
||||
finally:
|
||||
Functions.debug_log = None
|
||||
|
||||
def test_functions_run_bash_no_log_output():
|
||||
print()
|
||||
Functions.debug_log = []
|
||||
try:
|
||||
result = Functions.run_bash(Functions.EASYHAPROXY_LOG, "echo 'test run 2'", log_output=False, return_result=False)
|
||||
assert result == []
|
||||
assert len(Functions.debug_log) == 0
|
||||
finally:
|
||||
Functions.debug_log = None
|
||||
|
||||
def test_functions_run_bash_return():
|
||||
print()
|
||||
Functions.debug_log = []
|
||||
try:
|
||||
result = Functions.run_bash(Functions.EASYHAPROXY_LOG, "echo 'test run 3'", log_output=False, return_result=True)
|
||||
assert len(Functions.debug_log) == 0
|
||||
assert "".join(result) == 'test run 3'
|
||||
finally:
|
||||
Functions.debug_log = None
|
||||
|
||||
def test_functions_run_bash_log_and_return_output():
|
||||
print()
|
||||
Functions.debug_log = []
|
||||
try:
|
||||
result = Functions.run_bash(Functions.EASYHAPROXY_LOG, "echo 'test run 4'", log_output=True, return_result=True)
|
||||
assert "".join(result) == 'test run 4'
|
||||
assert len(Functions.debug_log) == 1
|
||||
assert re.match("\[EASYHAPROXY\] .* \[INFO\]: test run 4", Functions.debug_log[0])
|
||||
finally:
|
||||
Functions.debug_log = None
|
||||
Loading…
Add table
Add a link
Reference in a new issue