1 # Copyright (C) Catalyst IT Ltd. 2017
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.
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.
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/>.
17 Blackbox tests for blackboxtest check output methods.
22 from samba.tests import BlackboxTestCase
25 class TimeoutHelper():
27 Timeout class using alarm signal.
29 Raise a Timeout exception if a function timeout.
35 except TimeoutHelper.Timeout:
39 class Timeout(Exception):
42 def __init__(self, sec):
46 signal.signal(signal.SIGALRM, self.raise_timeout)
47 signal.alarm(self.sec)
49 def __exit__(self, *args):
50 signal.alarm(0) # disable alarm
52 def raise_timeout(self, *args):
53 raise TimeoutHelper.Timeout()
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,
63 class CheckOutputTests(BlackboxTestCase):
65 Blackbox tests for check_xxx methods.
67 The check_xxx methods in BlackboxTestCase will deadlock
68 on large output from command which caused by Popen.wait().
70 This is a test case to show the deadlock issue,
71 will fix in another commit.
74 def test_check_run_timeout(self):
75 """Call check_run with large output."""
77 with TimeoutHelper(10):
78 self.check_run(_make_cmdline())
79 except TimeoutHelper.Timeout:
80 self.fail(msg='Timeout!')
82 def test_check_exit_code_with_large_output_success(self):
84 with TimeoutHelper(10):
85 self.check_exit_code(_make_cmdline(retcode=0), 0)
86 except TimeoutHelper.Timeout:
87 self.fail(msg='Timeout!')
89 def test_check_exit_code_with_large_output_failure(self):
91 with TimeoutHelper(10):
92 self.check_exit_code(_make_cmdline(retcode=1), 1)
93 except TimeoutHelper.Timeout:
94 self.fail(msg='Timeout!')
96 def test_check_output_with_large_output(self):
98 repeat = 5 * 1024 * 1024 # 5M
99 expected = data * repeat
100 cmdline = _make_cmdline(data=data, repeat=repeat)
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!')