Move linux command and wait method to common tools

Signed-off-by: Katarzyna Treder <katarzyna.treder@h-partners.com>
This commit is contained in:
Katarzyna Treder
2024-12-10 13:57:18 +01:00
parent f60e90192e
commit a954e47b33
9 changed files with 26 additions and 18 deletions

View File

View File

@@ -0,0 +1,79 @@
#
# Copyright(c) 2019-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#
from collections import defaultdict
class LinuxCommand:
def __init__(self, command_executor, command_name):
self.command_executor = command_executor
self.command_param = defaultdict(list)
self.command_flags = []
self.command_name = command_name
self.param_name_prefix = ''
self.param_separator = ' '
self.param_value_prefix = '='
self.param_value_list_separator = ','
self.command_env_var = defaultdict(list)
self.env_var_separator = ' '
self.env_var_value_prefix = '='
def run(self):
return self.command_executor.run(str(self))
def run_in_background(self):
return self.command_executor.run_in_background(str(self))
def set_flags(self, *flag):
for f in flag:
self.command_flags.append(f)
return self
def remove_flag(self, flag):
if flag in self.command_flags:
self.command_flags.remove(flag)
return self
def set_param(self, key, *values):
self.remove_param(key)
for val in values:
self.command_param[key].append(str(val))
return self
def remove_param(self, key):
if key in self.command_param:
del self.command_param[key]
return self
def set_env_var(self, key, *values):
self.remove_env_var(key)
for val in values:
self.command_env_var[key].append(str(val))
return self
def remove_env_var(self, key):
if key in self.command_env_var:
del self.command_env_var[key]
return self
def get_parameter_value(self, param_name):
if param_name in self.command_param.keys():
return self.command_param[param_name]
return None
def __str__(self):
command = ''
for key, value in self.command_env_var.items():
command += f'{key}{self.env_var_value_prefix}{",".join(value)}' \
f'{self.env_var_separator}'
command += self.command_name
for key, value in self.command_param.items():
command += f'{self.param_separator}{self.param_name_prefix}' \
f'{key}{self.param_value_prefix}{",".join(value)}'
for flag in self.command_flags:
command += f'{self.param_separator}{self.param_name_prefix}{flag}'
return command

20
test_tools/common/wait.py Normal file
View File

@@ -0,0 +1,20 @@
#
# Copyright(c) 2019-2022 Intel Corporation
# Copyright(c) 2024 Huawei Technologies Co., Ltd.
# SPDX-License-Identifier: BSD-3-Clause
#
import time
from datetime import timedelta, datetime
def wait(predicate, timeout: timedelta, interval: timedelta = None):
start_time = datetime.now()
result = False
while start_time + timeout > datetime.now():
result = predicate()
if result:
break
if interval is not None:
time.sleep(interval.total_seconds())
return result

View File

@@ -3,7 +3,7 @@
# SPDX-License-Identifier: BSD-3-Clause
#
import test_utils.linux_command as linux_comm
import test_tools.common.linux_command as linux_comm
import types.size as size
from core.test_run import TestRun

View File

@@ -3,7 +3,7 @@
# SPDX-License-Identifier: BSD-3-Clause
#
import test_utils.linux_command as linux_comm
import test_tools.common.linux_command as linux_comm
import types.size as size
from core.test_run import TestRun

View File

@@ -8,7 +8,7 @@ from enum import Enum
from core.test_run import TestRun
from storage_devices.device import Device
from test_utils.linux_command import LinuxCommand
from test_tools.common.linux_command import LinuxCommand
from types.size import Size, Unit

View File

@@ -13,7 +13,7 @@ from connection.base_executor import BaseExecutor
from core.test_run import TestRun
from storage_devices.device import Device
from test_tools.fio.fio_result import FioResult
from test_utils.linux_command import LinuxCommand
from test_tools.common.linux_command import LinuxCommand
from types.size import Size