summaryrefslogtreecommitdiff
path: root/tests/unittests/helpers.py
diff options
context:
space:
mode:
authorharlowja <harlowja@virtualbox.rhel>2013-01-10 23:09:02 -0800
committerharlowja <harlowja@virtualbox.rhel>2013-01-10 23:09:02 -0800
commit9800832d4fbfef2624baa0d3c1a0aa737bc0dfb2 (patch)
tree409420860307ce2721e3be848936e54d66db132a /tests/unittests/helpers.py
parent3569e71a1579b97f4e33fb46ab3fcef08a4ddad4 (diff)
downloadvyos-cloud-init-9800832d4fbfef2624baa0d3c1a0aa737bc0dfb2.tar.gz
vyos-cloud-init-9800832d4fbfef2624baa0d3c1a0aa737bc0dfb2.zip
Add a context manager function in test helpers.
This function can be used to ensure that mocker objects are restored and verified during usage if exceptions are thrown while the mock object is being used. Ensure it is used in the config drive test when multiple mock objects are being created and restored. LP: #1098430
Diffstat (limited to 'tests/unittests/helpers.py')
-rw-r--r--tests/unittests/helpers.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index 92540b0c..4258a29d 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -2,6 +2,9 @@ import os
import sys
import unittest
+from contextlib import contextmanager
+
+from mocker import Mocker
from mocker import MockerTestCase
from cloudinit import helpers as ch
@@ -31,6 +34,17 @@ else:
pass
+@contextmanager
+def mocker(verify_calls=True):
+ m = Mocker()
+ try:
+ yield m
+ finally:
+ m.restore()
+ if verify_calls:
+ m.verify()
+
+
# Makes the old path start
# with new base instead of whatever
# it previously had