54f05da35faa5bed674eb686eca3eea93899e72e
[kai/samba-autobuild/.git] / python / samba / tests / netcmd.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009-2011
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 """Tests for samba.netcmd."""
19
20 from cStringIO import StringIO
21 from samba.netcmd import Command
22 from samba.netcmd.testparm import cmd_testparm
23 from samba.netcmd.main import cmd_sambatool
24 import samba.tests
25
26 class NetCmdTestCase(samba.tests.TestCase):
27
28     def run_netcmd(self, cmd_klass, args, retcode=0):
29         cmd = cmd_klass(outf=StringIO(), errf=StringIO())
30         try:
31             retval = cmd._run(cmd_klass.__name__, *args)
32         except Exception as e:
33             cmd.show_command_error(e)
34             retval = 1
35         self.assertEquals(retcode, retval)
36         return cmd.outf.getvalue(), cmd.errf.getvalue()
37
38     def iter_all_subcommands(self):
39         todo = []
40         todo.extend(cmd_sambatool.subcommands.items())
41         while todo:
42             (path, cmd) = todo.pop()
43             yield path, cmd
44             subcmds = getattr(cmd, "subcommands", {})
45             todo.extend([(path + " " + k, v) for (k, v) in
46                 subcmds.iteritems()])
47
48
49 class TestParmTests(NetCmdTestCase):
50
51     def test_no_client_ip(self):
52         out, err = self.run_netcmd(cmd_testparm, ["--client-name=foo"],
53             retcode=-1)
54         self.assertEquals("", out)
55         self.assertEquals(
56             "ERROR: Both a DNS name and an IP address are "
57             "required for the host access check\n", err)
58
59
60 class CommandTests(NetCmdTestCase):
61
62     def test_description(self):
63         class cmd_foo(Command):
64             """Mydescription"""
65         self.assertEquals("Mydescription", cmd_foo().short_description)
66
67     def test_name(self):
68         class cmd_foo(Command):
69             pass
70         self.assertEquals("foo", cmd_foo().name)
71
72     def test_synopsis_everywhere(self):
73         missing = []
74         for path, cmd in self.iter_all_subcommands():
75             if cmd.synopsis is None:
76                 missing.append(path)
77         if missing:
78             self.fail("The following commands do not have a synopsis set: %r" %
79                     missing)
80
81     def test_short_description_everywhere(self):
82         missing = []
83         for path, cmd in self.iter_all_subcommands():
84             if cmd.short_description is None:
85                 missing.append(path)
86         if not missing:
87             return
88         self.fail(
89             "The following commands do not have a short description set: %r" %
90                 missing)