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