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