aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/common/utils.py
diff options
context:
space:
mode:
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>2017-11-08 12:41:36 +0000
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>2017-12-22 11:40:01 +0000
commit090b1a166bd19bdb98b0311d58b85582bd1676ed (patch)
tree74e9c509a9b5e58d8e69773f31dca85b78e13216 /yardstick/common/utils.py
parent9bef739e89f3b230c9e0a53c2286a440017ec948 (diff)
Replace subprocess "check_output" with "Popen"
"check_output" is a blocking wrapper for "Popen" which returns the output of the command execution or raises an exception in case of error. "Popen" is a non-blocking function that allows to create asynchronous tasks. It returns any possible execution error but doesn't raise an exception; this is delegated to the developer. This code is used in the Yardstick CLI base test class. Change-Id: Ie3e1228b2d40cb306354447653678bf581bc9697 Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Diffstat (limited to 'yardstick/common/utils.py')
-rw-r--r--yardstick/common/utils.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py
index 51f6e1360..82e20bec7 100644
--- a/yardstick/common/utils.py
+++ b/yardstick/common/utils.py
@@ -76,7 +76,7 @@ def import_modules_from_package(package):
"""
yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__))
path = os.path.join(yardstick_root, *package.split("."))
- for root, dirs, files in os.walk(path):
+ for root, _, files in os.walk(path):
matches = (filename for filename in files if filename.endswith(".py") and
not filename.startswith("__"))
new_package = os.path.relpath(root, yardstick_root).replace(os.sep, ".")
@@ -251,10 +251,10 @@ def set_dict_value(dic, keys, value):
def get_free_port(ip):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
- while True:
+ port = random.randint(5000, 10000)
+ while s.connect_ex((ip, port)) == 0:
port = random.randint(5000, 10000)
- if s.connect_ex((ip, port)) != 0:
- return port
+ return port
def mac_address_to_hex_list(mac):
@@ -350,10 +350,13 @@ def config_to_dict(config):
def validate_non_string_sequence(value, default=None, raise_exc=None):
+ # NOTE(ralonsoh): refactor this function to check if raise_exc is an
+ # Exception. Remove duplicate code, this function is duplicated in this
+ # repository.
if isinstance(value, collections.Sequence) and not isinstance(value, six.string_types):
return value
if raise_exc:
- raise raise_exc
+ raise raise_exc # pylint: disable=raising-bad-type
return default
@@ -365,6 +368,13 @@ def join_non_strings(separator, *non_strings):
return str(separator).join(str(non_string) for non_string in non_strings)
+def safe_decode_utf8(s):
+ """Safe decode a str from UTF"""
+ if six.PY3 and isinstance(s, bytes):
+ return s.decode('utf-8', 'surrogateescape')
+ return s
+
+
class ErrorClass(object):
def __init__(self, *args, **kwargs):