Update Xtesting to 0.98
[functest.git] / features / doctor.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 # 0.1: This script boots the VM1 and allocates IP address from Nova
11 # Later, the VM2 boots then execute cloud-init to ping VM1.
12 # After successful ping, both the VMs are deleted.
13 # 0.2: measure test duration and publish results under json format
14 #
15 #
16 import argparse
17 import os
18 import time
19 import yaml
20
21 import functest.utils.functest_logger as ft_logger
22 import functest.utils.functest_utils as functest_utils
23
24 parser = argparse.ArgumentParser()
25 parser.add_argument("-r", "--report",
26                     help="Create json result file",
27                     action="store_true")
28 args = parser.parse_args()
29
30 with open(os.environ["CONFIG_FUNCTEST_YAML"]) as f:
31     functest_yaml = yaml.safe_load(f)
32
33 dirs = functest_yaml.get('general').get('directories')
34 FUNCTEST_REPO = dirs.get('dir_repo_functest')
35 DOCTOR_REPO = dirs.get('dir_repo_doctor')
36
37 logger = ft_logger.Logger("doctor").getLogger()
38
39
40 def main():
41     exit_code = -1
42     cmd = 'cd %s/tests && ./run.sh' % DOCTOR_REPO
43     start_time = time.time()
44
45     ret = functest_utils.execute_command(cmd, logger, info=True,
46                                          exit_on_error=False)
47
48     stop_time = time.time()
49     duration = round(stop_time - start_time, 1)
50     if ret == 0:
51         logger.info("doctor OK")
52         test_status = 'OK'
53         exit_code = 0
54     else:
55         logger.info("doctor FAILED")
56         test_status = 'NOK'
57
58     details = {
59         'timestart': start_time,
60         'duration': duration,
61         'status': test_status,
62     }
63     status = "FAIL"
64     if details['status'] == "OK":
65         status = "PASS"
66     functest_utils.logger_test_results(logger, "Doctor",
67                                        "doctor-notification",
68                                        status, details)
69     if args.report:
70         functest_utils.push_results_to_db("doctor",
71                                           "doctor-notification",
72                                           logger,
73                                           start_time,
74                                           stop_time,
75                                           status,
76                                           details)
77         logger.info("Doctor results pushed to DB")
78
79     exit(exit_code)
80
81 if __name__ == '__main__':
82     main()