diff options
Diffstat (limited to 'functest/utils')
-rw-r--r-- | functest/utils/config.py | 71 | ||||
-rw-r--r-- | functest/utils/functest_utils.py | 172 |
2 files changed, 0 insertions, 243 deletions
diff --git a/functest/utils/config.py b/functest/utils/config.py deleted file mode 100644 index 61d8401c..00000000 --- a/functest/utils/config.py +++ /dev/null @@ -1,71 +0,0 @@ -#!/usr/bin/env python - -# pylint: disable=missing-docstring - -import pkg_resources -import yaml - -import six - -from functest.utils import env - - -class Config(object): - def __init__(self): - try: - # pylint: disable=bad-continuation - with open(pkg_resources.resource_filename( - 'functest', 'ci/config_functest.yaml')) as yfile: - self.functest_yaml = yaml.safe_load(yfile) - except Exception as error: - raise Exception('Parse config failed: {}'.format(str(error))) - - @staticmethod - def _merge_dicts(dict1, dict2): - for k in set(dict1.keys()).union(dict2.keys()): - if k in dict1 and k in dict2: - if isinstance(dict1[k], dict) and isinstance(dict2[k], dict): - yield (k, dict(Config._merge_dicts(dict1[k], dict2[k]))) - else: - yield (k, dict2[k]) - elif k in dict1: - yield (k, dict1[k]) - else: - yield (k, dict2[k]) - - def patch_file(self, patch_file_path): - with open(patch_file_path) as yfile: - patch_file = yaml.safe_load(yfile) - - for key in patch_file: - if key in env.get('DEPLOY_SCENARIO'): - self.functest_yaml = dict(Config._merge_dicts( - self.functest_yaml, patch_file[key])) - - def _parse(self, attr_now, left_parametes): - for param_n, param_v in six.iteritems(left_parametes): - attr_further = self._get_attr_further(attr_now, param_n) - if attr_further: - setattr(self, attr_further, param_v) - if isinstance(param_v, dict): - self._parse(attr_further, param_v) - - @staticmethod - def _get_attr_further(attr_now, next): # pylint: disable=redefined-builtin - return attr_now if next == 'general' else ( - '{}_{}'.format(attr_now, next) if attr_now else next) - - def fill(self): - try: - self._parse(None, self.functest_yaml) - except Exception as error: - raise Exception('Parse config failed: {}'.format(str(error))) - - -CONF = Config() -CONF.patch_file(pkg_resources.resource_filename( - 'functest', 'ci/config_patch.yaml')) -if env.get("POD_ARCH") in ['aarch64']: - CONF.patch_file(pkg_resources.resource_filename( - 'functest', 'ci/config_aarch64_patch.yaml')) -CONF.fill() diff --git a/functest/utils/functest_utils.py b/functest/utils/functest_utils.py deleted file mode 100644 index 38a60d49..00000000 --- a/functest/utils/functest_utils.py +++ /dev/null @@ -1,172 +0,0 @@ -#!/usr/bin/env python -# -# jose.lausuch@ericsson.com -# valentin.boucher@orange.com -# All rights reserved. This program and the accompanying materials -# are made available under the terms of the Apache License, Version 2.0 -# which accompanies this distribution, and is available at -# http://www.apache.org/licenses/LICENSE-2.0 - -# pylint: disable=missing-docstring - -from __future__ import print_function -import logging -import re -import shutil -import subprocess -import sys - -import dns.resolver -from six.moves import urllib -import yaml - -from functest.utils import constants -from functest.utils import env - -LOGGER = logging.getLogger(__name__) - - -# ---------------------------------------------------------- -# -# INTERNET UTILS -# -# ----------------------------------------------------------- -def check_internet_connectivity(url='http://www.opnfv.org/'): - """ - Check if there is access to the internet - """ - try: - urllib.request.urlopen(url, timeout=5) - return True - except urllib.error.URLError: - return False - - -def download_url(url, dest_path): - """ - Download a file to a destination path given a URL - """ - name = url.rsplit('/')[-1] - dest = dest_path + "/" + name - try: - response = urllib.request.urlopen(url) - except (urllib.error.HTTPError, urllib.error.URLError): - return False - - with open(dest, 'wb') as lfile: - shutil.copyfileobj(response, lfile) - return True - - -# ---------------------------------------------------------- -# -# CI UTILS -# -# ----------------------------------------------------------- -def get_resolvconf_ns(): - """ - Get nameservers from current resolv.conf - """ - nameservers = [] - rconf = open("/etc/resolv.conf", "r") - line = rconf.readline() - resolver = dns.resolver.Resolver() - while line: - addr_ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line) - if addr_ip: - resolver.nameservers = [addr_ip.group(0)] - try: - result = resolver.query('opnfv.org')[0] - if result != "": - nameservers.append(addr_ip.group()) - except dns.exception.Timeout: - pass - line = rconf.readline() - return nameservers - - -def get_ci_envvars(): - """ - Get the CI env variables - """ - ci_env_var = { - "installer": env.get('INSTALLER_TYPE'), - "scenario": env.get('DEPLOY_SCENARIO')} - return ci_env_var - - -def execute_command_raise(cmd, info=False, error_msg="", - verbose=True, output_file=None): - ret = execute_command(cmd, info, error_msg, verbose, output_file) - if ret != 0: - raise Exception(error_msg) - - -def execute_command(cmd, info=False, error_msg="", - verbose=True, output_file=None): - if not error_msg: - error_msg = ("The command '%s' failed." % cmd) - msg_exec = ("Executing command: '%s'" % cmd) - if verbose: - if info: - LOGGER.info(msg_exec) - else: - LOGGER.debug(msg_exec) - popen = subprocess.Popen( - cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - if output_file: - ofd = open(output_file, "w") - for line in iter(popen.stdout.readline, b''): - if output_file: - ofd.write(line) - else: - line = line.replace('\n', '') - print (line) - sys.stdout.flush() - if output_file: - ofd.close() - popen.stdout.close() - returncode = popen.wait() - if returncode != 0: - if verbose: - LOGGER.error(error_msg) - - return returncode - - -# ---------------------------------------------------------- -# -# YAML UTILS -# -# ----------------------------------------------------------- -def get_parameter_from_yaml(parameter, yfile): - """ - Returns the value of a given parameter in file.yaml - parameter must be given in string format with dots - Example: general.openstack.image_name - """ - with open(yfile) as yfd: - file_yaml = yaml.safe_load(yfd) - value = file_yaml - for element in parameter.split("."): - value = value.get(element) - if value is None: - raise ValueError("The parameter %s is not defined in" - " %s" % (parameter, yfile)) - return value - - -def get_functest_config(parameter): - yaml_ = constants.CONFIG_FUNCTEST_YAML - return get_parameter_from_yaml(parameter, yaml_) - - -def get_functest_yaml(): - # pylint: disable=bad-continuation - with open(constants.CONFIG_FUNCTEST_YAML) as yaml_fd: - functest_yaml = yaml.safe_load(yaml_fd) - return functest_yaml - - -def print_separator(): - LOGGER.info("==============================================") |