python: bulk replace dict.iteritems to items for py3
[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 = list(cmd_sambatool.subcommands.items())
40         while todo:
41             (path, cmd) = todo.pop()
42             yield path, cmd
43             subcmds = getattr(cmd, "subcommands", {})
44             todo.extend([(path + " " + k, v) for (k, v) in
45                 subcmds.items()])
46
47
48 class TestParmTests(NetCmdTestCase):
49
50     def test_no_client_ip(self):
51         out, err = self.run_netcmd(cmd_testparm, ["--client-name=foo"],
52             retcode=-1)
53         self.assertEquals("", out)
54         self.assertEquals(
55             "ERROR: Both a DNS name and an IP address are "
56             "required for the host access check\n", err)
57
58
59 class CommandTests(NetCmdTestCase):
60
61     def test_description(self):
62         class cmd_foo(Command):
63             """Mydescription"""
64         self.assertEquals("Mydescription", cmd_foo().short_description)
65
66     def test_name(self):
67         class cmd_foo(Command):
68             pass
69         self.assertEquals("foo", cmd_foo().name)
70
71     def test_synopsis_everywhere(self):
72         missing = []
73         for path, cmd in self.iter_all_subcommands():
74             if cmd.synopsis is None:
75                 missing.append(path)
76         if missing:
77             self.fail("The following commands do not have a synopsis set: %r" %
78                     missing)
79
80     def test_short_description_everywhere(self):
81         missing = []
82         for path, cmd in self.iter_all_subcommands():
83             if cmd.short_description is None:
84                 missing.append(path)
85         if not missing:
86             return
87         self.fail(
88             "The following commands do not have a short description set: %r" %
89                 missing)