samba-tool test: ensure `samba-tool help` works
[samba.git] / python / samba / tests / samba_tool / help.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst IT Ltd 2017.
3 #
4 # Originally written by Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 import re
21 from samba.tests.samba_tool.base import SambaToolCmdTest
22 from samba.tests import BlackboxProcessError
23
24
25 class HelpTestCase(SambaToolCmdTest):
26     """Tests for samba-tool help and --help
27
28     We test for consistency and lack of crashes."""
29
30     def _find_sub_commands(self, args):
31         self.runcmd(*args)
32
33     def test_help_tree(self):
34         # we call actual subprocesses, because we are probing the
35         # actual help output where there is no sub-command. Don't copy
36         # this if you have an actual command: for that use
37         # self.runcmd() or self.runsubcmd().
38         known_commands = [[]]
39         failed_commands = []
40
41         for i in range(4):
42             new_commands = []
43             for c in known_commands:
44                 line = ' '.join(['samba-tool'] + c + ['--help'])
45                 try:
46                     output = self.check_output(line)
47                 except BlackboxProcessError as e:
48                     output = e.stdout
49                     failed_commands.append(c)
50
51                 tail = output.partition('Available subcommands:')[2]
52                 subcommands = re.findall(r'^\s*([\w-]+)\s+-', tail,
53                                          re.MULTILINE)
54                 for s in subcommands:
55                     new_commands.append(c + [s])
56
57                 # check that `samba-tool help X Y` == `samba-tool X Y --help`
58                 line = ' '.join(['samba-tool', 'help'] + c)
59                 try:
60                     output2 = self.check_output(line)
61                 except BlackboxProcessError as e:
62                     output2 = e.stdout
63                     failed_commands.append(c)
64
65                 self.assertEqual(output, output2)
66
67             if not new_commands:
68                 break
69
70             known_commands = new_commands
71
72         self.assertEqual(failed_commands, [])