Merge "Bugfix: yardstick always report 'PASS' to DB"
[yardstick.git] / yardstick / common / utils.py
index a4f7b30..7a64b8c 100644 (file)
@@ -24,7 +24,10 @@ import os
 import subprocess
 import sys
 import collections
+import socket
+import random
 from functools import reduce
+from contextlib import closing
 
 import yaml
 import six
@@ -169,7 +172,15 @@ def write_file(path, data, mode='w'):
 
 def parse_ini_file(path):
     parser = configparser.ConfigParser()
-    parser.read(path)
+
+    try:
+        files = parser.read(path)
+    except configparser.MissingSectionHeaderError:
+        logger.exception('invalid file type')
+        raise
+    else:
+        if not files:
+            raise RuntimeError('file not exist')
 
     try:
         default = {k: v for k, v in parser.items('DEFAULT')}
@@ -263,3 +274,11 @@ def set_dict_value(dic, keys, value):
         else:
             return_dic = return_dic[key]
     return dic
+
+
+def get_free_port(ip):
+    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
+        while True:
+            port = random.randint(5000, 10000)
+            if s.connect_ex((ip, port)) != 0:
+                return port