d7e41a838b40430fca74d92c0a8432cda2b954dd
[metze/samba/wip.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 {} --repeat {} --retcode {}'.format(data, repeat, retcode)
59
60
61 class CheckOutputTests(BlackboxTestCase):
62     """
63     Blackbox tests for check_xxx methods.
64
65     The check_xxx methods in BlackboxTestCase will deadlock
66     on large output from command which caused by Popen.wait().
67
68     This is a test case to show the deadlock issue,
69     will fix in another commit.
70     """
71
72     def test_check_run_timeout(self):
73         """Call check_run with large output."""
74         try:
75             with TimeoutHelper(10):
76                 self.check_run(_make_cmdline())
77         except TimeoutHelper.Timeout:
78             self.fail(msg='Timeout!')
79
80     def test_check_exit_code_with_large_output_success(self):
81         try:
82             with TimeoutHelper(10):
83                 self.check_exit_code(_make_cmdline(retcode=0), 0)
84         except TimeoutHelper.Timeout:
85             self.fail(msg='Timeout!')
86
87     def test_check_exit_code_with_large_output_failure(self):
88         try:
89             with TimeoutHelper(10):
90                 self.check_exit_code(_make_cmdline(retcode=1), 1)
91         except TimeoutHelper.Timeout:
92             self.fail(msg='Timeout!')
93
94     def test_check_output_with_large_output(self):
95         data = '@'
96         repeat = 5 * 1024 * 1024  # 5M
97         expected = data * repeat
98         cmdline = _make_cmdline(data=data, repeat=repeat)
99
100         try:
101             with TimeoutHelper(10):
102                 actual = self.check_output(cmdline)
103                 self.assertEqual(actual, expected)
104         except TimeoutHelper.Timeout:
105             self.fail(msg='Timeout!')