summaryrefslogtreecommitdiffstats
path: root/dovetail/utils/dovetail_utils.py
diff options
context:
space:
mode:
authorStamatis Katsaounis <mokats@intracom-telecom.com>2018-11-08 16:58:09 +0200
committerDan Xu <xudan16@huawei.com>2018-11-14 14:31:50 +0000
commitc92c49a9031602030f0f4d4fe2f99a9054eabaee (patch)
treee6ca8b2bbb9d80d064dfeae1e91ac86d50dc318b /dovetail/utils/dovetail_utils.py
parent930ebc4af5c9895775443b9a0ab17a73dc35693c (diff)
Add missing unit tests for utils files
JIRA: DOVETAIL-724 This patch adds unit tests which were missing from dovetail code base. Furthermore it updates the test_parser existing unit test. Finally, it fixes some minor issues in dovetail_utils itself. Change-Id: I8fd7cd4f6b1ede11664914746d2279f062511639 Signed-off-by: Stamatis Katsaounis <mokats@intracom-telecom.com>
Diffstat (limited to 'dovetail/utils/dovetail_utils.py')
-rw-r--r--dovetail/utils/dovetail_utils.py53
1 files changed, 26 insertions, 27 deletions
diff --git a/dovetail/utils/dovetail_utils.py b/dovetail/utils/dovetail_utils.py
index 4a8b45ff..50369e62 100644
--- a/dovetail/utils/dovetail_utils.py
+++ b/dovetail/utils/dovetail_utils.py
@@ -8,6 +8,7 @@
# http://www.apache.org/licenses/LICENSE-2.0
#
+from __future__ import print_function
import sys
import os
import re
@@ -35,8 +36,6 @@ def exec_log(verbose, logger, msg, level, flush=False):
logger.error(msg)
elif level == 'debug':
logger.debug(msg)
- else:
- pass
else:
print(msg)
if flush:
@@ -44,7 +43,8 @@ def exec_log(verbose, logger, msg, level, flush=False):
def exec_cmd(cmd, logger=None, exit_on_error=False, info=False,
- exec_msg_on=True, err_msg="", verbose=True):
+ exec_msg_on=True, err_msg="", verbose=True,
+ progress_bar=False):
msg_err = ("The command '%s' failed." % cmd) if not err_msg else err_msg
msg_exec = ("Executing command: '%s'" % cmd)
level = 'info' if info else 'debug'
@@ -54,14 +54,15 @@ def exec_cmd(cmd, logger=None, exit_on_error=False, info=False,
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout = ''
- # count = 1
- # DEBUG = os.getenv('DEBUG')
+ if progress_bar:
+ count = 1
+ DEBUG = os.getenv('DEBUG')
for line in iter(p.stdout.readline, b''):
exec_log(verbose, logger, line.strip(), level, True)
stdout += line
- # if DEBUG is None or DEBUG.lower() != "true":
- # show_progress_bar(count)
- # count += 1
+ if progress_bar and (DEBUG is None or DEBUG.lower() != "true"):
+ show_progress_bar(count)
+ count += 1
stdout = stdout.strip()
returncode = p.wait()
p.stdout.close()
@@ -109,6 +110,7 @@ def get_obj_by_path(obj, dst_path):
for path, obj in objwalk(obj):
if path == dst_path:
return obj
+ return None
def source_env(env_file):
@@ -324,22 +326,21 @@ def get_hosts_info(logger=None):
if not hosts_yaml:
logger.debug("File {} is empty.".format(hosts_config_file))
return hosts_config
- try:
- if not hosts_yaml['hosts_info']:
- return hosts_config
- for ip, hostnames in hosts_yaml['hosts_info'].iteritems():
- if not hostnames:
- continue
- add_hosts_info(ip, hostnames)
- names_str = ' '.join(hostname for hostname in hostnames
- if hostname)
- if not names_str:
- continue
- hosts_config += ' --add-host=\'{}\':{} '.format(names_str, ip)
- logger.debug('Get hosts info {}:{}.'.format(ip, names_str))
- except KeyError as e:
- logger.error("There is no key {} in file {}"
- .format(e, hosts_config_file))
+ hosts_info = hosts_yaml.get('hosts_info', None)
+ if not hosts_info:
+ logger.error("There is no key hosts_info in file {}"
+ .format(hosts_config_file))
+ return hosts_config
+ for ip, hostnames in hosts_info.iteritems():
+ if not hostnames:
+ continue
+ add_hosts_info(ip, hostnames)
+ names_str = ' '.join(hostname for hostname in hostnames
+ if hostname)
+ if not names_str:
+ continue
+ hosts_config += ' --add-host=\'{}\':{} '.format(names_str, ip)
+ logger.debug('Get hosts info {}:{}.'.format(ip, names_str))
return hosts_config
@@ -377,11 +378,9 @@ def get_value_from_dict(key_path, input_dict):
key_path must be given in string format with dots
Example: result.dir
"""
- if not isinstance(key_path, str):
+ if not isinstance(key_path, str) or not isinstance(input_dict, dict):
return None
for key in key_path.split("."):
- if not isinstance(input_dict, dict):
- return None
input_dict = input_dict.get(key)
if not input_dict:
return None