From nobody Wed Apr 16 03:29:38 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 (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1504618431202582.2613111096941; Tue, 5 Sep 2017 06:33:51 -0700 (PDT) Received: from localhost ([::1]:58967 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpDzC-000483-6i for importer@patchew.org; Tue, 05 Sep 2017 09:33:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51293) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dpDuy-0000nH-5F for qemu-devel@nongnu.org; Tue, 05 Sep 2017 09:29:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dpDuq-0003sZ-25 for qemu-devel@nongnu.org; Tue, 05 Sep 2017 09:29:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43328) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dpDup-0003ry-Qc for qemu-devel@nongnu.org; Tue, 05 Sep 2017 09:29:19 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 910CF6586E; Tue, 5 Sep 2017 13:29:18 +0000 (UTC) Received: from localhost (ovpn-117-180.ams2.redhat.com [10.36.117.180]) by smtp.corp.redhat.com (Postfix) with ESMTP id E4642835D3; Tue, 5 Sep 2017 13:29:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 910CF6586E Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=stefanha@redhat.com From: Stefan Hajnoczi To: Date: Tue, 5 Sep 2017 14:29:06 +0100 Message-Id: <20170905132908.30931-3-stefanha@redhat.com> In-Reply-To: <20170905132908.30931-1-stefanha@redhat.com> References: <20170905132908.30931-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 05 Sep 2017 13:29:18 +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 2/4] iotests.py: add FilePath context manager 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: Peter Maydell , Stefan Hajnoczi 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" The scratch/ (TEST_DIR) directory is not automatically cleaned up after test execution. It is the responsibility of tests to remove any files they create. A nice way of doing this is to declare files at the beginning of the test and automatically remove them with a context manager: with iotests.FilePath('test.img') as img_path: qemu_img(...) qemu_io(...) # img_path is guaranteed to be deleted here Signed-off-by: Stefan Hajnoczi Message-id: 20170824072202.26818-3-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- tests/qemu-iotests/iotests.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index 7233983f3c..07fa1626a0 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -160,6 +160,32 @@ class Timeout: def timeout(self, signum, frame): raise Exception(self.errmsg) =20 + +class FilePath(object): + '''An auto-generated filename that cleans itself up. + + Use this context manager to generate filenames and ensure that the file + gets deleted:: + + with TestFilePath('test.img') as img_path: + qemu_img('create', img_path, '1G') + # migration_sock_path is automatically deleted + ''' + def __init__(self, name): + filename =3D '{0}-{1}'.format(os.getpid(), name) + self.path =3D os.path.join(test_dir, filename) + + def __enter__(self): + return self.path + + def __exit__(self, exc_type, exc_val, exc_tb): + try: + os.remove(self.path) + except OSError: + pass + return False + + class VM(qtest.QEMUQtestMachine): '''A QEMU VM''' =20 --=20 2.13.5