python/samba/tests: PY3 port samba.tests.blackbox.netads_json
[samba.git] / python / samba / tests / blackbox / netads_json.py
1 # Blackbox tests for the "net ads ... --json" commands
2 # Copyright (C) 2018 Intra2net AG
3 #
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.
8 #
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.
13 #
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/>.
16 #
17
18 import json
19 import re
20
21 import samba.tests
22 from samba.compat import get_string
23
24 COMMAND         = "bin/net ads"
25 # extract keys from non-json version
26 PLAIN_KEY_REGEX = re.compile ("^([^ \t:][^:]*):")
27
28 class BaseWrapper (object):
29     """
30     Guard the base so it doesn't inherit from TestCase. This prevents it from
31     being run by unittest directly.
32     """
33
34     class NetAdsJSONTests_Base(samba.tests.BlackboxTestCase):
35         """Blackbox tests for JSON output of the net ads suite of commands."""
36         subcmd = None
37
38         def setUp(self):
39             super(BaseWrapper.NetAdsJSONTests_Base, self).setUp()
40
41         def test_json_wellformed (self):
42             """The output of ``--json`` commands must parse as JSON."""
43             argv = "%s %s --json" % (COMMAND, self.subcmd)
44             try:
45                 out = self.check_output(argv)
46                 json.loads (out)
47             except samba.tests.BlackboxProcessError as e:
48                 self.fail("Error calling [%s]: %s" % (argv, e))
49
50         def test_json_matching_entries (self):
51             """
52             The ``--json`` variants must contain the same keys as their
53             respective plain counterpart.
54
55             Does not check nested dictionaries (e. g. the ``Flags`` value of
56             ``net ads lookup``..
57             """
58             argv = "%s %s" % (COMMAND, self.subcmd)
59             try:
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))
63
64             argv = "%s %s --json" % (COMMAND, self.subcmd)
65             try:
66                 out_jsobj = self.check_output(argv)
67             except samba.tests.BlackboxProcessError as e:
68                 self.fail("Error calling [%s]: %s" % (argv, e))
69
70             parsed = json.loads (out_jsobj)
71
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)
76                 del parsed [key]
77
78             self.assertTrue (len (parsed) == 0) # tolerate no leftovers
79
80 class NetAdsJSONInfoTests(BaseWrapper.NetAdsJSONTests_Base):
81     subcmd = "info"
82
83 class NetAdsJSONlookupTests(BaseWrapper.NetAdsJSONTests_Base):
84     subcmd = "lookup"