Merge "Add Energy recording support"
[functest.git] / functest / tests / unit / ci / test_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 logging
9 import unittest
10 import urllib2
11
12 import mock
13
14 from functest.ci import generate_report as gen_report
15 from functest.tests.unit import test_utils
16 from functest.utils import functest_utils as ft_utils
17 from functest.utils.constants import CONST
18
19
20 class GenerateReportTesting(unittest.TestCase):
21
22     logging.disable(logging.CRITICAL)
23
24     def test_init(self):
25         test_array = gen_report.init()
26         self.assertEqual(test_array, [])
27
28     @mock.patch('functest.ci.generate_report.urllib2.urlopen',
29                 side_effect=urllib2.URLError('no host given'))
30     def test_get_results_from_db_fail(self, mock_method):
31         url = "%s?build_tag=%s" % (ft_utils.get_db_url(),
32                                    CONST.__getattribute__('BUILD_TAG'))
33         self.assertIsNone(gen_report.get_results_from_db())
34         mock_method.assert_called_once_with(url)
35
36     @mock.patch('functest.ci.generate_report.urllib2.urlopen',
37                 return_value={'results': []})
38     def test_get_results_from_db_success(self, mock_method):
39         url = "%s?build_tag=%s" % (ft_utils.get_db_url(),
40                                    CONST.__getattribute__('BUILD_TAG'))
41         self.assertEqual(gen_report.get_results_from_db(), None)
42         mock_method.assert_called_once_with(url)
43
44     def test_get_data(self):
45         self.assertIsInstance(gen_report.get_data({'result': ''}, ''), dict)
46
47     def test_print_line_with_ci_run(self):
48         CONST.__setattr__('IS_CI_RUN', True)
49         w1 = 'test_print_line'
50         test_str = ("| %s| %s| %s| %s| %s|\n"
51                     % (w1.ljust(gen_report.COL_1_LEN - 1),
52                        ''.ljust(gen_report.COL_2_LEN - 1),
53                        ''.ljust(gen_report.COL_3_LEN - 1),
54                        ''.ljust(gen_report.COL_4_LEN - 1),
55                        ''.ljust(gen_report.COL_5_LEN - 1)))
56         self.assertEqual(gen_report.print_line(w1), test_str)
57
58     def test_print_line_without_ci_run(self):
59         CONST.__setattr__('IS_CI_RUN', False)
60         w1 = 'test_print_line'
61         test_str = ("| %s| %s| %s| %s|\n"
62                     % (w1.ljust(gen_report.COL_1_LEN - 1),
63                        ''.ljust(gen_report.COL_2_LEN - 1),
64                        ''.ljust(gen_report.COL_3_LEN - 1),
65                        ''.ljust(gen_report.COL_4_LEN - 1)))
66         self.assertEqual(gen_report.print_line(w1), test_str)
67
68     def test_print_line_no_column_with_ci_run(self):
69         CONST.__setattr__('IS_CI_RUN', True)
70         TOTAL_LEN = gen_report.COL_1_LEN + gen_report.COL_2_LEN
71         TOTAL_LEN += gen_report.COL_3_LEN + gen_report.COL_4_LEN + 2
72         TOTAL_LEN += gen_report.COL_5_LEN + 1
73         test_str = ("| %s|\n" % 'test'.ljust(TOTAL_LEN))
74         self.assertEqual(gen_report.print_line_no_columns('test'), test_str)
75
76     def test_print_line_no_column_without_ci_run(self):
77         CONST.__setattr__('IS_CI_RUN', False)
78         TOTAL_LEN = gen_report.COL_1_LEN + gen_report.COL_2_LEN
79         TOTAL_LEN += gen_report.COL_3_LEN + gen_report.COL_4_LEN + 2
80         test_str = ("| %s|\n" % 'test'.ljust(TOTAL_LEN))
81         self.assertEqual(gen_report.print_line_no_columns('test'), test_str)
82
83     def test_print_separator_with_ci_run(self):
84         CONST.__setattr__('IS_CI_RUN', True)
85         test_str = ("+" + "=" * gen_report.COL_1_LEN +
86                     "+" + "=" * gen_report.COL_2_LEN +
87                     "+" + "=" * gen_report.COL_3_LEN +
88                     "+" + "=" * gen_report.COL_4_LEN +
89                     "+" + "=" * gen_report.COL_5_LEN)
90         test_str += '+\n'
91         self.assertEqual(gen_report.print_separator(), test_str)
92
93     def test_print_separator_without_ci_run(self):
94         CONST.__setattr__('IS_CI_RUN', False)
95         test_str = ("+" + "=" * gen_report.COL_1_LEN +
96                     "+" + "=" * gen_report.COL_2_LEN +
97                     "+" + "=" * gen_report.COL_3_LEN +
98                     "+" + "=" * gen_report.COL_4_LEN)
99         test_str += "+\n"
100         self.assertEqual(gen_report.print_separator(), test_str)
101
102     @mock.patch('functest.ci.generate_report.logger.info')
103     def test_main_with_ci_run(self, mock_method):
104         CONST.__setattr__('IS_CI_RUN', True)
105         gen_report.main()
106         mock_method.assert_called_once_with(test_utils.SubstrMatch('URL'))
107
108     @mock.patch('functest.ci.generate_report.logger.info')
109     def test_main_with_ci_loop(self, mock_method):
110         CONST.__setattr__('CI_LOOP', 'daily')
111         gen_report.main()
112         mock_method.assert_called_once_with(test_utils.SubstrMatch('CI LOOP'))
113
114     @mock.patch('functest.ci.generate_report.logger.info')
115     def test_main_with_scenario(self, mock_method):
116         CONST.__setattr__('DEPLOY_SCENARIO', 'test_scenario')
117         gen_report.main()
118         mock_method.assert_called_once_with(test_utils.SubstrMatch('SCENARIO'))
119
120     @mock.patch('functest.ci.generate_report.logger.info')
121     def test_main_with_build_tag(self, mock_method):
122         CONST.__setattr__('BUILD_TAG', 'test_build_tag')
123         gen_report.main()
124         mock_method.assert_called_once_with(test_utils.
125                                             SubstrMatch('BUILD TAG'))
126
127
128 if __name__ == "__main__":
129     unittest.main(verbosity=2)