~harlowja/cloud-init/cloud-init-net-refactor

« back to all changes in this revision

Viewing changes to tests/unittests/helpers.py

  • Committer: Joshua Harlow
  • Date: 2016-05-19 21:26:30 UTC
  • mfrom: (1215.1.18 cloud-init)
  • Revision ID: harlowja@gmail.com-20160519212630-b2l2fsshopo50fdk
RemergeĀ againstĀ head/master

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import tempfile
8
8
import unittest
9
9
 
 
10
import mock
10
11
import six
 
12
import unittest2
11
13
 
12
14
try:
13
 
    from unittest import mock
14
 
except ImportError:
15
 
    import mock
16
 
try:
17
15
    from contextlib import ExitStack
18
16
except ImportError:
19
17
    from contextlib2 import ExitStack
21
19
from cloudinit import helpers as ch
22
20
from cloudinit import util
23
21
 
 
22
# Used for skipping tests
 
23
SkipTest = unittest2.SkipTest
 
24
 
24
25
# Used for detecting different python versions
25
26
PY2 = False
26
27
PY26 = False
44
45
        if _PY_MINOR == 4 and _PY_MICRO < 3:
45
46
            FIX_HTTPRETTY = True
46
47
 
47
 
if PY26:
48
 
    # For now add these on, taken from python 2.7 + slightly adjusted.  Drop
49
 
    # all this once Python 2.6 is dropped as a minimum requirement.
50
 
    class TestCase(unittest.TestCase):
51
 
        def setUp(self):
52
 
            super(TestCase, self).setUp()
53
 
            self.__all_cleanups = ExitStack()
54
 
 
55
 
        def tearDown(self):
56
 
            self.__all_cleanups.close()
57
 
            unittest.TestCase.tearDown(self)
58
 
 
59
 
        def addCleanup(self, function, *args, **kws):
60
 
            self.__all_cleanups.callback(function, *args, **kws)
61
 
 
62
 
        def assertIs(self, expr1, expr2, msg=None):
63
 
            if expr1 is not expr2:
64
 
                standardMsg = '%r is not %r' % (expr1, expr2)
65
 
                self.fail(self._formatMessage(msg, standardMsg))
66
 
 
67
 
        def assertIn(self, member, container, msg=None):
68
 
            if member not in container:
69
 
                standardMsg = '%r not found in %r' % (member, container)
70
 
                self.fail(self._formatMessage(msg, standardMsg))
71
 
 
72
 
        def assertNotIn(self, member, container, msg=None):
73
 
            if member in container:
74
 
                standardMsg = '%r unexpectedly found in %r'
75
 
                standardMsg = standardMsg % (member, container)
76
 
                self.fail(self._formatMessage(msg, standardMsg))
77
 
 
78
 
        def assertIsNone(self, value, msg=None):
79
 
            if value is not None:
80
 
                standardMsg = '%r is not None'
81
 
                standardMsg = standardMsg % (value)
82
 
                self.fail(self._formatMessage(msg, standardMsg))
83
 
 
84
 
        def assertIsInstance(self, obj, cls, msg=None):
85
 
            """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
86
 
            default message."""
87
 
            if not isinstance(obj, cls):
88
 
                standardMsg = '%s is not an instance of %r' % (repr(obj), cls)
89
 
                self.fail(self._formatMessage(msg, standardMsg))
90
 
 
91
 
        def assertDictContainsSubset(self, expected, actual, msg=None):
92
 
            missing = []
93
 
            mismatched = []
94
 
            for k, v in expected.items():
95
 
                if k not in actual:
96
 
                    missing.append(k)
97
 
                elif actual[k] != v:
98
 
                    mismatched.append('%r, expected: %r, actual: %r'
99
 
                                      % (k, v, actual[k]))
100
 
 
101
 
            if len(missing) == 0 and len(mismatched) == 0:
102
 
                return
103
 
 
104
 
            standardMsg = ''
105
 
            if missing:
106
 
                standardMsg = 'Missing: %r' % ','.join(m for m in missing)
107
 
            if mismatched:
108
 
                if standardMsg:
109
 
                    standardMsg += '; '
110
 
                standardMsg += 'Mismatched values: %s' % ','.join(mismatched)
111
 
 
112
 
            self.fail(self._formatMessage(msg, standardMsg))
113
 
 
114
 
 
115
 
else:
116
 
    class TestCase(unittest.TestCase):
117
 
        pass
118
 
 
119
 
 
120
48
# Makes the old path start
121
49
# with new base instead of whatever
122
50
# it previously had
151
79
    return wrapper
152
80
 
153
81
 
 
82
class TestCase(unittest2.TestCase):
 
83
    pass
 
84
 
 
85
 
154
86
class ResourceUsingTestCase(TestCase):
155
87
    def setUp(self):
156
88
        super(ResourceUsingTestCase, self).setUp()