[libvirt] [test-API][PATCH v2] Add 2 vcpupin cases cover config and live flags

2012-12-13 Thread Wayne Sun
v1: add 2 vcpupin cases
* use pinVcpuFlags to pin domain vcpu to host cpu
* 2 cases cover config and live flags
* cpulist with '^', '-' and ',' is supported to give multiple
  host cpus
* vcpus and vcpuPinInfo are used as part of the checking
* a sample conf is added

v2: move format cpulist functions to utils
* the format cpulist functions could be reused for cases need
  parse param with '-', '^' and ','.

Signed-off-by: Wayne Sun g...@redhat.com
---
 cases/vcpupin.conf   |   67 +++
 repos/setVcpus/vcpupin_config.py |  109 ++
 repos/setVcpus/vcpupin_live.py   |  101 +++
 utils/utils.py   |   71 
 4 files changed, 348 insertions(+), 0 deletions(-)
 create mode 100644 cases/vcpupin.conf
 create mode 100644 repos/setVcpus/vcpupin_config.py
 create mode 100644 repos/setVcpus/vcpupin_live.py

diff --git a/cases/vcpupin.conf b/cases/vcpupin.conf
new file mode 100644
index 000..880247f
--- /dev/null
+++ b/cases/vcpupin.conf
@@ -0,0 +1,67 @@
+domain:install_linux_cdrom
+guestname
+$defaultname
+guestos
+$defaultos
+guestarch
+$defaultarch
+vcpu
+4
+memory
+$defaultmem
+hddriver
+$defaulthd
+nicdriver
+$defaultnic
+imageformat
+qcow2
+
+setVcpus:vcpupin_live
+guestname
+$defaultname
+vcpu
+0
+cpulist
+2,4-6,^4
+
+setVcpus:vcpupin_live
+guestname
+$defaultname
+vcpu
+1
+cpulist
+3
+
+domain:destroy
+guestname
+$defaultname
+
+setVcpus:vcpupin_config
+guestname
+$defaultname
+vcpu
+2
+cpulist
+0-8,^1
+
+setVcpus:vcpupin_config
+guestname
+$defaultname
+vcpu
+3
+cpulist
+^2,0-8
+
+domain:start
+guestname
+$defaultname
+
+domain:destroy
+guestname
+$defaultname
+
+domain:undefine
+guestname
+$defaultname
+
+options cleanup=enable
diff --git a/repos/setVcpus/vcpupin_config.py b/repos/setVcpus/vcpupin_config.py
new file mode 100644
index 000..80df659
--- /dev/null
+++ b/repos/setVcpus/vcpupin_config.py
@@ -0,0 +1,109 @@
+#!/usr/bin/env python
+# Test domain vcpu pin with flag VIR_DOMAIN_AFFECT_CONFIG, check
+# domain config xml with vcpupin configuration.
+
+import re
+from xml.dom import minidom
+
+import libvirt
+from libvirt import libvirtError
+
+from src import sharedmod
+from utils import utils
+
+required_params = ('guestname', 'vcpu', 'cpulist',)
+optional_params = {}
+
+def vcpupin_check(domobj, vcpu, cpumap):
+check domain config xml with vcpupin element
+
+guestxml = domobj.XMLDesc(2)
+logger.debug(domain %s xml :\n%s %(domobj.name(), guestxml))
+
+doc = minidom.parseString(guestxml)
+vcpupin = doc.getElementsByTagName('vcpupin')
+if not vcpupin:
+logger.error(no vcpupin element in domain xml)
+return 1
+
+for i in range(len(vcpupin)):
+if vcpupin[i].hasAttribute('vcpu') and \
+   vcpupin[i].hasAttribute('cpuset'):
+vcpu_attr = vcpupin[i].getAttributeNode('vcpu')
+cpu_attr = vcpupin[i].getAttributeNode('cpuset')
+if int(vcpu_attr.nodeValue) == vcpu:
+cpulist = cpu_attr.nodeValue
+if cpulist == '':
+cpumap_tmp = ()
+for i in range(maxcpu):
+cpumap_tmp += (False,)
+else:
+cpumap_tmp = utils.param_to_tuple(cpulist, maxcpu)
+
+if cpumap_tmp == cpumap:
+logger.info(cpuset is as expected in domain xml)
+return 0
+else:
+logger.error(cpuset is not as expected in domain xml)
+return 1
+
+if i == len(vcpupin) - 1:
+logger.error(the vcpupin element with given vcpu is not found)
+return 1
+
+def vcpupin_config(params):
+pin domain vcpu to host cpu with config flag
+
+global logger
+logger = params['logger']
+params.pop('logger')
+guestname = params['guestname']
+vcpu = int(params['vcpu'])
+cpulist = params['cpulist']
+
+logger.info(the name of virtual machine is %s % guestname)
+logger.info(the given vcpu is %s % vcpu)
+logger.info(the given cpulist is %s % cpulist)
+
+global maxcpu
+maxcpu = utils.get_host_cpus()
+logger.info(%s physical cpu on host % maxcpu)
+
+conn = sharedmod.libvirtobj['conn']
+
+try:
+domobj = conn.lookupByName(guestname)
+cpumap = utils.param_to_tuple(cpulist, maxcpu)
+
+if not cpumap:
+logger.error(cpulist: Invalid format)
+return 1
+
+logger.debug(cpumap for vcpu pin is:)
+logger.debug(cpumap)
+
+logger.info(pin domain vcpu %s to host cpulist %s with flag: %s %
+ 

Re: [libvirt] [test-API][PATCH v2] Add 2 vcpupin cases cover config and live flags

2012-12-13 Thread Guannan Ren

On 12/14/2012 11:57 AM, Wayne Sun wrote:

v1: add 2 vcpupin cases
* use pinVcpuFlags to pin domain vcpu to host cpu
* 2 cases cover config and live flags
* cpulist with '^', '-' and ',' is supported to give multiple
   host cpus
* vcpus and vcpuPinInfo are used as part of the checking
* a sample conf is added

v2: move format cpulist functions to utils
* the format cpulist functions could be reused for cases need
   parse param with '-', '^' and ','.

Signed-off-by: Wayne Sun g...@redhat.com
---
  cases/vcpupin.conf   |   67 +++
  repos/setVcpus/vcpupin_config.py |  109 ++
  repos/setVcpus/vcpupin_live.py   |  101 +++
  utils/utils.py   |   71 
  4 files changed, 348 insertions(+), 0 deletions(-)
  create mode 100644 cases/vcpupin.conf
  create mode 100644 repos/setVcpus/vcpupin_config.py
  create mode 100644 repos/setVcpus/vcpupin_live.py

diff --git a/cases/vcpupin.conf b/cases/vcpupin.conf
new file mode 100644
index 000..880247f
--- /dev/null
+++ b/cases/vcpupin.conf
@@ -0,0 +1,67 @@
+domain:install_linux_cdrom
+guestname
+$defaultname
+guestos
+$defaultos
+guestarch
+$defaultarch
+vcpu
+4
+memory
+$defaultmem
+hddriver
+$defaulthd
+nicdriver
+$defaultnic
+imageformat
+qcow2
+
+setVcpus:vcpupin_live
+guestname
+$defaultname
+vcpu
+0
+cpulist
+2,4-6,^4
+
+setVcpus:vcpupin_live
+guestname
+$defaultname
+vcpu
+1
+cpulist
+3
+
+domain:destroy
+guestname
+$defaultname
+
+setVcpus:vcpupin_config
+guestname
+$defaultname
+vcpu
+2
+cpulist
+0-8,^1
+
+setVcpus:vcpupin_config
+guestname
+$defaultname
+vcpu
+3
+cpulist
+^2,0-8
+
+domain:start
+guestname
+$defaultname
+
+domain:destroy
+guestname
+$defaultname
+
+domain:undefine
+guestname
+$defaultname
+
+options cleanup=enable
diff --git a/repos/setVcpus/vcpupin_config.py b/repos/setVcpus/vcpupin_config.py
new file mode 100644
index 000..80df659
--- /dev/null
+++ b/repos/setVcpus/vcpupin_config.py
@@ -0,0 +1,109 @@
+#!/usr/bin/env python
+# Test domain vcpu pin with flag VIR_DOMAIN_AFFECT_CONFIG, check
+# domain config xml with vcpupin configuration.
+
+import re
+from xml.dom import minidom
+
+import libvirt
+from libvirt import libvirtError
+
+from src import sharedmod
+from utils import utils
+
+required_params = ('guestname', 'vcpu', 'cpulist',)
+optional_params = {}
+
+def vcpupin_check(domobj, vcpu, cpumap):
+check domain config xml with vcpupin element
+
+guestxml = domobj.XMLDesc(2)
+logger.debug(domain %s xml :\n%s %(domobj.name(), guestxml))
+
+doc = minidom.parseString(guestxml)
+vcpupin = doc.getElementsByTagName('vcpupin')
+if not vcpupin:
+logger.error(no vcpupin element in domain xml)
+return 1
+
+for i in range(len(vcpupin)):
+if vcpupin[i].hasAttribute('vcpu') and \
+   vcpupin[i].hasAttribute('cpuset'):
+vcpu_attr = vcpupin[i].getAttributeNode('vcpu')
+cpu_attr = vcpupin[i].getAttributeNode('cpuset')
+if int(vcpu_attr.nodeValue) == vcpu:
+cpulist = cpu_attr.nodeValue
+if cpulist == '':
+cpumap_tmp = ()
+for i in range(maxcpu):
+cpumap_tmp += (False,)
+else:
+cpumap_tmp = utils.param_to_tuple(cpulist, maxcpu)
+
+if cpumap_tmp == cpumap:
+logger.info(cpuset is as expected in domain xml)
+return 0
+else:
+logger.error(cpuset is not as expected in domain xml)
+return 1
+
+if i == len(vcpupin) - 1:
+logger.error(the vcpupin element with given vcpu is not found)
+return 1
+
+def vcpupin_config(params):
+pin domain vcpu to host cpu with config flag
+
+global logger
+logger = params['logger']
+params.pop('logger')
+guestname = params['guestname']
+vcpu = int(params['vcpu'])
+cpulist = params['cpulist']
+
+logger.info(the name of virtual machine is %s % guestname)
+logger.info(the given vcpu is %s % vcpu)
+logger.info(the given cpulist is %s % cpulist)
+
+global maxcpu
+maxcpu = utils.get_host_cpus()
+logger.info(%s physical cpu on host % maxcpu)
+
+conn = sharedmod.libvirtobj['conn']
+
+try:
+domobj = conn.lookupByName(guestname)
+cpumap = utils.param_to_tuple(cpulist, maxcpu)
+
+if not cpumap:
+logger.error(cpulist: Invalid format)
+return 1
+
+logger.debug(cpumap for vcpu pin is:)
+logger.debug(cpumap)
+
+