Received: from eggs.gnu.org ([2001:4830:134:3::10]:44511) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzP7z-0000h4-ED for qemu-devel@nongnu.org; Wed, 26 Oct 2016 10:24:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bzP7v-0002vz-4H for qemu-devel@nongnu.org; Wed, 26 Oct 2016 10:24:27 -0400 References: <20161025131141.24762-1-mreitz@redhat.com> <20161025131141.24762-13-mreitz@redhat.com> <20161026104138.GM4758@noname.str.redhat.com> From: Max Reitz Message-ID: <9c2f8144-b6f9-1d84-d0b4-568afbb5d0ec@redhat.com> Date: Wed, 26 Oct 2016 16:24:04 +0200 MIME-Version: 1.0 In-Reply-To: <20161026104138.GM4758@noname.str.redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="qoiAJoBG8KcmT4w7OkFAbbG8AbxblFWLo" 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: Kevin Wolf Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Eric Blake , Paolo Bonzini , Markus Armbruster This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --qoiAJoBG8KcmT4w7OkFAbbG8AbxblFWLo From: Max Reitz To: Kevin Wolf Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org, Eric Blake , Paolo Bonzini , Markus Armbruster Message-ID: <9c2f8144-b6f9-1d84-d0b4-568afbb5d0ec@redhat.com> Subject: Re: [PATCH v5 12/13] iotests: Add assert_json_filename_equal() method References: <20161025131141.24762-1-mreitz@redhat.com> <20161025131141.24762-13-mreitz@redhat.com> <20161026104138.GM4758@noname.str.redhat.com> In-Reply-To: <20161026104138.GM4758@noname.str.redhat.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On 26.10.2016 12:41, Kevin Wolf wrote: > Am 25.10.2016 um 15:11 hat Max Reitz geschrieben: >> Since the order of keys in JSON filenames is not necessarily fixed, th= ey >> should not be compared to fixed strings. This method takes a Python di= ct >> 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/iotest= s.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 >> =20 >> + def flatten_qmp_object(self, obj, output=3DNone, basestr=3D''): >> + if output is None: >> + output =3D 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 + k= ey + '.') >> + else: >> + output[basestr[:-1]] =3D obj # Strip trailing '.' >> + return output >> + >> def assert_qmp_absent(self, d, path): >> try: >> result =3D 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)) >> =20 >> + def assert_json_filename_equal(self, json_filename, reference): >> + '''Asserts that the given filename is a json: filename and th= at its >> + content is equal to the given reference object''' >> + self.assertEqual(json_filename[:5], 'json:') >> + self.assertEqual(self.flatten_qmp_object(json.loads(json_file= name[5:])), >> + self.flatten_qmp_object(reference)) >=20 > Why do we have to flatten the dicts instead of comparing them directly?= Well, I believe flattened and unflattened dicts to be equal when it comes to JSON filenames, and I wanted to express this here. This is my personal opinion, though, and I can see how others might disagree. In any case, the block layer does not necessarily report JSON filenames fully unflattened, at least not today. The test added in patch 13 compares these filenames against unflattened dicts, however, because that was simpler to write. So there's a practical reason for flattening them. Maybe unflattening (or crumpling) the block layer options will be our next great project. :-) > Anyway, it seems to be correct: > Reviewed-by: Kevin Wolf Thanks for reviewing, Max --qoiAJoBG8KcmT4w7OkFAbbG8AbxblFWLo Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQEvBAEBCAAZBQJYELyEEhxtcmVpdHpAcmVkaGF0LmNvbQAKCRD0B9sAYdXPQI57 B/4rraW+r1skSG6t1p6b+4upGPFWvO+TBU/WhXXRo/8cCykS0EngwlRLojsNgKZR wEKopqMzmSReS1SWdBGe8miF9EX/gOY87Zybd8lRWzY5Wyf1R+JJifa7ltH/kvsa KYsqRCmFBWSiMCGt5AlsgyYgzUrGzl4s3mt5eyvddtua/Q8c4W6oBh4q+kFkPJtX J/OXH7OZH2mWMzboG/0vVgqsMMrUgtGuLza+Lvzy2/4UlAJQ2AHM+eIpgvLUq3Oy JJFTx9AJfPSjClXYkrVTeih0hFQoUyfwrnib4tooWZyNYizgBDHxVVXc8vIIp7Jc nxE+M+CI9lSB0q63S4g6iQCb =ka20 -----END PGP SIGNATURE----- --qoiAJoBG8KcmT4w7OkFAbbG8AbxblFWLo--