Merge "Delete functest.utils.functest_logger"
[functest.git] / functest / ci / generate_report.py
1 #!/usr/bin/env python
2 #
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 import json
9 import logging
10 import re
11 import sys
12 import urllib2
13
14 import functest.utils.functest_utils as ft_utils
15 from functest.utils.constants import CONST
16
17 COL_1_LEN = 25
18 COL_2_LEN = 15
19 COL_3_LEN = 12
20 COL_4_LEN = 15
21 COL_5_LEN = 75
22
23 # If we run from CI (Jenkins) we will push the results to the DB
24 # and then we can print the url to the specific test result
25
26
27 logger = logging.getLogger(__name__)
28
29
30 def init(tiers_to_run=[]):
31     test_cases_arr = []
32     for tier in tiers_to_run:
33         for test in tier.get_tests():
34             test_cases_arr.append({'test_name': test.get_name(),
35                                    'tier_name': tier.get_name(),
36                                    'result': 'Not executed',
37                                    'duration': '0',
38                                    'url': ''})
39     return test_cases_arr
40
41
42 def get_results_from_db():
43     url = "%s/results?build_tag=%s" % (ft_utils.get_db_url(),
44                                        CONST.BUILD_TAG)
45     logger.debug("Query to rest api: %s" % url)
46     try:
47         data = json.load(urllib2.urlopen(url))
48         return data['results']
49     except:
50         logger.error("Cannot read content from the url: %s" % url)
51         return None
52
53
54 def get_data(test, results):
55     test_result = test['result']
56     url = ''
57     for test_db in results:
58         if test['test_name'] in test_db['case_name']:
59             id = test_db['_id']
60             url = ft_utils.get_db_url() + '/results/' + id
61             test_result = test_db['criteria']
62
63     return {"url": url, "result": test_result}
64
65
66 def print_line(w1, w2='', w3='', w4='', w5=''):
67     str = ('| ' + w1.ljust(COL_1_LEN - 1) +
68            '| ' + w2.ljust(COL_2_LEN - 1) +
69            '| ' + w3.ljust(COL_3_LEN - 1) +
70            '| ' + w4.ljust(COL_4_LEN - 1))
71     if CONST.IS_CI_RUN:
72         str += ('| ' + w5.ljust(COL_5_LEN - 1))
73     str += '|\n'
74     return str
75
76
77 def print_line_no_columns(str):
78     TOTAL_LEN = COL_1_LEN + COL_2_LEN + COL_3_LEN + COL_4_LEN + 2
79     if CONST.IS_CI_RUN:
80         TOTAL_LEN += COL_5_LEN + 1
81     return ('| ' + str.ljust(TOTAL_LEN) + "|\n")
82
83
84 def print_separator(char="=", delimiter="+"):
85     str = ("+" + char * COL_1_LEN +
86            delimiter + char * COL_2_LEN +
87            delimiter + char * COL_3_LEN +
88            delimiter + char * COL_4_LEN)
89     if CONST.IS_CI_RUN:
90         str += (delimiter + char * COL_5_LEN)
91     str += '+\n'
92     return str
93
94
95 def main(args=[]):
96     executed_test_cases = args
97
98     if CONST.IS_CI_RUN:
99         results = get_results_from_db()
100         if results is not None:
101             for test in executed_test_cases:
102                 data = get_data(test, results)
103                 test.update({"url": data['url'],
104                              "result": data['result']})
105
106     TOTAL_LEN = COL_1_LEN + COL_2_LEN + COL_3_LEN + COL_4_LEN
107     if CONST.IS_CI_RUN:
108         TOTAL_LEN += COL_5_LEN
109     MID = TOTAL_LEN / 2
110
111     if CONST.BUILD_TAG is not None:
112         if re.search("daily", CONST.BUILD_TAG) is not None:
113             CONST.CI_LOOP = "daily"
114         else:
115             CONST.CI_LOOP = "weekly"
116
117     str = ''
118     str += print_separator('=', delimiter="=")
119     str += print_line_no_columns(' ' * (MID - 8) + 'FUNCTEST REPORT')
120     str += print_separator('=', delimiter="=")
121     str += print_line_no_columns(' ')
122     str += print_line_no_columns(" Deployment description:")
123     str += print_line_no_columns("   INSTALLER: %s"
124                                  % CONST.INSTALLER_TYPE)
125     if CONST.DEPLOY_SCENARIO is not None:
126         str += print_line_no_columns("   SCENARIO:  %s"
127                                      % CONST.DEPLOY_SCENARIO)
128     if CONST.BUILD_TAG is not None:
129         str += print_line_no_columns("   BUILD TAG: %s"
130                                      % CONST.BUILD_TAG)
131     if CONST.CI_LOOP is not None:
132         str += print_line_no_columns("   CI LOOP:   %s"
133                                      % CONST.CI_LOOP)
134     str += print_line_no_columns(' ')
135     str += print_separator('=')
136     if CONST.IS_CI_RUN:
137         str += print_line('TEST CASE', 'TIER', 'DURATION', 'RESULT', 'URL')
138     else:
139         str += print_line('TEST CASE', 'TIER', 'DURATION', 'RESULT')
140     str += print_separator('=')
141     for test in executed_test_cases:
142         str += print_line(test['test_name'],
143                           test['tier_name'],
144                           test['duration'],
145                           test['result'],
146                           test['url'])
147         str += print_separator('-')
148
149     logger.info("\n\n\n%s" % str)
150
151
152 if __name__ == '__main__':
153     logging.basicConfig()
154     main(sys.argv[1:])