SFC Testcase for functest
[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 time
16 import yaml
17 import ConfigParser
18
19 import functest.utils.functest_logger as ft_logger
20 import functest.utils.functest_utils as ft_utils
21
22 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
23     functest_yaml = yaml.safe_load(f)
24
25 """ tests configuration """
26 parser = argparse.ArgumentParser()
27 parser.add_argument("-d", "--debug",
28                     help="Debug mode",
29                     action="store_true")
30 parser.add_argument("-r", "--report",
31                     help="Create json result file",
32                     action="store_true")
33 args = parser.parse_args()
34
35 dirs = functest_yaml.get('general').get('directories')
36 FUNCTEST_REPO = dirs.get('dir_repo_functest')
37 BGPVPN_REPO = dirs.get('dir_repo_bgpvpn')
38 TEST_DB_URL = functest_yaml.get('results').get('test_db_url')
39
40 logger = ft_logger.Logger("bgpvpn").getLogger()
41
42
43 def main():
44     logger.info("Running BGPVPN Tempest test case...")
45     start_time = time.time()
46
47     cmd = 'cd ' + BGPVPN_REPO + ';pip install --no-deps -e .'
48     ft_utils.execute_command(cmd, logger, exit_on_error=False)
49
50     src_tempest_dir = ft_utils.get_deployment_dir(logger)
51     if not src_tempest_dir:
52         logger.error("Rally deployment not found.")
53         exit(-1)
54
55     src_tempest_conf = src_tempest_dir + '/tempest.conf'
56     dst_tempest_conf = src_tempest_dir + '/etc/tempest.conf'
57
58     config = ConfigParser.RawConfigParser()
59     config.read(src_tempest_conf)
60     config.set('service_available', 'bgpvpn', 'True')
61     with open(dst_tempest_conf, 'wb') as config_file:
62         config.write(config_file)
63
64     cmd_line = (src_tempest_dir +
65                 '/run_tempest.sh -t -N -- networking_bgpvpn_tempest;'
66                 'rm -rf ' + dst_tempest_conf)
67     cmd = os.popen(cmd_line)
68     output = cmd.read()
69     # Results parsing
70     error_logs = ""
71     duration = 0
72     tests = 0
73     failed = 0
74     try:
75         # Look For errors
76         error_logs = ""
77         for match in re.findall('(.*?)[. ]*FAILED', output):
78             error_logs += match
79         # look for duration
80         m = re.search('tests in(.*)sec', output)
81         duration = m.group(1)
82         # Look for tests run
83         m = re.search('Ran:(.*)tests', output)
84         tests = m.group(1)
85         # Look for tests failed
86         m = re.search('Failed:(.*)', output)
87         failed = m.group(1)
88     except:
89         logger.error("Impossible to parse the result file")
90
91     # Generate json results for DB
92     json_results = {"duration": float(duration),
93                     "tests": int(tests),
94                     "failures": int(failed),
95                     "errors": error_logs}
96
97     logger.info("Results: " + str(json_results))
98     criteria = "FAIL"
99     # criteria = success rate = 100% (i.e all tests passed)
100     # TODO use criteria defined in config file
101     criteria_run = int(tests)
102     if not failed:
103         criteria_failed = 0
104     else:
105         criteria_failed = int(failed)
106
107     if criteria_run > 0 and criteria_failed < 1:
108         criteria = "PASS"
109
110     # Push results in payload of testcase
111     if args.report:
112         logger.debug("Push bgpvpn results into DB")
113         stop_time = time.time()
114         ft_utils.push_results_to_db("sdnvpn",
115                                     "bgpvpn_api",
116                                     logger,
117                                     start_time,
118                                     stop_time,
119                                     criteria,
120                                     json_results)
121
122 if __name__ == '__main__':
123     main()