[Kimchi-devel] [PATCH] Fix issue #589: Add listener to remove Kimchi leftovers

Aline Manera alinefm at linux.vnet.ibm.com
Wed Feb 11 17:06:21 UTC 2015


If Kimchi is stopped before it gets completely up, it can leave some
leftovers on system due the Feature tests.
So add a listener to cherrypy server to clean up any leftovers when
server is stopped.

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/kvmusertests.py       | 19 ++++++++-----------
 src/kimchi/model/config.py       | 27 +++++++++++++++++++++++++--
 src/kimchi/model/featuretests.py | 21 +++++++++++++--------
 3 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/src/kimchi/kvmusertests.py b/src/kimchi/kvmusertests.py
index 408706d..37a80d7 100644
--- a/src/kimchi/kvmusertests.py
+++ b/src/kimchi/kvmusertests.py
@@ -18,18 +18,18 @@
 
 import platform
 import psutil
-import uuid
 
 import libvirt
 
 from kimchi.rollbackcontext import RollbackContext
 
+KVMUSERTEST_VM_NAME = "KVMUSERTEST_VM"
+
 
 class UserTests(object):
     SIMPLE_VM_XML = """
     <domain type='kvm'>
-      <name>%(vm_name)s</name>
-      <uuid>%(vm_uuid)s</uuid>
+      <name>%(name)s</name>
       <memory unit='KiB'>262144</memory>
       <os>
         <type arch='%(arch)s'>hvm</type>
@@ -43,22 +43,19 @@ class UserTests(object):
         if cls.user:
             return cls.user
 
-        vm_uuid = uuid.uuid1()
-        vm_name = "kimchi_test_%s" % vm_uuid
         arch = 'ppc64' if platform.machine() == 'ppc64le' \
             else platform.machine()
 
-        xml = cls.SIMPLE_VM_XML % {'vm_name': vm_name, 'vm_uuid': vm_uuid,
-                                   'arch': arch}
+        xml = cls.SIMPLE_VM_XML % {'name': KVMUSERTEST_VM_NAME, 'arch': arch}
 
         with RollbackContext() as rollback:
             conn = libvirt.open(None)
             rollback.prependDefer(conn.close)
-            dom = conn.defineXML(xml)
-            rollback.prependDefer(dom.undefine)
-            dom.create()
+            dom = conn.createXML(xml,
+                                 flags=libvirt.VIR_DOMAIN_START_AUTODESTROY)
             rollback.prependDefer(dom.destroy)
-            with open('/var/run/libvirt/qemu/%s.pid' % vm_name) as f:
+            filename = '/var/run/libvirt/qemu/%s.pid' % KVMUSERTEST_VM_NAME
+            with open(filename) as f:
                 pidStr = f.read()
             p = psutil.Process(int(pidStr))
 
diff --git a/src/kimchi/model/config.py b/src/kimchi/model/config.py
index 6a7df41..1c43360 100644
--- a/src/kimchi/model/config.py
+++ b/src/kimchi/model/config.py
@@ -1,7 +1,7 @@
 #
 # Project Kimchi
 #
-# Copyright IBM, Corp. 2014
+# Copyright IBM, Corp. 2014-2015
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -27,7 +27,8 @@ from kimchi.config import find_qemu_binary, get_version
 from kimchi.distroloader import DistroLoader
 from kimchi.exception import NotFoundError
 from kimchi.model.debugreports import DebugReportsModel
-from kimchi.model.featuretests import FeatureTests
+from kimchi.model.featuretests import FeatureTests, FEATURETEST_POOL_NAME
+from kimchi.model.featuretests import FEATURETEST_VM_NAME
 from kimchi.repositories import Repositories
 from kimchi.screenshot import VMScreenshot
 from kimchi.swupdate import SoftwareUpdate
@@ -61,6 +62,28 @@ class CapabilitiesModel(object):
         # It is needed because some features tests depends on the server
         cherrypy.engine.subscribe('start', self._set_capabilities)
 
+        # Subscribe function to clean any Kimchi leftovers
+        cherrypy.engine.subscribe('stop', self._clean_leftovers)
+
+    def _clean_leftovers(self):
+        conn = self.conn.get()
+        FeatureTests.disable_libvirt_error_logging()
+        try:
+            dom = conn.lookupByName(FEATURETEST_VM_NAME)
+            dom.undefine()
+        except Exception:
+            # Any exception can be ignored here
+            pass
+
+        try:
+            pool = conn.storagePoolLookupByName(FEATURETEST_POOL_NAME)
+            pool.undefine()
+        except Exception:
+            # Any exception can be ignored here
+            pass
+
+        FeatureTests.enable_libvirt_error_logging()
+
     def _set_capabilities(self):
         kimchi_log.info("*** Running feature tests ***")
         conn = self.conn.get()
diff --git a/src/kimchi/model/featuretests.py b/src/kimchi/model/featuretests.py
index 5a45990..6ffdf4a 100644
--- a/src/kimchi/model/featuretests.py
+++ b/src/kimchi/model/featuretests.py
@@ -25,17 +25,19 @@ import socket
 import subprocess
 import threading
 
-
 from lxml.builder import E
 
-
 from kimchi.rollbackcontext import RollbackContext
 from kimchi.utils import kimchi_log, run_command
 
 
+FEATURETEST_VM_NAME = "FEATURETEST_VM"
+FEATURETEST_POOL_NAME = "FEATURETEST_POOL"
+
+
 ISO_STREAM_XML = """
 <domain type='%(domain)s'>
-  <name>ISO_STREAMING</name>
+  <name>%(name)s</name>
   <memory unit='KiB'>1048576</memory>
   <os>
     <type arch='%(arch)s'>hvm</type>
@@ -57,7 +59,7 @@ ISO_STREAM_XML = """
 
 SIMPLE_VM_XML = """
 <domain type='%(domain)s'>
-  <name>A_SIMPLE_VM</name>
+  <name>%(name)s</name>
   <memory unit='KiB'>10240</memory>
   <os>
     <type arch='%(arch)s'>hvm</type>
@@ -67,7 +69,7 @@ SIMPLE_VM_XML = """
 
 SCSI_FC_XML = """
 <pool type='scsi'>
-  <name>TEST_SCSI_FC_POOL</name>
+  <name>%(name)s</name>
   <source>
     <adapter type='fc_host' wwnn='1234567890abcdef' wwpn='abcdef1234567890'/>
   </source>
@@ -105,7 +107,8 @@ class FeatureTests(object):
         domain_type = 'test' if conn.getType().lower() == 'test' else 'kvm'
         arch = 'ppc64' if platform.machine() == 'ppc64le' \
             else platform.machine()
-        xml = ISO_STREAM_XML % {'domain': domain_type, 'protocol': protocol,
+        xml = ISO_STREAM_XML % {'name': FEATURETEST_VM_NAME,
+                                'domain': domain_type, 'protocol': protocol,
                                 'arch': arch}
         try:
             FeatureTests.disable_libvirt_error_logging()
@@ -176,7 +179,8 @@ class FeatureTests(object):
         try:
             FeatureTests.disable_libvirt_error_logging()
             pool = None
-            pool = conn.storagePoolDefineXML(SCSI_FC_XML, 0)
+            pool_xml = SCSI_FC_XML % {'name': FEATURETEST_POOL_NAME}
+            pool = conn.storagePoolDefineXML(pool_xml, 0)
         except libvirt.libvirtError as e:
             if e.get_error_code() == 27:
                 # Libvirt requires adapter name, not needed when supports to FC
@@ -196,7 +200,8 @@ class FeatureTests(object):
             domain_type = 'test' if conn.getType().lower() == 'test' else 'kvm'
             arch = 'ppc64' if platform.machine() == 'ppc64le' \
                 else platform.machine()
-            dom = conn.defineXML(SIMPLE_VM_XML % {'domain': domain_type,
+            dom = conn.defineXML(SIMPLE_VM_XML % {'name': FEATURETEST_VM_NAME,
+                                                  'domain': domain_type,
                                                   'arch': arch})
             rollback.prependDefer(dom.undefine)
             try:
-- 
2.1.0




More information about the Kimchi-devel mailing list