Re: [libvirt] [test-API][PATCH] Add cpu hotplug test case

2012-11-16 Thread Guannan Ren

On 11/16/2012 02:47 PM, Wayne Sun wrote:

Test hotplug domain CPU, loop increase cpu to max then decrease
to min. Now qemu did not support decrease plug of cpu, so decrease
part will fail, leave it in the case to keep integrity.

Signed-off-by: Wayne Sun g...@redhat.com
---
  cases/cpu_hotplug.conf  |   38 +++
  repos/domain/cpu_hotplug.py |  257 +++
  2 files changed, 295 insertions(+), 0 deletions(-)
  create mode 100644 cases/cpu_hotplug.conf
  create mode 100644 repos/domain/cpu_hotplug.py

diff --git a/cases/cpu_hotplug.conf b/cases/cpu_hotplug.conf
new file mode 100644
index 000..7ac0612
--- /dev/null
+++ b/cases/cpu_hotplug.conf
@@ -0,0 +1,38 @@
+domain:install_linux_cdrom
+guestname
+$defaultname
+guestos
+$defaultos
+guestarch
+$defaultarch
+vcpu
+$defaultvcpu
+memory
+$defaultmem
+hddriver
+$defaulthd
+nicdriver
+$defaultnic
+macaddr
+54:52:00:45:c3:8a
+
+domain:cpu_hotplug
+guestname
+$defaultname
+vcpu
+4
+username
+$username
+password
+$password
+
+domain:destroy
+guestname
+$defaultname
+
+domain:undefine
+guestname
+$defaultname
+
+options cleanup=enable
+
diff --git a/repos/domain/cpu_hotplug.py b/repos/domain/cpu_hotplug.py
new file mode 100644
index 000..3626db7
--- /dev/null
+++ b/repos/domain/cpu_hotplug.py
@@ -0,0 +1,257 @@
+#!/usr/bin/env python
+# Test hotplug domain CPU, loop increase cpu to max then decrease
+# to min
+
+import time
+import commands
+from xml.dom import minidom
+
+import libvirt
+from libvirt import libvirtError
+
+from src import sharedmod
+from utils import utils
+
+required_params = ('guestname', 'vcpu', 'username', 'password')
+optional_params = {}
+
+def check_domain_running(conn, guestname):
+ check if the domain exists, may or may not be active 
+guest_names = []
+ids = conn.listDomainsID()
+for id in ids:
+obj = conn.lookupByID(id)
+guest_names.append(obj.name())
+
+if guestname not in guest_names:
+logger.error(%s doesn't exist or not running % guestname)
+return 1
+else:
+return 0
+
+def redefine_vcpu_number(domobj, guestname, vcpu):
+dump domain xml description to change the vcpu number,
+   then, define the domain again
+
+guestxml = domobj.XMLDesc(0)
+logger.debug('''original guest %s xml :\n%s''' %(guestname, guestxml))
+
+doc = minidom.parseString(guestxml)
+
+newvcpu = doc.createElement('vcpu')
+newvcpuval = doc.createTextNode(str(vcpu))
+newvcpu.appendChild(newvcpuval)
+newvcpu.setAttribute('current', '1')
+
+domain = doc.getElementsByTagName('domain')[0]
+oldvcpu = doc.getElementsByTagName('vcpu')[0]
+
+domain.replaceChild(newvcpu, oldvcpu)
+
+return doc.toxml()
+
+def check_current_vcpu(domobj, username, password):
+dump domain xml description to get current vcpu number
+
+guestxml = domobj.XMLDesc(1)
+logger.debug(domain %s xml is :\n%s %(domobj.name(), guestxml))
+
+xml = minidom.parseString(guestxml)
+vcpu = xml.getElementsByTagName('vcpu')[0]
+if vcpu.hasAttribute('current'):
+attr = vcpu.getAttributeNode('current')
+current_vcpu = int(attr.nodeValue)
+else:
+logger.info(domain did not have 'current' attribute in vcpu element)
+current_vcpu = int(vcpu.childNodes[0].data)
+
+logger.info(check cpu number in domain)
+ip = utils.mac_to_ip(mac, 180)
+
+cmd = cat /proc/cpuinfo | grep processor | wc -l
+ret, output = utils.remote_exec_pexpect(ip, username, password, cmd)
+if not ret:
+logger.info(cpu number in domain is %s % output)
+if int(output) == current_vcpu:
+logger.info(cpu number in domain is equal to current vcpu value)
+return current_vcpu
+else:
+logger.error(current vcpu is not equal as check in domain)
+return False
+else:
+logger.error(check in domain fail)
+return False
+
+
+def set_vcpus(domobj, guestname, vcpu, username, password):
+set the value of virtual machine to vcpu offline , then boot up
+   the virtual machine
+
+timeout = 60
+logger.info('destroy domain')
+
+try:
+domobj.destroy()
+except libvirtError, e:
+logger.error(API error message: %s, error code is %s \
+% (e.message, e.get_error_code()))
+logger.error(fail to destroy domain)
+return 1
+
+newguestxml = redefine_vcpu_number(domobj, guestname, vcpu)
+logger.debug('''new guest %s xml :\n%s''' %(guestname, newguestxml))
+
+logger.info(undefine the original guest)
+try:
+domobj.undefine()
+except libvirtError, e:
+logger.error(API error message: %s, error code is %s \
+ % (e.message, e.get_error_code()))
+

[libvirt] [test-API][PATCH] Add cpu hotplug test case

2012-11-15 Thread Wayne Sun
Test hotplug domain CPU, loop increase cpu to max then decrease
to min. Now qemu did not support decrease plug of cpu, so decrease
part will fail, leave it in the case to keep integrity.

Signed-off-by: Wayne Sun g...@redhat.com
---
 cases/cpu_hotplug.conf  |   38 +++
 repos/domain/cpu_hotplug.py |  257 +++
 2 files changed, 295 insertions(+), 0 deletions(-)
 create mode 100644 cases/cpu_hotplug.conf
 create mode 100644 repos/domain/cpu_hotplug.py

diff --git a/cases/cpu_hotplug.conf b/cases/cpu_hotplug.conf
new file mode 100644
index 000..7ac0612
--- /dev/null
+++ b/cases/cpu_hotplug.conf
@@ -0,0 +1,38 @@
+domain:install_linux_cdrom
+guestname
+$defaultname
+guestos
+$defaultos
+guestarch
+$defaultarch
+vcpu
+$defaultvcpu
+memory
+$defaultmem
+hddriver
+$defaulthd
+nicdriver
+$defaultnic
+macaddr
+54:52:00:45:c3:8a
+
+domain:cpu_hotplug
+guestname
+$defaultname
+vcpu
+4
+username
+$username
+password
+$password
+
+domain:destroy
+guestname
+$defaultname
+
+domain:undefine
+guestname
+$defaultname
+
+options cleanup=enable
+
diff --git a/repos/domain/cpu_hotplug.py b/repos/domain/cpu_hotplug.py
new file mode 100644
index 000..3626db7
--- /dev/null
+++ b/repos/domain/cpu_hotplug.py
@@ -0,0 +1,257 @@
+#!/usr/bin/env python
+# Test hotplug domain CPU, loop increase cpu to max then decrease
+# to min
+
+import time
+import commands
+from xml.dom import minidom
+
+import libvirt
+from libvirt import libvirtError
+
+from src import sharedmod
+from utils import utils
+
+required_params = ('guestname', 'vcpu', 'username', 'password')
+optional_params = {}
+
+def check_domain_running(conn, guestname):
+ check if the domain exists, may or may not be active 
+guest_names = []
+ids = conn.listDomainsID()
+for id in ids:
+obj = conn.lookupByID(id)
+guest_names.append(obj.name())
+
+if guestname not in guest_names:
+logger.error(%s doesn't exist or not running % guestname)
+return 1
+else:
+return 0
+
+def redefine_vcpu_number(domobj, guestname, vcpu):
+dump domain xml description to change the vcpu number,
+   then, define the domain again
+
+guestxml = domobj.XMLDesc(0)
+logger.debug('''original guest %s xml :\n%s''' %(guestname, guestxml))
+
+doc = minidom.parseString(guestxml)
+
+newvcpu = doc.createElement('vcpu')
+newvcpuval = doc.createTextNode(str(vcpu))
+newvcpu.appendChild(newvcpuval)
+newvcpu.setAttribute('current', '1')
+
+domain = doc.getElementsByTagName('domain')[0]
+oldvcpu = doc.getElementsByTagName('vcpu')[0]
+
+domain.replaceChild(newvcpu, oldvcpu)
+
+return doc.toxml()
+
+def check_current_vcpu(domobj, username, password):
+dump domain xml description to get current vcpu number
+
+guestxml = domobj.XMLDesc(1)
+logger.debug(domain %s xml is :\n%s %(domobj.name(), guestxml))
+
+xml = minidom.parseString(guestxml)
+vcpu = xml.getElementsByTagName('vcpu')[0]
+if vcpu.hasAttribute('current'):
+attr = vcpu.getAttributeNode('current')
+current_vcpu = int(attr.nodeValue)
+else:
+logger.info(domain did not have 'current' attribute in vcpu element)
+current_vcpu = int(vcpu.childNodes[0].data)
+
+logger.info(check cpu number in domain)
+ip = utils.mac_to_ip(mac, 180)
+
+cmd = cat /proc/cpuinfo | grep processor | wc -l
+ret, output = utils.remote_exec_pexpect(ip, username, password, cmd)
+if not ret:
+logger.info(cpu number in domain is %s % output)
+if int(output) == current_vcpu:
+logger.info(cpu number in domain is equal to current vcpu value)
+return current_vcpu
+else:
+logger.error(current vcpu is not equal as check in domain)
+return False
+else:
+logger.error(check in domain fail)
+return False
+
+
+def set_vcpus(domobj, guestname, vcpu, username, password):
+set the value of virtual machine to vcpu offline , then boot up
+   the virtual machine
+
+timeout = 60
+logger.info('destroy domain')
+
+try:
+domobj.destroy()
+except libvirtError, e:
+logger.error(API error message: %s, error code is %s \
+% (e.message, e.get_error_code()))
+logger.error(fail to destroy domain)
+return 1
+
+newguestxml = redefine_vcpu_number(domobj, guestname, vcpu)
+logger.debug('''new guest %s xml :\n%s''' %(guestname, newguestxml))
+
+logger.info(undefine the original guest)
+try:
+domobj.undefine()
+except libvirtError, e:
+logger.error(API error message: %s, error code is %s \
+ % (e.message, e.get_error_code()))
+logger.error(fail to undefine guest % % guestname)
+