Modify tests to require 100% subtest success, fix tempest
[sdnvpn.git] / sdnvpn / test / functest / tempest.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 #
11 import ConfigParser
12 import os
13 import re
14 import shutil
15
16 import functest.utils.functest_logger as ft_logger
17 import functest.utils.functest_utils as ft_utils
18
19 logger = ft_logger.Logger("sdnvpn-tempest").getLogger()
20
21
22 def main():
23     src_tempest_dir = ft_utils.get_deployment_dir()
24     if not src_tempest_dir:
25         logger.error("Rally deployment not found.")
26         exit(-1)
27
28     src_tempest_conf = src_tempest_dir + '/tempest.conf'
29     bgpvpn_tempest_conf = src_tempest_dir + '/bgpvpn_tempest.conf'
30
31     if not os.path.isfile(src_tempest_conf):
32         logger.error("tempest.conf not found in %s." % src_tempest_conf)
33         exit(-1)
34     shutil.copy(src_tempest_conf, bgpvpn_tempest_conf)
35
36     logger.info("Copying tempest.conf to %s." % bgpvpn_tempest_conf)
37     config = ConfigParser.RawConfigParser()
38     config.read(bgpvpn_tempest_conf)
39     config.set('service_available', 'bgpvpn', 'True')
40     logger.debug("Updating %s with bgpvpn=True" % bgpvpn_tempest_conf)
41     with open(bgpvpn_tempest_conf, 'wb') as tempest_conf:
42         config.write(tempest_conf)
43
44     cmd_line = (src_tempest_dir +
45                 "/run_tempest.sh -C %s -t -N -- "
46                 "networking_bgpvpn_tempest" % bgpvpn_tempest_conf)
47     logger.info("Executing: %s" % cmd_line)
48     cmd = os.popen(cmd_line)
49     output = cmd.read()
50     logger.debug(output)
51     # Results parsing
52     error_logs = ""
53     duration = 0
54     failed = 0
55     try:
56         # Look For errors
57         error_logs = ""
58         for match in re.findall('(.*?)[. ]*FAILED', output):
59             error_logs += match
60         # look for duration
61         m = re.search('tests in(.*)sec', output)
62         duration = m.group(1)
63         # Look for num tests run
64         m = re.search('Ran:(.*)tests', output)
65         num_tests = m.group(1)
66         # Look for tests failed
67         m = re.search('Failed:(.*)', output)
68         failed = m.group(1)
69         # Look for name of the tests
70         testcases = re.findall("\{0\} (.*)", output)
71
72         results = {"duration": duration,
73                    "num_tests": num_tests, "failed": failed,
74                    "tests": testcases}
75         if int(failed) == 0:
76             status = "PASS"
77         else:
78             status = "FAILED"
79
80         return {"status": status, "details": results}
81     except:
82         logger.error("Problem when parsing the results.")
83
84
85 if __name__ == '__main__':
86     main()