Commit 6760e4ed authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

jqueue: Use separate function for encoding errors



Comes with unittest.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 697a3d61
......@@ -676,6 +676,18 @@ class _WaitForJobChangesHelper(object):
return constants.JOB_NOTCHANGED
def _EncodeOpError(err):
"""Encodes an error which occurred while processing an opcode.
"""
if isinstance(err, errors.GenericError):
to_encode = err
else:
to_encode = errors.OpExecError(str(err))
return errors.EncodeException(to_encode)
class _JobQueueWorker(workerpool.BaseWorker):
"""The actual job workers.
......@@ -764,18 +776,14 @@ class _JobQueueWorker(workerpool.BaseWorker):
try:
logging.debug("Opcode %s/%s failed", idx + 1, count)
op.status = constants.OP_STATUS_ERROR
if isinstance(err, errors.GenericError):
to_encode = err
else:
to_encode = errors.OpExecError(str(err))
op.result = errors.EncodeException(to_encode)
op.result = _EncodeOpError(err)
op.end_timestamp = TimeStampNow()
logging.info("Op %s/%s: Error in opcode %s: %s",
idx + 1, count, op_summary, err)
to_encode = errors.OpExecError("Preceding opcode failed")
job.MarkUnfinishedOps(constants.OP_STATUS_ERROR,
errors.EncodeException(to_encode))
_EncodeOpError(to_encode))
# Consistency check
assert compat.all(i.status == constants.OP_STATUS_SUCCESS
......
......@@ -220,5 +220,24 @@ class TestWaitForJobChangesHelper(unittest.TestCase):
["status"], None, None, 1.0) is None)
class TestEncodeOpError(unittest.TestCase):
def test(self):
encerr = jqueue._EncodeOpError(errors.LockError("Test 1"))
self.assert_(isinstance(encerr, tuple))
self.assertRaises(errors.LockError, errors.MaybeRaise, encerr)
encerr = jqueue._EncodeOpError(errors.GenericError("Test 2"))
self.assert_(isinstance(encerr, tuple))
self.assertRaises(errors.GenericError, errors.MaybeRaise, encerr)
encerr = jqueue._EncodeOpError(NotImplementedError("Foo"))
self.assert_(isinstance(encerr, tuple))
self.assertRaises(errors.OpExecError, errors.MaybeRaise, encerr)
encerr = jqueue._EncodeOpError("Hello World")
self.assert_(isinstance(encerr, tuple))
self.assertRaises(errors.OpExecError, errors.MaybeRaise, encerr)
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment