summaryrefslogtreecommitdiff
path: root/cloudinit/tests/helpers.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/tests/helpers.py')
-rw-r--r--cloudinit/tests/helpers.py72
1 files changed, 18 insertions, 54 deletions
diff --git a/cloudinit/tests/helpers.py b/cloudinit/tests/helpers.py
index 70f6bad7..58f63b69 100644
--- a/cloudinit/tests/helpers.py
+++ b/cloudinit/tests/helpers.py
@@ -1,7 +1,5 @@
# This file is part of cloud-init. See LICENSE file for license information.
-from __future__ import print_function
-
import functools
import httpretty
import io
@@ -13,20 +11,10 @@ import string
import sys
import tempfile
import time
+import unittest
+from contextlib import ExitStack, contextmanager
from unittest import mock
-
-import unittest2
-from unittest2.util import strclass
-
-try:
- from contextlib import ExitStack, contextmanager
-except ImportError:
- from contextlib2 import ExitStack, contextmanager
-
-try:
- from configparser import ConfigParser
-except ImportError:
- from ConfigParser import ConfigParser
+from unittest.util import strclass
from cloudinit.config.schema import (
SchemaValidationError, validate_cloudconfig_schema)
@@ -35,13 +23,14 @@ from cloudinit import distros
from cloudinit import helpers as ch
from cloudinit.sources import DataSourceNone
from cloudinit.templater import JINJA_AVAILABLE
+from cloudinit import subp
from cloudinit import util
-_real_subp = util.subp
+_real_subp = subp.subp
# Used for skipping tests
-SkipTest = unittest2.SkipTest
-skipIf = unittest2.skipIf
+SkipTest = unittest.SkipTest
+skipIf = unittest.skipIf
# Makes the old path start
@@ -78,7 +67,7 @@ def retarget_many_wrapper(new_base, am, old_func):
return wrapper
-class TestCase(unittest2.TestCase):
+class TestCase(unittest.TestCase):
def reset_global_state(self):
"""Reset any global state to its original settings.
@@ -114,16 +103,6 @@ class TestCase(unittest2.TestCase):
self.addCleanup(m.stop)
setattr(self, attr, p)
- # prefer python3 read_file over readfp but allow fallback
- def parse_and_read(self, contents):
- parser = ConfigParser()
- if hasattr(parser, 'read_file'):
- parser.read_file(contents)
- elif hasattr(parser, 'readfp'):
- # pylint: disable=W1505
- parser.readfp(contents)
- return parser
-
class CiTestCase(TestCase):
"""This is the preferred test case base class unless user
@@ -156,14 +135,17 @@ class CiTestCase(TestCase):
self.old_handlers = self.logger.handlers
self.logger.handlers = [handler]
if self.allowed_subp is True:
- util.subp = _real_subp
+ subp.subp = _real_subp
else:
- util.subp = self._fake_subp
+ subp.subp = self._fake_subp
def _fake_subp(self, *args, **kwargs):
if 'args' in kwargs:
cmd = kwargs['args']
else:
+ if not args:
+ raise TypeError(
+ "subp() missing 1 required positional argument: 'args'")
cmd = args[0]
if not isinstance(cmd, str):
@@ -190,7 +172,7 @@ class CiTestCase(TestCase):
# Remove the handler we setup
logging.getLogger().handlers = self.old_handlers
logging.getLogger().level = None
- util.subp = _real_subp
+ subp.subp = _real_subp
super(CiTestCase, self).tearDown()
def tmp_dir(self, dir=None, cleanup=True):
@@ -212,16 +194,6 @@ class CiTestCase(TestCase):
dir = self.tmp_dir()
return os.path.normpath(os.path.abspath(os.path.join(dir, path)))
- def sys_exit(self, code):
- """Provide a wrapper around sys.exit for python 2.6
-
- In 2.6, this code would produce 'cm.exception' with value int(2)
- rather than the SystemExit that was raised by sys.exit(2).
- with assertRaises(SystemExit) as cm:
- sys.exit(2)
- """
- raise SystemExit(code)
-
def tmp_cloud(self, distro, sys_cfg=None, metadata=None):
"""Create a cloud with tmp working directory paths.
@@ -309,13 +281,13 @@ class FilesystemMockingTestCase(ResourceUsingTestCase):
mock.patch.object(mod, f, trap_func))
# Handle subprocess calls
- func = getattr(util, 'subp')
+ func = getattr(subp, 'subp')
def nsubp(*_args, **_kwargs):
return ('', '')
self.patched_funcs.enter_context(
- mock.patch.object(util, 'subp', nsubp))
+ mock.patch.object(subp, 'subp', nsubp))
def null_func(*_args, **_kwargs):
return None
@@ -363,6 +335,7 @@ class FilesystemMockingTestCase(ResourceUsingTestCase):
root = self.tmp_dir()
self.patchUtils(root)
self.patchOS(root)
+ self.patchOpen(root)
return root
@contextmanager
@@ -396,7 +369,7 @@ class HttprettyTestCase(CiTestCase):
super(HttprettyTestCase, self).tearDown()
-class SchemaTestCaseMixin(unittest2.TestCase):
+class SchemaTestCaseMixin(unittest.TestCase):
def assertSchemaValid(self, cfg, msg="Valid Schema failed validation."):
"""Assert the config is valid per self.schema.
@@ -528,13 +501,4 @@ if not hasattr(mock.Mock, 'assert_not_called'):
raise AssertionError(msg)
mock.Mock.assert_not_called = __mock_assert_not_called
-
-# older unittest2.TestCase (centos6) have only the now-deprecated
-# assertRaisesRegexp. Simple assignment makes pylint complain, about
-# users of assertRaisesRegex so we use getattr to trick it.
-# https://github.com/PyCQA/pylint/issues/1946
-if not hasattr(unittest2.TestCase, 'assertRaisesRegex'):
- unittest2.TestCase.assertRaisesRegex = (
- getattr(unittest2.TestCase, 'assertRaisesRegexp'))
-
# vi: ts=4 expandtab