Additional Unit Tests for core modules
[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 urllib2
12
13 import functest.utils.functest_utils as ft_utils
14 from functest.utils.constants import CONST
15
16 COL_1_LEN = 25
17 COL_2_LEN = 15
18 COL_3_LEN = 12
19 COL_4_LEN = 15
20 COL_5_LEN = 75
21
22 # If we run from CI (Jenkins) we will push the results to the DB
23 # and then we can print the url to the specific test result
24
25
26 logger = logging.getLogger(__name__)
27
28
29 def init(tiers_to_run=[]):
30     test_cases_arr = []
31     for tier in tiers_to_run:
32         for test in tier.get_tests():
33             test_cases_arr.append({'test_name': test.get_name(),
34                                    'tier_name': tier.get_name(),
35                                    'result': 'Not executed',
36                                    'duration': '0',
37                                    'url': ''})
38     return test_cases_arr
39
40
41 def get_results_from_db():
42     url = "%s?build_tag=%s" % (ft_utils.get_db_url(),
43                                CONST.BUILD_TAG)
44     logger.debug("Query to rest api: %s" % url)
45     try:
46         data = json.load(urllib2.urlopen(url))
47         return data['results']
48     except:
49         logger.error("Cannot read content from the url: %s" % url)
50         return None
51
52
53 def get_data(test, results):
54     test_result = test['result']
55     url = ''
56     for test_db in results:
57         if test['test_name'] in test_db['case_name']:
58             id = test_db['_id']
59             url = ft_utils.get_db_url() + '/' + id
60             test_result = test_db['criteria']
61
62     return {"url": url, "result": test_result}
63
64
65 def print_line(w1, w2='', w3='', w4='', w5=''):
66     str = ('| ' + w1.ljust(COL_1_LEN - 1) +
67            '| ' + w2.ljust(COL_2_LEN - 1) +
68            '| ' + w3.ljust(COL_3_LEN - 1) +
69            '| ' + w4.ljust(COL_4_LEN - 1))
70     if CONST.__getattribute__('IS_CI_RUN'):
71         str += ('| ' + w5.ljust(COL_5_LEN - 1))
72     str += '|\n'
73     return str
74
75
76 def print_line_no_columns(str):
77     TOTAL_LEN = COL_1_LEN + COL_2_LEN + COL_3_LEN + COL_4_LEN + 2
78     if CONST.__getattribute__('IS_CI_RUN'):
79         TOTAL_LEN += COL_5_LEN + 1
80     return ('| ' + str.ljust(TOTAL_LEN) + "|\n")
81
82
83 def print_separator(char="=", delimiter="+"):
84     str = ("+" + char * COL_1_LEN +
85            delimiter + char * COL_2_LEN +
86            delimiter + char * COL_3_LEN +
87            delimiter + char * COL_4_LEN)
88     if CONST.__getattribute__('IS_CI_RUN'):
89         str += (delimiter + char * COL_5_LEN)
90     str += '+\n'
91     return str
92
93
94 def main(args=[]):
95     executed_test_cases = args
96
97     if CONST.__getattribute__('IS_CI_RUN'):
98         results = get_results_from_db()
99         if results is not None:
100             for test in executed_test_cases:
101                 data = get_data(test, results)
102                 test.update({"url": data['url'],
103                              "result": data['result']})
104
105     TOTAL_LEN = COL_1_LEN + COL_2_LEN + COL_3_LEN + COL_4_LEN
106     if CONST.__getattribute__('IS_CI_RUN'):
107         TOTAL_LEN += COL_5_LEN
108     MID = TOTAL_LEN / 2
109
110     if CONST.__getattribute__('BUILD_TAG') is not None:
111         if re.search("daily", CONST.__getattribute__('BUILD_TAG')) is not None:
112             CONST.__setattr__('CI_LOOP', 'daily')
113         else:
114             CONST.__setattr__('CI_LOOP', 'weekly')
115
116     str = ''
117     str += print_separator('=', delimiter="=")
118     str += print_line_no_columns(' ' * (MID - 8) + 'FUNCTEST REPORT')
119     str += print_separator('=', delimiter="=")
120     str += print_line_no_columns(' ')
121     str += print_line_no_columns(" Deployment description:")
122     str += print_line_no_columns("   INSTALLER: %s"
123                                  % CONST.__getattribute__('INSTALLER_TYPE'))
124     if CONST.__getattribute__('DEPLOY_SCENARIO') is not None:
125         str += print_line_no_columns("   SCENARIO:  %s"
126                                      % CONST.__getattribute__(
127                                          'DEPLOY_SCENARIO'))
128     if CONST.__getattribute__('BUILD_TAG') is not None:
129         str += print_line_no_columns("   BUILD TAG: %s"
130                                      % CONST.__getattribute__('BUILD_TAG'))
131     if CONST.__getattribute__('CI_LOOP') is not None:
132         str += print_line_no_columns("   CI LOOP:   %s"
133                                      % CONST.__getattribute__('CI_LOOP'))
134     str += print_line_no_columns(' ')
135     str += print_separator('=')
136     if CONST.__getattribute__('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)