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