1 # Blackbox tests for the "net ads ... --json" commands
2 # Copyright (C) 2018 Intra2net AG
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/>.
22 from samba.compat import get_string
24 COMMAND = "bin/net ads"
25 # extract keys from non-json version
26 PLAIN_KEY_REGEX = re.compile ("^([^ \t:][^:]*):")
28 class BaseWrapper (object):
30 Guard the base so it doesn't inherit from TestCase. This prevents it from
31 being run by unittest directly.
34 class NetAdsJSONTests_Base(samba.tests.BlackboxTestCase):
35 """Blackbox tests for JSON output of the net ads suite of commands."""
39 super(BaseWrapper.NetAdsJSONTests_Base, self).setUp()
41 def test_json_wellformed (self):
42 """The output of ``--json`` commands must parse as JSON."""
43 argv = "%s %s --json" % (COMMAND, self.subcmd)
45 out = self.check_output(argv)
47 except samba.tests.BlackboxProcessError as e:
48 self.fail("Error calling [%s]: %s" % (argv, e))
50 def test_json_matching_entries (self):
52 The ``--json`` variants must contain the same keys as their
53 respective plain counterpart.
55 Does not check nested dictionaries (e. g. the ``Flags`` value of
58 argv = "%s %s" % (COMMAND, self.subcmd)
60 out_plain = get_string(self.check_output(argv))
61 except samba.tests.BlackboxProcessError as e:
62 self.fail("Error calling [%s]: %s" % (argv, e))
64 argv = "%s %s --json" % (COMMAND, self.subcmd)
66 out_jsobj = self.check_output(argv)
67 except samba.tests.BlackboxProcessError as e:
68 self.fail("Error calling [%s]: %s" % (argv, e))
70 parsed = json.loads (out_jsobj)
72 for key in [ re.match (PLAIN_KEY_REGEX, line).group(1)
73 for line in out_plain.split ("\n")
74 if line != "" and line [0] not in " \t:" ]:
75 self.assertTrue (parsed.get (key) is not None)
78 self.assertTrue (len (parsed) == 0) # tolerate no leftovers
80 class NetAdsJSONInfoTests(BaseWrapper.NetAdsJSONTests_Base):
83 class NetAdsJSONlookupTests(BaseWrapper.NetAdsJSONTests_Base):