From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50939) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzLeW-0004Vc-0s for qemu-devel@nongnu.org; Wed, 26 Oct 2016 06:41:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bzLeV-0008O4-4g for qemu-devel@nongnu.org; Wed, 26 Oct 2016 06:41:48 -0400 Date: Wed, 26 Oct 2016 12:41:38 +0200 From: Kevin Wolf Message-ID: <20161026104138.GM4758@noname.str.redhat.com> References: <20161025131141.24762-1-mreitz@redhat.com> <20161025131141.24762-13-mreitz@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20161025131141.24762-13-mreitz@redhat.com> Subject: Re: [Qemu-devel] [PATCH v5 12/13] iotests: Add assert_json_filename_equal() method List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Eric Blake , Paolo Bonzini , Markus Armbruster Am 25.10.2016 um 15:11 hat Max Reitz geschrieben: > Since the order of keys in JSON filenames is not necessarily fixed, they > should not be compared to fixed strings. This method takes a Python dict > as a reference, parses a given JSON filename and compares both. > > Signed-off-by: Max Reitz > --- > tests/qemu-iotests/iotests.py | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py > index c589deb..1f30cfc 100644 > --- a/tests/qemu-iotests/iotests.py > +++ b/tests/qemu-iotests/iotests.py > @@ -222,6 +222,19 @@ class QMPTestCase(unittest.TestCase): > self.fail('invalid index "%s" in path "%s" in "%s"' % (idx, path, str(d))) > return d > > + def flatten_qmp_object(self, obj, output=None, basestr=''): > + if output is None: > + output = dict() > + if isinstance(obj, list): > + for i in range(len(obj)): > + self.flatten_qmp_object(obj[i], output, basestr + str(i) + '.') > + elif isinstance(obj, dict): > + for key in obj: > + self.flatten_qmp_object(obj[key], output, basestr + key + '.') > + else: > + output[basestr[:-1]] = obj # Strip trailing '.' > + return output > + > def assert_qmp_absent(self, d, path): > try: > result = self.dictpath(d, path) > @@ -252,6 +265,13 @@ class QMPTestCase(unittest.TestCase): > self.assertTrue(False, "Cannot find %s %s in result:\n%s" % \ > (node_name, file_name, result)) > > + def assert_json_filename_equal(self, json_filename, reference): > + '''Asserts that the given filename is a json: filename and that its > + content is equal to the given reference object''' > + self.assertEqual(json_filename[:5], 'json:') > + self.assertEqual(self.flatten_qmp_object(json.loads(json_filename[5:])), > + self.flatten_qmp_object(reference)) Why do we have to flatten the dicts instead of comparing them directly? Anyway, it seems to be correct: Reviewed-by: Kevin Wolf