Re: [test-API PATCH 2/7] lib: fix streamAPI class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 03/21/2012 08:46 PM, Peter Krempa wrote:
The streamAPI class that encapsulates work with libvirt's streams was
fundamentaly broken:
  - each call to one of the methods created a new stream and
    performed the call
  - some methods called virStream methods with different numbers
    of arguments
  - there was no way to extract the actual libvirt stream object
              thanks for the patch.

---
  lib/streamAPI.py |   52 ++++++++++++++++++++++++++--------------------------
  1 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/lib/streamAPI.py b/lib/streamAPI.py
index bc7d217..69e183e 100644
--- a/lib/streamAPI.py
+++ b/lib/streamAPI.py
@@ -38,76 +38,76 @@ append_path(result.group(0))
  import exception

  class StreamAPI(object):
-    def __init__(self, connection):
-        self.conn = connection
+    def __init__(self, conn, flags = 0):
+        try:
+            self.stream = conn.newStream(flags)
+        except libvirt.libvirtError, e:
+            message = e.get_error_message()
+            code = e.get_error_code()
+            raise exception.LibvirtAPI(message, code)
+
+    def getStream(self):
+        return self.stream

-    def abort(self, flag = 0):
+    def abort(self):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.abort()
+            return self.stream.abort()
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def connect(self, flag = 0):
+    def connect(self):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.connect()
+            return self.stream.connect()
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def finish(self, flag = 0):
+    def finish(self):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.finish()
+            return self.stream.finish()
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def recv(self, flag = 0, data, nbytes):
+    def recv(self, nbytes):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.recv(data, nbytes)
+            return self.stream.recv(nbytes)
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def send(self, flag = 0, data, nbytes):
+    def send(self, data):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.send(data, nbytes)
+            return self.stream.send(data)
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def eventAddCallback(self, flag = 0, cb, opaque):
+    def eventAddCallback(self, cb, opaque):

The API need an argument event to pass in which is virEventHandleType
             VIR_EVENT_HANDLE_READABLE = 1
             VIR_EVENT_HANDLE_WRITABLE = 2
             VIR_EVENT_HANDLE_ERROR = 4
             VIR_EVENT_HANDLE_HANGUP = 8

We could just overdefine it in streamAPI.py which make use of them easier for
       writing testcase.
       It's not a good idea to import libvirt.py directly in testcases.



          try:
-            stream_obj = newStream(flag)
-            return stream_obj.eventAddCallback(cb, opaque)
+            return self.stream.eventAddCallback(cb, opaque)
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def eventRemoveCallback(self, flag = 0):
+    def eventRemoveCallback(self):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.eventRemoveCallback()
+            return self.stream.eventRemoveCallback()
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
              raise exception.LibvirtAPI(message, code)

-    def eventUpdateCallback(self, flag = 0, events)
+    def eventUpdateCallback(self, events):
          try:
-            stream_obj = newStream(flag)
-            return stream_obj.eventUpdateCallback(events)
+            return self.stream.eventUpdateCallback(events)
          except libvirt.libvirtError, e:
              message = e.get_error_message()
              code = e.get_error_code()
Miss a "raise exception.LibvirtAPI(message, code)" in the eventUpdateCallback The framework has a its own exception set, it includes the error exception from libvirtError So, in each testcases, we just catch the LibvirtAPI to raise API error in try..catch clause rather than catch all of other errors from python basic modules or somewhere.

        Guannan Ren

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]