Google
  Web www.spinics.net

[test-API 03/14] Destroy utils/check.py and move the functions into utils/utils.py

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


Scripts which use 'check.py' are updated to use "utils.py".
---
 repos/domain/balloon_memory.py      |    2 +-
 repos/domain/dump.py                |    2 +-
 repos/domain/install_linux_check.py |   15 +-
 repos/snapshot/file_flag.py         |    3 +-
 repos/snapshot/flag_check.py        |    3 +-
 utils/check.py                      |  392 -----------------------------------
 utils/utils.py                      |  362 ++++++++++++++++++++++++++++++++
 7 files changed, 373 insertions(+), 406 deletions(-)
 delete mode 100644 utils/check.py

diff --git a/repos/domain/balloon_memory.py b/repos/domain/balloon_memory.py
index f5beae4..fc7383c 100644
--- a/repos/domain/balloon_memory.py
+++ b/repos/domain/balloon_memory.py
@@ -23,7 +23,7 @@ def get_mem_size(ip):
 
     username = 'root'
     password = 'redhat'
-    current_memory = check.get_remote_memory(ip, username, password)
+    current_memory = utils.get_remote_memory(ip, username, password)
     return current_memory
 
 def compare_memory(expect_memory, actual_memory):
diff --git a/repos/domain/dump.py b/repos/domain/dump.py
index 60c5a91..24b2353 100644
--- a/repos/domain/dump.py
+++ b/repos/domain/dump.py
@@ -45,7 +45,7 @@ def check_guest_kernel(*args):
 
     logger.debug("guest ip address: %s" %ipaddr)
 
-    kernel = check.get_remote_kernel(ipaddr, "root", "redhat")
+    kernel = utils.get_remote_kernel(ipaddr, "root", "redhat")
     logger.debug("current kernel version: %s" %kernel)
 
     if kernel:
diff --git a/repos/domain/install_linux_check.py b/repos/domain/install_linux_check.py
index c6d2cb3..7a5a9ac 100644
--- a/repos/domain/install_linux_check.py
+++ b/repos/domain/install_linux_check.py
@@ -13,7 +13,6 @@ from libvirt import libvirtError
 
 from src import sharedmod
 from utils import utils
-from utils import check
 from utils import env_parser
 
 required_params = ('guestname', 'guesttype', 'hdmodel', 'nicmodel',)
@@ -95,9 +94,9 @@ def install_linux_check(params):
 
     # Creat file and read file in guest.
     logger.info("check point2: creat and read dirctory/file in guest")
-    if check.create_dir(ipaddr, "root", "redhat") == 0:
+    if utils.create_dir(ipaddr, "root", "redhat") == 0:
         logger.info("create dir - /tmp/test successfully")
-        if check.write_file(ipaddr, "root", "redhat") == 0:
+        if utils.write_file(ipaddr, "root", "redhat") == 0:
             logger.info("write and read file: /tmp/test/test.log successfully")
         else:
             logger.error("Error: fail to write/read file - /tmp/test/test.log")
@@ -114,7 +113,7 @@ def install_linux_check(params):
     vcpunum_expect = int(utils.get_num_vcpus(domain_name))
     logger.info("vcpu number in domain config xml - %s is %s" % \
                  (domain_name, vcpunum_expect))
-    vcpunum_actual = int(check.get_remote_vcpus(ipaddr, "root", "redhat"))
+    vcpunum_actual = int(utils.get_remote_vcpus(ipaddr, "root", "redhat"))
     logger.info("The actual vcpu number in guest - %s is %s" %
                  (domain_name, vcpunum_actual))
     if vcpunum_expect == vcpunum_actual:
@@ -132,7 +131,7 @@ def install_linux_check(params):
     mem_expect = utils.get_size_mem(domain_name)
     logger.info("current mem size in domain config xml - %s is %s" %
                  (domain_name, mem_expect))
-    mem_actual = check.get_remote_memory(ipaddr, "root", "redhat")
+    mem_actual = utils.get_remote_memory(ipaddr, "root", "redhat")
     logger.info("The actual mem size in guest - %s is %s" %
                 (domain_name, mem_actual))
     diff_range = int(mem_expect) * 0.07
@@ -155,7 +154,7 @@ def install_linux_check(params):
     envparser = env_parser.Envparser(envfile)
     file_url = envparser.get_value("other", "wget_url")
 
-    if check.run_wget_app(ipaddr, "root", "redhat", file_url, logger) == 0:
+    if utils.run_wget_app(ipaddr, "root", "redhat", file_url, logger) == 0:
         logger.info("run wget successfully in guest.")
     else:
         logger.error("Error: fail to run wget in guest")
@@ -166,9 +165,9 @@ def install_linux_check(params):
     if 'kvm' in guesttype or 'xenfv' in guesttype:
         logger.info("check point6: check nic and blk driver in guest is \
                      expected as your config:")
-        if check.validate_remote_nic_type(ipaddr, "root", "redhat",
+        if utils.validate_remote_nic_type(ipaddr, "root", "redhat",
            nic_type, logger) == 0 and \
-           check.validate_remote_blk_type(ipaddr, "root", "redhat",
+           utils.validate_remote_blk_type(ipaddr, "root", "redhat",
                                         blk_type, logger) == 0:
             logger.info("nic type - %s and blk type - %s check successfully" %
                        (nic_type, blk_type))
diff --git a/repos/snapshot/file_flag.py b/repos/snapshot/file_flag.py
index 9f1c181..5f8a17e 100644
--- a/repos/snapshot/file_flag.py
+++ b/repos/snapshot/file_flag.py
@@ -11,7 +11,6 @@ from libvirt import libvirtError
 
 from src import sharedmod
 from utils import utils
-from utils import check
 
 required_params = ('guestname', 'username', 'password',)
 optional_params = ()
@@ -35,7 +34,7 @@ def check_domain_running(conn, guestname, logger):
 
 def make_flag(ipaddr, username, password, logger):
     """ enter guest OS, create a file in /tmp folder """
-    ret = check.remote_exec_pexpect(ipaddr, username, password, MAKE_FLAG)
+    ret = utils.remote_exec_pexpect(ipaddr, username, password, MAKE_FLAG)
     if ret == "TIMEOUT!!!":
         logger.error("connecting to guest OS timeout")
         return False
diff --git a/repos/snapshot/flag_check.py b/repos/snapshot/flag_check.py
index 9386e2b..3a0f3ec 100644
--- a/repos/snapshot/flag_check.py
+++ b/repos/snapshot/flag_check.py
@@ -10,7 +10,6 @@ from libvirt import libvirtError
 
 from src import sharedmod
 from utils import utils
-from utils import check
 
 required_params = ('guestname', 'username', 'password',)
 optional_params = ('expectedret')
@@ -69,7 +68,7 @@ def flag_check(params):
         logger.info("vm %s failed to get ip address" % guestname)
         return 1
 
-    ret = check.remote_exec_pexpect(ipaddr, username, password, FLAG_CHECK)
+    ret = utils.remote_exec_pexpect(ipaddr, username, password, FLAG_CHECK)
     if ret == "TIMEOUT!!!":
         logger.error("connecting to guest OS timeout")
         return 1
diff --git a/utils/check.py b/utils/check.py
deleted file mode 100644
index 1db49e7..0000000
--- a/utils/check.py
+++ /dev/null
@@ -1,392 +0,0 @@
-#!/usr/bin/env python
-#
-# Copyright (C) 2010-2012 Red Hat, Inc.
-#
-# libvirt-test-API is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation, either version 2 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranties of
-# TITLE, NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
-# PURPOSE.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-#
-# Filename: check.py
-# Summary: basic check operation needed by test
-# Description: The module is a tool to help conduct basic
-#              check operation on specified host
-
-import os
-import re
-import time
-import string
-import pty
-import commands
-import signal
-import pexpect
-
-def support_virt(self):
-    cmd = "cat /proc/cpuinfo | egrep '(vmx|svm)'"
-    if commands.getoutput(cmd) is None:
-        print 'CPU does not support VT.'
-        return False
-    else:
-        return True
-
-def subproc(a, b):
-    subproc_flag = 1
-
-def remote_exec(hostname, username, password, cmd):
-    """Remote execution on specified host"""
-    pid, fd = pty.fork()
-    if pid == 0:
-        try:
-            os.execv("/usr/bin/ssh", ["/usr/bin/ssh", "-l",
-                     username, hostname, cmd])
-        except OSError, e:
-            print "OSError: " + str(e)
-            return -1
-    else:
-        signal.signal(signal.SIGCHLD, subproc)
-        try:
-            timeout = 50
-            i = 0
-            while  i <= timeout:
-
-                time.sleep(1)
-                str = os.read(fd, 10240)
-
-                if re.search('(yes\/no)', str):
-                    os.write(fd, "yes\r")
-
-                elif re.search('password:', str):
-                    os.write(fd, password + "\r")
-
-                elif subproc_flag == 1:
-                    ret = string.strip(str)
-                    break
-                elif i == timeout:
-                    print "TIMEOUT!!!!"
-                    return -1
-
-                i = i+1
-
-            subproc_flag = 0
-            return ret
-        except Exception, e:
-            subproc_flag = 0
-            return -1
-
-def remote_exec_pexpect(hostname, username, password, cmd):
-    """Remote exec function via pexpect"""
-    user_hostname = "%s@%s" % (username, hostname)
-    child = pexpect.spawn("/usr/bin/ssh", [user_hostname, cmd],
-                          timeout = 60, maxread = 2000, logfile = None)
-    #child.logfile = sys.stdout
-    while True:
-        index = child.expect(['(yes\/no)', 'password:', pexpect.EOF,
-                             pexpect.TIMEOUT])
-        if index == 0:
-            child.sendline("yes")
-        elif index == 1:
-            child.sendline(password)
-        elif index == 2:
-            return string.strip(child.before)
-        elif index == 3:
-            return "TIMEOUT!!!"
-
-def get_remote_vcpus(hostname, username, password):
-    """Get cpu number of specified host"""
-    cmd = "cat /proc/cpuinfo | grep processor | wc -l"
-    cpunum = -1
-    i = 0
-    while i < 3:
-        i += 1
-        cpunum = int(remote_exec(hostname, username, password, cmd))
-        if cpunum == -1:
-            continue
-        else:
-            break
-    return cpunum
-
-def get_remote_memory(hostname, username, password):
-    """Get memory statics of specified host"""
-    cmd = "free -m | grep -i mem | awk '{print $2}'"
-    memsize = -1
-    i = 0
-    while i < 3:
-        i += 1
-        memsize = \
-        int(remote_exec_pexpect(hostname, username, password, cmd)) * 1024
-        if memsize == -1:
-            continue
-        else:
-            break
-    return memsize
-
-def get_remote_kernel(hostname, username, password):
-    """Get kernel info of specified host"""
-    cmd = "uname -r"
-    kernel = None
-    i = 0
-    while i < 3:
-        i += 1
-        kernel = remote_exec(hostname, username, password, cmd)
-        if kernel:
-            break
-        else:
-            continue
-    return kernel
-
-def install_package(package = ''):
-    """Install specified package"""
-    if package:
-        cmd = "rpm -qa " + package
-        output = commands.getoutput(cmd)
-        pkg = output.split('\n')[0]
-        if pkg:
-            os.system("yum -y -q update " + package)
-            return pkg
-        else:
-            ret = os.system("yum -y -q install " + package)
-            if ret == 0:
-                output = commands.getoutput(cmd)
-                pkg = output.split('\n')[0]
-                if pkg:
-                    return pkg
-            else:
-                return "failed to install package"
-    else:
-        return "please input package name"
-
-def libvirt_version(latest_ver = ''):
-    """Get libvirt version info"""
-    query_virt_ver = 'rpm -qa|grep libvirt'
-    ret = commands.getoutput(query_virt_ver)
-    if ret:
-        mas_ver = ret.split('-')[-2]
-        sec_ver = (ret.split('-')[-1])
-        curr_ver = mas_ver + '-' + sec_ver
-        if latest_ver != '':
-            if curr_ver != latest_ver:
-                return (False, curr_ver)
-            else:
-                return (True, curr_ver)
-        else:
-            return curr_ver
-    else:
-        return (False, '')
-
-def create_dir(hostname, username, password):
-    """Create new dir"""
-    cmd = "mkdir /tmp/test"
-    mkdir_ret = remote_exec_pexpect(hostname, username, password, cmd)
-    if mkdir_ret == '':
-        cmd = "ls -d /tmp/test"
-        check_str = remote_exec_pexpect(hostname, username,
-                                             password, cmd)
-        if check_str == "/tmp/test":
-            return 0
-        else:
-            print "check_str = ", check_str
-            return 1
-    else:
-        print "mkdir_ret = ", mkdir_ret
-        return 1
-
-def write_file(hostname, username, password):
-    """Simple test for writting file on specified host"""
-    test_string = 'hello word testing'
-    cmd = """echo '%s'>/tmp/test/test.log""" % (test_string)
-    write_file_ret = remote_exec_pexpect(hostname, username,
-                                              password, cmd)
-    if write_file_ret == '':
-        cmd = """grep '%s' /tmp/test/test.log""" % ("hello")
-        check_str = remote_exec_pexpect(hostname, username,
-                                             password, cmd)
-        if check_str == test_string:
-            return 0
-        else:
-            print "check_str = ", check_str
-            return 1
-    else:
-        print "write_file_ret = ", write_file_ret
-        return 1
-
-def run_mount_app(hostname, username, password,
-                  target_mount, mount_point):
-    """Simple test for mount operation on specified host"""
-    cmd = """mount %s %s""" % (target_mount, mount_point)
-    mount_ret = remote_exec(hostname, username, password, cmd)
-    if mount_ret == '':
-        cmd = """df | grep '%s'""" % (target_mount)
-        check_str = remote_exec(hostname, username, password, cmd)
-        if check_str != '':
-            return 0
-        else:
-            print "mount check fail"
-            return 1
-    else:
-        print "mount fail"
-        return 1
-
-def run_wget_app(hostname, username, password, file_url, logger):
-    """Simple test for wget app on specified host"""
-    cmd_line = "wget -P /tmp %s -o /tmp/wget.log" % (file_url)
-    logger.info("Command: %s" % (cmd_line))
-    wget_ret = remote_exec_pexpect(hostname, username,
-                                        password, cmd_line)
-    cmd_line = "grep %s %s" % ('100%', '/tmp/wget.log')
-    check_ret = remote_exec_pexpect(hostname, username,
-                                         password, cmd_line)
-    if check_ret == "":
-        logger.info("grep output is nothing")
-        return 1
-    else:
-        if re.search("100%", check_ret):
-            logger.info("wget is running successfully")
-            logger.info("check_retrun: %s" % (check_ret))
-            return 0
-        else:
-            logger.info("can not find 100% in wget output")
-            logger.info("check_retrun: %s" % (check_ret))
-            return 1
-
-def validate_remote_nic_type(hostname, username,
-                             password, nic_type, logger):
-    """Validate network interface type on specified host"""
-    nic_type_to_name_dict = {'e1000':
-                             '82540EM Gigabit Ethernet Controller',
-                             'rtl8139':
-                             'RTL-8139/8139C/8139C+',
-                             'virtio':'Virtio network device'}
-    nic_type_to_driver_dict = {'e1000':'e1000', 'rtl8139':'8139cp',
-                              'virtio':'virtio_net'}
-    nic_name = nic_type_to_name_dict[nic_type]
-    nic_driver = nic_type_to_driver_dict[nic_type]
-    logger.info("nic_name = %s" % (nic_name))
-    logger.info("nic_driver = %s" % (nic_driver))
-    lspci_cmd = "lspci"
-    lsmod_cmd = "lsmod"
-    lspci_cmd_ret = remote_exec_pexpect(hostname, username,
-                                             password, lspci_cmd)
-    lsmod_cmd_ret = remote_exec_pexpect(hostname, username,
-                                             password, lsmod_cmd)
-    logger.info("------------")
-    logger.info("lspci_cmd_ret:\n %s" % (lspci_cmd_ret))
-    logger.info("------------")
-    logger.info("lsmod_cmd_ret:\n %s" % (lsmod_cmd_ret))
-    logger.info("------------")
-    if lspci_cmd_ret != "" and lsmod_cmd_ret != "":
-        cmd1 = """echo "%s" | grep '%s'""" % (lspci_cmd_ret, nic_name)
-        cmd2 = """echo "%s" | grep '%s'""" % (lsmod_cmd_ret, nic_driver)
-        status1, output1 = commands.getstatusoutput(cmd1)
-        status2, output2 = commands.getstatusoutput(cmd2)
-        if status1 == 0 and status2 == 0:
-            # other nic should not be seen in guest
-            nic_type_to_name_dict.pop(nic_type)
-            for key in nic_type_to_name_dict.keys():
-                logger.info("now try to grep other nic type \
-                            in lspci output: %s" % key)
-                other_name_cmd = """echo '%s' | grep '%s'""" % \
-                                 (lspci_cmd_ret, nic_type_to_name_dict[key])
-                ret, out = commands.getstatusoutput(other_name_cmd)
-                if ret == 0:
-                    logger.info("unspecified nic name is seen in \
-                               guest's lspci command: \n %s \n" % out)
-                    return 1
-
-            nic_type_to_driver_dict.pop(nic_type)
-            for key in nic_type_to_driver_dict.keys():
-                logger.info("now try to grep other nic type \
-                          in lsmod output: %s" % key)
-                other_driver_cmd = """echo '%s' | grep '%s'""" % \
-                               (lsmod_cmd_ret,
-                                nic_type_to_driver_dict[key])
-                ret1, out1 = commands.getstatusoutput(other_driver_cmd)
-                if ret1 == 0:
-                    logger.info("unspecified nic driver is seen \
-                               in guest's lsmod command: %s" % out)
-                    return 1
-
-            logger.info("lspci ouput about nic is: \n %s; \n \
-                        lsmod output about nic is \n %s \n" %
-                        (output1,output2))
-            return 0
-        else:
-            logger.info("lspci and lsmod and grep fail")
-            return 1
-    else:
-        logger.info("lspci and lsmod return nothing")
-        return 1
-
-def validate_remote_blk_type(hostname, username, password,
-                             blk_type, logger):
-    """Validate block device type on specified host"""
-    blk_type_to_name_dict = {'ide':'Intel Corporation 82371SB PIIX3 IDE',
-                             'virtio':'Virtio block device'}
-    blk_type_to_driver_dict = {'ide':'unknow', 'virtio':'virtio_blk'}
-    lspci_cmd = "lspci"
-    lsmod_cmd = "lsmod"
-    lspci_cmd_ret = remote_exec_pexpect(hostname, username,
-                                             password, lspci_cmd)
-    lsmod_cmd_ret = remote_exec_pexpect(hostname, username,
-                                            password, lsmod_cmd)
-    logger.info("------------")
-    logger.info("lspci_cmd_ret:\n %s" % (lspci_cmd_ret))
-    logger.info("------------")
-    logger.info("lsmod_cmd_ret: \n %s" % (lsmod_cmd_ret))
-    logger.info("------------")
-    if lspci_cmd_ret != "" and lsmod_cmd_ret != "":
-        if blk_type == "virtio":
-            blk_name = blk_type_to_name_dict[blk_type]
-            blk_driver = blk_type_to_driver_dict[blk_type]
-            logger.info("blk_name = %s \n blk_driver = %s" %
-                        (blk_name, blk_driver))
-            cmd1 = """echo "%s" | grep '%s'""" % (lspci_cmd_ret, blk_name)
-            cmd2 = """echo "%s" | grep '%s'""" % (lsmod_cmd_ret, blk_driver)
-            status1, output1 = commands.getstatusoutput(cmd1)
-            status2, output2 = commands.getstatusoutput(cmd2)
-            if status1 == 0 and status2 == 0:
-                logger.info("block device type is virtio")
-                return 0
-            else:
-                return 1
-
-        # this check will not check ide type block device
-        if blk_type == "ide":
-            # virtio block device should not be seen in guest
-            blk_type_to_name_dict.pop(blk_type)
-            for key in blk_type_to_name_dict.keys():
-                logger.info(
-                    "now try to grep other blk type in lspci output: %s" %
-                    key)
-                other_name_cmd = """echo "%s" | grep '%s'""" % \
-                                 (lspci_cmd_ret, blk_type_to_name_dict[key])
-                ret, out = commands.getstatusoutput(other_name_cmd)
-                if ret == 0:
-                    logger.info("unspecified blk name is seen in guest's \
-                                lspci command: \n %s \n" % out)
-                    return 1
-            blk_type_to_driver_dict.pop(blk_type)
-            for key in blk_type_to_driver_dict.keys():
-                logger.info(
-                    "now try to grep other blk type in lsmod output: %s" %
-                    key)
-                other_driver_cmd = """echo '%s' | grep '%s'""" % \
-                                   (lsmod_cmd_ret,
-                                   blk_type_to_driver_dict[key])
-                ret1, out1 = commands.getstatusoutput(other_driver_cmd)
-                if ret1 == 0:
-                    logger.info("unspecified blk driver is seen \
-                                in guest's lsmod command: \n %s \n" % out)
-                    return 1
-            logger.info("block device type is ide")
-            return 0
-    else:
-        logger.info("lspci and lsmod return nothing")
-        return 1
diff --git a/utils/utils.py b/utils/utils.py
index 124c986..a216fa7 100644
--- a/utils/utils.py
+++ b/utils/utils.py
@@ -427,3 +427,365 @@ def scp_file(hostname, username, password, target_path, file):
             return 1
 
     return 0
+
+def support_virt(self):
+    cmd = "cat /proc/cpuinfo | egrep '(vmx|svm)'"
+    if commands.getoutput(cmd) is None:
+        print 'CPU does not support VT.'
+        return False
+    else:
+        return True
+
+def subproc(a, b):
+    subproc_flag = 1
+
+def remote_exec(hostname, username, password, cmd):
+    """Remote execution on specified host"""
+    pid, fd = pty.fork()
+    if pid == 0:
+        try:
+            os.execv("/usr/bin/ssh", ["/usr/bin/ssh", "-l",
+                     username, hostname, cmd])
+        except OSError, e:
+            print "OSError: " + str(e)
+            return -1
+    else:
+        signal.signal(signal.SIGCHLD, subproc)
+        try:
+            timeout = 50
+            i = 0
+            while  i <= timeout:
+
+                time.sleep(1)
+                str = os.read(fd, 10240)
+
+                if re.search('(yes\/no)', str):
+                    os.write(fd, "yes\r")
+
+                elif re.search('password:', str):
+                    os.write(fd, password + "\r")
+
+                elif subproc_flag == 1:
+                    ret = string.strip(str)
+                    break
+                elif i == timeout:
+                    print "TIMEOUT!!!!"
+                    return -1
+
+                i = i+1
+
+            subproc_flag = 0
+            return ret
+        except Exception, e:
+            subproc_flag = 0
+            return -1
+
+def remote_exec_pexpect(hostname, username, password, cmd):
+    """Remote exec function via pexpect"""
+    user_hostname = "%s@%s" % (username, hostname)
+    child = pexpect.spawn("/usr/bin/ssh", [user_hostname, cmd],
+                          timeout = 60, maxread = 2000, logfile = None)
+    #child.logfile = sys.stdout
+    while True:
+        index = child.expect(['(yes\/no)', 'password:', pexpect.EOF,
+                             pexpect.TIMEOUT])
+        if index == 0:
+            child.sendline("yes")
+        elif index == 1:
+            child.sendline(password)
+        elif index == 2:
+            return string.strip(child.before)
+        elif index == 3:
+            return "TIMEOUT!!!"
+
+def get_remote_vcpus(hostname, username, password):
+    """Get cpu number of specified host"""
+    cmd = "cat /proc/cpuinfo | grep processor | wc -l"
+    cpunum = -1
+    i = 0
+    while i < 3:
+        i += 1
+        cpunum = int(remote_exec(hostname, username, password, cmd))
+        if cpunum == -1:
+            continue
+        else:
+            break
+    return cpunum
+
+def get_remote_memory(hostname, username, password):
+    """Get memory statics of specified host"""
+    cmd = "free -m | grep -i mem | awk '{print $2}'"
+    memsize = -1
+    i = 0
+    while i < 3:
+        i += 1
+        memsize = \
+        int(remote_exec_pexpect(hostname, username, password, cmd)) * 1024
+        if memsize == -1:
+            continue
+        else:
+            break
+    return memsize
+
+def get_remote_kernel(hostname, username, password):
+    """Get kernel info of specified host"""
+    cmd = "uname -r"
+    kernel = None
+    i = 0
+    while i < 3:
+        i += 1
+        kernel = remote_exec(hostname, username, password, cmd)
+        if kernel:
+            break
+        else:
+            continue
+    return kernel
+
+def install_package(package = ''):
+    """Install specified package"""
+    if package:
+        cmd = "rpm -qa " + package
+        output = commands.getoutput(cmd)
+        pkg = output.split('\n')[0]
+        if pkg:
+            os.system("yum -y -q update " + package)
+            return pkg
+        else:
+            ret = os.system("yum -y -q install " + package)
+            if ret == 0:
+                output = commands.getoutput(cmd)
+                pkg = output.split('\n')[0]
+                if pkg:
+                    return pkg
+            else:
+                return "failed to install package"
+    else:
+        return "please input package name"
+
+def libvirt_version(latest_ver = ''):
+    """Get libvirt version info"""
+    query_virt_ver = 'rpm -qa|grep libvirt'
+    ret = commands.getoutput(query_virt_ver)
+    if ret:
+        mas_ver = ret.split('-')[-2]
+        sec_ver = (ret.split('-')[-1])
+        curr_ver = mas_ver + '-' + sec_ver
+        if latest_ver != '':
+            if curr_ver != latest_ver:
+                return (False, curr_ver)
+            else:
+                return (True, curr_ver)
+        else:
+            return curr_ver
+    else:
+        return (False, '')
+
+def create_dir(hostname, username, password):
+    """Create new dir"""
+    cmd = "mkdir /tmp/test"
+    mkdir_ret = remote_exec_pexpect(hostname, username, password, cmd)
+    if mkdir_ret == '':
+        cmd = "ls -d /tmp/test"
+        check_str = remote_exec_pexpect(hostname, username,
+                                             password, cmd)
+        if check_str == "/tmp/test":
+            return 0
+        else:
+            print "check_str = ", check_str
+            return 1
+    else:
+        print "mkdir_ret = ", mkdir_ret
+        return 1
+
+def write_file(hostname, username, password):
+    """Simple test for writting file on specified host"""
+    test_string = 'hello word testing'
+    cmd = """echo '%s'>/tmp/test/test.log""" % (test_string)
+    write_file_ret = remote_exec_pexpect(hostname, username,
+                                              password, cmd)
+    if write_file_ret == '':
+        cmd = """grep '%s' /tmp/test/test.log""" % ("hello")
+        check_str = remote_exec_pexpect(hostname, username,
+                                             password, cmd)
+        if check_str == test_string:
+            return 0
+        else:
+            print "check_str = ", check_str
+            return 1
+    else:
+        print "write_file_ret = ", write_file_ret
+        return 1
+
+def run_mount_app(hostname, username, password,
+                  target_mount, mount_point):
+    """Simple test for mount operation on specified host"""
+    cmd = """mount %s %s""" % (target_mount, mount_point)
+    mount_ret = remote_exec(hostname, username, password, cmd)
+    if mount_ret == '':
+        cmd = """df | grep '%s'""" % (target_mount)
+        check_str = remote_exec(hostname, username, password, cmd)
+        if check_str != '':
+            return 0
+        else:
+            print "mount check fail"
+            return 1
+    else:
+        print "mount fail"
+        return 1
+
+def run_wget_app(hostname, username, password, file_url, logger):
+    """Simple test for wget app on specified host"""
+    cmd_line = "wget -P /tmp %s -o /tmp/wget.log" % (file_url)
+    logger.info("Command: %s" % (cmd_line))
+    wget_ret = remote_exec_pexpect(hostname, username,
+                                        password, cmd_line)
+    cmd_line = "grep %s %s" % ('100%', '/tmp/wget.log')
+    check_ret = remote_exec_pexpect(hostname, username,
+                                         password, cmd_line)
+    if check_ret == "":
+        logger.info("grep output is nothing")
+        return 1
+    else:
+        if re.search("100%", check_ret):
+            logger.info("wget is running successfully")
+            logger.info("check_retrun: %s" % (check_ret))
+            return 0
+        else:
+            logger.info("can not find 100% in wget output")
+            logger.info("check_retrun: %s" % (check_ret))
+            return 1
+
+def validate_remote_nic_type(hostname, username,
+                             password, nic_type, logger):
+    """Validate network interface type on specified host"""
+    nic_type_to_name_dict = {'e1000':
+                             '82540EM Gigabit Ethernet Controller',
+                             'rtl8139':
+                             'RTL-8139/8139C/8139C+',
+                             'virtio':'Virtio network device'}
+    nic_type_to_driver_dict = {'e1000':'e1000', 'rtl8139':'8139cp',
+                              'virtio':'virtio_net'}
+    nic_name = nic_type_to_name_dict[nic_type]
+    nic_driver = nic_type_to_driver_dict[nic_type]
+    logger.info("nic_name = %s" % (nic_name))
+    logger.info("nic_driver = %s" % (nic_driver))
+    lspci_cmd = "lspci"
+    lsmod_cmd = "lsmod"
+    lspci_cmd_ret = remote_exec_pexpect(hostname, username,
+                                             password, lspci_cmd)
+    lsmod_cmd_ret = remote_exec_pexpect(hostname, username,
+                                             password, lsmod_cmd)
+    logger.info("------------")
+    logger.info("lspci_cmd_ret:\n %s" % (lspci_cmd_ret))
+    logger.info("------------")
+    logger.info("lsmod_cmd_ret:\n %s" % (lsmod_cmd_ret))
+    logger.info("------------")
+    if lspci_cmd_ret != "" and lsmod_cmd_ret != "":
+        cmd1 = """echo "%s" | grep '%s'""" % (lspci_cmd_ret, nic_name)
+        cmd2 = """echo "%s" | grep '%s'""" % (lsmod_cmd_ret, nic_driver)
+        status1, output1 = commands.getstatusoutput(cmd1)
+        status2, output2 = commands.getstatusoutput(cmd2)
+        if status1 == 0 and status2 == 0:
+            # other nic should not be seen in guest
+            nic_type_to_name_dict.pop(nic_type)
+            for key in nic_type_to_name_dict.keys():
+                logger.info("now try to grep other nic type \
+                            in lspci output: %s" % key)
+                other_name_cmd = """echo '%s' | grep '%s'""" % \
+                                 (lspci_cmd_ret, nic_type_to_name_dict[key])
+                ret, out = commands.getstatusoutput(other_name_cmd)
+                if ret == 0:
+                    logger.info("unspecified nic name is seen in \
+                               guest's lspci command: \n %s \n" % out)
+                    return 1
+
+            nic_type_to_driver_dict.pop(nic_type)
+            for key in nic_type_to_driver_dict.keys():
+                logger.info("now try to grep other nic type \
+                          in lsmod output: %s" % key)
+                other_driver_cmd = """echo '%s' | grep '%s'""" % \
+                               (lsmod_cmd_ret,
+                                nic_type_to_driver_dict[key])
+                ret1, out1 = commands.getstatusoutput(other_driver_cmd)
+                if ret1 == 0:
+                    logger.info("unspecified nic driver is seen \
+                               in guest's lsmod command: %s" % out)
+                    return 1
+
+            logger.info("lspci ouput about nic is: \n %s; \n \
+                        lsmod output about nic is \n %s \n" %
+                        (output1,output2))
+            return 0
+        else:
+            logger.info("lspci and lsmod and grep fail")
+            return 1
+    else:
+        logger.info("lspci and lsmod return nothing")
+        return 1
+
+def validate_remote_blk_type(hostname, username, password,
+                             blk_type, logger):
+    """Validate block device type on specified host"""
+    blk_type_to_name_dict = {'ide':'Intel Corporation 82371SB PIIX3 IDE',
+                             'virtio':'Virtio block device'}
+    blk_type_to_driver_dict = {'ide':'unknow', 'virtio':'virtio_blk'}
+    lspci_cmd = "lspci"
+    lsmod_cmd = "lsmod"
+    lspci_cmd_ret = remote_exec_pexpect(hostname, username,
+                                             password, lspci_cmd)
+    lsmod_cmd_ret = remote_exec_pexpect(hostname, username,
+                                            password, lsmod_cmd)
+    logger.info("------------")
+    logger.info("lspci_cmd_ret:\n %s" % (lspci_cmd_ret))
+    logger.info("------------")
+    logger.info("lsmod_cmd_ret: \n %s" % (lsmod_cmd_ret))
+    logger.info("------------")
+    if lspci_cmd_ret != "" and lsmod_cmd_ret != "":
+        if blk_type == "virtio":
+            blk_name = blk_type_to_name_dict[blk_type]
+            blk_driver = blk_type_to_driver_dict[blk_type]
+            logger.info("blk_name = %s \n blk_driver = %s" %
+                        (blk_name, blk_driver))
+            cmd1 = """echo "%s" | grep '%s'""" % (lspci_cmd_ret, blk_name)
+            cmd2 = """echo "%s" | grep '%s'""" % (lsmod_cmd_ret, blk_driver)
+            status1, output1 = commands.getstatusoutput(cmd1)
+            status2, output2 = commands.getstatusoutput(cmd2)
+            if status1 == 0 and status2 == 0:
+                logger.info("block device type is virtio")
+                return 0
+            else:
+                return 1
+
+        # this check will not check ide type block device
+        if blk_type == "ide":
+            # virtio block device should not be seen in guest
+            blk_type_to_name_dict.pop(blk_type)
+            for key in blk_type_to_name_dict.keys():
+                logger.info(
+                    "now try to grep other blk type in lspci output: %s" %
+                    key)
+                other_name_cmd = """echo "%s" | grep '%s'""" % \
+                                 (lspci_cmd_ret, blk_type_to_name_dict[key])
+                ret, out = commands.getstatusoutput(other_name_cmd)
+                if ret == 0:
+                    logger.info("unspecified blk name is seen in guest's \
+                                lspci command: \n %s \n" % out)
+                    return 1
+            blk_type_to_driver_dict.pop(blk_type)
+            for key in blk_type_to_driver_dict.keys():
+                logger.info(
+                    "now try to grep other blk type in lsmod output: %s" %
+                    key)
+                other_driver_cmd = """echo '%s' | grep '%s'""" % \
+                                   (lsmod_cmd_ret,
+                                   blk_type_to_driver_dict[key])
+                ret1, out1 = commands.getstatusoutput(other_driver_cmd)
+                if ret1 == 0:
+                    logger.info("unspecified blk driver is seen \
+                                in guest's lsmod command: \n %s \n" % out)
+                    return 1
+            logger.info("block device type is ide")
+            return 0
+    else:
+        logger.info("lspci and lsmod return nothing")
+        return 1
-- 
1.7.7.3

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


[Virt Tools]     [Libvirt Users]     [Fedora Users]     [Fedora Legacy]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]

Powered by Linux

Google
  Web www.spinics.net