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