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
22 from six.moves import urllib
25 from functest.utils import constants
27 LOGGER = logging.getLogger(__name__)
30 # ----------------------------------------------------------
34 # -----------------------------------------------------------
35 def check_internet_connectivity(url='http://www.opnfv.org/'):
37 Check if there is access to the internet
40 urllib.request.urlopen(url, timeout=5)
42 except urllib.error.URLError:
46 def download_url(url, dest_path):
48 Download a file to a destination path given a URL
50 name = url.rsplit('/')[-1]
51 dest = dest_path + "/" + name
53 response = urllib.request.urlopen(url)
54 except (urllib.error.HTTPError, urllib.error.URLError):
57 with open(dest, 'wb') as lfile:
58 shutil.copyfileobj(response, lfile)
62 # ----------------------------------------------------------
66 # -----------------------------------------------------------
67 def get_resolvconf_ns():
69 Get nameservers from current resolv.conf
72 rconf = open("/etc/resolv.conf", "r")
73 line = rconf.readline()
74 resolver = dns.resolver.Resolver()
76 addr_ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
78 resolver.nameservers = [addr_ip.group(0)]
80 result = resolver.query('opnfv.org')[0]
82 nameservers.append(addr_ip.group())
83 except dns.exception.Timeout:
85 line = rconf.readline()
91 Get the CI env variables
94 "installer": os.environ.get('INSTALLER_TYPE'),
95 "scenario": os.environ.get('DEPLOY_SCENARIO')}
99 def execute_command_raise(cmd, info=False, error_msg="",
100 verbose=True, output_file=None):
101 ret = execute_command(cmd, info, error_msg, verbose, output_file)
103 raise Exception(error_msg)
106 def execute_command(cmd, info=False, error_msg="",
107 verbose=True, output_file=None):
109 error_msg = ("The command '%s' failed." % cmd)
110 msg_exec = ("Executing command: '%s'" % cmd)
113 LOGGER.info(msg_exec)
115 LOGGER.debug(msg_exec)
116 popen = subprocess.Popen(
117 cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
119 ofd = open(output_file, "w")
120 for line in iter(popen.stdout.readline, b''):
124 line = line.replace('\n', '')
130 returncode = popen.wait()
133 LOGGER.error(error_msg)
138 def get_dict_by_test(testname):
139 # pylint: disable=bad-continuation
140 with open(pkg_resources.resource_filename(
141 'functest', 'ci/testcases.yaml')) as tyaml:
142 testcases_yaml = yaml.safe_load(tyaml)
144 for dic_tier in testcases_yaml.get("tiers"):
145 for dic_testcase in dic_tier['testcases']:
146 if dic_testcase['case_name'] == testname:
149 LOGGER.error('Project %s is not defined in testcases.yaml', testname)
153 def get_criteria_by_test(testname):
154 tdict = get_dict_by_test(testname)
156 return tdict['criteria']
160 # ----------------------------------------------------------
164 # -----------------------------------------------------------
165 def get_parameter_from_yaml(parameter, yfile):
167 Returns the value of a given parameter in file.yaml
168 parameter must be given in string format with dots
169 Example: general.openstack.image_name
171 with open(yfile) as yfd:
172 file_yaml = yaml.safe_load(yfd)
174 for element in parameter.split("."):
175 value = value.get(element)
177 raise ValueError("The parameter %s is not defined in"
178 " %s" % (parameter, yfile))
182 def get_functest_config(parameter):
183 yaml_ = constants.CONST.__getattribute__('CONFIG_FUNCTEST_YAML')
184 return get_parameter_from_yaml(parameter, yaml_)
187 def get_functest_yaml():
188 # pylint: disable=bad-continuation
189 with open(constants.CONST.__getattribute__(
190 'CONFIG_FUNCTEST_YAML')) as yaml_fd:
191 functest_yaml = yaml.safe_load(yaml_fd)
195 def print_separator():
196 LOGGER.info("==============================================")