Tests: preserving unit.log when run without restart.

Introducing "unit.log.Log" class for "unit.log" file management.
Moving "findall()" function into TestApplicationProto.
Using "os.kill()" to send signals.
This commit is contained in:
Max Romanov
2021-04-08 19:11:11 +03:00
parent 30922c5741
commit 74b1b1fc17
9 changed files with 94 additions and 96 deletions

View File

@@ -22,6 +22,7 @@ from unit.check.regex import check_regex
from unit.check.tls import check_openssl
from unit.http import TestHTTP
from unit.option import option
from unit.log import Log
from unit.utils import public_dir
from unit.utils import waitforfiles
@@ -71,7 +72,6 @@ def pytest_addoption(parser):
unit_instance = {}
unit_log_copy = "unit.log.copy"
_processes = []
_fds_check = {
'main': {'fds': 0, 'skip': False},
@@ -165,12 +165,11 @@ def pytest_sessionstart(session):
option.available = {'modules': {}, 'features': {}}
unit = unit_run()
option.temp_dir = unit['temp_dir']
# read unit.log
for i in range(50):
with open(unit['temp_dir'] + '/unit.log', 'r') as f:
with open(Log.get_path(), 'r') as f:
log = f.read()
m = re.search('controller started', log)
@@ -216,9 +215,6 @@ def pytest_sessionstart(session):
if option.restart:
shutil.rmtree(unit_instance['temp_dir'])
elif option.save_log:
open(unit_instance['temp_dir'] + '/' + unit_log_copy, 'w').close()
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
@@ -269,7 +265,6 @@ def check_prerequisites(request):
@pytest.fixture(autouse=True)
def run(request):
unit = unit_run()
option.temp_dir = unit['temp_dir']
option.skip_alerts = [
r'read signalfd\(4\) failed',
@@ -291,34 +286,25 @@ def run(request):
# prepare log
with open(
unit_instance['log'], 'r', encoding='utf-8', errors='ignore'
) as f:
with Log.open(encoding='utf-8') as f:
log = f.read()
if not option.restart and option.save_log:
with open(unit_instance['temp_dir'] + '/' + unit_log_copy, 'a') as f:
f.write(log)
# remove unit.log
Log.set_pos(f.tell())
if not option.save_log and option.restart:
shutil.rmtree(unit['temp_dir'])
Log.set_pos(0)
# clean temp_dir before the next test
if not option.restart:
_clear_conf(unit['temp_dir'] + '/control.unit.sock', log)
open(unit['log'], 'w').close()
for item in os.listdir(unit['temp_dir']):
if item not in [
'control.unit.sock',
'state',
'unit.pid',
'unit.log',
unit_log_copy,
]:
path = os.path.join(unit['temp_dir'], item)
@@ -439,10 +425,12 @@ def unit_run():
exit('Could not start unit')
unit_instance['temp_dir'] = temp_dir
unit_instance['log'] = temp_dir + '/unit.log'
unit_instance['control_sock'] = temp_dir + '/control.unit.sock'
unit_instance['unitd'] = unitd
option.temp_dir = temp_dir
Log.temp_dir = temp_dir
with open(temp_dir + '/unit.pid', 'r') as f:
unit_instance['pid'] = f.read().rstrip()
@@ -489,12 +477,9 @@ def unit_stop():
return 'Could not terminate unit'
def _check_alerts(path=None, log=None):
if path is None:
path = unit_instance['log']
def _check_alerts(log=None):
if log is None:
with open(path, 'r', encoding='utf-8', errors='ignore') as f:
with Log.open(encoding='utf-8') as f:
log = f.read()
found = False
@@ -526,7 +511,7 @@ def _check_alerts(path=None, log=None):
def _print_log(data=None):
path = unit_instance['log']
path = Log.get_path()
print('Path to unit.log:\n' + path + '\n')
@@ -679,7 +664,7 @@ def unit_pid(request):
def pytest_sessionfinish(session):
if not option.restart and option.save_log:
print('Path to unit.log:\n' + unit_instance['log'] + '\n')
print('Path to unit.log:\n' + Log.get_path() + '\n')
option.restart = True