summaryrefslogtreecommitdiff
path: root/tests/unittests/helpers.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2015-01-22 15:41:52 -0500
committerBarry Warsaw <barry@python.org>2015-01-22 15:41:52 -0500
commit6f3fadcfeec94711de32574f7738f98aa0a697ac (patch)
treeff64de868b0cdc154b754a40e91530e5a5396469 /tests/unittests/helpers.py
parent9a16a6a2e9929098474e0db23db130cf172aeba4 (diff)
downloadvyos-cloud-init-6f3fadcfeec94711de32574f7738f98aa0a697ac.tar.gz
vyos-cloud-init-6f3fadcfeec94711de32574f7738f98aa0a697ac.zip
Convert helpers.py and test_data.py from mocker to mock.
Diffstat (limited to 'tests/unittests/helpers.py')
-rw-r--r--tests/unittests/helpers.py56
1 files changed, 33 insertions, 23 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index 4ca460f1..43dd5aa0 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -1,7 +1,17 @@
import os
import sys
+import tempfile
import unittest
+try:
+ from unittest import mock
+except ImportError:
+ import mock
+try:
+ from contextlib import ExitStack
+except ImportError:
+ from contextlib2 import ExitStack
+
from cloudinit import helpers as ch
from cloudinit import util
@@ -111,7 +121,11 @@ def retarget_many_wrapper(new_base, am, old_func):
class ResourceUsingTestCase(unittest.TestCase):
- def __init__(self, methodName="runTest"):
+ ## def __init__(self, methodName="runTest"):
+ ## self.resource_path = None
+
+ def setUp(self):
+ unittest.TestCase.setUp(self)
self.resource_path = None
def resourceLocation(self, subname=None):
@@ -139,17 +153,23 @@ class ResourceUsingTestCase(unittest.TestCase):
return fh.read()
def getCloudPaths(self):
+ tmpdir = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, tmpdir)
cp = ch.Paths({
- 'cloud_dir': self.makeDir(),
+ 'cloud_dir': tmpdir,
'templates_dir': self.resourceLocation(),
})
return cp
class FilesystemMockingTestCase(ResourceUsingTestCase):
- def __init__(self, methodName="runTest"):
- ResourceUsingTestCase.__init__(self, methodName)
- self.patched_funcs = []
+ ## def __init__(self, methodName="runTest"):
+ ## ResourceUsingTestCase.__init__(self, methodName)
+
+ def setUp(self):
+ ResourceUsingTestCase.setUp(self)
+ self.patched_funcs = ExitStack()
+ self.addCleanup(self.patched_funcs.close)
def replicateTestRoot(self, example_root, target_root):
real_root = self.resourceLocation()
@@ -163,15 +183,6 @@ class FilesystemMockingTestCase(ResourceUsingTestCase):
make_path = util.abs_join(make_path, f)
shutil.copy(real_path, make_path)
- def tearDown(self):
- self.restore()
- ResourceUsingTestCase.tearDown(self)
-
- def restore(self):
- for (mod, f, func) in self.patched_funcs:
- setattr(mod, f, func)
- self.patched_funcs = []
-
def patchUtils(self, new_root):
patch_funcs = {
util: [('write_file', 1),
@@ -188,8 +199,8 @@ class FilesystemMockingTestCase(ResourceUsingTestCase):
for (f, am) in funcs:
func = getattr(mod, f)
trap_func = retarget_many_wrapper(new_root, am, func)
- setattr(mod, f, trap_func)
- self.patched_funcs.append((mod, f, func))
+ self.patched_funcs.enter_context(
+ mock.patch.object(mod, f, trap_func))
# Handle subprocess calls
func = getattr(util, 'subp')
@@ -197,16 +208,15 @@ class FilesystemMockingTestCase(ResourceUsingTestCase):
def nsubp(*_args, **_kwargs):
return ('', '')
- setattr(util, 'subp', nsubp)
- self.patched_funcs.append((util, 'subp', func))
+ self.patched_funcs.enter_context(
+ mock.patch.object(util, 'subp', nsubp))
def null_func(*_args, **_kwargs):
return None
for f in ['chownbyid', 'chownbyname']:
- func = getattr(util, f)
- setattr(util, f, null_func)
- self.patched_funcs.append((util, f, func))
+ self.patched_funcs.enter_context(
+ mock.patch.object(util, f, null_func))
def patchOS(self, new_root):
patch_funcs = {
@@ -217,8 +227,8 @@ class FilesystemMockingTestCase(ResourceUsingTestCase):
for f in funcs:
func = getattr(mod, f)
trap_func = retarget_many_wrapper(new_root, 1, func)
- setattr(mod, f, trap_func)
- self.patched_funcs.append((mod, f, func))
+ self.patched_funcs.enter_context(
+ mock.patch.object(mod, f, trap_func))
class HttprettyTestCase(TestCase):