X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=tests%2Ftransportpce_tests%2Fcommon%2Ftest_utils.py;h=888a8e91fc228751b85ecd386c41bd49cd2f4537;hb=65f292021789e99cbf735b83537ca80554a75ba8;hp=8ba812204b97e611af1e3d109124831d95a164e9;hpb=0a83b6936702d50a080b76615b89992d6adc2c4b;p=transportpce.git diff --git a/tests/transportpce_tests/common/test_utils.py b/tests/transportpce_tests/common/test_utils.py index 8ba812204..888a8e91f 100644 --- a/tests/transportpce_tests/common/test_utils.py +++ b/tests/transportpce_tests/common/test_utils.py @@ -273,34 +273,29 @@ def wait_until_log_contains(log_file, regexp, time_to_wait=60): # pylint: disable=lost-exception # pylint: disable=consider-using-with stringfound = False - filefound = False line = None try: with TimeOut(seconds=time_to_wait): while not os.path.exists(log_file): time.sleep(0.2) - filelogs = open(log_file, 'r', encoding='utf-8') - filelogs.seek(0, 2) - filefound = True - print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True) - compiled_regexp = re.compile(regexp) - while True: - line = filelogs.readline() - if compiled_regexp.search(line): - print('Pattern found!', end=' ') - stringfound = True - break - if not line: - time.sleep(0.1) + with open(log_file, 'r', encoding='utf-8') as filelogs: + filelogs.seek(0, 2) + print("Searching for pattern '" + regexp + "' in " + os.path.basename(log_file), end='... ', flush=True) + compiled_regexp = re.compile(regexp) + while True: + line = filelogs.readline() + if compiled_regexp.search(line): + print('Pattern found!', end=' ') + stringfound = True + break + if not line: + time.sleep(0.1) + return stringfound except TimeoutError: print('Pattern not found after ' + str(time_to_wait), end=' seconds! ', flush=True) + return stringfound except PermissionError: print('Permission Error when trying to access the log file', end=' ... ', flush=True) - finally: - if filefound: - filelogs.close() - else: - print('log file does not exist or is not accessible... ', flush=True) return stringfound