diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py
index 25a403b..63ea20f 100644
--- a/client/virt/libvirt_vm.py
+++ b/client/virt/libvirt_vm.py
@@ -465,6 +465,156 @@ def virsh_migrate(name="", dest_uri="", option="", extra="", 
uri="",

+
+def virsh_list(option="--all", uri = ""):
+    """
+    - list domains -
+    @param option: Can take various values see 'virsh help list'
+                   for more details.
+    @param uri   : connect uri
+
+    Returns a list :
+    Returns dictionary with each entry
+    as :<'dom-name'>: [<dom-id>  ,<dom-status>  ]
+
+    """

Hi Lucas:

I need to test virsh list.
Since this function just return output of virsh list,
I can not get status from this function.

I have sent a same name function of virsh_list in my virsh list patchset.
Do you have some idea about this problem?

Should I move my virsh_list to my test file virsh_list.py?

+    cmd = "list %s" % option
+    try:
+        cmd_res = virsh_cmd(cmd, uri).stdout.strip()
+        d = {}
+        for line in cmd_res.split("\n"):
+            l = []
+            m = re.search('^\s([0-9]*\-?)\s+([0-9a-zA-Z\.-_]+)\s+(.*)', line)
+            if m:
+                l.append(m.group(1))
+                l.append(m.group(3))
+                d[m.group(2)] = l
+        return d
+    except error.CmdError, details:
+        raise virt_vm.LibvirtVirshCmdError(virsh_cmd=cmd, connuri=uri,
+        details=details)
+
+
+def list_all_domains(uri, option="--all"):
+    """
+    Returns list of domains for the uri
+    """
+    try:
+        val = virsh_list(option, uri)
+        return val
+    except virt_vm.LibvirtVirshCmdError:
+        raise virt_vm.LibvirtFailedGettingInfoError(connuri=uri)
+
+
+def get_graphics_info(name, uri, displaytype):
+    """
+    Returns Graphics display Information configured for a given
+    domain.
+    ** Currently supports only VNC display **
+    """
+    if displaytype == "vnc":
+        try:
+            """
+            Add virsh functions to get graphics information
+            here.
+            """
+            vncinfo = virsh_vncdisplay(name, uri)
+            if vncinfo:
+                ip = vncinfo[0]
+                port = vncinfo[1]
+                return [ip, port]
+        except virt_vm.UnsupportedDisplayError:
+            raise NotImplementedError("Only VMs with VNC"
+                                          "displays are supported")
+        except virt_vm.LibvirtVirshCmdError:
+            raise virt_vm.LibvirtFailedGettingInfoError(name, uri)
+
+
+def get_free_port(name, uri, type, startport, endport):
+    """
+    Get first free port available for a given application.
+    ** Currently supports only VNC **
+    Returns : port (>0) : success
+              -1        : Failure
+    """
+    if type == "vnc":
+        val={}
+        assigned_ports=[]
+        maxport = 0
+        port = 0
+        try:
+            val = list_all_domains(uri)
+        except virt_vm.LibvirtFailedGettingInfoError:
+            logging.error("Failed to get domains information for "
+                          "%s" % (self.connect_uri))
+            return -1
+        for key in val.keys():
+            try:
+                vncinfo = get_graphics_info(key, uri, type)
+                p = int(vncinfo[1])
+                if p>= startport:
+                    p = p-int(startport)
+                if p>= maxport:
+                    maxport = p
+                assigned_ports.append(p)
+            except (virt_vm.LibvirtFailedGettingInfoError,
+                    NotImplementedError):
+                logging.error("Failed to get information for VM %s "
+                              "at URI %s" % (name, uri))
+                return -1
+        cmd = "vncserver -list | grep ^:[0-9]* | cut -c1-4"
+        cmd_result = str(utils.run(cmd))
+        for line in cmd_result.split("\n"):
+            m = re.search('^:(\d)\s*$', line)
+            if m:
+                vncsp = m.group(1)
+                vncserverport =  int(vncsp)
+                if vncserverport>= maxport:
+                    maxport = vncserverport
+                assigned_ports.append(vncserverport)
+        for p in range(0,endport-startport):
+            if not p in assigned_ports:
+                port = p
+                if virt_utils.find_free_port(port,port+1):
+                    break
+                else:
+                    continue
+        port += startport
+        return port
+

  def virsh_attach_device(name, xml_file, extra="", uri=""):
      """
@@ -1061,8 +1211,10 @@ class VM(virt_vm.BaseVM):
                  vm.vnclisten = params.get("vnclisten")
              virt_install_cmd += add_vnclisten(help, vm.vnclisten)
          elif params.get("display") == "sdl":
+            self.displaytype = "sdl"
              virt_install_cmd += add_sdl(help)
          elif params.get("display") == "nographic":
+            self.displaytype = "nographic"
              virt_install_cmd += add_nographic(help)

          video_device = params.get("video_device")


--
Best Regards
Yu Mingfei

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to