[Doctor test case] Redirect output to a log file
[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 time
18
19 import functest.utils.functest_logger as ft_logger
20 import functest.utils.functest_utils as functest_utils
21
22
23 parser = argparse.ArgumentParser()
24 parser.add_argument("-r", "--report",
25                     help="Create json result file",
26                     action="store_true")
27 args = parser.parse_args()
28
29 functest_yaml = functest_utils.get_functest_yaml()
30
31 dirs = functest_yaml.get('general').get('directories')
32 DOCTOR_REPO = dirs.get('dir_repo_doctor')
33 RESULTS_DIR = functest_utils.get_parameter_from_yaml(
34     'general.directories.dir_results')
35
36 logger = ft_logger.Logger("doctor").getLogger()
37
38
39 def main():
40     exit_code = -1
41     cmd = 'cd %s/tests && ./run.sh' % DOCTOR_REPO
42     log_file = RESULTS_DIR + "/doctor.log"
43
44     start_time = time.time()
45
46     ret = functest_utils.execute_command(cmd,
47                                          info=True,
48                                          exit_on_error=False,
49                                          output_file=log_file)
50
51     stop_time = time.time()
52     duration = round(stop_time - start_time, 1)
53     if ret == 0:
54         logger.info("Doctor test case OK")
55         test_status = 'OK'
56         exit_code = 0
57     else:
58         logger.info("Doctor test case FAILED")
59         test_status = 'NOK'
60
61     details = {
62         'timestart': start_time,
63         'duration': duration,
64         'status': test_status,
65     }
66     status = "FAIL"
67     if details['status'] == "OK":
68         status = "PASS"
69     functest_utils.logger_test_results("Doctor",
70                                        "doctor-notification",
71                                        status, details)
72     if args.report:
73         functest_utils.push_results_to_db("doctor",
74                                           "doctor-notification",
75                                           start_time,
76                                           stop_time,
77                                           status,
78                                           details)
79         logger.info("Doctor results pushed to DB")
80
81     exit(exit_code)
82
83 if __name__ == '__main__':
84     main()