Adapting testcase to Boron SR1
[functest.git] / testcases / 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
20 import functest.utils.functest_logger as ft_logger
21 import functest.utils.functest_utils as functest_utils
22
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 functest_yaml = functest_utils.get_functest_yaml()
31
32 DOCTOR_REPO = \
33     functest_utils.get_functest_config('general.directories.dir_repo_doctor')
34 RESULTS_DIR = \
35     functest_utils.get_functest_config('general.directories.dir_results')
36
37 logger = ft_logger.Logger("doctor").getLogger()
38
39
40 def main():
41     exit_code = -1
42
43     # if the image name is explicitly set for the doctor suite, set it as
44     # enviroment variable
45     if 'doctor' in functest_yaml and 'image_name' in functest_yaml['doctor']:
46         os.environ["IMAGE_NAME"] = functest_yaml['doctor']['image_name']
47
48     cmd = 'cd %s/tests && ./run.sh' % DOCTOR_REPO
49     log_file = RESULTS_DIR + "/doctor.log"
50
51     start_time = time.time()
52
53     ret = functest_utils.execute_command(cmd,
54                                          info=True,
55                                          exit_on_error=False,
56                                          output_file=log_file)
57
58     stop_time = time.time()
59     duration = round(stop_time - start_time, 1)
60     if ret == 0:
61         logger.info("Doctor test case OK")
62         test_status = 'OK'
63         exit_code = 0
64     else:
65         logger.info("Doctor test case FAILED")
66         test_status = 'NOK'
67
68     details = {
69         'timestart': start_time,
70         'duration': duration,
71         'status': test_status,
72     }
73     status = "FAIL"
74     if details['status'] == "OK":
75         status = "PASS"
76     functest_utils.logger_test_results("Doctor",
77                                        "doctor-notification",
78                                        status, details)
79     if args.report:
80         functest_utils.push_results_to_db("doctor",
81                                           "doctor-notification",
82                                           start_time,
83                                           stop_time,
84                                           status,
85                                           details)
86         logger.info("Doctor results pushed to DB")
87
88     exit(exit_code)
89
90 if __name__ == '__main__':
91     main()