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