3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # pylint: disable=missing-docstring
12 from __future__ import print_function
20 from six.moves import urllib
23 from functest.utils import constants
24 from functest.utils import env
26 LOGGER = logging.getLogger(__name__)
29 # ----------------------------------------------------------
33 # -----------------------------------------------------------
34 def check_internet_connectivity(url='http://www.opnfv.org/'):
36 Check if there is access to the internet
39 urllib.request.urlopen(url, timeout=5)
41 except urllib.error.URLError:
45 def download_url(url, dest_path):
47 Download a file to a destination path given a URL
49 name = url.rsplit('/')[-1]
50 dest = dest_path + "/" + name
52 response = urllib.request.urlopen(url)
53 except (urllib.error.HTTPError, urllib.error.URLError):
56 with open(dest, 'wb') as lfile:
57 shutil.copyfileobj(response, lfile)
61 # ----------------------------------------------------------
65 # -----------------------------------------------------------
66 def get_resolvconf_ns():
68 Get nameservers from current resolv.conf
71 rconf = open("/etc/resolv.conf", "r")
72 line = rconf.readline()
73 resolver = dns.resolver.Resolver()
75 addr_ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
77 resolver.nameservers = [addr_ip.group(0)]
79 result = resolver.query('opnfv.org')[0]
81 nameservers.append(addr_ip.group())
82 except dns.exception.Timeout:
84 line = rconf.readline()
90 Get the CI env variables
93 "installer": env.get('INSTALLER_TYPE'),
94 "scenario": env.get('DEPLOY_SCENARIO')}
98 def execute_command_raise(cmd, info=False, error_msg="",
99 verbose=True, output_file=None):
100 ret = execute_command(cmd, info, error_msg, verbose, output_file)
102 raise Exception(error_msg)
105 def execute_command(cmd, info=False, error_msg="",
106 verbose=True, output_file=None):
108 error_msg = ("The command '%s' failed." % cmd)
109 msg_exec = ("Executing command: '%s'" % cmd)
112 LOGGER.info(msg_exec)
114 LOGGER.debug(msg_exec)
115 popen = subprocess.Popen(
116 cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
118 ofd = open(output_file, "w")
119 for line in iter(popen.stdout.readline, b''):
123 line = line.replace('\n', '')
129 returncode = popen.wait()
132 LOGGER.error(error_msg)
137 # ----------------------------------------------------------
141 # -----------------------------------------------------------
142 def get_parameter_from_yaml(parameter, yfile):
144 Returns the value of a given parameter in file.yaml
145 parameter must be given in string format with dots
146 Example: general.openstack.image_name
148 with open(yfile) as yfd:
149 file_yaml = yaml.safe_load(yfd)
151 for element in parameter.split("."):
152 value = value.get(element)
154 raise ValueError("The parameter %s is not defined in"
155 " %s" % (parameter, yfile))
159 def get_functest_config(parameter):
160 yaml_ = constants.Constants.CONFIG_FUNCTEST_YAML
161 return get_parameter_from_yaml(parameter, yaml_)
164 def get_functest_yaml():
165 # pylint: disable=bad-continuation
166 with open(constants.Constants.CONFIG_FUNCTEST_YAML) as yaml_fd:
167 functest_yaml = yaml.safe_load(yaml_fd)
171 def print_separator():
172 LOGGER.info("==============================================")