From patchwork Thu Oct 7 19:52:06 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 238801 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id o97JqOvM021243 for ; Thu, 7 Oct 2010 19:52:24 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755057Ab0JGTwV (ORCPT ); Thu, 7 Oct 2010 15:52:21 -0400 Received: from mx1.redhat.com ([209.132.183.28]:22112 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754976Ab0JGTwU (ORCPT ); Thu, 7 Oct 2010 15:52:20 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o97JqJ7Z006479 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 7 Oct 2010 15:52:19 -0400 Received: from localhost (ovpn-113-106.phx2.redhat.com [10.3.113.106]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o97JqIgL009030; Thu, 7 Oct 2010 15:52:19 -0400 From: Luiz Capitulino To: autotest@test.kernel.org Cc: kvm@vger.kernel.org, lmr@redhat.com, ehabkost@redhat.com, armbru@redhat.com Subject: [PATCH 2/3] QMPMonitor: Introduce the send() method Date: Thu, 7 Oct 2010 16:52:06 -0300 Message-Id: <1286481127-24640-3-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1286481127-24640-1-git-send-email-lcapitulino@redhat.com> References: <1286481127-24640-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Thu, 07 Oct 2010 19:52:25 +0000 (UTC) diff --git a/client/tests/kvm/kvm_monitor.py b/client/tests/kvm/kvm_monitor.py index c23835c..9a66388 100644 --- a/client/tests/kvm/kvm_monitor.py +++ b/client/tests/kvm/kvm_monitor.py @@ -604,6 +604,55 @@ class QMPMonitor(Monitor): return self._greeting + def send(self, data, timeout=20): + """ + Send data to the QMP monitor and return its response. + + @param data: Data to send + @param timeout: Time duration to wait for response + @return: The response received + @raise MonitorLockError: Raised if the lock cannot be acquired + @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorProtocolError: Raised if no response is received + """ + if not self._acquire_lock(20): + raise MonitorLockError("Could not acquire exclusive lock to send " + "QMP command '%s'" % cmd) + try: + self._read_objects() + self._socket.sendall(data) + end_time = time.time() + timeout + while time.time() < end_time: + for obj in self._read_objects(): + if isinstance(obj, dict): + if "return" in obj or "error" in obj: + return obj + time.sleep(0.1) + else: + raise MonitorProtocolError("Received no response (data: %s)" + % str(data)) + except socket.error: + raise MonitorSendError("Could not send data '%s'" % str(data)) + finally: + self._lock.release() + + + def send_cmd(self, command, timeout=20): + """ + Send a QMP command. This is a simple wrapper to send() method that does + JSON formatting. + + @param command: QMP command to send + @param timeout: Time duration to wait for response + @return: The response received + @raise MonitorLockError: Raised if the lock cannot be acquired + @raise MonitorSendError: Raised if the command cannot be sent + @raise MonitorProtocolError: Raised if no response is received + + """ + return self.send(json.dumps(command) + "\n", timeout) + + # Command wrappers # Note: all of the following functions raise exceptions in a similar manner # to cmd() and _get_command_output().