[libvirt] [libvirt-test-api][PATCH 1/2] introduce a new helper to parse mountinfo

And will return list like this : [{'devminor': '25', 'mountdir': '/sys/fs/cgroup/cpuset', 'devmajor': '0', 'mounttype': 'cgroup'},...] Signed-off-by: Luyao Huang <lhuang@redhat.com> --- utils/utils.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/utils/utils.py b/utils/utils.py index a6e6965..6909fed 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -925,3 +925,40 @@ def param_to_tuple_nolength(paramlist): lengh = max(d) return param_to_tuple(paramlist, int(lengh) + 1) + +def parse_mountinfo(string): + """a helper to parse mountinfo in /proc/self/mountinfo + and return a list contains multiple dict + """ + + ret = [] + mount_list = string.split("\n") + for n in mount_list: + mount_dict = {} + if n.find("/") > 0: + tmp = n[:n.find("/")] + if len(tmp.split()) != 3: + continue + + if tmp.split()[2].find(":") < 0: + continue + + mount_dict['devmajor'] = tmp.split()[2].split(":")[0] + mount_dict['devminor'] = tmp.split()[2].split(":")[1] + + tmp = n[n.find("/") + 1:] + + mount_dict['mountdir'] = tmp.split()[0] + + if tmp.find(" - ") < 0: + continue + + tmp = tmp.split(" - ")[1] + + mount_dict['mounttype'] = tmp.split()[0] + if tmp.split()[1].find("/") > 0: + mount_dict['sourcedir'] = tmp.split()[1] + + ret.append(mount_dict) + + return ret -- 1.8.3.1

Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/linux_domain.conf | 10 +++++ repos/domain/fsinfo.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 repos/domain/fsinfo.py diff --git a/cases/linux_domain.conf b/cases/linux_domain.conf index 19daded..faa9df9 100644 --- a/cases/linux_domain.conf +++ b/cases/linux_domain.conf @@ -299,6 +299,16 @@ domain:info_iothread conn qemu:///system +domain:fsinfo + guestname + $defaultname + username + $username + userpassword + $password + conn + qemu:///system + domain:destroy guestname $defaultname diff --git a/repos/domain/fsinfo.py b/repos/domain/fsinfo.py new file mode 100644 index 0000000..e6d1bf0 --- /dev/null +++ b/repos/domain/fsinfo.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python + +import libvirt +from libvirt import libvirtError +import lxml +import lxml.etree +from utils import utils + +required_params = ('guestname', 'username', 'userpassword',) +optional_params = {'conn': 'qemu:///system'} + +def get_guest_mac(vm): + tree = lxml.etree.fromstring(vm.XMLDesc(0)) + set = tree.xpath("/domain/devices/interface/mac") + + for n in set: + return n.attrib['address'] + + return False + + +def check_agent_status(vm): + """ make sure agent is okay to use """ + + tree = lxml.etree.fromstring(vm.XMLDesc(0)) + + set = tree.xpath("//channel[@type='unix']/target[@name='org.qemu.guest_agent.0']") + for n in set: + if n.attrib['state'] == 'connected': + return True + + return False + +def check_fsinfo(ipaddr, username, userpasswd, fsinfo, logger): + """ equal the fsinfo from libvirt and we get in guest mountinfo """ + + cmd = "cat /proc/self/mountinfo" + ret, mounts_needparse = utils.remote_exec_pexpect(ipaddr, username, userpasswd, cmd) + mounts = utils.parse_mountinfo(mounts_needparse) + + for n in fsinfo: + mountdir = n[0] + name = n[1] + type1 = n[2] + target = n[3][0] + found = 0 + + for i in mounts: + if mountdir == i['mountdir']: + found = 1 + if i['mounttype'] != type1: + logger.error("Fail: mount type is not equal: libvirt: %s but we get: %s" % (type1, i['mounttype'])) + return False + + if found == 0: + logger.error("Fail: cannot find %s in guest mount info" % mountdir) + return False + + return True + + +def fsinfo(params): + """ + test API for fsInfo in class virDomain + """ + + logger = params['logger'] + guest = params['guestname'] + username = params['username'] + userpasswd = params['userpassword'] + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection to libvirtd") + vm = conn.lookupByName(guest) + logger.info("test guest name: %s" % guest) + + if not check_agent_status(vm): + logger.error("guest agent is not connected") + return 1 + + fsinfo = vm.fsInfo() + logger.info("get geust filesystem information") + + mac = get_guest_mac(vm) + if not mac: + logger.error("cannot get guest interface mac") + return 1 + + ipaddr = utils.mac_to_ip(mac, 180) + if not ipaddr: + logger.error("cannot get guest IP") + return 1 + + if not check_fsinfo(ipaddr, username, userpasswd, fsinfo, logger): + return 1 + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + return 1 + + return 0 -- 1.8.3.1

On 09/14/2015 11:43 AM, Luyao Huang wrote:
Signed-off-by: Luyao Huang <lhuang@redhat.com> --- cases/linux_domain.conf | 10 +++++ repos/domain/fsinfo.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 repos/domain/fsinfo.py
diff --git a/cases/linux_domain.conf b/cases/linux_domain.conf index 19daded..faa9df9 100644 --- a/cases/linux_domain.conf +++ b/cases/linux_domain.conf @@ -299,6 +299,16 @@ domain:info_iothread conn qemu:///system
+domain:fsinfo + guestname + $defaultname + username + $username + userpassword + $password + conn + qemu:///system + domain:destroy guestname $defaultname diff --git a/repos/domain/fsinfo.py b/repos/domain/fsinfo.py new file mode 100644 index 0000000..e6d1bf0 --- /dev/null +++ b/repos/domain/fsinfo.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python + +import libvirt +from libvirt import libvirtError +import lxml +import lxml.etree +from utils import utils + +required_params = ('guestname', 'username', 'userpassword',) +optional_params = {'conn': 'qemu:///system'} + +def get_guest_mac(vm): + tree = lxml.etree.fromstring(vm.XMLDesc(0)) + set = tree.xpath("/domain/devices/interface/mac") + + for n in set: + return n.attrib['address'] + + return False + + +def check_agent_status(vm): + """ make sure agent is okay to use """ + + tree = lxml.etree.fromstring(vm.XMLDesc(0)) + + set = tree.xpath("//channel[@type='unix']/target[@name='org.qemu.guest_agent.0']") + for n in set: + if n.attrib['state'] == 'connected': + return True + + return False + +def check_fsinfo(ipaddr, username, userpasswd, fsinfo, logger): + """ equal the fsinfo from libvirt and we get in guest mountinfo """ + + cmd = "cat /proc/self/mountinfo" + ret, mounts_needparse = utils.remote_exec_pexpect(ipaddr, username, userpasswd, cmd) + mounts = utils.parse_mountinfo(mounts_needparse) + + for n in fsinfo: + mountdir = n[0] + name = n[1] + type1 = n[2] + target = n[3][0] + found = 0 + + for i in mounts: + if mountdir == i['mountdir']: + found = 1 + if i['mounttype'] != type1: + logger.error("Fail: mount type is not equal: libvirt: %s but we get: %s" % (type1, i['mounttype'])) + return False + + if found == 0: + logger.error("Fail: cannot find %s in guest mount info" % mountdir) + return False + + return True + + +def fsinfo(params): + """ + test API for fsInfo in class virDomain + """ + + logger = params['logger'] + guest = params['guestname'] + username = params['username'] + userpasswd = params['userpassword'] + + try: + conn = libvirt.open(params['conn']) + + logger.info("get connection to libvirtd") + vm = conn.lookupByName(guest) + logger.info("test guest name: %s" % guest) + + if not check_agent_status(vm): + logger.error("guest agent is not connected") + return 1 + + fsinfo = vm.fsInfo() + logger.info("get geust filesystem information") + + mac = get_guest_mac(vm) + if not mac: + logger.error("cannot get guest interface mac") + return 1 + + ipaddr = utils.mac_to_ip(mac, 180) + if not ipaddr: + logger.error("cannot get guest IP") + return 1 + + if not check_fsinfo(ipaddr, username, userpasswd, fsinfo, logger): + return 1 + + except libvirtError, e: + logger.error("API error message: %s" % e.message) + return 1 + + return 0
ACK and Pushed Thanks Hongming
participants (2)
-
hongming
-
Luyao Huang