68045fa35ce340a28acc8fe6b0870feea3b3c347
[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     deployment_id = tempest_utils.get_verifier_deployment_id()
25     src_tempest_dir = tempest_utils.get_verifier_deployment_dir(
26         verifier_id, deployment_id)
27
28     if not src_tempest_dir:
29         logger.error("Rally deployment not found.")
30         exit(-1)
31
32     tempest_utils.configure_verifier(src_tempest_dir)
33
34     src_tempest_conf = os.path.join(src_tempest_dir, 'tempest.conf')
35     bgpvpn_tempest_conf = os.path.join(src_tempest_dir, 'bgpvpn_tempest.conf')
36
37     if not os.path.isfile(src_tempest_conf):
38         logger.error("tempest.conf not found in %s." % src_tempest_conf)
39         exit(-1)
40     shutil.copy(src_tempest_conf, bgpvpn_tempest_conf)
41
42     logger.info("Copying tempest.conf to %s." % bgpvpn_tempest_conf)
43     config = ConfigParser.RawConfigParser()
44     config.read(bgpvpn_tempest_conf)
45     config.set('service_available', 'bgpvpn', 'True')
46     logger.debug("Updating %s with bgpvpn=True" % bgpvpn_tempest_conf)
47     with open(bgpvpn_tempest_conf, 'wb') as tempest_conf:
48         config.write(tempest_conf)
49
50     # TODO: Though --config-file parameter is set during the tempest run,
51     # it looks for tempest.conf at /etc/tempest/ directory. so applying
52     # the following workaround. Will remove it when the root cause is found.
53     cmd = ("mkdir /etc/tempest;"
54            "cp {0} /etc/tempest/tempest.conf".format(bgpvpn_tempest_conf))
55     logger.info("Configuring default tempest conf file")
56     os.popen(cmd)
57
58     cmd_line = "tempest run -t --regex networking_bgpvpn_tempest " \
59                "--config-file /etc/tempest/tempest.conf"
60     logger.info("Executing: %s" % cmd_line)
61     cmd = os.popen(cmd_line)
62     output = cmd.read()
63     logger.debug(output)
64
65     # Results parsing
66     error_logs = ""
67     duration = 0
68     failed = 0
69     try:
70         # Look For errors
71         error_logs = ""
72         for match in re.findall('(.*?)[. ]*FAILED', output):
73             error_logs += match
74         # look for duration
75         m = re.search('tests in(.*)sec', output)
76         duration = m.group(1)
77         # Look for num tests run
78         m = re.search('Ran:(.*)tests', output)
79         num_tests = m.group(1)
80         # Look for tests failed
81         m = re.search('- Failed:(.*)', output)
82         failed = m.group(1)
83         # Look for name of the tests
84         testcases = re.findall("\{0\} (.*)", output)
85
86         results = {"duration": duration,
87                    "num_tests": num_tests, "failed": failed,
88                    "tests": testcases}
89         if int(failed) == 0:
90             status = "PASS"
91         else:
92             status = "FAIL"
93
94         return {"status": status, "details": results}
95     except Exception as e:
96         logger.error("Problem when parsing the results: %s", e)
97
98
99 if __name__ == '__main__':
100     main()