From nobody Tue Apr 15 03:09:24 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506436542869222.82610598776307; Tue, 26 Sep 2017 07:35:42 -0700 (PDT) Received: from localhost ([::1]:47845 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dwqxQ-0004Ku-0t for importer@patchew.org; Tue, 26 Sep 2017 10:35:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33194) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dwqkp-0001qp-Lh for qemu-devel@nongnu.org; Tue, 26 Sep 2017 10:22:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dwqko-0005ec-4e for qemu-devel@nongnu.org; Tue, 26 Sep 2017 10:22:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33986) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dwqkg-0005aK-Cx; Tue, 26 Sep 2017 10:22:22 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6FA1886647; Tue, 26 Sep 2017 14:22:21 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-39.ams2.redhat.com [10.36.117.39]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0903C66D26; Tue, 26 Sep 2017 14:22:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6FA1886647 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=kwolf@redhat.com From: Kevin Wolf To: qemu-block@nongnu.org Date: Tue, 26 Sep 2017 16:21:32 +0200 Message-Id: <20170926142133.2498-24-kwolf@redhat.com> In-Reply-To: <20170926142133.2498-1-kwolf@redhat.com> References: <20170926142133.2498-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 26 Sep 2017 14:22:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 23/24] qemu-iotests: add shrinking image test X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Pavel Butsykin Signed-off-by: Pavel Butsykin Reviewed-by: Max Reitz Reviewed-by: John Snow Message-id: 20170918124230.8152-5-pbutsykin@virtuozzo.com Signed-off-by: Max Reitz --- tests/qemu-iotests/163 | 170 +++++++++++++++++++++++++++++++++++++++++= ++++ tests/qemu-iotests/163.out | 5 ++ tests/qemu-iotests/group | 1 + 3 files changed, 176 insertions(+) create mode 100644 tests/qemu-iotests/163 create mode 100644 tests/qemu-iotests/163.out diff --git a/tests/qemu-iotests/163 b/tests/qemu-iotests/163 new file mode 100644 index 0000000000..403842354e --- /dev/null +++ b/tests/qemu-iotests/163 @@ -0,0 +1,170 @@ +#!/usr/bin/env python +# +# Tests for shrinking images +# +# Copyright (c) 2016-2017 Parallels International GmbH +# +# 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 2 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 . +# + +import os, random, iotests, struct, qcow2 +from iotests import qemu_img, qemu_io, image_size + +test_img =3D os.path.join(iotests.test_dir, 'test.img') +check_img =3D os.path.join(iotests.test_dir, 'check.img') + +def size_to_int(str): + suff =3D ['B', 'K', 'M', 'G', 'T'] + return int(str[:-1]) * 1024**suff.index(str[-1:]) + +class ShrinkBaseClass(iotests.QMPTestCase): + image_len =3D '128M' + shrink_size =3D '10M' + chunk_size =3D '16M' + refcount_bits =3D '16' + + def __qcow2_check(self, filename): + entry_bits =3D 3 + entry_size =3D 1 << entry_bits + l1_mask =3D 0x00fffffffffffe00 + div_roundup =3D lambda n, d: (n + d - 1) / d + + def split_by_n(data, n): + for x in xrange(0, len(data), n): + yield struct.unpack('>Q', data[x:x + n])[0] & l1_mask + + def check_l1_table(h, l1_data): + l1_list =3D list(split_by_n(l1_data, entry_size)) + real_l1_size =3D div_roundup(h.size, + 1 << (h.cluster_bits*2 - entry_size= )) + used, unused =3D l1_list[:real_l1_size], l1_list[real_l1_size:] + + self.assertTrue(len(used) !=3D 0, "Verifying l1 table content") + self.assertFalse(any(unused), "Verifying l1 table content") + + def check_reftable(fd, h, reftable): + for offset in split_by_n(reftable, entry_size): + if offset !=3D 0: + fd.seek(offset) + cluster =3D fd.read(1 << h.cluster_bits) + self.assertTrue(any(cluster), "Verifying reftable cont= ent") + + with open(filename, "rb") as fd: + h =3D qcow2.QcowHeader(fd) + + fd.seek(h.l1_table_offset) + l1_table =3D fd.read(h.l1_size << entry_bits) + + fd.seek(h.refcount_table_offset) + reftable =3D fd.read(h.refcount_table_clusters << h.cluster_bi= ts) + + check_l1_table(h, l1_table) + check_reftable(fd, h, reftable) + + def __raw_check(self, filename): + pass + + image_check =3D { + 'qcow2' : __qcow2_check, + 'raw' : __raw_check + } + + def setUp(self): + if iotests.imgfmt =3D=3D 'raw': + qemu_img('create', '-f', iotests.imgfmt, test_img, self.image_= len) + qemu_img('create', '-f', iotests.imgfmt, check_img, + self.shrink_size) + else: + qemu_img('create', '-f', iotests.imgfmt, + '-o', 'cluster_size=3D' + self.cluster_size + + ',refcount_bits=3D' + self.refcount_bits, + test_img, self.image_len) + qemu_img('create', '-f', iotests.imgfmt, + '-o', 'cluster_size=3D%s'% self.cluster_size, + check_img, self.shrink_size) + qemu_io('-c', 'write -P 0xff 0 ' + self.shrink_size, check_img) + + def tearDown(self): + os.remove(test_img) + os.remove(check_img) + + def image_verify(self): + self.assertEqual(image_size(test_img), image_size(check_img), + "Verifying image size") + self.image_check[iotests.imgfmt](self, test_img) + + if iotests.imgfmt =3D=3D 'raw': + return + self.assertEqual(qemu_img('check', test_img), 0, + "Verifying image corruption") + + def test_empty_image(self): + qemu_img('resize', '-f', iotests.imgfmt, '--shrink', test_img, + self.shrink_size) + + self.assertEqual( + qemu_io('-c', 'read -P 0x00 %s'%self.shrink_size, test_img), + qemu_io('-c', 'read -P 0x00 %s'%self.shrink_size, check_img), + "Verifying image content") + + self.image_verify() + + def test_sequential_write(self): + for offs in range(0, size_to_int(self.image_len), + size_to_int(self.chunk_size)): + qemu_io('-c', 'write -P 0xff %d %s' % (offs, self.chunk_size), + test_img) + + qemu_img('resize', '-f', iotests.imgfmt, '--shrink', test_img, + self.shrink_size) + + self.assertEqual(qemu_img("compare", test_img, check_img), 0, + "Verifying image content") + + self.image_verify() + + def test_random_write(self): + offs_list =3D range(0, size_to_int(self.image_len), + size_to_int(self.chunk_size)) + random.shuffle(offs_list) + for offs in offs_list: + qemu_io('-c', 'write -P 0xff %d %s' % (offs, self.chunk_size), + test_img) + + qemu_img('resize', '-f', iotests.imgfmt, '--shrink', test_img, + self.shrink_size) + + self.assertEqual(qemu_img("compare", test_img, check_img), 0, + "Verifying image content") + + self.image_verify() + +class TestShrink512(ShrinkBaseClass): + image_len =3D '3M' + shrink_size =3D '1M' + chunk_size =3D '256K' + cluster_size =3D '512' + refcount_bits =3D '64' + +class TestShrink64K(ShrinkBaseClass): + cluster_size =3D '64K' + +class TestShrink1M(ShrinkBaseClass): + cluster_size =3D '1M' + refcount_bits =3D '1' + +ShrinkBaseClass =3D None + +if __name__ =3D=3D '__main__': + iotests.main(supported_fmts=3D['raw', 'qcow2']) diff --git a/tests/qemu-iotests/163.out b/tests/qemu-iotests/163.out new file mode 100644 index 0000000000..dae404e278 --- /dev/null +++ b/tests/qemu-iotests/163.out @@ -0,0 +1,5 @@ +......... +---------------------------------------------------------------------- +Ran 9 tests + +OK diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index 108339cd03..cdccee319e 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -166,6 +166,7 @@ 159 rw auto quick 160 rw auto quick 162 auto quick +163 rw auto quick 165 rw auto quick 170 rw auto quick 171 rw auto quick --=20 2.13.5