[libvirt] [test-API][PATCH v2] Reconnct libvirt after libvirtd restart

Wayne Sun gsun at redhat.com
Wed Aug 8 08:07:15 UTC 2012


In domain_nfs_start case, libvirtd will be restarted during test,
which broke existing connection. User need re-init connection in
test case, for this:
  * New get_conn function in utils for get libvirt connection
  * sharemod_init in env_inspect use get_conn to get libvirt
    connection
  * In case domain_nfs_start, use get_conn to get new connectin
    after libvirtd restarted.

Signed-off-by: Wayne Sun <gsun at redhat.com>
---
 repos/sVirt/domain_nfs_start.py |    9 +++++++--
 src/env_inspect.py              |   22 ++--------------------
 utils/utils.py                  |   29 ++++++++++++++++++++++++++++-
 3 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/repos/sVirt/domain_nfs_start.py b/repos/sVirt/domain_nfs_start.py
index 88d349c..5475945 100644
--- a/repos/sVirt/domain_nfs_start.py
+++ b/repos/sVirt/domain_nfs_start.py
@@ -12,7 +12,6 @@ import sys
 
 import libvirt
 from libvirt import libvirtError
-
 from src import sharedmod
 from utils import utils
 from shutil import copy
@@ -163,6 +162,8 @@ def domain_nfs_start(params):
         logger.error("Error: fail to get domain %s xml" % guestname)
         return 1
 
+    conn.close()
+
     # set env
     logger.info("prepare the environment")
     ret = prepare_env(dynamic_ownership, virt_use_nfs, guestname, \
@@ -171,6 +172,10 @@ def domain_nfs_start(params):
         logger.error("failed to prepare the environment")
         return 1
 
+    # reconnect libvirt
+    conn = utils.get_conn()
+    sharedmod.libvirtobj['conn'] = conn
+
     domobj = conn.lookupByName(guestname)
 
     logger.info("begin to test start domain from nfs storage")
@@ -283,7 +288,7 @@ def domain_nfs_start(params):
         logger.error("Error: fail to get domain %s state" % guestname)
         return 1
 
-    if state != "shutoff":
+    if state != libvirt.VIR_DOMAIN_SHUTOFF:
         logger.info("shut down the domain %s" % guestname)
         try:
             domobj.destroy()
diff --git a/src/env_inspect.py b/src/env_inspect.py
index b260ff8..2c1a701 100644
--- a/src/env_inspect.py
+++ b/src/env_inspect.py
@@ -20,6 +20,7 @@
 import commands
 import libvirt
 import sharedmod
+from utils import utils
 
 def check_libvirt(logger):
     virsh = 'virsh -v'
@@ -68,20 +69,6 @@ def hostinfo(logger):
         return 1
     return 0
 
-def request_credentials(credentials, user_data):
-    for credential in credentials:
-        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
-            credential[4] = user_data[0]
-
-            if len(credential[4]) == 0:
-                credential[4] = credential[3]
-        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
-            credential[4] = user_data[1]
-        else:
-            return -1
-
-    return 0
-
 def sharemod_init(env_parser, logger):
     """ get connection object from libvirt module
         initialize sharemod for use by testcases
@@ -89,12 +76,7 @@ def sharemod_init(env_parser, logger):
     uri = env_parser.get_value('variables', 'defaulturi')
     username = env_parser.get_value('variables', 'username')
     password = env_parser.get_value('variables', 'password')
-    user_data = [username, password]
-    auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data]
-    conn = libvirt.openAuth(uri, auth, 0)
-    if not conn:
-        logger.error("Failed to setup libvirt connection");
-        return 1
+    conn = utils.get_conn(uri, username, password)
 
     # initialize conn object in sharedmod
     sharedmod.libvirtobj.clear()
diff --git a/utils/utils.py b/utils/utils.py
index be87cdc..9167c29 100644
--- a/utils/utils.py
+++ b/utils/utils.py
@@ -29,6 +29,7 @@ import struct
 import pexpect
 import string
 import subprocess
+import libvirt
 from xml.dom import minidom
 from urlparse import urlparse
 
@@ -57,6 +58,32 @@ def get_uri(ip):
             uri = "qemu+ssh://%s/system" % ip
     return uri
 
+def request_credentials(credentials, user_data):
+    for credential in credentials:
+        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
+            credential[4] = user_data[0]
+
+            if len(credential[4]) == 0:
+                credential[4] = credential[3]
+        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
+            credential[4] = user_data[1]
+        else:
+            return -1
+
+    return 0
+
+def get_conn(uri=None, username='', password=''):
+    """ get connection object from libvirt module
+    """
+    user_data = [username, password]
+    auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data]
+    conn = libvirt.openAuth(uri, auth, 0)
+    if not conn:
+        logger.error("Failed to setup libvirt connection");
+        sys.exit(1)
+    else:
+        return conn
+
 def parse_uri(uri):
     # This is a simple parser for uri
     return urlparse(uri)
@@ -501,7 +528,7 @@ def remote_exec_pexpect(hostname, username, password, cmd):
         elif index == 1:
             child.sendline(password)
         elif index == 2:
-            return string.strip(child.before)
+            return 0, string.strip(child.before)
         elif index == 3:
             return "TIMEOUT!!!"
 
-- 
1.7.1




More information about the libvir-list mailing list