s4:python tests __init__.py - do not depend on "subprocess.check_call()"
[samba.git] / source4 / scripting / python / samba / tests / netcmd.py
1 #!/usr/bin/env python
2
3 # Unix SMB/CIFS implementation.
4 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2009-2011
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 """Tests for samba.netcmd."""
21
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
26 import samba.tests
27
28 class NetCmdTestCase(samba.tests.TestCase):
29
30     def run_netcmd(self, cmd_klass, args, retcode=0):
31         cmd = cmd_klass(outf=StringIO(), errf=StringIO())
32         try:
33             retval = cmd._run(cmd_klass.__name__, *args)
34         except Exception, e:
35             cmd.show_command_error(e)
36             retval = 1
37         self.assertEquals(retcode, retval)
38         return cmd.outf.getvalue(), cmd.errf.getvalue()
39
40     def iter_all_subcommands(self):
41         todo = []
42         todo.extend(cmd_sambatool.subcommands.items())
43         while todo:
44             (path, cmd) = todo.pop()
45             yield path, cmd
46             subcmds = getattr(cmd, "subcommands", {})
47             todo.extend([(path + " " + k, v) for (k, v) in
48                 subcmds.iteritems()])
49
50
51 class TestParmTests(NetCmdTestCase):
52
53     def test_no_client_ip(self):
54         out, err = self.run_netcmd(cmd_testparm, ["--client-name=foo"],
55             retcode=-1)
56         self.assertEquals("", out)
57         self.assertEquals(
58             "ERROR: Both a DNS name and an IP address are "
59             "required for the host access check\n", err)
60
61
62 class CommandTests(NetCmdTestCase):
63
64     def test_description(self):
65         class cmd_foo(Command):
66             """Mydescription"""
67         self.assertEquals("Mydescription", cmd_foo().short_description)
68
69     def test_name(self):
70         class cmd_foo(Command):
71             pass
72         self.assertEquals("foo", cmd_foo().name)
73
74     def test_synopsis_everywhere(self):
75         missing = []
76         for path, cmd in self.iter_all_subcommands():
77             if cmd.synopsis is None:
78                 missing.append(path)
79         if missing:
80             self.fail("The following commands do not have a synopsis set: %r" %
81                     missing)
82
83     def test_short_description_everywhere(self):
84         missing = []
85         for path, cmd in self.iter_all_subcommands():
86             if cmd.short_description is None:
87                 missing.append(path)
88         if not missing:
89             return
90         self.fail(
91             "The following commands do not have a short description set: %r" %
92                 missing)