[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 09/14] iotests/migration-permissions: use assertRaises() for qemu_
From: |
Hanna Reitz |
Subject: |
[PULL 09/14] iotests/migration-permissions: use assertRaises() for qemu_io() negative test |
Date: |
Mon, 25 Apr 2022 16:08:16 +0200 |
From: John Snow <jsnow@redhat.com>
Modify this test to use assertRaises for its negative testing of
qemu_io. If the exception raised does not match the one we tell it to
expect, we get *that* exception unhandled. If we get no exception, we
get a unittest assertion failure and the provided emsg printed to
screen.
If we get the CalledProcessError exception but the output is not what we
expect, we re-raise the original CalledProcessError.
Tidy.
(Note: Yes, you can reference "with" objects after that block ends; it
just means that ctx.__exit__(...) will have been called on it. It does
not *actually* go out of scope. unittests expects you to want to inspect
the Exception object, so they leave it defined post-exit.)
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Tested-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-Id: <20220418211504.943969-9-jsnow@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
---
.../qemu-iotests/tests/migration-permissions | 28 +++++++++----------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/tests/qemu-iotests/tests/migration-permissions
b/tests/qemu-iotests/tests/migration-permissions
index c7afb1bd2c..4e1da369c9 100755
--- a/tests/qemu-iotests/tests/migration-permissions
+++ b/tests/qemu-iotests/tests/migration-permissions
@@ -18,6 +18,8 @@
#
import os
+from subprocess import CalledProcessError
+
import iotests
from iotests import imgfmt, qemu_img_create, qemu_io
@@ -69,13 +71,12 @@ class TestMigrationPermissions(iotests.QMPTestCase):
def test_post_migration_permissions(self):
# Try to access the image R/W, which should fail because virtio-blk
# has not been configured with share-rw=on
- log = qemu_io('-f', imgfmt, '-c', 'quit', test_img, check=False).stdout
- if not log.strip():
- print('ERROR (pre-migration): qemu-io should not be able to '
- 'access this image, but it reported no error')
- else:
- # This is the expected output
- assert 'Is another process using the image' in log
+ emsg = ('ERROR (pre-migration): qemu-io should not be able to '
+ 'access this image, but it reported no error')
+ with self.assertRaises(CalledProcessError, msg=emsg) as ctx:
+ qemu_io('-f', imgfmt, '-c', 'quit', test_img)
+ if 'Is another process using the image' not in ctx.exception.stdout:
+ raise ctx.exception
# Now migrate the VM
self.vm_s.qmp('migrate', uri=f'unix:{mig_sock}')
@@ -84,13 +85,12 @@ class TestMigrationPermissions(iotests.QMPTestCase):
# Try the same qemu-io access again, verifying that the WRITE
# permission remains unshared
- log = qemu_io('-f', imgfmt, '-c', 'quit', test_img, check=False).stdout
- if not log.strip():
- print('ERROR (post-migration): qemu-io should not be able to '
- 'access this image, but it reported no error')
- else:
- # This is the expected output
- assert 'Is another process using the image' in log
+ emsg = ('ERROR (post-migration): qemu-io should not be able to '
+ 'access this image, but it reported no error')
+ with self.assertRaises(CalledProcessError, msg=emsg) as ctx:
+ qemu_io('-f', imgfmt, '-c', 'quit', test_img)
+ if 'Is another process using the image' not in ctx.exception.stdout:
+ raise ctx.exception
if __name__ == '__main__':
--
2.35.1
- [PULL 00/14] Block patches, Hanna Reitz, 2022/04/25
- [PULL 01/14] block: add 'force' parameter to 'blockdev-change-medium' command, Hanna Reitz, 2022/04/25
- [PULL 02/14] iotests: replace calls to log(qemu_io(...)) with qemu_io_log(), Hanna Reitz, 2022/04/25
- [PULL 03/14] iotests/163: Fix broken qemu-io invocation, Hanna Reitz, 2022/04/25
- [PULL 04/14] iotests: Don't check qemu_io() output for specific error strings, Hanna Reitz, 2022/04/25
- [PULL 05/14] iotests/040: Don't check image pattern on zero-length image, Hanna Reitz, 2022/04/25
- [PULL 06/14] iotests/040: Fix TestCommitWithFilters test, Hanna Reitz, 2022/04/25
- [PULL 07/14] iotests: create generic qemu_tool() function, Hanna Reitz, 2022/04/25
- [PULL 09/14] iotests/migration-permissions: use assertRaises() for qemu_io() negative test,
Hanna Reitz <=
- [PULL 08/14] iotests: rebase qemu_io() on top of qemu_tool(), Hanna Reitz, 2022/04/25
- [PULL 10/14] iotests/image-fleecing: switch to qemu_io(), Hanna Reitz, 2022/04/25
- [PULL 12/14] iotests: remove qemu_io_silent() and qemu_io_silent_check()., Hanna Reitz, 2022/04/25
- [PULL 13/14] iotests: make qemu_io_log() check return codes by default, Hanna Reitz, 2022/04/25
- [PULL 11/14] iotests: remove qemu_io_pipe_and_status(), Hanna Reitz, 2022/04/25
- [PULL 14/14] iotests/108: Fix when missing user_allow_other, Hanna Reitz, 2022/04/25
- Re: [PULL 00/14] Block patches, Richard Henderson, 2022/04/25