Reviewed-by: Royce Lv<lvroyce(a)linux.vnet.ibm.com>
On 12/29/2014 09:53 AM, Aline Manera wrote:
The rollback_wrapper function is used to avoid a NotFoundError when
the
tests finishes correctly. Move it to a common place to allow reuse.
Signed-off-by: Aline Manera <alinefm(a)linux.vnet.ibm.com>
---
tests/test_model.py | 23 +++++++----------------
tests/utils.py | 15 ++++++++++++++-
2 files changed, 21 insertions(+), 17 deletions(-)
diff --git a/tests/test_model.py b/tests/test_model.py
index 5984906..a8d35dc 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -964,10 +964,10 @@ class ModelTests(unittest.TestCase):
params_1 = {'name': 'kimchi-vm1', 'template':
'/templates/test'}
params_2 = {'name': 'kimchi-vm2', 'template':
'/templates/test'}
inst.vms_create(params_1)
- rollback.prependDefer(self._rollback_wrapper, inst.vm_delete,
+ rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
'kimchi-vm1')
inst.vms_create(params_2)
- rollback.prependDefer(self._rollback_wrapper, inst.vm_delete,
+ rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
'kimchi-vm2')
vms = inst.vms_get_list()
@@ -978,7 +978,7 @@ class ModelTests(unittest.TestCase):
{"graphics": {"passwd":
"123456"}})
inst.vm_start('kimchi-vm1')
- rollback.prependDefer(self._rollback_wrapper, inst.vm_poweroff,
+ rollback.prependDefer(utils.rollback_wrapper, inst.vm_poweroff,
'kimchi-vm1')
vm_info = inst.vm_lookup(u'kimchi-vm1')
@@ -1019,7 +1019,7 @@ class ModelTests(unittest.TestCase):
params = {'name': u'пeω-∨м', 'cpus': 4,
'memory': 2048}
inst.vm_update('kimchi-vm1', params)
- rollback.prependDefer(self._rollback_wrapper, inst.vm_delete,
+ rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
u'пeω-∨м')
self.assertEquals(info['uuid'],
inst.vm_lookup(u'пeω-∨м')['uuid'])
info = inst.vm_lookup(u'пeω-∨м')
@@ -1296,16 +1296,7 @@ class ModelTests(unittest.TestCase):
inst.task_wait(taskid, timeout=10)
self.assertEquals('finished',
inst.task_lookup(taskid)['status'])
- # This wrapper function is needed due to the new backend messaging in
- # vm model. vm_poweroff and vm_delete raise exception if vm is not found.
- # These functions are called after vm has been deleted if test finishes
- # correctly, then NofFoundError exception is raised and rollback breaks
- def _rollback_wrapper(self, func, vmname):
- try:
- func(vmname)
- except NotFoundError:
- # VM has been deleted already
- return
+
@unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
def test_delete_running_vm(self):
@@ -1318,11 +1309,11 @@ class ModelTests(unittest.TestCase):
params = {'name': u'kīмсhī-∨м', 'template':
u'/templates/test'}
inst.vms_create(params)
- rollback.prependDefer(self._rollback_wrapper, inst.vm_delete,
+ rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
u'kīмсhī-∨м')
inst.vm_start(u'kīмсhī-∨м')
- rollback.prependDefer(self._rollback_wrapper, inst.vm_poweroff,
+ rollback.prependDefer(utils.rollback_wrapper, inst.vm_poweroff,
u'kīмсhī-∨м')
inst.vm_delete(u'kīмсhī-∨м')
diff --git a/tests/utils.py b/tests/utils.py
index c692041..3677851 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -38,7 +38,7 @@ import kimchi.mockmodel
import kimchi.server
from kimchi.config import config, paths
from kimchi.auth import User, USER_NAME, USER_GROUPS, USER_ROLES, tabs
-from kimchi.exception import OperationFailed
+from kimchi.exception import NotFoundError, OperationFailed
from kimchi.utils import kimchi_log
_ports = {}
@@ -226,3 +226,16 @@ def wait_task(task_lookup, taskid, timeout=10):
return
kimchi_log.error("Timeout while process long-run task, "
"try to increase timeout value.")
+
+
+# The action functions in model backend raise NotFoundError exception if the
+# element is not found. But in some tests, these functions are called after
+# the element has been deleted if test finishes correctly, then NofFoundError
+# exception is raised and rollback breaks. To avoid it, this wrapper ignores
+# the NotFoundError.
+def rollback_wrapper(func, resource):
+ try:
+ func(resource)
+ except NotFoundError:
+ # VM has been deleted already
+ return