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
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
20 class GenerateReportTesting(unittest.TestCase):
22 logging.disable(logging.CRITICAL)
25 test_array = gen_report.init()
26 self.assertEqual(test_array, [])
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/results?build_tag=%s" % (ft_utils.get_db_url(),
33 self.assertIsNone(gen_report.get_results_from_db())
34 mock_method.assert_called_once_with(url)
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/results?build_tag=%s" % (ft_utils.get_db_url(),
41 self.assertEqual(gen_report.get_results_from_db(), None)
42 mock_method.assert_called_once_with(url)
44 def test_get_data(self):
45 self.assertIsInstance(gen_report.get_data({'result': ''}, ''), dict)
47 def test_print_line_with_ci_run(self):
48 CONST.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)
58 def test_print_line_without_ci_run(self):
59 CONST.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)
68 def test_print_line_no_column_with_ci_run(self):
69 CONST.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)
76 def test_print_line_no_column_without_ci_run(self):
77 CONST.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)
83 def test_print_separator_with_ci_run(self):
84 CONST.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)
91 self.assertEqual(gen_report.print_separator(), test_str)
93 def test_print_separator_without_ci_run(self):
94 CONST.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)
100 self.assertEqual(gen_report.print_separator(), test_str)
102 @mock.patch('functest.ci.generate_report.logger.info')
103 def test_main_with_ci_run(self, mock_method):
104 CONST.IS_CI_RUN = True
106 mock_method.assert_called_once_with(test_utils.SubstrMatch('URL'))
108 @mock.patch('functest.ci.generate_report.logger.info')
109 def test_main_with_ci_loop(self, mock_method):
110 CONST.CI_LOOP = 'daily'
112 mock_method.assert_called_once_with(test_utils.SubstrMatch('CI LOOP'))
114 @mock.patch('functest.ci.generate_report.logger.info')
115 def test_main_with_scenario(self, mock_method):
116 CONST.DEPLOY_SCENARIO = 'test_scenario'
118 mock_method.assert_called_once_with(test_utils.SubstrMatch('SCENARIO'))
120 @mock.patch('functest.ci.generate_report.logger.info')
121 def test_main_with_build_tag(self, mock_method):
122 CONST.BUILD_TAG = 'test_build_tag'
124 mock_method.assert_called_once_with(test_utils.
125 SubstrMatch('BUILD TAG'))
128 if __name__ == "__main__":
129 unittest.main(verbosity=2)