Remove unused CI_DEBUG
[functest.git] / testcases / features / bgpvpn.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Execute BGPVPN Tempest test cases
11 #
12 import argparse
13 import os
14 import re
15 import yaml
16 import ConfigParser
17
18 import functest.utils.functest_logger as ft_logger
19 import functest.utils.functest_utils as ft_utils
20
21 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
22     functest_yaml = yaml.safe_load(f)
23
24 """ tests configuration """
25 parser = argparse.ArgumentParser()
26 parser.add_argument("-d", "--debug",
27                     help="Debug mode",
28                     action="store_true")
29 parser.add_argument("-r", "--report",
30                     help="Create json result file",
31                     action="store_true")
32 args = parser.parse_args()
33
34 dirs = functest_yaml.get('general').get('directories')
35 FUNCTEST_REPO = dirs.get('dir_repo_functest')
36 BGPVPN_REPO = dirs.get('dir_repo_bgpvpn')
37 TEST_DB_URL = functest_yaml.get('results').get('test_db_url')
38
39 logger = ft_logger.Logger("bgpvpn").getLogger()
40
41
42 def main():
43     logger.info("Running BGPVPN Tempest test case...")
44
45     cmd = 'cd ' + BGPVPN_REPO + ';pip install --no-deps -e .'
46     ft_utils.execute_command(cmd, logger, exit_on_error=False)
47
48     src_tempest_dir = ft_utils.get_deployment_dir(logger)
49     if not src_tempest_dir:
50         logger.error("Rally deployment not found.")
51         exit(-1)
52
53     src_tempest_conf = src_tempest_dir + '/tempest.conf'
54     dst_tempest_conf = src_tempest_dir + '/etc/tempest.conf'
55
56     config = ConfigParser.RawConfigParser()
57     config.read(src_tempest_conf)
58     config.set('service_available', 'bgpvpn', 'True')
59     with open(dst_tempest_conf, 'wb') as config_file:
60         config.write(config_file)
61
62     cmd_line = (src_tempest_dir +
63                 '/run_tempest.sh -t -N -- networking_bgpvpn_tempest;'
64                 'rm -rf ' + dst_tempest_conf)
65     cmd = os.popen(cmd_line)
66     output = cmd.read()
67     # Results parsing
68     error_logs = ""
69     duration = 0
70     tests = 0
71     failed = 0
72     try:
73         # Look For errors
74         error_logs = ""
75         for match in re.findall('(.*?)[. ]*FAILED', output):
76             error_logs += match
77         # look for duration
78         m = re.search('tests in(.*)sec', output)
79         duration = m.group(1)
80         # Look for tests run
81         m = re.search('Ran:(.*)tests', output)
82         tests = m.group(1)
83         # Look for tests failed
84         m = re.search('Failed:(.*)', output)
85         if m is not None:
86             failed = m.group(1)
87     except:
88         logger.error("Impossible to parse the result file")
89
90     # Generate json results for DB
91     json_results = {"duration": float(duration),
92                     "tests": int(tests),
93                     "failures": int(failed),
94                     "errors": error_logs}
95
96     logger.info("Results: " + str(json_results))
97
98     # Push results in payload of testcase
99     if args.report:
100         logger.debug("Push result into DB")
101         url = TEST_DB_URL
102         scenario = ft_utils.get_scenario(logger)
103         version = ft_utils.get_version(logger)
104         pod_name = ft_utils.get_pod_name(logger)
105         build_tag = ft_utils.get_build_tag(logger)
106         criteria = "failed"
107         if int(tests) > 0 and int(failed) < 1:
108             criteria = "passed"
109
110         ft_utils.push_results_to_db(url, "sdnvpn", "bgpvpn_api", logger,
111                                     pod_name, version, scenario, criteria,
112                                     build_tag, json_results)
113
114 if __name__ == '__main__':
115     main()