From 9800832d4fbfef2624baa0d3c1a0aa737bc0dfb2 Mon Sep 17 00:00:00 2001 From: harlowja Date: Thu, 10 Jan 2013 23:09:02 -0800 Subject: 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 --- tests/unittests/helpers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tests/unittests/helpers.py') 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 -- cgit v1.2.3