vfs_ceph_new: common prefix to debug-log messages
[Samba.git] / python / samba / tests / netcmd.py
blob63f204a2c2393d6df00aae0cc8d9fe1a0269c233
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 import os
21 import tempfile
23 from io import StringIO
24 from samba.netcmd import Command
25 from samba.netcmd.testparm import cmd_testparm
26 from samba.netcmd.main import cmd_sambatool
27 import samba.tests
30 class NetCmdTestCase(samba.tests.TestCaseInTempDir):
32 def run_netcmd(self, cmd_klass, args, retcode=0):
33 cmd = cmd_klass(outf=StringIO(), errf=StringIO())
34 cmd.command_name = "apricots"
35 try:
36 retval = cmd._run(*args)
37 except Exception as e:
38 cmd.show_command_error(e)
39 retval = 1
40 self.assertEqual(retcode, retval)
41 return cmd.outf.getvalue(), cmd.errf.getvalue()
43 def iter_all_subcommands(self):
44 todo = list(cmd_sambatool.subcommands.items())
45 while todo:
46 (path, cmd) = todo.pop()
47 yield path, cmd
48 subcmds = getattr(cmd, "subcommands", {})
49 todo.extend([(path + " " + k, v) for (k, v) in
50 subcmds.items()])
53 class TestParmTests(NetCmdTestCase):
55 def setUp(self):
56 super().setUp()
58 # Override these global parameters in case their default values are
59 # invalid.
60 contents = """[global]
61 netbios name = test
62 lock dir = /
63 pid directory = /
64 [tmp]
65 path = /
66 """
67 self.smbconf = self.create_smbconf(contents)
69 def create_smbconf(self, contents):
70 smbconf = tempfile.NamedTemporaryFile(mode='w',
71 dir=self.tempdir,
72 delete=False)
73 self.addCleanup(os.remove, smbconf.name)
75 try:
76 smbconf.write(contents)
77 finally:
78 smbconf.close()
80 return smbconf
82 def test_no_client_ip(self):
83 out, err = self.run_netcmd(cmd_testparm, ["--client-name=foo"],
84 retcode=-1)
85 self.assertEqual("", out)
86 self.assertEqual(
87 "ERROR: Both a DNS name and an IP address are "
88 "required for the host access check\n", err)
90 def test_section(self):
91 # We don't get an opportunity to verify the output, as the parameters
92 # are dumped directly to stdout, so just check the return code.
93 self.run_netcmd(cmd_testparm,
94 ["--configfile=%s" % self.smbconf.name,
95 "--section-name=tmp"],
96 retcode=None)
98 def test_section_globals(self):
99 # We can have '[global]' and '[globals]'
100 for name in ['global', 'globals']:
101 self.run_netcmd(cmd_testparm,
102 [f"--configfile={self.smbconf.name}",
103 f"--section-name={name}"],
104 retcode=None)
106 def test_no_such_section(self):
107 out, err = self.run_netcmd(cmd_testparm,
108 ["--configfile=%s" % self.smbconf.name,
109 "--section-name=foo"],
110 retcode=-1)
111 # Ensure all exceptions are caught.
112 self.assertEqual("", out)
113 self.assertNotIn("uncaught exception", err)
115 out, err = self.run_netcmd(cmd_testparm,
116 ["--configfile=%s" % self.smbconf.name,
117 "--section-name=foo",
118 "--parameter-name=foo"],
119 retcode=-1)
120 # Ensure all exceptions are caught.
121 self.assertEqual("", out)
122 self.assertNotIn("uncaught exception", err)
124 def test_no_such_parameter(self):
125 out, err = self.run_netcmd(cmd_testparm,
126 ["--configfile=%s" % self.smbconf.name,
127 "--section-name=tmp",
128 "--parameter-name=foo"],
129 retcode=-1)
130 # Ensure all exceptions are caught.
131 self.assertEqual("", out)
132 self.assertNotIn("uncaught exception", err)
135 class CommandTests(NetCmdTestCase):
137 def test_description(self):
138 class cmd_foo(Command):
139 """Mydescription"""
140 self.assertEqual("Mydescription", cmd_foo().short_description)
142 def test_name(self):
143 class cmd_foo(Command):
144 pass
145 self.assertEqual("foo", cmd_foo().name)
147 def test_synopsis_everywhere(self):
148 missing = []
149 for path, cmd in self.iter_all_subcommands():
150 if cmd.synopsis is None:
151 missing.append(path)
152 if missing:
153 self.fail("The following commands do not have a synopsis set: %r" %
154 missing)
156 def test_short_description_everywhere(self):
157 missing = []
158 for path, cmd in self.iter_all_subcommands():
159 if cmd.short_description is None:
160 missing.append(path)
161 if not missing:
162 return
163 self.fail(
164 "The following commands do not have a short description set: %r" %
165 missing)