]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
netcmd: tests: avoid the need to create a random command in GetSamDB
authorRob van der Linde <rob@catalyst.net.nz>
Sun, 24 Sep 2023 23:51:19 +0000 (12:51 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Fri, 29 Sep 2023 02:18:34 +0000 (02:18 +0000)
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>
python/samba/tests/samba_tool/base.py

index 9df8ee1e6606f7e4f9f8cb4f55796efd34b0efba..3098817869c13854adcda050efac7f031b97fd98 100644 (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"""