Update logger via logging.getLogger()
[sdnvpn.git] / sdnvpn / test / functest / tempest.py
1 #!/usr/bin/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_repo_dir = tempest_utils.get_verifier_repo_dir(None)
24     src_tempest_dir = tempest_utils.get_verifier_deployment_dir(None, None)
25
26     if not src_tempest_dir:
27         logger.error("Rally deployment not found.")
28         exit(-1)
29
30     tempest_utils.configure_verifier(src_tempest_dir)
31
32     src_tempest_conf = os.path.join(src_tempest_dir, 'tempest.conf')
33     bgpvpn_tempest_conf = src_tempest_dir + '/bgpvpn_tempest.conf'
34
35     if not os.path.isfile(src_tempest_conf):
36         logger.error("tempest.conf not found in %s." % src_tempest_conf)
37         exit(-1)
38     shutil.copy(src_tempest_conf, bgpvpn_tempest_conf)
39
40     logger.info("Copying tempest.conf to %s." % bgpvpn_tempest_conf)
41     config = ConfigParser.RawConfigParser()
42     config.read(bgpvpn_tempest_conf)
43     config.set('service_available', 'bgpvpn', 'True')
44     logger.debug("Updating %s with bgpvpn=True" % bgpvpn_tempest_conf)
45     with open(bgpvpn_tempest_conf, 'wb') as tempest_conf:
46         config.write(tempest_conf)
47
48     cmd_line = (verifier_repo_dir +
49                 "/run_tempest.sh -C %s -t -N -- "
50                 "networking_bgpvpn_tempest" % bgpvpn_tempest_conf)
51     logger.info("Executing: %s" % cmd_line)
52     cmd = os.popen(cmd_line)
53     output = cmd.read()
54     logger.debug(output)
55
56     # Results parsing
57     error_logs = ""
58     duration = 0
59     failed = 0
60     try:
61         # Look For errors
62         error_logs = ""
63         for match in re.findall('(.*?)[. ]*FAILED', output):
64             error_logs += match
65         # look for duration
66         m = re.search('tests in(.*)sec', output)
67         duration = m.group(1)
68         # Look for num tests run
69         m = re.search('Ran:(.*)tests', output)
70         num_tests = m.group(1)
71         # Look for tests failed
72         m = re.search('Failed:(.*)', output)
73         failed = m.group(1)
74         # Look for name of the tests
75         testcases = re.findall("\{0\} (.*)", output)
76
77         results = {"duration": duration,
78                    "num_tests": num_tests, "failed": failed,
79                    "tests": testcases}
80         if int(failed) == 0:
81             status = "PASS"
82         else:
83             status = "FAILED"
84
85         return {"status": status, "details": results}
86     except:
87         logger.error("Problem when parsing the results.")
88
89
90 if __name__ == '__main__':
91     logging.basicConfig(level=logging.INFO)
92     main()