From 6ea311bea11fdb87c083b0b988505f9d7a5600e3 Mon Sep 17 00:00:00 2001 From: Andrew Melton Date: Wed, 7 Aug 2013 12:02:24 -0400 Subject: [PATCH] Fixing tests to work with python 2.6 --- tests/unit/__init__.py | 56 ++++++++++++++++++++++++++ tests/unit/test_datetime_to_decimal.py | 4 +- tests/unit/test_dbapi.py | 4 +- tests/unit/test_image_type.py | 5 +-- tests/unit/test_models.py | 4 +- tests/unit/test_notification.py | 9 ++--- tests/unit/test_reconciler.py | 6 +-- tests/unit/test_stacktach.py | 10 ++--- tests/unit/test_stacktach_db.py | 6 +-- tests/unit/test_stacktach_utils.py | 5 +-- tests/unit/test_stacky_server.py | 5 ++- tests/unit/test_verifier_db.py | 4 +- tests/unit/test_worker.py | 4 +- 13 files changed, 87 insertions(+), 35 deletions(-) diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py index dc3a522..9d9bbf0 100644 --- a/tests/unit/__init__.py +++ b/tests/unit/__init__.py @@ -19,7 +19,9 @@ # IN THE SOFTWARE. import os +import re import sys +import unittest def setup_sys_path(): @@ -45,3 +47,57 @@ setup_environment() from stacktach import stacklog stacklog.set_default_logger_location("/tmp/%s.log") + + +class _AssertRaisesContext(object): + """A context manager used to implement TestCase.assertRaises* methods.""" + + def __init__(self, expected, test_case, expected_regexp=None): + self.expected = expected + self.failureException = test_case.failureException + self.expected_regexp = expected_regexp + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_value, tb): + if exc_type is None: + try: + exc_name = self.expected.__name__ + except AttributeError: + exc_name = str(self.expected) + raise self.failureException( + "{0} not raised".format(exc_name)) + if not issubclass(exc_type, self.expected): + # let unexpected exceptions pass through + return False + self.exception = exc_value # store for later retrieval + if self.expected_regexp is None: + return True + + expected_regexp = self.expected_regexp + if isinstance(expected_regexp, basestring): + expected_regexp = re.compile(expected_regexp) + if not expected_regexp.search(str(exc_value)): + raise self.failureException('"%s" does not match "%s"' % + (expected_regexp.pattern, str(exc_value))) + return True + + +class StacktachBaseTestCase(unittest.TestCase): + + def assertIsNotNone(self, obj, msg=None): + self.assertTrue(obj is not None, msg) + + def assertIsNone(self, obj, msg=None): + self.assertTrue(obj is None, msg) + + def assertIsInstance(self, obj, cls, msg=None): + self.assertTrue(isinstance(obj, cls), msg) + + def assertRaises(self, excClass, callableObj=None, *args, **kwargs): + context = _AssertRaisesContext(excClass, self) + if callableObj is None: + return context + with context: + callableObj(*args, **kwargs) \ No newline at end of file diff --git a/tests/unit/test_datetime_to_decimal.py b/tests/unit/test_datetime_to_decimal.py index ac42886..f64ac11 100644 --- a/tests/unit/test_datetime_to_decimal.py +++ b/tests/unit/test_datetime_to_decimal.py @@ -20,11 +20,11 @@ import datetime import decimal -import unittest from stacktach import datetime_to_decimal +from tests.unit import StacktachBaseTestCase -class DatetimeToDecimalTestCase(unittest.TestCase): +class DatetimeToDecimalTestCase(StacktachBaseTestCase): def test_datetime_to_decimal(self): expected_decimal = decimal.Decimal('1356093296.123') diff --git a/tests/unit/test_dbapi.py b/tests/unit/test_dbapi.py index 9cc83d5..87e0de9 100644 --- a/tests/unit/test_dbapi.py +++ b/tests/unit/test_dbapi.py @@ -20,7 +20,6 @@ import datetime import json -import unittest from django.db.models import FieldDoesNotExist from django.db import transaction @@ -29,13 +28,14 @@ import mox from stacktach import dbapi from stacktach import models from stacktach import utils as stacktach_utils +from tests.unit import StacktachBaseTestCase import utils from utils import INSTANCE_ID_1 from utils import MESSAGE_ID_1 from utils import MESSAGE_ID_2 -class DBAPITestCase(unittest.TestCase): +class DBAPITestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() dne_exception = models.InstanceExists.DoesNotExist diff --git a/tests/unit/test_image_type.py b/tests/unit/test_image_type.py index 9758279..5077ea4 100644 --- a/tests/unit/test_image_type.py +++ b/tests/unit/test_image_type.py @@ -18,12 +18,11 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import unittest - from stacktach import image_type +from tests.unit import StacktachBaseTestCase -class ImageTypeTestCase(unittest.TestCase): +class ImageTypeTestCase(StacktachBaseTestCase): # Abstractions def _test_get_numeric_code(self, image, os_type, os_distro, expected, diff --git a/tests/unit/test_models.py b/tests/unit/test_models.py index 9501e68..e585a33 100644 --- a/tests/unit/test_models.py +++ b/tests/unit/test_models.py @@ -18,11 +18,11 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import unittest from stacktach.models import RawData, GlanceRawData, GenericRawData +from tests.unit import StacktachBaseTestCase -class ModelsTestCase(unittest.TestCase): +class ModelsTestCase(StacktachBaseTestCase): def test_get_name_for_rawdata(self): self.assertEquals(RawData.get_name(), 'RawData') diff --git a/tests/unit/test_notification.py b/tests/unit/test_notification.py index 926d118..d86a508 100644 --- a/tests/unit/test_notification.py +++ b/tests/unit/test_notification.py @@ -18,8 +18,6 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import unittest - import mox from stacktach import notification @@ -29,6 +27,7 @@ from stacktach.notification import Notification from stacktach.notification import NovaNotification from stacktach.notification import GlanceNotification from stacktach import db +from tests.unit import StacktachBaseTestCase from tests.unit.utils import REQUEST_ID_1 from tests.unit.utils import DECIMAL_DUMMY_TIME from tests.unit.utils import DUMMY_TIME @@ -38,7 +37,7 @@ from tests.unit.utils import INSTANCE_ID_1 from tests.unit.utils import MESSAGE_ID_1 -class NovaNotificationTestCase(unittest.TestCase): +class NovaNotificationTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() @@ -150,7 +149,7 @@ class NovaNotificationTestCase(unittest.TestCase): self.mox.VerifyAll() -class GlanceNotificationTestCase(unittest.TestCase): +class GlanceNotificationTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() @@ -423,7 +422,7 @@ class GlanceNotificationTestCase(unittest.TestCase): self.mox.VerifyAll() -class NotificationTestCase(unittest.TestCase): +class NotificationTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() diff --git a/tests/unit/test_reconciler.py b/tests/unit/test_reconciler.py index b02e19f..98585e7 100644 --- a/tests/unit/test_reconciler.py +++ b/tests/unit/test_reconciler.py @@ -19,7 +19,6 @@ # IN THE SOFTWARE. import datetime -import unittest import mox import requests @@ -30,6 +29,7 @@ from stacktach import utils as stackutils from stacktach.reconciler import exceptions from stacktach.reconciler import nova from stacktach.reconciler import utils as rec_utils +from tests.unit import StacktachBaseTestCase from tests.unit import utils from tests.unit.utils import INSTANCE_ID_1 from tests.unit.utils import TENANT_ID_1 @@ -45,7 +45,7 @@ DEFAULT_OS_VERSION = "1.1" DEFAULT_RAX_OPTIONS = "rax_ops" -class ReconcilerTestCase(unittest.TestCase): +class ReconcilerTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() self.client = self.mox.CreateMockAnything() @@ -415,7 +415,7 @@ json_bridge_config = { } -class NovaJSONBridgeClientTestCase(unittest.TestCase): +class NovaJSONBridgeClientTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() self.client = nova.JSONBridgeClient(json_bridge_config) diff --git a/tests/unit/test_stacktach.py b/tests/unit/test_stacktach.py index a57c08e..b770cd9 100644 --- a/tests/unit/test_stacktach.py +++ b/tests/unit/test_stacktach.py @@ -20,7 +20,6 @@ import datetime import json -import unittest import mox @@ -40,9 +39,10 @@ from utils import IMAGE_UUID_1 from stacktach import stacklog from stacktach import notification from stacktach import views +from tests.unit import StacktachBaseTestCase -class StacktachRawParsingTestCase(unittest.TestCase): +class StacktachRawParsingTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() views.STACKDB = self.mox.CreateMockAnything() @@ -101,7 +101,7 @@ class StacktachRawParsingTestCase(unittest.TestCase): self.mox.VerifyAll() -class StacktachLifecycleTestCase(unittest.TestCase): +class StacktachLifecycleTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() views.STACKDB = self.mox.CreateMockAnything() @@ -287,7 +287,7 @@ class StacktachLifecycleTestCase(unittest.TestCase): self.mox.VerifyAll() -class StacktachUsageParsingTestCase(unittest.TestCase): +class StacktachUsageParsingTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() views.STACKDB = self.mox.CreateMockAnything() @@ -830,7 +830,7 @@ class StacktachUsageParsingTestCase(unittest.TestCase): self.mox.VerifyAll() -class StacktachImageUsageParsingTestCase(unittest.TestCase): +class StacktachImageUsageParsingTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() views.STACKDB = self.mox.CreateMockAnything() diff --git a/tests/unit/test_stacktach_db.py b/tests/unit/test_stacktach_db.py index 142b961..b73efeb 100644 --- a/tests/unit/test_stacktach_db.py +++ b/tests/unit/test_stacktach_db.py @@ -18,17 +18,15 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import datetime -import unittest - import mox from stacktach import db from stacktach import stacklog from stacktach import models +from tests.unit import StacktachBaseTestCase -class StacktachDBTestCase(unittest.TestCase): +class StacktachDBTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() self.log = self.mox.CreateMockAnything() diff --git a/tests/unit/test_stacktach_utils.py b/tests/unit/test_stacktach_utils.py index 0c1377c..8e30a33 100644 --- a/tests/unit/test_stacktach_utils.py +++ b/tests/unit/test_stacktach_utils.py @@ -18,17 +18,16 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # IN THE SOFTWARE. -import unittest - import mox from stacktach import utils as stacktach_utils from utils import INSTANCE_ID_1 from utils import MESSAGE_ID_1 from utils import REQUEST_ID_1 +from tests.unit import StacktachBaseTestCase -class StacktachUtilsTestCase(unittest.TestCase): +class StacktachUtilsTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() diff --git a/tests/unit/test_stacky_server.py b/tests/unit/test_stacky_server.py index 512ec98..f07bff3 100644 --- a/tests/unit/test_stacky_server.py +++ b/tests/unit/test_stacky_server.py @@ -21,7 +21,6 @@ import datetime import decimal import json -import unittest import mox @@ -33,8 +32,10 @@ from utils import INSTANCE_ID_1 from utils import INSTANCE_ID_2 from utils import REQUEST_ID_1 +from tests.unit import StacktachBaseTestCase -class StackyServerTestCase(unittest.TestCase): + +class StackyServerTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() self.mox.StubOutWithMock(models, 'RawData', use_mock_anything=True) diff --git a/tests/unit/test_verifier_db.py b/tests/unit/test_verifier_db.py index 795528b..c2c19cf 100644 --- a/tests/unit/test_verifier_db.py +++ b/tests/unit/test_verifier_db.py @@ -22,7 +22,6 @@ import datetime import decimal import json import time -import unittest import uuid @@ -51,9 +50,10 @@ from verifier import AmbiguousResults from verifier import FieldMismatch from verifier import NotFound from verifier import VerificationException +from tests.unit import StacktachBaseTestCase -class VerifierTestCase(unittest.TestCase): +class VerifierTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox() self.mox.StubOutWithMock(models, 'RawData', use_mock_anything=True) diff --git a/tests/unit/test_worker.py b/tests/unit/test_worker.py index 52da770..9b32ea5 100644 --- a/tests/unit/test_worker.py +++ b/tests/unit/test_worker.py @@ -19,7 +19,6 @@ # IN THE SOFTWARE. import json -import unittest import kombu import mox @@ -27,9 +26,10 @@ import mox from stacktach import db from stacktach import views import worker.worker as worker +from tests.unit import StacktachBaseTestCase -class ConsumerTestCase(unittest.TestCase): +class ConsumerTestCase(StacktachBaseTestCase): def setUp(self): self.mox = mox.Mox()