python tests: fix format() strings for Python 2.6
[samba.git] / python / samba / tests / blackbox / check_output.py
1 # Copyright (C) Catalyst IT Ltd. 2017
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 """
17 Blackbox tests for blackboxtest check output methods.
18 """
19
20 import time
21 import signal
22 from samba.tests import BlackboxTestCase
23
24
25 class TimeoutHelper():
26     """
27     Timeout class using alarm signal.
28
29     Raise a Timeout exception if a function timeout.
30     Usage:
31
32         try:
33             with Timeout(3):
34                 foobar("Request 1")
35         except TimeoutHelper.Timeout:
36             print "Timeout"
37     """
38
39     class Timeout(Exception):
40         pass
41
42     def __init__(self, sec):
43         self.sec = sec
44
45     def __enter__(self):
46         signal.signal(signal.SIGALRM, self.raise_timeout)
47         signal.alarm(self.sec)
48
49     def __exit__(self, *args):
50         signal.alarm(0)    # disable alarm
51
52     def raise_timeout(self, *args):
53         raise TimeoutHelper.Timeout()
54
55
56 def _make_cmdline(data='$', repeat=(5 * 1024 * 1024), retcode=0):
57     """Build a command to call gen_output.py to generate large output"""
58     return 'gen_output.py --data {0} --repeat {1} --retcode {2}'.format(data,
59                                                                         repeat,
60                                                                         retcode)
61
62
63 class CheckOutputTests(BlackboxTestCase):
64     """
65     Blackbox tests for check_xxx methods.
66
67     The check_xxx methods in BlackboxTestCase will deadlock
68     on large output from command which caused by Popen.wait().
69
70     This is a test case to show the deadlock issue,
71     will fix in another commit.
72     """
73
74     def test_check_run_timeout(self):
75         """Call check_run with large output."""
76         try:
77             with TimeoutHelper(10):
78                 self.check_run(_make_cmdline())
79         except TimeoutHelper.Timeout:
80             self.fail(msg='Timeout!')
81
82     def test_check_exit_code_with_large_output_success(self):
83         try:
84             with TimeoutHelper(10):
85                 self.check_exit_code(_make_cmdline(retcode=0), 0)
86         except TimeoutHelper.Timeout:
87             self.fail(msg='Timeout!')
88
89     def test_check_exit_code_with_large_output_failure(self):
90         try:
91             with TimeoutHelper(10):
92                 self.check_exit_code(_make_cmdline(retcode=1), 1)
93         except TimeoutHelper.Timeout:
94             self.fail(msg='Timeout!')
95
96     def test_check_output_with_large_output(self):
97         data = '@'
98         repeat = 5 * 1024 * 1024  # 5M
99         expected = data * repeat
100         cmdline = _make_cmdline(data=data, repeat=repeat)
101
102         try:
103             with TimeoutHelper(10):
104                 actual = self.check_output(cmdline)
105                 # check_output will return bytes
106                 # convert expected to bytes for python 3
107                 self.assertEqual(actual, expected.encode('utf-8'))
108         except TimeoutHelper.Timeout:
109             self.fail(msg='Timeout!')