Merge "Add jenkins build tag for result api"
[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
17 import logging
18 import os
19 import sys
20 import time
21 import yaml
22
23
24 with open('/home/opnfv/functest/conf/config_functest.yaml') as f:
25     functest_yaml = yaml.safe_load(f)
26
27 dirs = functest_yaml.get('general').get('directories')
28 FUNCTEST_REPO = dirs.get('dir_repo_functest')
29 DOCTOR_REPO = dirs.get('dir_repo_doctor')
30 TEST_DB_URL = functest_yaml.get('results').get('test_db_url')
31
32 sys.path.append('%s/testcases' % FUNCTEST_REPO)
33 import functest_utils
34
35 logger = logging.getLogger('doctor')
36 logger.setLevel(logging.DEBUG)
37 ch = logging.StreamHandler()
38 ch.setLevel(logging.DEBUG)
39 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
40 ch.setFormatter(formatter)
41 logger.addHandler(ch)
42
43
44 def main():
45     cmd = 'cd %s/tests && ./run.sh' % DOCTOR_REPO
46     start_time_ts = time.time()
47
48     ret = functest_utils.execute_command(cmd, logger, exit_on_error=False)
49
50     end_time_ts = time.time()
51     duration = round(end_time_ts - start_time_ts, 1)
52     if ret:
53         logger.info("doctor OK")
54         test_status = 'OK'
55     else:
56         logger.info("doctor FAILED")
57         test_status = 'NOK'
58
59     details = {
60         'timestart': start_time_ts,
61         'duration': duration,
62         'status': test_status,
63     }
64     pod_name = functest_utils.get_pod_name(logger)
65     scenario = functest_utils.get_scenario(logger)
66     build_tag = functest_utils.get_build_tag(logger)
67     logger.info("Pushing result: TEST_DB_URL=%(db)s pod_name=%(pod)s "
68                 "scenario=%(s)s details=%(d)s" % {
69                     'db': TEST_DB_URL,
70                     'pod': pod_name,
71                     's': scenario,
72                     'b': build_tag,
73                     'd': details,
74                 })
75     functest_utils.push_results_to_db(TEST_DB_URL,
76                                       'doctor','doctor-notification',
77                                       logger, pod_name, scenario,
78                                       build_tag, details)
79
80
81 if __name__ == '__main__':
82     main()