[PATCH] create python test for samba-tool time command
Jelmer Vernooij
jelmer at samba.org
Sun Oct 23 16:12:31 MDT 2011
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 20/10/11 14:35, Sean Dague wrote:
> This patch creates a new subdirectory at
source4/scripting/python/samba/tests/samba_tool and implements the first
samba-tool test, which tests the time command. It does this with the
planpythontestsuite infrastructure talking to local dc.
>
> Assuming this approach is accepted, we'll use the same pattern to flesh
testing for the reset of the samba-tool subcommands.
Thanks, I think this looks great. Some comments below:
> diff --git a/source4/scripting/python/samba/tests/samba_tool/base.py
b/source4/scripting/python/samba/tests/samba_tool/base.py
> new file mode 100644
> index 0000000..9120c9e
> --- /dev/null
> +++ b/source4/scripting/python/samba/tests/samba_tool/base.py
> @@ -0,0 +1,78 @@
> +
> +# This provides a wrapper around the cmd interface so that tests can
> +# easily be built on top of it and have minimal code to run basic tests
> +# of the commands.
> +
> +# A number of environmental variables are provided via the testsuite
> +# interface. These currently include:
>
This file lacks a copyright header - does the source test pass for you?
> +# # domain stuff
> +# "DOMAIN",
> +# "REALM",
> +
> +# # domain controller stuff
> +# "DC_SERVER",
> +# "DC_SERVER_IP",
> +# "DC_NETBIOSNAME",
> +# "DC_NETBIOSALIAS",
> +
> +# # domain member
> +# "MEMBER_SERVER",
> +# "MEMBER_SERVER_IP",
> +# "MEMBER_NETBIOSNAME",
> +# "MEMBER_NETBIOSALIAS",
> +
> +# # rpc proxy controller stuff
> +# "RPC_PROXY_SERVER",
> +# "RPC_PROXY_SERVER_IP",
> +# "RPC_PROXY_NETBIOSNAME",
> +# "RPC_PROXY_NETBIOSALIAS",
> +
> +# # domain controller stuff for Vampired DC
> +# "VAMPIRE_DC_SERVER",
> +# "VAMPIRE_DC_SERVER_IP",
> +# "VAMPIRE_DC_NETBIOSNAME",
> +# "VAMPIRE_DC_NETBIOSALIAS",
> +
> +# # server stuff
> +# "SERVER",
> +# "SERVER_IP",
> +# "NETBIOSNAME",
> +# "NETBIOSALIAS",
> +
> +# # user stuff
> +# "USERNAME",
> +# "USERID",
> +# "PASSWORD",
> +# "DC_USERNAME",
> +# "DC_PASSWORD",
> +
> +# # misc stuff
> +# "KRB5_CONFIG",
> +# "WINBINDD_SOCKET_DIR",
> +# "WINBINDD_PRIV_PIPE_DIR",
> +# "NMBD_SOCKET_DIR",
> +# "LOCAL_PATH"
> +
> +# These can all be accesses via os.environ["VARIBLENAME"] when needed
s/VARIBLENAME/VARIABLENAME/
Rather than duplicating this list here, it would probably be better to
point at the master list of the variables. The risk of duplicating it is
that it will get out of date when the main list gets changed.
> +import os
> +import sys
> +from cStringIO import StringIO
> +from samba.netcmd.main import cmd_sambatool
> +import samba.tests
> +
> +class SambaToolCmdTest(samba.tests.TestCase):
> +
> + def runcmd(self, name, *args):
> + cmd = cmd_sambatool.subcommands[name]
> + cmd.outf = StringIO()
> + cmd.errf = StringIO()
> + result = cmd._run(name, *args)
> + return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
This duplicates code from samba.tests.netcmd - it would be nicer to
derive from the class there instead.
> +
> + def assertCmdSuccess(self, val, msg=""):
> + self.assertIsNone(val, msg)
> +
> + def assertCmdFail(self, val, msg=""):
> + self.assertIsNotNone(val, msg)
These methods seem pretty useless given output and stderr are never
going to be None, they'll always be a string.
> diff --git a/source4/scripting/python/samba/tests/samba_tool/timecmd.py
b/source4/scripting/python/samba/tests/samba_tool/timecmd.py
> new file mode 100644
> index 0000000..2de9685
> --- /dev/null
> +++ b/source4/scripting/python/samba/tests/samba_tool/timecmd.py
> +
> +class TimeCmdTestCase(SambaToolCmdTest):
> + """Tests for samba-tool time subcommands"""
> +
> + def test_timeget(self):
> + """Run time against the server and make sure it looks accurate"""
> + (result, out, err) = self.runcmd("time", os.environ["SERVER"])
> + self.assertCmdSuccess(result, "Ensuring time ran successfully")
> +
> + timefmt = strptime(out, "%a %b %d %H:%M:%S %Y %Z\n")
> + servertime = int(mktime(timefmt))
> + now = int(mktime(localtime()))
> +
> + # because there is a race here, allow up to 5 seconds difference in times
> + delta = 5
> + self.assertTrue((servertime > (now - delta) and (servertime < (now +
delta)), "Time is now"))
> +
> + def test_timefail(self):
> + """Run time against a non-existant server, and make sure it fails"""
> + try:
> + (result, out, err) = self.runcmd("time", "notaserver")
> + self.assertCmdFail(result, "Ensuring time failed as expected")
> + except:
> + self.assertTrue(True, "Failure caused an exception, ok and expected
for now")
except: (rather than e.g. "except Exception:") is evil (it can "eat"
exceptions like KeyboardInterrupt). Rather than using this, it's nicer
and easier to use self.assertRaises, e.g.
self.assertRaises(self.runcmd, "time", "notaserver")
Cheers,
Jelmer
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBAgAGBQJOpJFOAAoJEACAbyvXKaRXDcUQAJT1KSnHdMinrCk32uYapAcN
td4i52sfYXlzocYLe+NgnfGBIZbtdTT+Kglip7tkEIi5WvuAoa/pdRzenTb4v57W
6y/8omMGsUJndZ5ypH2CRViecYKWcYXLQ+YEyCFaaz5T1vxgKFt20weQsBimufCk
kgyCYzK7NDRkZytSc1cv+dKtHFMZ9Sfa/k3K4IAX4vgV+DthZkoyrgggeD3QfpI5
QODYoIxboOVY0x9dBHKkw7IrUxHpvdJ8HIPKrg2YoGuNQ/JhV1cohPLS5jfRpomB
9T76ss0tfSTZHhkSmUATl6o0pLNWiyxtfyn/tdwQC8v2flxYlCwW7azK4kc4AGIe
Dbb516TVrIisln3zMZSG7KWcRfm/v34xBz7KszNUAvfF/fsCRf4hb+HJ5/RBjsaY
P8WT64zwZLD4pXnvYUILhXP/wCntwY7bcpABoqJl/ND9zMUiHbCE9hiLIExQgI1q
8im2lsvc8dpTNRqG2b3VHGY6smaSMZwqyCs+96v82AJd2/R44qBKTTd2008DOXkV
Nocf/FhuyQhJmsQyyHqZ1/u3N9mqxVBHOkvGRYOF3qtprcss5SbB442097fmOYWP
RyqF60S8PjhwwfAxr5QvlpfQuccTBcWp4tMVIa13rHTp/iAju8JOTrC+8hE02x/e
2QEAAOB4s4MZGy79Nqec
=UmMz
-----END PGP SIGNATURE-----
More information about the samba-technical
mailing list