1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009-2011
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.
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.
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/>.
18 """Tests for samba.netcmd."""
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
26 class NetCmdTestCase(samba.tests.TestCase):
28 def run_netcmd(self, cmd_klass, args, retcode=0):
29 cmd = cmd_klass(outf=StringIO(), errf=StringIO())
31 retval = cmd._run(cmd_klass.__name__, *args)
32 except Exception as e:
33 cmd.show_command_error(e)
35 self.assertEquals(retcode, retval)
36 return cmd.outf.getvalue(), cmd.errf.getvalue()
38 def iter_all_subcommands(self):
39 todo = list(cmd_sambatool.subcommands.items())
41 (path, cmd) = todo.pop()
43 subcmds = getattr(cmd, "subcommands", {})
44 todo.extend([(path + " " + k, v) for (k, v) in
48 class TestParmTests(NetCmdTestCase):
50 def test_no_client_ip(self):
51 out, err = self.run_netcmd(cmd_testparm, ["--client-name=foo"],
53 self.assertEquals("", out)
55 "ERROR: Both a DNS name and an IP address are "
56 "required for the host access check\n", err)
59 class CommandTests(NetCmdTestCase):
61 def test_description(self):
62 class cmd_foo(Command):
64 self.assertEquals("Mydescription", cmd_foo().short_description)
67 class cmd_foo(Command):
69 self.assertEquals("foo", cmd_foo().name)
71 def test_synopsis_everywhere(self):
73 for path, cmd in self.iter_all_subcommands():
74 if cmd.synopsis is None:
77 self.fail("The following commands do not have a synopsis set: %r" %
80 def test_short_description_everywhere(self):
82 for path, cmd in self.iter_all_subcommands():
83 if cmd.short_description is None:
88 "The following commands do not have a short description set: %r" %