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
20 from datetime import datetime as dt
24 from six.moves import urllib
27 from functest.utils import constants
28 from functest.utils import decorators
29 from functest.utils.constants import CONST
31 logger = logging.getLogger(__name__)
34 # ----------------------------------------------------------
38 # -----------------------------------------------------------
39 def check_internet_connectivity(url='http://www.opnfv.org/'):
41 Check if there is access to the internet
44 urllib.request.urlopen(url, timeout=5)
46 except urllib.error.URLError:
50 def download_url(url, dest_path):
52 Download a file to a destination path given a URL
54 name = url.rsplit('/')[-1]
55 dest = dest_path + "/" + name
57 response = urllib.request.urlopen(url)
58 except (urllib.error.HTTPError, urllib.error.URLError):
61 with open(dest, 'wb') as f:
62 shutil.copyfileobj(response, f)
66 # ----------------------------------------------------------
70 # -----------------------------------------------------------
75 # Use the build tag to retrieve the version
76 # By default version is unknown
77 # if launched through CI the build tag has the following format
78 # jenkins-<project>-<installer>-<pod>-<job>-<branch>-<id>
79 # e.g. jenkins-functest-fuel-opnfv-jump-2-daily-master-190
80 # jenkins-functest-fuel-baremetal-weekly-master-8
81 # use regex to match branch info
82 rule = "(dai|week)ly-(.+?)-[0-9]*"
83 build_tag = CONST.__getattribute__('BUILD_TAG')
86 m = re.search(rule, build_tag)
93 def logger_test_results(project, case_name, status, details):
95 Format test case results for the logger
97 pod_name = CONST.__getattribute__('NODE_NAME')
98 scenario = CONST.__getattribute__('DEPLOY_SCENARIO')
99 version = get_version()
100 build_tag = CONST.__getattribute__('BUILD_TAG')
101 db_url = CONST.__getattribute__("results_test_db_url")
105 "****************************************\n"
106 "\t %(p)s/%(n)s results \n\n"
107 "****************************************\n"
113 "build tag:\t%(b)s\n"
126 @decorators.can_dump_request_to_file
127 def push_results_to_db(project, case_name,
128 start_date, stop_date, result, details):
130 POST results to the Result target DB
132 # Retrieve params from CI and conf
133 if (hasattr(CONST, 'TEST_DB_URL')):
134 url = CONST.__getattribute__('TEST_DB_URL')
136 url = CONST.__getattribute__("results_test_db_url")
139 installer = os.environ['INSTALLER_TYPE']
140 scenario = os.environ['DEPLOY_SCENARIO']
141 pod_name = os.environ['NODE_NAME']
142 build_tag = os.environ['BUILD_TAG']
143 except KeyError as e:
144 logger.error("Please set env var: " + str(e))
146 version = get_version()
147 test_start = dt.fromtimestamp(start_date).strftime('%Y-%m-%d %H:%M:%S')
148 test_stop = dt.fromtimestamp(stop_date).strftime('%Y-%m-%d %H:%M:%S')
150 params = {"project_name": project, "case_name": case_name,
151 "pod_name": pod_name, "installer": installer,
152 "version": version, "scenario": scenario, "criteria": result,
153 "build_tag": build_tag, "start_date": test_start,
154 "stop_date": test_stop, "details": details}
157 headers = {'Content-Type': 'application/json'}
159 r = requests.post(url, data=json.dumps(params, sort_keys=True),
163 except requests.RequestException as exc:
165 error = ("Pushing Result to DB(%s) failed: %s" %
168 error = ("Pushing Result to DB(%s) failed: %s" % (url, exc))
169 except Exception as e:
170 error = ("Error [push_results_to_db("
172 "project: '%(project)s', "
176 "scenario: '%(s)s', "
177 "criteria: '%(c)s', "
178 "build_tag: '%(t)s', "
179 "details: '%(d)s')]: "
200 def get_resolvconf_ns():
202 Get nameservers from current resolv.conf
205 rconf = open("/etc/resolv.conf", "r")
206 line = rconf.readline()
207 resolver = dns.resolver.Resolver()
209 ip = re.search(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", line)
211 resolver.nameservers = [ip.group(0)]
213 result = resolver.query('opnfv.org')[0]
215 nameservers.append(ip.group())
216 except dns.exception.Timeout:
218 line = rconf.readline()
222 def get_ci_envvars():
224 Get the CI env variables
227 "installer": os.environ.get('INSTALLER_TYPE'),
228 "scenario": os.environ.get('DEPLOY_SCENARIO')}
232 def execute_command_raise(cmd, info=False, error_msg="",
233 verbose=True, output_file=None):
234 ret = execute_command(cmd, info, error_msg, verbose, output_file)
236 raise Exception(error_msg)
239 def execute_command(cmd, info=False, error_msg="",
240 verbose=True, output_file=None):
242 error_msg = ("The command '%s' failed." % cmd)
243 msg_exec = ("Executing command: '%s'" % cmd)
246 logger.info(msg_exec)
248 logger.debug(msg_exec)
249 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
250 stderr=subprocess.STDOUT)
252 f = open(output_file, "w")
253 for line in iter(p.stdout.readline, b''):
257 line = line.replace('\n', '')
263 returncode = p.wait()
266 logger.error(error_msg)
271 def get_dict_by_test(testname):
272 with open(pkg_resources.resource_filename(
273 'functest', 'ci/testcases.yaml')) as f:
274 testcases_yaml = yaml.safe_load(f)
276 for dic_tier in testcases_yaml.get("tiers"):
277 for dic_testcase in dic_tier['testcases']:
278 if dic_testcase['case_name'] == testname:
281 logger.error('Project %s is not defined in testcases.yaml' % testname)
285 def get_criteria_by_test(testname):
286 dict = get_dict_by_test(testname)
288 return dict['criteria']
292 # ----------------------------------------------------------
296 # -----------------------------------------------------------
297 def get_parameter_from_yaml(parameter, file):
299 Returns the value of a given parameter in file.yaml
300 parameter must be given in string format with dots
301 Example: general.openstack.image_name
303 with open(file) as f:
304 file_yaml = yaml.safe_load(f)
307 for element in parameter.split("."):
308 value = value.get(element)
310 raise ValueError("The parameter %s is not defined in"
311 " %s" % (parameter, file))
315 def get_functest_config(parameter):
316 yaml_ = constants.CONST.__getattribute__('CONFIG_FUNCTEST_YAML')
317 return get_parameter_from_yaml(parameter, yaml_)
320 def get_functest_yaml():
321 with open(constants.CONST.__getattribute__('CONFIG_FUNCTEST_YAML')) as f:
322 functest_yaml = yaml.safe_load(f)
327 def print_separator():
328 logger.info("==============================================")
332 """Measure the time it takes for a function to complete"""
333 @functools.wraps(func)
334 def timed(*args, **kwargs):
336 result = func(*args, **kwargs)
338 elapsed = '{0}'.format(te - ts)
339 logger.info('{f}(*{a}, **{kw}) took: {t} sec'.format(
340 f=func.__name__, a=args, kw=kwargs, t=elapsed))
341 return result, elapsed