diff options
| author | Joshua Harlow <harlowja@gmail.com> | 2013-01-14 09:20:53 -0500 | 
|---|---|---|
| committer | Scott Moser <smoser@ubuntu.com> | 2013-01-14 09:20:53 -0500 | 
| commit | 0c38be4ae18e08640269e2f45e9243ea4867153c (patch) | |
| tree | 33be0d7078485b88d37172c4e235ba682d2e2db3 /tests/unittests/helpers.py | |
| parent | bf7bdf27e109ffb4964d4869b79da964fc40fb65 (diff) | |
| parent | 9800832d4fbfef2624baa0d3c1a0aa737bc0dfb2 (diff) | |
| download | vyos-cloud-init-0c38be4ae18e08640269e2f45e9243ea4867153c.tar.gz vyos-cloud-init-0c38be4ae18e08640269e2f45e9243ea4867153c.zip  | |
tests: 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.py | 14 | 
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  | 
