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