tests/blackbox: add test for net ads JSON output
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 20 Aug 2018 12:50:39 +0000 (14:50 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 22 Sep 2018 07:20:09 +0000 (09:20 +0200)
Implement blackbox tests for

    $ net ads info --json
    $ net ads lookup --json

that validate

    a) JSON wellformedness (by feeding it into the JSON library
       that ships with Python), and
    b) equality of the set of keys printed to that of the
       non-JSON version.

Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
Signed-off-by: Philipp Gesang <philipp.gesang@intra2net.com>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Autobuild-User(master): Andrew Bartlett <abartlet@samba.org>
Autobuild-Date(master): Sat Sep 22 09:20:09 CEST 2018 on sn-devel-144

python/samba/tests/blackbox/netads_json.py [new file with mode: 0644]
source4/selftest/tests.py

diff --git a/python/samba/tests/blackbox/netads_json.py b/python/samba/tests/blackbox/netads_json.py
new file mode 100644 (file)
index 0000000..ce48a2b
--- /dev/null
@@ -0,0 +1,83 @@
+# Blackbox tests for the "net ads ... --json" commands
+# Copyright (C) 2018 Intra2net AG
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import json
+import re
+
+import samba.tests
+
+COMMAND         = "bin/net ads"
+# extract keys from non-json version
+PLAIN_KEY_REGEX = re.compile ("^([^ \t:][^:]*):")
+
+class BaseWrapper (object):
+    """
+    Guard the base so it doesn't inherit from TestCase. This prevents it from
+    being run by unittest directly.
+    """
+
+    class NetAdsJSONTests_Base(samba.tests.BlackboxTestCase):
+        """Blackbox tests for JSON output of the net ads suite of commands."""
+        subcmd = None
+
+        def setUp(self):
+            super(BaseWrapper.NetAdsJSONTests_Base, self).setUp()
+
+        def test_json_wellformed (self):
+            """The output of ``--json`` commands must parse as JSON."""
+            argv = "%s %s --json" % (COMMAND, self.subcmd)
+            try:
+                out = self.check_output(argv)
+                json.loads (out)
+            except samba.tests.BlackboxProcessError as e:
+                self.fail("Error calling [%s]: %s" % (argv, e))
+
+        def test_json_matching_entries (self):
+            """
+            The ``--json`` variants must contain the same keys as their
+            respective plain counterpart.
+
+            Does not check nested dictionaries (e. g. the ``Flags`` value of
+            ``net ads lookup``..
+            """
+            argv = "%s %s" % (COMMAND, self.subcmd)
+            try:
+                out_plain = self.check_output(argv)
+            except samba.tests.BlackboxProcessError as e:
+                self.fail("Error calling [%s]: %s" % (argv, e))
+
+            argv = "%s %s --json" % (COMMAND, self.subcmd)
+            try:
+                out_jsobj = self.check_output(argv)
+            except samba.tests.BlackboxProcessError as e:
+                self.fail("Error calling [%s]: %s" % (argv, e))
+
+            parsed = json.loads (out_jsobj)
+
+            for key in [ re.match (PLAIN_KEY_REGEX, line).group(1)
+                         for line in out_plain.split ("\n")
+                            if line != "" and line [0] not in " \t:" ]:
+                self.assertTrue (parsed.get (key) is not None)
+                del parsed [key]
+
+            self.assertTrue (len (parsed) == 0) # tolerate no leftovers
+
+class NetAdsJSONInfoTests(BaseWrapper.NetAdsJSONTests_Base):
+    subcmd = "info"
+
+class NetAdsJSONlookupTests(BaseWrapper.NetAdsJSONTests_Base):
+    subcmd = "lookup"
index a305ef62af5a6f17fcdfac8d85caf8ee1d3bfa7c..e39148607eed260b30e860f3df21741c5d60f151 100755 (executable)
@@ -464,6 +464,9 @@ plantestsuite("samba4.blackbox.client_etypes_legacy(ad_dc:client)", "ad_dc:clien
 plantestsuite("samba4.blackbox.client_etypes_strong(ad_dc:client)", "ad_dc:client", [os.path.join(bbdir, "test_client_etypes.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', '$PREFIX_ABS', 'strong', '17_18'])
 plantestsuite("samba4.blackbox.net_ads_dns(ad_member:local)", "ad_member:local", [os.path.join(bbdir, "test_net_ads_dns.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', '$REALM', '$USERNAME', '$PASSWORD'])
 plantestsuite_loadlist("samba4.rpc.echo against NetBIOS alias", "ad_dc_ntvfs", [valgrindify(smbtorture4), "$LISTOPT", "$LOADLIST", 'ncacn_np:$NETBIOSALIAS', '-U$DOMAIN/$USERNAME%$PASSWORD', 'rpc.echo'])
+# json tests hook into ``chgdcpass'' to make them run in contributor CI on
+# gitlab
+planpythontestsuite("chgdcpass", "samba.tests.blackbox.netads_json")
 
 # Tests using the "Simple" NTVFS backend
 for t in ["base.rw1"]: