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