Make sdnvpn logging proper
[sdnvpn.git] / sdnvpn / test / functest / run_tempest.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2017 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.opnfv_tests.openstack.tempest.conf_utils as tempest_utils
17 from sdnvpn.lib import logutil
18
19 logger = logutil.getLogger('sdnvpn-tempest')
20
21
22 def main():
23     verifier_id = tempest_utils.get_verifier_id()
24     verifier_repo_dir = tempest_utils.get_verifier_repo_dir(
25         verifier_id)
26     deployment_id = tempest_utils.get_verifier_deployment_id()
27     src_tempest_dir = tempest_utils.get_verifier_deployment_dir(
28         verifier_id, deployment_id)
29
30     if not src_tempest_dir:
31         logger.error("Rally deployment not found.")
32         exit(-1)
33
34     tempest_utils.configure_verifier(src_tempest_dir)
35
36     src_tempest_conf = os.path.join(src_tempest_dir, 'tempest.conf')
37     bgpvpn_tempest_conf = os.path.join(src_tempest_dir, 'bgpvpn_tempest.conf')
38     bgpvpn_tempest_list = os.path.join(src_tempest_dir, 'tempest_list.txt')
39
40     if not os.path.isfile(src_tempest_conf):
41         logger.error("tempest.conf not found in %s." % src_tempest_conf)
42         exit(-1)
43     shutil.copy(src_tempest_conf, bgpvpn_tempest_conf)
44
45     logger.info("Copying tempest.conf to %s." % bgpvpn_tempest_conf)
46     config = ConfigParser.RawConfigParser()
47     config.read(bgpvpn_tempest_conf)
48     config.set('service_available', 'bgpvpn', 'True')
49     logger.debug("Updating %s with bgpvpn=True" % bgpvpn_tempest_conf)
50     with open(bgpvpn_tempest_conf, 'wb') as tempest_conf:
51         config.write(tempest_conf)
52
53     cmd = ("cd {0};"
54            "testr list-tests networking_bgpvpn_tempest > {1};"
55            "cd -;".format(verifier_repo_dir, bgpvpn_tempest_list))
56     logger.info("Generating bgpvpn tempest list: %s" % cmd)
57     os.popen(cmd)
58     # TODO: Though --config-file parameter is set during the tempest run,
59     # it looks for tempest.conf at /etc/tempest/ directory. so applying
60     # the following workaround. Will remove it when the root cause is found.
61     cmd = ("mkdir /etc/tempest;"
62            "cp {0} /etc/tempest/tempest.conf".format(bgpvpn_tempest_conf))
63     logger.info("Configuring default tempest conf file")
64     os.popen(cmd)
65
66     cmd_line = ("tempest run --config-file {0} -t --whitelist-file {1}"
67                 .format(bgpvpn_tempest_conf, bgpvpn_tempest_list))
68     logger.info("Executing: %s" % cmd_line)
69     cmd = os.popen(cmd_line)
70     output = cmd.read()
71     logger.debug(output)
72
73     # Results parsing
74     error_logs = ""
75     duration = 0
76     failed = 0
77     try:
78         # Look For errors
79         error_logs = ""
80         for match in re.findall('(.*?)[. ]*FAILED', output):
81             error_logs += match
82         # look for duration
83         m = re.search('tests in(.*)sec', output)
84         duration = m.group(1)
85         # Look for num tests run
86         m = re.search('Ran:(.*)tests', output)
87         num_tests = m.group(1)
88         # Look for tests failed
89         m = re.search('- Failed:(.*)', output)
90         failed = m.group(1)
91         # Look for name of the tests
92         testcases = re.findall("\{0\} (.*)", output)
93
94         results = {"duration": duration,
95                    "num_tests": num_tests, "failed": failed,
96                    "tests": testcases}
97         if int(failed) == 0:
98             status = "PASS"
99         else:
100             status = "FAIL"
101
102         return {"status": status, "details": results}
103     except Exception as e:
104         logger.error("Problem when parsing the results: %s", e)
105
106
107 if __name__ == '__main__':
108     main()