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 {} --repeat {} --retcode {}'.format(data, repeat, retcode)
61 class CheckOutputTests(BlackboxTestCase):
63 Blackbox tests for check_xxx methods.
65 The check_xxx methods in BlackboxTestCase will deadlock
66 on large output from command which caused by Popen.wait().
68 This is a test case to show the deadlock issue,
69 will fix in another commit.
72 def test_check_run_timeout(self):
73 """Call check_run with large output."""
75 with TimeoutHelper(10):
76 self.check_run(_make_cmdline())
77 except TimeoutHelper.Timeout:
78 self.fail(msg='Timeout!')
80 def test_check_exit_code_with_large_output_success(self):
82 with TimeoutHelper(10):
83 self.check_exit_code(_make_cmdline(retcode=0), 0)
84 except TimeoutHelper.Timeout:
85 self.fail(msg='Timeout!')
87 def test_check_exit_code_with_large_output_failure(self):
89 with TimeoutHelper(10):
90 self.check_exit_code(_make_cmdline(retcode=1), 1)
91 except TimeoutHelper.Timeout:
92 self.fail(msg='Timeout!')
94 def test_check_output_with_large_output(self):
96 repeat = 5 * 1024 * 1024 # 5M
97 expected = data * repeat
98 cmdline = _make_cmdline(data=data, repeat=repeat)
101 with TimeoutHelper(10):
102 actual = self.check_output(cmdline)
103 # check_output will return bytes
104 # convert expected to bytes for python 3
105 self.assertEqual(actual, expected.encode('utf-8'))
106 except TimeoutHelper.Timeout:
107 self.fail(msg='Timeout!')