[libvirt] [test-API][PATCH] utils: Update remote_exec_pexpect function and sync cases

Wayne Sun gsun at redhat.com
Mon Aug 20 10:09:33 UTC 2012


After delete duplicate remote_exec_pexect function, the left function
with same name causes some problem with cases, so update the function
and sync all cases using it.

Signed-off-by: Wayne Sun <gsun at redhat.com>
---
 repos/domain/cpu_topology.py    |    8 +++---
 repos/domain/define.py          |    7 +++--
 repos/remoteAccess/tcp_setup.py |   36 +++++++++++++++++-----------------
 repos/remoteAccess/tls_setup.py |   41 ++++++++++++++++++++-------------------
 utils/utils.py                  |   19 ++++++++---------
 5 files changed, 56 insertions(+), 55 deletions(-)

diff --git a/repos/domain/cpu_topology.py b/repos/domain/cpu_topology.py
index 5f4ef52..14fe67d 100644
--- a/repos/domain/cpu_topology.py
+++ b/repos/domain/cpu_topology.py
@@ -126,17 +126,17 @@ def cpu_topology_chk(ip, username, password,
     lscpu = "lscpu"
     # sleep for 5 seconds
     time.sleep(40)
-    ret, output = utils.remote_exec_pexpect(ip, username, password, lscpu)
+    ret = utils.remote_exec_pexpect(ip, username, password, lscpu)
     logger.debug("lscpu:")
-    logger.debug(output)
-    if ret:
+    logger.debug(ret)
+    if ret == "TIMEOUT!!!":
         logger.error("failed to run lscpu on guest OS")
         return 1
 
     int = 0
     actual_thread = actual_core = actual_socket = ''
 
-    for item in output.strip().split('\r'):
+    for item in ret.split('\r'):
         if int == 5:
             actual_thread = item.split()[-1]
             logger.info("the actual thread in the guest is %s" % actual_thread)
diff --git a/repos/domain/define.py b/repos/domain/define.py
index a70ddad..2b127a7 100644
--- a/repos/domain/define.py
+++ b/repos/domain/define.py
@@ -44,9 +44,10 @@ def check_define_domain(guestname, virt_type, hostname, username, \
 
     if hostname:
         cmd = "ls %s" % path
-        ret, output = utils.remote_exec_pexpect(hostname, username, \
-                                               password, cmd)
-        if ret:
+        ret = utils.remote_exec_pexpect(hostname, username, password, cmd)
+        cmd = "grep 'No such file' %s" % ret
+        ret = utils.exec_cmd(cmd)
+        if ret == 0:
             logger.error("guest %s xml file doesn't exsits" % guestname)
             return False
         else:
diff --git a/repos/remoteAccess/tcp_setup.py b/repos/remoteAccess/tcp_setup.py
index ddbe333..7a963e7 100644
--- a/repos/remoteAccess/tcp_setup.py
+++ b/repos/remoteAccess/tcp_setup.py
@@ -26,9 +26,9 @@ def sasl_user_add(target_machine, username, password, logger):
     """ execute saslpasswd2 to add sasl user """
     logger.info("add sasl user on server side")
     saslpasswd2_add = "echo %s | %s -a libvirt %s" % (password, SASLPASSWD2, username)
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, saslpasswd2_add)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to add sasl user")
         return 1
 
@@ -40,18 +40,18 @@ def tcp_libvirtd_set(target_machine, username, password,
     logger.info("setting libvirtd.conf on libvirt server")
     # open libvirtd --listen option
     listen_open_cmd = "echo 'LIBVIRTD_ARGS=\"--listen\"' >> %s" % SYSCONFIG_LIBVIRTD
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, listen_open_cmd)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to uncomment --listen in %s" % SYSCONFIG_LIBVIRTD)
         return 1
 
     # set listen_tls
     logger.info("set listen_tls to 0 in %s" % LIBVIRTD_CONF)
     listen_tls_disable = "echo \"listen_tls = 0\" >> %s" % LIBVIRTD_CONF
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, listen_tls_disable)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to set listen_tls to 0 in %s" % LIBVIRTD_CONF)
         return 1
 
@@ -59,27 +59,27 @@ def tcp_libvirtd_set(target_machine, username, password,
     if listen_tcp == 'enable':
         logger.info("enable listen_tcp = 1 in %s" % LIBVIRTD_CONF)
         listen_tcp_set = "echo 'listen_tcp = 1' >> %s" % LIBVIRTD_CONF
-        ret, output = utils.remote_exec_pexpect(target_machine, username,
+        ret = utils.remote_exec_pexpect(target_machine, username,
                                         password, listen_tcp_set)
-        if ret:
+        if ret == "TIMEOUT!!!":
             logger.error("failed to set listen_tcp in %s" % LIBVIRTD_CONF)
             return 1
 
     # set auth_tcp
     logger.info("set auth_tcp to \"%s\" in %s" % (auth_tcp, LIBVIRTD_CONF))
     auth_tcp_set = "echo 'auth_tcp = \"%s\"' >> %s" % (auth_tcp, LIBVIRTD_CONF)
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                        password, auth_tcp_set)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to set auth_tcp in %s" % LIBVIRTD_CONF)
         return 1
 
     # restart remote libvirtd service
     libvirtd_restart_cmd = "service libvirtd restart"
     logger.info("libvirtd restart")
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, libvirtd_restart_cmd)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to restart libvirtd service")
         return 1
 
@@ -187,18 +187,18 @@ def tcp_setup_clean(params):
 
     if auth_tcp == 'sasl':
         saslpasswd2_delete = "%s -a libvirt -d %s" % (SASLPASSWD2, username)
-        ret, output = utils.remote_exec_pexpect(target_machine, username,
+        ret = utils.remote_exec_pexpect(target_machine, username,
                                         password, saslpasswd2_delete)
-        if ret:
+        if ret == "TIMEOUT!!!":
             logger.error("failed to delete sasl user")
     libvirtd_conf_retore = "sed -i -n '/^[ #]/p' %s" % LIBVIRTD_CONF
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, libvirtd_conf_retore)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to restore %s" % LIBVIRTD_CONF)
 
     sysconfig_libvirtd_restore = "sed -i -n '/^[ #]/p' %s" % SYSCONFIG_LIBVIRTD
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, sysconfig_libvirtd_restore)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to restore %s" % SYSCONFIG_LIBVIRTD)
diff --git a/repos/remoteAccess/tls_setup.py b/repos/remoteAccess/tls_setup.py
index f8b3cea..3e614f5 100644
--- a/repos/remoteAccess/tls_setup.py
+++ b/repos/remoteAccess/tls_setup.py
@@ -178,8 +178,9 @@ def deliver_cert(target_machine, username, password, pkipath, logger):
 
     # mkdir /etc/pki/libvirt/private on remote host
     libvirt_priv_cmd = "mkdir -p %s" % PRIVATE_KEY_FOLDER
-    ret, output = utils.remote_exec_pexpect(target_machine, username, password, libvirt_priv_cmd)
-    if ret:
+    ret = utils.remote_exec_pexpect(target_machine, username, password,
+                                    libvirt_priv_cmd)
+    if ret == "TIMEOUT!!!":
         logger.error("failed to make /etc/pki/libvirt/private on %s" % target_machine)
         return 1
 
@@ -222,9 +223,9 @@ def sasl_user_add(target_machine, username, password, logger):
     """ execute saslpasswd2 to add sasl user """
     logger.info("add sasl user on server side")
     saslpasswd2_add = "echo %s | %s -a libvirt %s" % (password, SASLPASSWD2, username)
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, saslpasswd2_add)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to add sasl user")
         return 1
 
@@ -236,36 +237,36 @@ def tls_libvirtd_set(target_machine, username, password,
     logger.info("setting libvirtd.conf on tls server")
     # open libvirtd --listen option
     listen_open_cmd = "echo 'LIBVIRTD_ARGS=\"--listen\"' >> /etc/sysconfig/libvirtd"
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, listen_open_cmd)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to uncomment --listen in /etc/sysconfig/libvirtd")
         return 1
 
     if listen_tls == 'disable':
         logger.info("set listen_tls to 0 in %s" % LIBVIRTD_CONF)
         listen_tls_disable = "echo \"listen_tls = 0\" >> %s" % LIBVIRTD_CONF
-        ret, output = utils.remote_exec_pexpect(target_machine, username,
+        ret = utils.remote_exec_pexpect(target_machine, username,
                                         password, listen_tls_disable)
-        if ret:
+        if ret == "TIMEOUT!!!":
             logger.error("failed to set listen_tls to 0 in %s" % LIBVIRTD_CONF)
             return 1
 
     if auth_tls == 'sasl':
         logger.info("enable auth_tls = sasl in %s" % LIBVIRTD_CONF)
         auth_tls_set = "echo 'auth_tls = \"sasl\"' >> %s" % LIBVIRTD_CONF
-        ret, output = utils.remote_exec_pexpect(target_machine, username,
+        ret = utils.remote_exec_pexpect(target_machine, username,
                                        password, auth_tls_set)
-        if ret:
+        if ret == "TIMEOUT!!!":
             logger.error("failed to set auth_tls to sasl in %s" % LIBVIRTD_CONF)
             return 1
 
     # restart remote libvirtd service
     libvirtd_restart_cmd = "service libvirtd restart"
     logger.info("libvirtd restart")
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, libvirtd_restart_cmd)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to restart libvirtd service")
         return 1
 
@@ -276,9 +277,9 @@ def iptables_stop(target_machine, username, password, logger):
     """ This is a temprory method in favor of migration """
     logger.info("stop local and remote iptables temprorily")
     iptables_stop_cmd = "service iptables stop"
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                    password, iptables_stop_cmd)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to stop remote iptables service")
         return 1
 
@@ -423,15 +424,15 @@ def tls_setup_clean(params):
     auth_tls = params['auth_tls']
 
     cacert_rm = "rm -f %s/cacert.pem" % CA_FOLDER
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, cacert_rm)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to remove cacert.pem on remote machine")
 
     ca_libvirt_rm = "rm -rf %s" % CERTIFICATE_FOLDER
-    ret, output = utils.remote_exec_pexpect(target_machine, username,
+    ret = utils.remote_exec_pexpect(target_machine, username,
                                     password, ca_libvirt_rm)
-    if ret:
+    if ret == "TIMEOUT!!!":
         logger.error("failed to remove libvirt folder")
 
     os.remove("%s/cacert.pem" % CA_FOLDER)
@@ -439,8 +440,8 @@ def tls_setup_clean(params):
 
     if auth_tls == 'sasl':
         saslpasswd2_delete = "%s -a libvirt -d %s" % (SASLPASSWD2, username)
-        ret, output = utils.remote_exec_pexpect(target_machine, username,
+        ret = utils.remote_exec_pexpect(target_machine, username,
                                         password, saslpasswd2_delete)
-        if ret:
+        if ret == "TIMEOUT!!!":
             logger.error("failed to delete sasl user")
 
diff --git a/utils/utils.py b/utils/utils.py
index b174a58..40b3724 100644
--- a/utils/utils.py
+++ b/utils/utils.py
@@ -431,12 +431,10 @@ def remote_exec_pexpect(hostname, username, password, cmd):
             child.sendline(password)
         elif index == 2:
             child.close()
-            return 0, child.before
+            return string.strip(child.before)
         elif index == 3:
             child.close()
-            return 1, ""
-
-    return 0
+            return "TIMEOUT!!!"
 
 def scp_file(hostname, username, password, target_path, file):
     """ Scp file to remote host """
@@ -535,12 +533,13 @@ def get_remote_memory(hostname, username, password):
     i = 0
     while i < 3:
         i += 1
-        memsize = \
-        int(remote_exec_pexpect(hostname, username, password, cmd)) * 1024
-        if memsize == -1:
-            continue
-        else:
-            break
+        ret = remote_exec_pexpect(hostname, username, password, cmd)
+        if ret != "TIMEOUT!!!":
+            memsize = int(ret) * 1024
+            if memsize == -1:
+                continue
+            else:
+                break
     return memsize
 
 def get_remote_kernel(hostname, username, password):
-- 
1.7.1




More information about the libvir-list mailing list