* repos/domain/blkstats.py
* repos/domain/domain_blkinfo.py
Function blkstats and domain_blkinfo used virsh commands to test result,
that is not fit for testing api, so I replace commands with invoking api.
V1->V2:
Removed blockdev params for domain_blkinfo, this hard code disk device is
not convenient for users.
---
repos/domain/blkstats.py | 2 -
repos/domain/domain_blkinfo.py | 94 ++++++++++++++++++++++++------------------
2 files changed, 55 insertions(+), 41 deletions(-)
diff --git a/repos/domain/blkstats.py b/repos/domain/blkstats.py
index 0254922..27c2a46 100644
--- a/repos/domain/blkstats.py
+++ b/repos/domain/blkstats.py
@@ -1,8 +1,6 @@
#!/usr/bin/evn python
# To test domain block device statistics
-import os
-import sys
import time
import libxml2
diff --git a/repos/domain/domain_blkinfo.py b/repos/domain/domain_blkinfo.py
index b6051aa..81035ed 100644
--- a/repos/domain/domain_blkinfo.py
+++ b/repos/domain/domain_blkinfo.py
@@ -1,22 +1,17 @@
#!/usr/bin/env python
-# To test "virsh domblkinfo" command
+# To test domain's blockkinfo API
-import os
-import sys
-import re
import commands
-
+import libxml2
import libvirt
from libvirt import libvirtError
from src import sharedmod
-GET_DOMBLKINFO_MAC = "virsh domblkinfo %s %s | awk '{print $2}'"
GET_CAPACITY = "du -b %s | awk '{print $1}'"
GET_PHYSICAL_K = " du -B K %s | awk '{print $1}'"
-VIRSH_DOMBLKINFO = "virsh domblkinfo %s %s"
-required_params = ('guestname', 'blockdev',)
+required_params = ('guestname', )
optional_params = {}
def get_output(command, logger):
@@ -32,8 +27,8 @@ def check_domain_exists(conn, guestname, logger):
""" check if the domain exists, may or may not be active
"""
guest_names = []
ids = conn.listDomainsID()
- for id in ids:
- obj = conn.lookupByID(id)
+ for domain_id in ids:
+ obj = conn.lookupByID(domain_id)
guest_names.append(obj.name())
guest_names += conn.listDefinedDomains()
@@ -44,17 +39,27 @@ def check_domain_exists(conn, guestname, logger):
else:
return True
+def check_guest_status(domobj):
+ """Check guest current status"""
+ state = domobj.info()[0]
+ if state == libvirt.VIR_DOMAIN_SHUTOFF or \
+ state == libvirt.VIR_DOMAIN_SHUTDOWN:
+ # add check function
+ return False
+ else:
+ return True
+
def check_block_data(blockdev, blkdata, logger):
""" check data about capacity,allocation,physical """
status, apparent_size = get_output(GET_CAPACITY % blockdev, logger)
if not status:
- if apparent_size == blkdata[0]:
- logger.info("the capacity of '%s' is %s, checking
succeeded" % \
- (blockdev, apparent_size))
+ if apparent_size == str(blkdata[0]):
+ logger.info("the capacity of '%s' is %s, checking
succeeded"
+ % (blockdev, apparent_size))
else:
- logger.error("apparent-size from 'du' is %s, \n\
- but from 'domblkinfo' is %s, checking failed" % \
- (apparent_size, blkdata[0]))
+ logger.error("apparent-size from 'du' is %s" %
apparent_size)
+ logger.error("but from 'domain blockinfo' is %d, checking
failed"
+ % blkdata[0])
return 1
else:
return 1
@@ -64,14 +69,15 @@ def check_block_data(blockdev, blkdata, logger):
block_size_b = int(block_size_k[:-1]) * 1024
# Temporarily, we only test the default case, assuming
# Allocation value is equal to Physical value
- if str(block_size_b) == blkdata[1] and str(block_size_b) == blkdata[2]:
- logger.info("the block size of '%s' is %s, same with \n\
- Allocation and Physical value, checking succeeded" % \
- (blockdev, block_size_b))
+ if block_size_b == blkdata[1] and block_size_b == blkdata[2]:
+ logger.info("the block size of '%s' is %s"
+ % (blockdev, block_size_b))
+ logger.info("Allocation and Physical value's checking
succeeded")
else:
- logger.error("the block size from 'du' is %s, \n\
- the Allocation value is %s, Physical value is %s, \n\
- checking failed" % (block_size_b, blkdata[1],
blkdata[2]))
+ logger.error("the block size from 'du' is %d" %
block_size_b)
+ logger.error("the Allocation value is %d, Physical value is %d"
+ % (blkdata[1], blkdata[2]))
+ logger.error("checking failed")
return 1
return 0
@@ -79,14 +85,12 @@ def check_block_data(blockdev, blkdata, logger):
def domain_blkinfo(params):
""" using du command to check the data
- in the output of virsh domblkinfo
+ in the output of API blockinfo
"""
logger = params['logger']
guestname = params.get('guestname')
- blockdev = params.get('blockdev')
logger.info("the name of guest is %s" % guestname)
- logger.info("the block device is %s" % blockdev)
conn = sharedmod.libvirtobj['conn']
@@ -94,23 +98,35 @@ def domain_blkinfo(params):
logger.error("need a defined guest")
return 1
- logger.info("the output of virsh domblkinfo is:")
- status, output = get_output(VIRSH_DOMBLKINFO % (guestname, blockdev), logger)
- if not status:
- logger.info("\n" + output)
- else:
+ domobj = conn.lookupByName(guestname)
+
+ xml = domobj.XMLDesc(0)
+ doc = libxml2.parseDoc(xml)
+ cont = doc.xpathNewContext()
+ vdevs = cont.xpathEval("/domain/devices/disk/source/@file")
+ blockdev = vdevs[0].content
+ logger.info("the block device is %s" % blockdev)
+
+ if not check_guest_status(domobj):
+ logger.error("guest is not started.")
return 1
- status, data_str = get_output(GET_DOMBLKINFO_MAC % (guestname, blockdev), logger)
- if not status:
- blkdata = data_str.rstrip().split('\n')
- logger.info("capacity,allocation,physical list: %s" % blkdata)
- else:
+ try:
+ logger.info("the output of domain blockinfo is:")
+ block_info = domobj.blockInfo(blockdev, 0)
+ logger.info("Capacity : %d " % block_info[0])
+ logger.info("Allocation: %d " % block_info[1])
+ logger.info("Physical : %d " % block_info[2])
+
+ except libvirtError, e:
+ logger.error("API error message: %s, error code is %s"
+ % (e.message, e.get_error_code()))
return 1
- if check_block_data(blockdev, blkdata, logger):
- logger.error("checking domblkinfo data FAILED")
+ if check_block_data(blockdev, block_info, logger):
+ logger.error("checking domain blockinfo data FAILED")
return 1
else:
- logger.info("checking domblkinfo data SUCCEEDED")
+ logger.info("checking domain blockinfo data SUCCEEDED")
+
return 0
--
1.8.3.1