Adapt some calls to functest_utils that don't require logger
[sdnvpn.git] / 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 import functest.utils.functest_logger as ft_logger
16 import functest.utils.functest_utils as ft_utils
17
18
19 logger = ft_logger.Logger("sdnvpn-tempest").getLogger()
20
21 REPO_PATH = os.environ['repos_dir'] + '/sdnvpn/'
22 config_file = REPO_PATH + 'test/functest/config.yaml'
23
24 SUCCESS_CRITERIA = ft_utils.get_parameter_from_yaml(
25     "testcases.testcase_1.succes_criteria", config_file)
26
27
28 def main():
29     src_tempest_dir = ft_utils.get_deployment_dir()
30     if not src_tempest_dir:
31         logger.error("Rally deployment not found.")
32         exit(-1)
33
34     src_tempest_conf = src_tempest_dir + '/tempest.conf'
35     bgpvpn_tempest_conf = 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 config_file:
48         config.write(config_file)
49
50     cmd_line = (src_tempest_dir +
51                 "/run_tempest.sh -C %s -t -N -- "
52                 "networking_bgpvpn_tempest" % bgpvpn_tempest_conf)
53     logger.info("Executing: %s" % cmd_line)
54     cmd = os.popen(cmd_line)
55     output = cmd.read()
56     logger.debug(output)
57     # Results parsing
58     error_logs = ""
59     duration = 0
60     failed = 0
61     try:
62         # Look For errors
63         error_logs = ""
64         for match in re.findall('(.*?)[. ]*FAILED', output):
65             error_logs += match
66         # look for duration
67         m = re.search('tests in(.*)sec', output)
68         duration = m.group(1)
69         # Look for num tests run
70         m = re.search('Ran:(.*)tests', output)
71         num_tests = m.group(1)
72         # Look for tests failed
73         m = re.search('Failed:(.*)', output)
74         failed = m.group(1)
75         # Look for name of the tests
76         testcases = re.findall("\{0\} (.*)", output)
77
78         results = {"duration": duration,
79                    "num_tests": num_tests, "failed": failed,
80                    "tests": testcases}
81         status = "PASS"
82         if 100 - (100 * int(failed) / int(num_tests)) < int(SUCCESS_CRITERIA):
83             status = "FAILED"
84         return {"status": status, "details": results}
85     except:
86         logger.error("Problem when parsing the results.")
87
88
89 if __name__ == '__main__':
90     main()