Vered Volansky has uploaded a new change for review.
Change subject: utils: Moved ContextManager from misc to utils ......................................................................
utils: Moved ContextManager from misc to utils
The ContextManager was located in storage/misc, and is used by several non-storage tests. It was therefore moved to utils, where it belongs. Accordingly, ContextManagerTests class was moved to utilstests from mistests.
Change-Id: I985103650a5706d35d9cd519618d09c692feb0be Signed-off-by: Vered Volansky vvolansk@redhat.com --- M lib/vdsm/utils.py M tests/functional/networkTests.py M tests/functional/storageTests.py M tests/functional/virtTests.py M tests/miscTests.py M tests/utilsTests.py M vdsm/storage/misc.py M vdsm/storage/resourceManager.py 8 files changed, 118 insertions(+), 121 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/61/22861/1
diff --git a/lib/vdsm/utils.py b/lib/vdsm/utils.py index b072bd4..d6c65b2 100644 --- a/lib/vdsm/utils.py +++ b/lib/vdsm/utils.py @@ -1077,3 +1077,44 @@ :return: """ self.callbacks.append(Callback(func, args, kwargs)) + + +class RollbackContext(object): + ''' + A context manager for recording and playing rollback. + The first exception will be remembered and re-raised after rollback + + Sample usage: + with RollbackContext() as rollback: + step1() + rollback.prependDefer(lambda: undo step1) + def undoStep2(arg): pass + step2() + rollback.prependDefer(undoStep2, arg) + + More examples see tests/miscTests.py + ''' + def __init__(self, *args): + self._finally = [] + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, traceback): + for undo, args, kwargs in self._finally: + try: + undo(*args, **kwargs) + except Exception: + # keep the earliest exception info + if exc_type is None: + exc_type, exc_value, traceback = sys.exc_info() + + # re-raise the earliest exception + if exc_type is not None: + raise exc_type, exc_value, traceback + + def defer(self, func, *args, **kwargs): + self._finally.append((func, args, kwargs)) + + def prependDefer(self, func, *args, **kwargs): + self._finally.insert(0, (func, args, kwargs)) diff --git a/tests/functional/networkTests.py b/tests/functional/networkTests.py index 21d00ff..9f8e50f 100644 --- a/tests/functional/networkTests.py +++ b/tests/functional/networkTests.py @@ -21,8 +21,6 @@ import os.path
import neterrors -from storage.misc import RollbackContext - from hookValidation import ValidatesHook from testrunner import (VdsmTestCase as TestCaseBase, expandPermutations, permutations) @@ -40,6 +38,7 @@ ruleExists, Route, Rule, addrFlush, LinkType, getLinks)
+from vdsm.utils import RollbackContext from vdsm.netinfo import operstate, prefix2netmask, getRouteDeviceTo from vdsm import ipwrapper
diff --git a/tests/functional/storageTests.py b/tests/functional/storageTests.py index 542f72a..de70864 100644 --- a/tests/functional/storageTests.py +++ b/tests/functional/storageTests.py @@ -40,12 +40,11 @@ import storage.storage_exception as se import storage.volume from storage.misc import execCmd -from storage.misc import RollbackContext from storage.mount import Mount
from vdsm.config import config from vdsm.constants import VDSM_USER, VDSM_GROUP -from vdsm.utils import CommandPath +from vdsm.utils import CommandPath, RollbackContext from vdsm import vdscli
_VARTMP = '/var/tmp' diff --git a/tests/functional/virtTests.py b/tests/functional/virtTests.py index bd54621..f41de07 100644 --- a/tests/functional/virtTests.py +++ b/tests/functional/virtTests.py @@ -30,10 +30,9 @@ from testrunner import VdsmTestCase as TestCaseBase from testrunner import permutations, expandPermutations
-from vdsm.utils import CommandPath +from vdsm.utils import CommandPath, RollbackContext import storageTests as storage from storage.misc import execCmd -from storage.misc import RollbackContext
from utils import VdsProxy, SUCCESS
diff --git a/tests/miscTests.py b/tests/miscTests.py index c726908..824e783 100644 --- a/tests/miscTests.py +++ b/tests/miscTests.py @@ -1086,77 +1086,6 @@ proc.wait()
-class RollbackContextTests(TestCaseBase): - def setUp(self): - self._called = 0 - - def _callDef(self): - self._called += 1 - self.log.info("Incremented call count (%d)", self._called) - - def _raiseDef(self, ex=Exception()): - self.log.info("Raised exception (%s)", ex.__class__.__name__) - raise ex - - def test(self): - with misc.RollbackContext() as rollback: - rollback.prependDefer(self._callDef) - - self.assertEquals(self._called, 1) - - def testRaise(self): - """ - Test that raising an exception in a deferred action does - not block all subsequent actions from running - """ - try: - with misc.RollbackContext() as rollback: - rollback.prependDefer(self._callDef) - rollback.prependDefer(self._raiseDef) - rollback.prependDefer(self._callDef) - except Exception: - self.assertEquals(self._called, 2) - return - - self.fail("Exception was not raised") - - def testFirstException(self): - """ - Test that if multiple actions raise an exception only the first one is - raised. When performing a batch rollback operations, probably the first - exception is the root cause. - """ - try: - with misc.RollbackContext() as rollback: - rollback.prependDefer(self._callDef) - rollback.prependDefer(self._raiseDef) - rollback.prependDefer(self._callDef) - rollback.prependDefer(self._raiseDef, RuntimeError()) - rollback.prependDefer(self._callDef) - except RuntimeError: - self.assertEquals(self._called, 3) - return - except Exception: - self.fail("Wrong exception was raised") - - self.fail("Exception was not raised") - - def testKeyErrorException(self): - """ - KeyError is raised as a tuple and not expection. Re-rasing it - should be aware of this fact and handled carfully. - """ - try: - with misc.RollbackContext(): - {}['aKey'] - except KeyError: - return - except Exception: - self.fail("Wrong exception was raised") - - self.fail("Exception was not raised") - - class FindCallerTests(TestCaseBase): def _assertFindCaller(self, callback): frame = inspect.currentframe() diff --git a/tests/utilsTests.py b/tests/utilsTests.py index c5b250d..7cde5a3 100644 --- a/tests/utilsTests.py +++ b/tests/utilsTests.py @@ -302,3 +302,74 @@ def handle(self, record): assert self.record is None self.record = record + + +class RollbackContextTests(TestCaseBase): + def setUp(self): + self._called = 0 + + def _callDef(self): + self._called += 1 + self.log.info("Incremented call count (%d)", self._called) + + def _raiseDef(self, ex=Exception()): + self.log.info("Raised exception (%s)", ex.__class__.__name__) + raise ex + + def test(self): + with utils.RollbackContext() as rollback: + rollback.prependDefer(self._callDef) + + self.assertEquals(self._called, 1) + + def testRaise(self): + """ + Test that raising an exception in a deferred action does + not block all subsequent actions from running + """ + try: + with utils.RollbackContext() as rollback: + rollback.prependDefer(self._callDef) + rollback.prependDefer(self._raiseDef) + rollback.prependDefer(self._callDef) + except Exception: + self.assertEquals(self._called, 2) + return + + self.fail("Exception was not raised") + + def testFirstException(self): + """ + Test that if multiple actions raise an exception only the first one is + raised. When performing a batch rollback operations, probably the first + exception is the root cause. + """ + try: + with utils.RollbackContext() as rollback: + rollback.prependDefer(self._callDef) + rollback.prependDefer(self._raiseDef) + rollback.prependDefer(self._callDef) + rollback.prependDefer(self._raiseDef, RuntimeError()) + rollback.prependDefer(self._callDef) + except RuntimeError: + self.assertEquals(self._called, 3) + return + except Exception: + self.fail("Wrong exception was raised") + + self.fail("Exception was not raised") + + def testKeyErrorException(self): + """ + KeyError is raised as a tuple and not expection. Re-rasing it + should be aware of this fact and handled carfully. + """ + try: + with utils.RollbackContext(): + {}['aKey'] + except KeyError: + return + except Exception: + self.fail("Wrong exception was raised") + + self.fail("Exception was not raised") diff --git a/vdsm/storage/misc.py b/vdsm/storage/misc.py index f1d8ab0..2c06af3 100644 --- a/vdsm/storage/misc.py +++ b/vdsm/storage/misc.py @@ -43,7 +43,6 @@ import select import string import struct -import sys import threading import types import weakref @@ -675,47 +674,6 @@ nextRequest, self._currentState = self._queue.get_nowait()
nextRequest.set() - - -class RollbackContext(object): - ''' - A context manager for recording and playing rollback. - The first exception will be remembered and re-raised after rollback - - Sample usage: - with RollbackContext() as rollback: - step1() - rollback.prependDefer(lambda: undo step1) - def undoStep2(arg): pass - step2() - rollback.prependDefer(undoStep2, arg) - - More examples see tests/miscTests.py - ''' - def __init__(self, *args): - self._finally = [] - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc_value, traceback): - for undo, args, kwargs in self._finally: - try: - undo(*args, **kwargs) - except Exception: - # keep the earliest exception info - if exc_type is None: - exc_type, exc_value, traceback = sys.exc_info() - - # re-raise the earliest exception - if exc_type is not None: - raise exc_type, exc_value, traceback - - def defer(self, func, *args, **kwargs): - self._finally.append((func, args, kwargs)) - - def prependDefer(self, func, *args, **kwargs): - self._finally.insert(0, (func, args, kwargs))
class DynamicBarrier(object): diff --git a/vdsm/storage/resourceManager.py b/vdsm/storage/resourceManager.py index 14049dc..9d30eba 100644 --- a/vdsm/storage/resourceManager.py +++ b/vdsm/storage/resourceManager.py @@ -29,6 +29,7 @@ import storage_exception as se import misc from logUtils import SimpleLogAdapter +from vdsm.utils import RollbackContext
# Errors @@ -539,7 +540,7 @@ request = Request(namespace, name, lockType, callback) self._log.debug("Trying to register resource '%s' for lock type '%s'", fullName, lockType) - with nested(misc.RollbackContext(), + with nested(RollbackContext(), self._syncRoot.shared) as (contextCleanup, lock): try: namespaceObj = self._namespaces[namespace] @@ -613,7 +614,7 @@ fullName = "%s.%s" % (namespace, name)
self._log.debug("Trying to release resource '%s'", fullName) - with nested(misc.RollbackContext(), + with nested(RollbackContext(), self._syncRoot.shared) as (contextCleanup, lock): try: namespaceObj = self._namespaces[namespace]
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved ContextManager from misc to utils ......................................................................
Patch Set 1: Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6467/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5574/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1115/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6380/ : SUCCESS
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 2:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6471/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5578/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1116/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6384/ : SUCCESS
Nir Soffer has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 2:
Looks like a patch is missing - the code in RollbackContext is different from the code we can see in current master.
Nir Soffer has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 2:
Try to checkout this on master and you will see the conflicts.
Nir Soffer has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 2:
This patch should be rebased on http://gerrit.ovirt.org/22860
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 3: Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6477/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5584/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1117/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6390/ : SUCCESS
Nir Soffer has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 3:
(3 comments)
Looks good, needs little cleanup.
.................................................... Commit Message Line 7: utils: Moved RollbackContext from misc to utils Line 8: Line 9: The RollbackContext class was located in storage/misc, and is used by Line 10: several non-storage tests. It was therefore moved to utils, where it Line 11: belongs. Accordingly, ContextManagerTests class was moved to utilsTests Replace "ContextManagerTests" with "RollbackContextTests" Line 12: from miscTests. Line 13: Line 14: Change-Id: I985103650a5706d35d9cd519618d09c692feb0be
.................................................... File lib/vdsm/utils.py Line 1091: def undoStep2(arg): pass Line 1092: step2() Line 1093: rollback.prependDefer(undoStep2, arg) Line 1094: Line 1095: More examples see tests/miscTests.py Replace "miscTests.py" to "utilsTests.py" Line 1096: ''' Line 1097: def __init__(self, *args): Line 1098: self._finally = [] Line 1099:
.................................................... File vdsm/storage/resourceManager.py Line 28: Line 29: import storage_exception as se Line 30: import misc Line 31: from logUtils import SimpleLogAdapter Line 32: from vdsm.utils import RollbackContext Please keep the previous way, importing utils, and using utils.RollbackContext.
We don't have yet a policy to import only modules, but we should have. Line 33: Line 34: Line 35: # Errors Line 36:
Allon Mureinik has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 3:
(2 comments)
Basically +1, please handle Nir's comments.
.................................................... Commit Message Line 7: utils: Moved RollbackContext from misc to utils Line 8: Line 9: The RollbackContext class was located in storage/misc, and is used by Line 10: several non-storage tests. It was therefore moved to utils, where it Line 11: belongs. Accordingly, ContextManagerTests class was moved to utilsTests +1. Line 12: from miscTests. Line 13: Line 14: Change-Id: I985103650a5706d35d9cd519618d09c692feb0be
.................................................... File lib/vdsm/utils.py Line 1091: def undoStep2(arg): pass Line 1092: step2() Line 1093: rollback.prependDefer(undoStep2, arg) Line 1094: Line 1095: More examples see tests/miscTests.py +1. Line 1096: ''' Line 1097: def __init__(self, *args): Line 1098: self._finally = [] Line 1099:
Vered Volansky has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 3:
(3 comments)
.................................................... Commit Message Line 7: utils: Moved RollbackContext from misc to utils Line 8: Line 9: The RollbackContext class was located in storage/misc, and is used by Line 10: several non-storage tests. It was therefore moved to utils, where it Line 11: belongs. Accordingly, ContextManagerTests class was moved to utilsTests Done Line 12: from miscTests. Line 13: Line 14: Change-Id: I985103650a5706d35d9cd519618d09c692feb0be
.................................................... File lib/vdsm/utils.py Line 1091: def undoStep2(arg): pass Line 1092: step2() Line 1093: rollback.prependDefer(undoStep2, arg) Line 1094: Line 1095: More examples see tests/miscTests.py Done Line 1096: ''' Line 1097: def __init__(self, *args): Line 1098: self._finally = [] Line 1099:
.................................................... File vdsm/storage/resourceManager.py Line 28: Line 29: import storage_exception as se Line 30: import misc Line 31: from logUtils import SimpleLogAdapter Line 32: from vdsm.utils import RollbackContext Done Line 33: Line 34: Line 35: # Errors Line 36:
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 4: Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6481/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5588/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1118/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6394/ : SUCCESS
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 5:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6484/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5591/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1119/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6397/ : SUCCESS
Vered Volansky has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 6: Verified+1
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 6: Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6486/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5593/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1120/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6399/ : SUCCESS
Vered Volansky has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 7: Verified+1
Rebased
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 7:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6490/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5597/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1121/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6402/ : SUCCESS
Allon Mureinik has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 7: Code-Review+1
Ayal Baron has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 7: Code-Review+1
Nir Soffer has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 7: Code-Review+1
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 8: Code-Review-1 Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6581/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5688/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1127/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6493/ : UNSTABLE
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 9: Code-Review-1 Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6586/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5693/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1128/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6499/ : UNSTABLE
Vered Volansky has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 10: Verified+1
Rebased.
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 10: Code-Review-1 Verified-1
Build Failed
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6613/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5720/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1130/ : FAILURE
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6525/ : UNSTABLE
oVirt Jenkins CI Server has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 11:
Build Successful
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit/6617/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_unit_tests_gerrit_el/5724/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_network_functional_tests/1131/ : SUCCESS
http://jenkins.ovirt.org/job/vdsm_pep8_gerrit/6530/ : SUCCESS
Vered Volansky has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 11: Verified+1
Nir Soffer has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 11: Code-Review+1
Yaniv Bronhaim has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 11: Code-Review+1
Dan Kenigsberg has posted comments on this change.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
Patch Set 11: Code-Review+2
Dan Kenigsberg has submitted this change and it was merged.
Change subject: utils: Moved RollbackContext from misc to utils ......................................................................
utils: Moved RollbackContext from misc to utils
The RollbackContext class was located in storage/misc, and is used by several non-storage tests. It was therefore moved to utils, where it belongs. Accordingly, RollbackContextTests class was moved to utilsTests from miscTests.
Change-Id: I985103650a5706d35d9cd519618d09c692feb0be Signed-off-by: Vered Volansky vvolansk@redhat.com Reviewed-on: http://gerrit.ovirt.org/22861 Reviewed-by: Nir Soffer nsoffer@redhat.com Reviewed-by: Yaniv Bronhaim ybronhei@redhat.com Reviewed-by: Dan Kenigsberg danken@redhat.com --- M lib/vdsm/utils.py M tests/functional/networkTests.py M tests/functional/storageTests.py M tests/functional/virtTests.py M tests/miscTests.py M tests/utilsTests.py M vdsm/storage/misc.py M vdsm/storage/resourceManager.py 8 files changed, 152 insertions(+), 155 deletions(-)
Approvals: Nir Soffer: Looks good to me, but someone else must approve Yaniv Bronhaim: Looks good to me, but someone else must approve Dan Kenigsberg: Looks good to me, approved Vered Volansky: Verified
vdsm-patches@lists.fedorahosted.org