1
0
mirror of https://github.com/samba-team/samba.git synced 2025-12-18 08:23:51 +03:00

netcmd: tests: avoid the need to create a random command in GetSamDB

Also the code that looks over kwargs is somewhat confusing and unnecessary.

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Rob van der Linde
2023-09-25 12:51:19 +13:00
committed by Andrew Bartlett
parent 08b9d5c7b9
commit 7f4db71025

View File

@@ -28,6 +28,8 @@ from samba.auth import system_session
from samba.samdb import SamDB
from io import StringIO
from samba.netcmd.main import cmd_sambatool
from optparse import OptionParser
import samba.getopt as options
import samba.tests
@@ -46,32 +48,19 @@ class SambaToolCmdTest(samba.tests.BlackboxTestCase):
def getSamDB(*argv):
"""a convenience function to get a samdb instance so that we can query it"""
# We build a fake command to get the options created the same
# way the command classes do it. It would be better if the command
# classes had a way to more cleanly do this, but this lets us write
# tests for now
cmd = cmd_sambatool.subcommands["user"].subcommands["setexpiry"]
parser, optiongroups = cmd._create_parser("user")
parser = OptionParser()
sambaopts = options.SambaOptions(parser)
credopts = options.CredentialsOptions(parser)
parser.add_option("-H", "--URL",
help="LDB URL for database or target server",
type=str, metavar="URL", dest="H")
opts, args = parser.parse_args(list(argv))
# Filter out options from option groups
args = args[1:]
kwargs = dict(opts.__dict__)
for option_group in parser.option_groups:
for option in option_group.option_list:
if option.dest is not None:
del kwargs[option.dest]
kwargs.update(optiongroups)
H = kwargs.get("H", None)
sambaopts = kwargs.get("sambaopts", None)
credopts = kwargs.get("credopts", None)
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp, fallback_machine=True)
samdb = SamDB(url=H, session_info=system_session(),
credentials=creds, lp=lp)
return samdb
return SamDB(url=opts.H, session_info=system_session(),
credentials=creds, lp=lp)
def _run(self, *argv):
"""run a samba-tool command"""