From 662fcb66f0029e356a76d34ea626900859c09dc7 Mon Sep 17 00:00:00 2001
From: Zhu Huankai <zhuhuankai1@huawei.com>
Date: Tue, 12 Oct 2021 11:04:43 +0800
Subject: [PATCH 1/4] hydropper/testcases: move some funcs

move some funcs to testcases.

Signed-off-by: Zhu Huankai <zhuhuankai1@huawei.com>
---
 .../functional/test_microvm_virtio_blk.py     | 39 ++++++++-----
 .../microvm/functional/test_microvm_vmlife.py | 35 +++++++++++-
 tests/hydropper/virt/basevm.py                | 56 -------------------
 3 files changed, 58 insertions(+), 72 deletions(-)

diff --git a/tests/hydropper/testcases/microvm/functional/test_microvm_virtio_blk.py b/tests/hydropper/testcases/microvm/functional/test_microvm_virtio_blk.py
index dea710d..1dde7b6 100644
--- a/tests/hydropper/testcases/microvm/functional/test_microvm_virtio_blk.py
+++ b/tests/hydropper/testcases/microvm/functional/test_microvm_virtio_blk.py
@@ -18,19 +18,28 @@ from subprocess import PIPE
 import pytest
 from utils.config import CONFIG
 
-def _check_virtio_blk_vectors(microvm):
-    _cmd = "lspci |grep -w Virtio"
-    _, output = microvm.serial_cmd(_cmd)
-    index = 0
-    for line in output.splitlines():
-        if "block" in line.strip():
-            _check_vec_cmd = "grep -w virtio%d /proc/interrupts | wc -l" % index
-            _status, _output = microvm.serial_cmd(_check_vec_cmd)
-            vecs = int(_output.splitlines()[-2].strip())
-            expect_vecs = 2
-            assert vecs == expect_vecs
-        index += 1
+def _get_lsblk_info(test_vm):
+    """
+    Get lsblk info
 
+    Returns:
+        {
+            "vdx": {"size": xx, "readonly": xx},
+        }
+    """
+    retdict = {}
+    if test_vm.ssh_session is not None:
+        _output = test_vm.ssh_session.cmd_output("lsblk")
+        for line in _output.split("\n"):
+            temp = line.split()
+            if len(temp) == 6:
+                name = temp[0]
+                size = temp[3]
+                readonly = temp[4]
+                if name not in retdict:
+                    retdict[name] = {"size": size, "readonly": readonly}
+
+    return retdict
 
 @pytest.mark.acceptance
 @pytest.mark.parametrize("readonly", [True, False])
@@ -87,7 +96,7 @@ def test_microvm_virtio_blk_at_dt(test_session_root_path, microvm, testtimes):
             test_vm.add_disk(disk, index=index)
             index += 1
 
-        blkinfo = test_vm.get_lsblk_info()
+        blkinfo = _get_lsblk_info(test_vm)
         logging.debug("blkinfo is %s", blkinfo)
 
         for devid in ["vdb", "vdc", "vdd"]:
@@ -99,7 +108,7 @@ def test_microvm_virtio_blk_at_dt(test_session_root_path, microvm, testtimes):
             test_vm.del_disk(index=index)
             index += 1
 
-        blkinfo = test_vm.get_lsblk_info()
+        blkinfo = _get_lsblk_info(test_vm)
         for devid in ["vdb", "vdc", "vdd"]:
             assert devid not in blkinfo
 
@@ -122,7 +131,7 @@ def test_microvm_virtio_blk_md5(test_session_root_path, microvm):
     test_vm.launch()
     test_vm.add_disk(temp_disk)
 
-    blkinfo = test_vm.get_lsblk_info()
+    blkinfo = _get_lsblk_info(test_vm)
     logging.debug("blkinfo is %s", blkinfo)
 
     format_cmd = "mkfs.ext4 /dev/vdb"
diff --git a/tests/hydropper/testcases/microvm/functional/test_microvm_vmlife.py b/tests/hydropper/testcases/microvm/functional/test_microvm_vmlife.py
index 30de187..8923af6 100644
--- a/tests/hydropper/testcases/microvm/functional/test_microvm_vmlife.py
+++ b/tests/hydropper/testcases/microvm/functional/test_microvm_vmlife.py
@@ -17,6 +17,39 @@ from utils import utils_qmp
 from utils.config import CONFIG
 from utils.exception import QMPTimeoutError
 
+def _get_guest_hwinfo(test_vm):
+    """
+    Get guest hwinfo via ssh_session
+
+    Returns:
+        {"cpu": {"vcpu_count": xx, "maxvcpu": xx},
+        "mem": {"memsize": xx, "maxmem": xx},
+        "virtio": {"virtio_blk": [{"name": "virtio0"}],
+                "virtio_console": [{"name": "virtio1"}],
+                "virtio_net": [{"name": "virtio2"}],
+                "virtio_rng": [{"name": "virtio3"}],
+                }
+        }
+    """
+    retdict = {"cpu": {}, "mem": {}, "virtio": {}}
+    if test_vm.ssh_session is not None:
+        vcpu_count = int(test_vm.ssh_session.cmd_output("grep -c processor /proc/cpuinfo"))
+        memsize = int(test_vm.ssh_session.cmd_output("grep MemTotal /proc/meminfo | awk '{print $2}'"))
+        retdict["cpu"] = {"vcpu_count": vcpu_count, "maxvcpu": vcpu_count}
+        retdict["mem"] = {"memsize": memsize, "maxmem": memsize}
+        # ignore virtio_rng device now
+        for dev in ["virtio_blk", "virtio_net", "virtio_console"]:
+            devdir = "/sys/bus/virtio/drivers/%s" % dev
+            _cmd = "test -d %s && ls %s | grep virtio" % (devdir, devdir)
+            virtiodevs = test_vm.ssh_session.cmd_output(_cmd).strip().split()
+            for virtiodev in virtiodevs:
+                _tempdev = {"name": virtiodev}
+                if dev not in retdict["virtio"]:
+                    retdict["virtio"][dev] = list()
+                retdict["virtio"][dev].append(_tempdev)
+
+    return retdict
+
 @pytest.mark.acceptance
 @pytest.mark.parametrize("vcpu_count, memsize, vnetnums",
                          [(1, 256, 1),
@@ -26,7 +59,7 @@ def test_microvm_start(microvm, vcpu_count, memsize, vnetnums):
     test_vm = microvm
     test_vm.basic_config(vcpu_count=vcpu_count, mem_size=memsize, vnetnums=vnetnums)
     test_vm.launch()
-    vmhwinfo = test_vm.get_guest_hwinfo()
+    vmhwinfo = _get_guest_hwinfo(test_vm)
     logging.debug("current vmhwinfo is %s", vmhwinfo)
     assert vmhwinfo["cpu"]["vcpu_count"] == vcpu_count
     assert vmhwinfo["mem"]["memsize"] > (memsize * 1024 * 90 / 100)
diff --git a/tests/hydropper/virt/basevm.py b/tests/hydropper/virt/basevm.py
index 3e2fa51..0766068 100644
--- a/tests/hydropper/virt/basevm.py
+++ b/tests/hydropper/virt/basevm.py
@@ -715,62 +715,6 @@ class BaseVM:
         LOG.debug("Attempting to run cmd '%s' in vm" % cmd)
         return self.serial_session.run_func("cmd_status_output", cmd, internal_timeout=SERIAL_TIMEOUT)
 
-    def get_guest_hwinfo(self):
-        """
-        Get guest hwinfo via ssh_session
-
-        Returns:
-            {"cpu": {"vcpu_count": xx, "maxvcpu": xx},
-            "mem": {"memsize": xx, "maxmem": xx},
-            "virtio": {"virtio_blk": [{"name": "virtio0"}],
-                    "virtio_console": [{"name": "virtio1"}],
-                    "virtio_net": [{"name": "virtio2"}],
-                    "virtio_rng": [{"name": "virtio3"}],
-                    }
-            }
-        """
-        retdict = {"cpu": {}, "mem": {}, "virtio": {}}
-        if self.ssh_session is not None:
-            vcpu_count = int(self.ssh_session.cmd_output("grep -c processor /proc/cpuinfo"))
-            memsize = int(self.ssh_session.cmd_output("grep MemTotal /proc/meminfo | awk '{print $2}'"))
-            retdict["cpu"] = {"vcpu_count": vcpu_count, "maxvcpu": vcpu_count}
-            retdict["mem"] = {"memsize": memsize, "maxmem": memsize}
-            # ignore virtio_rng device now
-            for dev in ["virtio_blk", "virtio_net", "virtio_console"]:
-                devdir = "/sys/bus/virtio/drivers/%s" % dev
-                _cmd = "test -d %s && ls %s | grep virtio" % (devdir, devdir)
-                virtiodevs = self.ssh_session.cmd_output(_cmd).strip().split()
-                for virtiodev in virtiodevs:
-                    _tempdev = {"name": virtiodev}
-                    if dev not in retdict["virtio"]:
-                        retdict["virtio"][dev] = list()
-                    retdict["virtio"][dev].append(_tempdev)
-
-        return retdict
-
-    def get_lsblk_info(self):
-        """
-        Get lsblk info
-
-        Returns:
-            {
-                "vdx": {"size": xx, "readonly": xx},
-            }
-        """
-        retdict = {}
-        if self.ssh_session is not None:
-            _output = self.ssh_session.cmd_output("lsblk")
-            for line in _output.split("\n"):
-                temp = line.split()
-                if len(temp) == 6:
-                    name = temp[0]
-                    size = temp[3]
-                    readonly = temp[4]
-                    if name not in retdict:
-                        retdict[name] = {"size": size, "readonly": readonly}
-
-        return retdict
-
     def stop(self):
         """Pause all vcpu"""
         return self.qmp_command("stop")
-- 
2.30.0

