Move rename_ping_staistics_file_to_checked function to common os module

This patch moves function `rename_ping_staistics_file_to_checked` to the
`tobiko.common._os` module and renames it to the `truncate_file`.
Additionally it adds helper function to get name of the truncated file.
The reason for those changes is that the same functions will be used in
the background iperf tests also.

Related: #TOBIKO-128
Change-Id: I14bbfc51f37323a106bd5e0e4fbf6d7145f716d8
This commit is contained in:
Slawek Kaplonski 2025-02-28 15:06:33 +01:00
parent ef3d773e9a
commit fcd6f19f6e
3 changed files with 15 additions and 8 deletions
tobiko

@ -107,6 +107,8 @@ interworker_synched = _lockutils.interworker_synched
makedirs = _os.makedirs
open_output_file = _os.open_output_file
get_truncated_filename = _os.get_truncated_filename
truncate_logfile = _os.truncate_logfile
runs_operation = _operation.runs_operation
before_operation = _operation.before_operation

@ -16,6 +16,7 @@ from __future__ import absolute_import
import contextlib
import os
import tempfile
import time
from oslo_log import log
@ -57,3 +58,13 @@ def open_output_file(filename, mode='w', temp_dir=None, text=False):
with _exception.exc_info():
if os.path.isfile(temp_filename):
os.remove(temp_filename)
def get_truncated_filename(filepath: str) -> str:
check_time = time.strftime("%Y_%m_%d-%H-%M-%S")
return f'{filepath}_checked_{check_time}'
def truncate_logfile(filepath):
"""append _checked to a ping statistics file once finished it's check"""
os.rename(filepath, get_truncated_filename(filepath))

@ -466,12 +466,6 @@ def get_vm_ping_log_files(glob_ping_log_pattern='tobiko_ping_results/ping_'
yield vm_ping_log_filename
def rename_ping_staistics_file_to_checked(filepath):
"""append _checked to a ping statistics file once finished it's check"""
check_time = time.strftime("%Y_%m_%d-%H-%M-%S")
os.rename(filepath, f'{filepath}_checked_{check_time}')
def check_ping_statistics(failure_limit=10):
"""Gets a list of ping_vm_log files and
iterates their lines, checks if max ping
@ -495,7 +489,7 @@ def check_ping_statistics(failure_limit=10):
else:
LOG.info(f'no failures in ping log file: {filename}')
rename_ping_staistics_file_to_checked(filename)
tobiko.truncate_logfile(filename)
if ping_failures_len >= failure_limit:
tobiko.fail(f'{ping_failures_len} pings failure found '
@ -505,5 +499,5 @@ def check_ping_statistics(failure_limit=10):
def skip_check_ping_statistics():
for filename in list(get_vm_ping_log_files()):
rename_ping_staistics_file_to_checked(filename)
tobiko.truncate_logfile(filename)
LOG.info(f'skipping ping failures in ping log file: {filename}')