From patchwork Thu Oct 27 15:22:55 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 122167 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1E20C1007D8 for ; Fri, 28 Oct 2011 02:24:07 +1100 (EST) Received: from localhost ([::1]:54359 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJRo6-0000RS-Mg for incoming@patchwork.ozlabs.org; Thu, 27 Oct 2011 11:23:50 -0400 Received: from eggs.gnu.org ([140.186.70.92]:39727) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJRnq-0000GJ-Ui for qemu-devel@nongnu.org; Thu, 27 Oct 2011 11:23:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RJRnp-0005JO-Ku for qemu-devel@nongnu.org; Thu, 27 Oct 2011 11:23:34 -0400 Received: from mtagate7.uk.ibm.com ([194.196.100.167]:60292) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJRnp-0005JH-B0 for qemu-devel@nongnu.org; Thu, 27 Oct 2011 11:23:33 -0400 Received: from d06nrmr1307.portsmouth.uk.ibm.com (d06nrmr1307.portsmouth.uk.ibm.com [9.149.38.129]) by mtagate7.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p9RFNW3Q026345 for ; Thu, 27 Oct 2011 15:23:32 GMT Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p9RFNWkI2523274 for ; Thu, 27 Oct 2011 16:23:32 +0100 Received: from d06av07.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p9RFNV88026125 for ; Thu, 27 Oct 2011 09:23:32 -0600 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p9RFNTc9026095; Thu, 27 Oct 2011 09:23:30 -0600 From: Stefan Hajnoczi To: Date: Thu, 27 Oct 2011 16:22:55 +0100 Message-Id: <1319728975-6069-9-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7 In-Reply-To: <1319728975-6069-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1319728975-6069-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.167 Cc: Kevin Wolf , Anthony Liguori , Marcelo Tosatti , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 8/8] test: add image streaming test cases X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org python test-stream.py Signed-off-by: Stefan Hajnoczi --- test-stream.py | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 200 insertions(+), 0 deletions(-) create mode 100644 test-stream.py diff --git a/test-stream.py b/test-stream.py new file mode 100644 index 0000000..22200ec --- /dev/null +++ b/test-stream.py @@ -0,0 +1,200 @@ +#!/usr/bin/env python +import unittest +import subprocess +import re +import os +import sys; sys.path.append('QMP/') +import qmp + +def qemu_img(*args): + devnull = open('/dev/null', 'r+') + return subprocess.call(['./qemu-img'] + list(args), stdin=devnull, stdout=devnull) + +def qemu_io(*args): + args = ['./qemu-io'] + list(args) + return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0] + +class VM(object): + def __init__(self): + self._monitor_path = '/tmp/qemu-mon.%d' % os.getpid() + self._qemu_log_path = '/tmp/qemu-log.%d' % os.getpid() + self._args = ['x86_64-softmmu/qemu-system-x86_64', + '-chardev', 'socket,id=mon,path=' + self._monitor_path, + '-mon', 'chardev=mon,mode=control', '-nographic'] + self._num_drives = 0 + + def add_drive(self, path, opts=''): + options = ['if=virtio', + 'cache=none', + 'file=%s' % path, + 'id=drive%d' % self._num_drives] + if opts: + options.append(opts) + + self._args.append('-drive') + self._args.append(','.join(options)) + self._num_drives += 1 + return self + + def launch(self): + devnull = open('/dev/null', 'rb') + qemulog = open(self._qemu_log_path, 'wb') + self._qmp = qmp.QEMUMonitorProtocol(self._monitor_path, server=True) + self._popen = subprocess.Popen(self._args, stdin=devnull, stdout=qemulog, + stderr=subprocess.STDOUT) + self._qmp.accept() + + def shutdown(self): + self._qmp.cmd('quit') + self._popen.wait() + os.remove(self._monitor_path) + os.remove(self._qemu_log_path) + + def qmp(self, cmd, **args): + return self._qmp.cmd(cmd, args=args) + + def get_qmp_events(self, wait=False): + events = self._qmp.get_events(wait=wait) + self._qmp.clear_events() + return events + +index_re = re.compile(r'([^\[]+)\[([^\]]+)\]') + +class QMPTestCase(unittest.TestCase): + def dictpath(self, d, path): + """Traverse a path in a nested dict""" + for component in path.split('/'): + m = index_re.match(component) + if m: + component, idx = m.groups() + idx = int(idx) + + if not isinstance(d, dict) or component not in d: + self.fail('failed path traversal for "%s" in "%s"' % (path, str(d))) + d = d[component] + + if m: + if not isinstance(d, list): + self.fail('path component "%s" in "%s" is not a list in "%s"' % (component, path, str(d))) + try: + d = d[idx] + except IndexError: + self.fail('invalid index "%s" in path "%s" in "%s"' % (idx, path, str(d))) + return d + + def assert_qmp(self, d, path, value): + result = self.dictpath(d, path) + self.assertEqual(result, value, 'values not equal "%s" and "%s"' % (str(result), str(value))) + + def assert_no_active_streams(self): + result = self.vm.qmp('query-block-jobs') + self.assert_qmp(result, 'return', []) + +class TestSingleDrive(QMPTestCase): + image_len = 1 * 1024 * 1024 # MB + + def setUp(self): + qemu_img('create', 'backing.img', str(TestSingleDrive.image_len)) + qemu_img('create', '-f', 'qed', '-o', 'backing_file=backing.img', 'test.qed') + self.vm = VM().add_drive('test.qed') + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + os.remove('test.qed') + os.remove('backing.img') + + def test_stream(self): + self.assert_no_active_streams() + + result = self.vm.qmp('block_stream', device='drive0') + self.assert_qmp(result, 'return', {}) + + 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/type', 'stream') + self.assert_qmp(event, 'data/device', 'drive0') + self.assert_qmp(event, 'data/offset', self.image_len) + self.assert_qmp(event, 'data/len', self.image_len) + completed = True + + self.assert_no_active_streams() + + self.assertFalse('sectors not allocated' in qemu_io('-c', 'map', 'test.qed'), + 'image file not fully populated after streaming') + + def test_device_not_found(self): + result = self.vm.qmp('block_stream', device='nonexistent') + self.assert_qmp(result, 'error/class', 'DeviceNotFound') + +class TestStreamStop(QMPTestCase): + image_len = 8 * 1024 * 1024 * 1024 # GB + + def setUp(self): + qemu_img('create', 'backing.img', str(TestStreamStop.image_len)) + qemu_img('create', '-f', 'qed', '-o', 'backing_file=backing.img', 'test.qed') + self.vm = VM().add_drive('test.qed') + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + os.remove('test.qed') + os.remove('backing.img') + + def test_stream_stop(self): + import time + + self.assert_no_active_streams() + + result = self.vm.qmp('block_stream', device='drive0') + self.assert_qmp(result, 'return', {}) + + time.sleep(1) + events = self.vm.get_qmp_events(wait=False) + self.assertEqual(events, [], 'unexpected QMP event: %s' % events) + + self.vm.qmp('block_job_cancel', device='drive0') + self.assert_qmp(result, 'return', {}) + + self.assert_no_active_streams() + +class TestSetSpeed(QMPTestCase): + image_len = 80 * 1024 * 1024 # MB + + def setUp(self): + qemu_img('create', 'backing.img', str(TestSetSpeed.image_len)) + qemu_img('create', '-f', 'qed', '-o', 'backing_file=backing.img', 'test.qed') + self.vm = VM().add_drive('test.qed') + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + os.remove('test.qed') + os.remove('backing.img') + + # This doesn't print or verify anything, only use it via "test-stream.py TestSetSpeed" + def test_stream_set_speed(self): + self.assert_no_active_streams() + + result = self.vm.qmp('block_stream', device='drive0') + self.assert_qmp(result, 'return', {}) + + result = self.vm.qmp('block_job_set_speed', device='drive0', value=8 * 1024 * 1024) + self.assert_qmp(result, 'return', {}) + + 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/type', 'stream') + self.assert_qmp(event, 'data/device', 'drive0') + self.assert_qmp(event, 'data/offset', self.image_len) + self.assert_qmp(event, 'data/len', self.image_len) + completed = True + + self.assert_no_active_streams() + +if __name__ == '__main__': + unittest.main()