diff mbox

[V1,2/2] Add tests for sync modes 'TOP' and 'NONE'

Message ID 1372386525-23155-3-git-send-email-imain@redhat.com
State New
Headers show

Commit Message

Ian Main June 28, 2013, 2:28 a.m. UTC
This patch adds tests for sync modes top and none.  I'd be interested in
hearing ideas on how to improve these tests if people feel they are
inadequate.  Especially SYNC_MODE_NONE would be hard to test.

Signed-off-by: Ian Main <imain@redhat.com>
---
 tests/qemu-iotests/055     | 63 +++++++++++++++++++++++++++++++++++++++++++---
 tests/qemu-iotests/055.out |  4 +--
 tests/qemu-iotests/group   |  2 +-
 3 files changed, 63 insertions(+), 6 deletions(-)

Comments

Stefan Hajnoczi July 4, 2013, 1:20 p.m. UTC | #1
On Thu, Jun 27, 2013 at 07:28:45PM -0700, Ian Main wrote:
> diff --git a/tests/qemu-iotests/055 b/tests/qemu-iotests/055
> index c66f8db..6de81ff 100755
> --- a/tests/qemu-iotests/055
> +++ b/tests/qemu-iotests/055
> @@ -23,8 +23,9 @@
>  import time
>  import os
>  import iotests
> -from iotests import qemu_img, qemu_io
> +from iotests import qemu_img, qemu_io, create_image
>  
> +backing_img = os.path.join(iotests.test_dir, 'backing.img')
>  test_img = os.path.join(iotests.test_dir, 'test.img')
>  target_img = os.path.join(iotests.test_dir, 'target.img')
>  
> @@ -60,6 +61,20 @@ class TestSingleDrive(iotests.QMPTestCase):
>          event = self.cancel_and_wait()
>          self.assert_qmp(event, 'data/type', 'backup')
>  
> +    def test_cancel_sync_none(self):
> +        self.assert_no_active_block_jobs()
> +
> +        result = self.vm.qmp('drive-backup', device='drive0',
> +                             sync='none', target=target_img)
> +        self.assert_qmp(result, 'return', {})
> +        time.sleep(1)
> +
> +        # This is generally very hard to test, we would have to
> +        # have some writing going on in the VM to test and know
> +        # what the result should be.

You can use the qemu-io HMP command to write to the disk from this test
case.

First take a look at the qemu-io(1) command-line help output.  To fill
the first sector with 0x5e you would do something like this:

  $ qemu-io -c 'write -P0x5e 0 512' test.img

Kevin recently added an HMP command so you can invoke this from the
monitor.  It operates on an open drive, see hmp-commands.hx.

Finally, you need to use the QMP 'human-monitor-command' to invoke HMP
commands from your test case.

Basically you need something like:

  self.vm.qmp('human-monitor-command',
              command-line='drive0 "write -P0x5e 0 512"')

We should probably wrap this in a new method called VM.hmp_qemu_io() in
iotests.py, so that tests can simply do:

  self.vm.hmp_qemu_io('drive0', 'write -P0x5e 0 512')

Anyway, this lets you write to the drive.  This should be good for
testing sync=none.
diff mbox

Patch

diff --git a/tests/qemu-iotests/055 b/tests/qemu-iotests/055
index c66f8db..6de81ff 100755
--- a/tests/qemu-iotests/055
+++ b/tests/qemu-iotests/055
@@ -23,8 +23,9 @@ 
 import time
 import os
 import iotests
-from iotests import qemu_img, qemu_io
+from iotests import qemu_img, qemu_io, create_image
 
+backing_img = os.path.join(iotests.test_dir, 'backing.img')
 test_img = os.path.join(iotests.test_dir, 'test.img')
 target_img = os.path.join(iotests.test_dir, 'target.img')
 
@@ -60,6 +61,20 @@  class TestSingleDrive(iotests.QMPTestCase):
         event = self.cancel_and_wait()
         self.assert_qmp(event, 'data/type', 'backup')
 
+    def test_cancel_sync_none(self):
+        self.assert_no_active_block_jobs()
+
+        result = self.vm.qmp('drive-backup', device='drive0',
+                             sync='none', target=target_img)
+        self.assert_qmp(result, 'return', {})
+        time.sleep(1)
+
+        # This is generally very hard to test, we would have to
+        # have some writing going on in the VM to test and know
+        # what the result should be.
+        event = self.cancel_and_wait()
+        self.assert_qmp(event, 'data/type', 'backup')
+
     def test_pause(self):
         self.assert_no_active_block_jobs()
 
@@ -102,6 +117,47 @@  class TestSingleDrive(iotests.QMPTestCase):
                              target=target_img, sync='full')
         self.assert_qmp(result, 'error/class', 'DeviceNotFound')
 
+class TestSyncModeTop(iotests.QMPTestCase):
+    image_len = 2 * 1024 * 1024 # MB
+
+    def setUp(self):
+        create_image(backing_img, TestSyncModeTop.image_len)
+        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
+        qemu_io('-c', 'write -P0x5d 0 64k', test_img)
+        qemu_io('-c', 'write -P0xd5 1M 32k', test_img)
+        qemu_io('-c', 'write -P0xdc 32M 124k', test_img)
+        self.vm = iotests.VM().add_drive(test_img)
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        os.remove(test_img)
+        os.remove(backing_img)
+        try:
+            os.remove(target_img)
+        except OSError:
+            pass
+
+    def test_complete_top(self):
+        self.assert_no_active_block_jobs()
+        result = self.vm.qmp('drive-backup', device='drive0', sync='top',
+                             target=target_img)
+        self.assert_qmp(result, 'return', {})
+
+        # Custom completed check as we are not copying all data.
+        completed = False
+        while not completed:
+            for event in self.vm.get_qmp_events(wait=True):
+                if event['event'] == 'BLOCK_JOB_COMPLETED':
+                    self.assert_qmp(event, 'data/device', 'drive0')
+                    self.assert_qmp_absent(event, 'data/error')
+                    completed = True
+
+        self.assert_no_active_block_jobs()
+        self.vm.shutdown()
+        self.assertTrue(iotests.compare_images(test_img, target_img),
+                        'target image does not match source after backup')
+
 class TestSetSpeed(iotests.QMPTestCase):
     image_len = 80 * 1024 * 1024 # MB
 
@@ -127,7 +183,8 @@  class TestSetSpeed(iotests.QMPTestCase):
         self.assert_qmp(result, 'return[0]/device', 'drive0')
         self.assert_qmp(result, 'return[0]/speed', 0)
 
-        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
+        result = self.vm.qmp('block-job-set-speed', device='drive0',
+                             speed=8 * 1024 * 1024)
         self.assert_qmp(result, 'return', {})
 
         # Ensure the speed we set was accepted
@@ -285,4 +342,4 @@  class TestSingleTransaction(iotests.QMPTestCase):
         self.assert_no_active_block_jobs()
 
 if __name__ == '__main__':
-    iotests.main(supported_fmts=['raw', 'qcow2'])
+    iotests.main(supported_fmts=['qcow2', 'qed'])
diff --git a/tests/qemu-iotests/055.out b/tests/qemu-iotests/055.out
index fa16b5c..96961ed 100644
--- a/tests/qemu-iotests/055.out
+++ b/tests/qemu-iotests/055.out
@@ -1,5 +1,5 @@ 
-.............
+...............
 ----------------------------------------------------------------------
-Ran 13 tests
+Ran 15 tests
 
 OK
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
index fdc6ed1..d3c3f61 100644
--- a/tests/qemu-iotests/group
+++ b/tests/qemu-iotests/group
@@ -61,4 +61,4 @@ 
 052 rw auto backing
 053 rw auto
 054 rw auto
-055 rw auto
+055 rw auto backing