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."""
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
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"
36 retval
= cmd
._run
(*args
)
37 except Exception as e
:
38 cmd
.show_command_error(e
)
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())
46 (path
, cmd
) = todo
.pop()
48 subcmds
= getattr(cmd
, "subcommands", {})
49 todo
.extend([(path
+ " " + k
, v
) for (k
, v
) in
53 class TestParmTests(NetCmdTestCase
):
58 # Override these global parameters in case their default values are
60 contents
= """[global]
67 self
.smbconf
= self
.create_smbconf(contents
)
69 def create_smbconf(self
, contents
):
70 smbconf
= tempfile
.NamedTemporaryFile(mode
='w',
73 self
.addCleanup(os
.remove
, smbconf
.name
)
76 smbconf
.write(contents
)
82 def test_no_client_ip(self
):
83 out
, err
= self
.run_netcmd(cmd_testparm
, ["--client-name=foo"],
85 self
.assertEqual("", out
)
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"],
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}"],
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"],
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"],
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"],
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
):
140 self
.assertEqual("Mydescription", cmd_foo().short_description
)
143 class cmd_foo(Command
):
145 self
.assertEqual("foo", cmd_foo().name
)
147 def test_synopsis_everywhere(self
):
149 for path
, cmd
in self
.iter_all_subcommands():
150 if cmd
.synopsis
is None:
153 self
.fail("The following commands do not have a synopsis set: %r" %
156 def test_short_description_everywhere(self
):
158 for path
, cmd
in self
.iter_all_subcommands():
159 if cmd
.short_description
is None:
164 "The following commands do not have a short description set: %r" %