3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009-2011
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.
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.
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/>.
20 """Tests for samba.netcmd."""
22 from cStringIO import StringIO
23 from samba.netcmd import Command
24 from samba.netcmd.testparm import cmd_testparm
25 from samba.netcmd.main import cmd_sambatool
28 class NetCmdTestCase(samba.tests.TestCase):
30 def run_netcmd(self, cmd_klass, args, retcode=0):
31 cmd = cmd_klass(outf=StringIO(), errf=StringIO())
33 retval = cmd._run(cmd_klass.__name__, *args)
35 cmd.show_command_error(e)
37 self.assertEquals(retcode, retval)
38 return cmd.outf.getvalue(), cmd.errf.getvalue()
40 def iter_all_subcommands(self):
42 todo.extend(cmd_sambatool.subcommands.items())
44 (path, cmd) = todo.pop()
46 subcmds = getattr(cmd, "subcommands", {})
47 todo.extend([(path + " " + k, v) for (k, v) in
51 class TestParmTests(NetCmdTestCase):
53 def test_no_client_ip(self):
54 out, err = self.run_netcmd(cmd_testparm, ["--client-name=foo"],
56 self.assertEquals("", out)
58 "ERROR: Both a DNS name and an IP address are "
59 "required for the host access check\n", err)
62 class CommandTests(NetCmdTestCase):
64 def test_description(self):
65 class cmd_foo(Command):
67 self.assertEquals("Mydescription", cmd_foo().short_description)
70 class cmd_foo(Command):
72 self.assertEquals("foo", cmd_foo().name)
74 def test_synopsis_everywhere(self):
76 for path, cmd in self.iter_all_subcommands():
77 if cmd.synopsis is None:
80 self.fail("The following commands do not have a synopsis set: %r" %
83 def test_short_description_everywhere(self):
85 for path, cmd in self.iter_all_subcommands():
86 if cmd.short_description is None:
91 "The following commands do not have a short description set: %r" %