summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/__init__.py13
-rw-r--r--tests/unittests/test__init__.py20
2 files changed, 28 insertions, 5 deletions
diff --git a/cloudinit/__init__.py b/cloudinit/__init__.py
index 7a34e053..4338b46f 100644
--- a/cloudinit/__init__.py
+++ b/cloudinit/__init__.py
@@ -572,10 +572,14 @@ def handler_handle_part(mod, data, ctype, filename, payload, frequency):
if not (modfreq == per_always or
(frequency == per_instance and modfreq == per_instance)):
return
- if mod.handler_version == 1:
- mod.handle_part(data, ctype, filename, payload)
- else:
- mod.handle_part(data, ctype, filename, payload, frequency)
+ try:
+ if mod.handler_version == 1:
+ mod.handle_part(data, ctype, filename, payload)
+ else:
+ mod.handle_part(data, ctype, filename, payload, frequency)
+ except:
+ util.logexc(log)
+ traceback.print_exc(file=sys.stderr)
def partwalker_handle_handler(pdata, _ctype, _filename, payload):
@@ -594,7 +598,6 @@ def partwalker_handle_handler(pdata, _ctype, _filename, payload):
except:
util.logexc(log)
traceback.print_exc(file=sys.stderr)
- return
def partwalker_callback(pdata, ctype, filename, payload):
diff --git a/tests/unittests/test__init__.py b/tests/unittests/test__init__.py
index 79df8e72..f1818e9c 100644
--- a/tests/unittests/test__init__.py
+++ b/tests/unittests/test__init__.py
@@ -144,3 +144,23 @@ class TestHandlerHandlePart(MockerTestCase):
self.mocker.replay()
handler_handle_part(mod_mock, self.data, self.ctype, self.filename, self.payload, self.frequency)
+
+ def test_exception_is_caught(self):
+ """Exceptions within C{handle_part} are caught and logged."""
+ # Build a mock part-handler module
+ mod_mock = self.mocker.mock()
+ getattr(mod_mock, "frequency")
+ self.mocker.result("once-per-instance")
+ getattr(mod_mock, "handler_version")
+ self.mocker.result(1)
+ mod_mock.handle_part(self.data, self.ctype, self.filename, self.payload)
+ self.mocker.throw(Exception())
+ # Mock log function
+ logexc_mock = self.mocker.replace(logexc, passthrough=False)
+ logexc_mock(ANY)
+ # Mock the print_exc function
+ print_exc_mock = self.mocker.replace("traceback.print_exc", passthrough=False)
+ print_exc_mock(ARGS, KWARGS)
+ self.mocker.replay()
+
+ handler_handle_part(mod_mock, self.data, self.ctype, self.filename, self.payload, self.frequency)