From 98b66c1ddd308bacb8ad8a78f71ea75353c3a01e Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Tue, 29 May 2012 08:51:12 +0100 Subject: [PATCH] Add a base class for tests This base class will allow us to DRY up the config setup and teardown. Change-Id: I49fff259a38657110a4117134b1a8bd728404269 --- glance/tests/functional/__init__.py | 3 +- glance/tests/functional/test_schema.py | 9 +- glance/tests/unit/base.py | 5 +- glance/tests/unit/test_auth.py | 6 +- glance/tests/unit/test_clients.py | 7 +- glance/tests/unit/test_config.py | 5 +- glance/tests/unit/test_context.py | 5 +- glance/tests/unit/test_http_store.py | 1 - glance/tests/unit/test_image_cache.py | 16 +- glance/tests/unit/test_migrations.py | 5 +- glance/tests/unit/test_misc.py | 4 +- glance/tests/unit/test_notifier.py | 27 +-- glance/tests/unit/test_s3_store.py | 1 - glance/tests/unit/test_schema.py | 6 +- glance/tests/unit/test_skip_examples.py | 4 +- glance/tests/unit/test_store_location.py | 2 - glance/tests/unit/test_swift_store.py | 1 - glance/tests/unit/test_utils.py | 7 +- glance/tests/unit/test_wsgi.py | 16 +- glance/tests/unit/v1/test_api.py | 5 +- .../unit/v2/test_image_access_resource.py | 111 +++++----- .../tests/unit/v2/test_image_data_resource.py | 55 ++--- .../tests/unit/v2/test_image_tags_resource.py | 29 +-- glance/tests/unit/v2/test_images_resource.py | 194 ++++++++++-------- glance/tests/unit/v2/test_root_resource.py | 9 +- glance/tests/unit/v2/test_schemas_resource.py | 16 +- glance/tests/utils.py | 10 + 27 files changed, 302 insertions(+), 257 deletions(-) diff --git a/glance/tests/functional/__init__.py b/glance/tests/functional/__init__.py index fd57db01..35bdc9ff 100644 --- a/glance/tests/functional/__init__.py +++ b/glance/tests/functional/__init__.py @@ -32,7 +32,6 @@ import shutil import signal import socket import time -import unittest import urlparse from sqlalchemy import create_engine @@ -415,7 +414,7 @@ glance.app_factory = glance.store.scrubber:Scrubber """ -class FunctionalTest(unittest.TestCase): +class FunctionalTest(test_utils.BaseTestCase): """ Base test class for any test that wants to test the actual diff --git a/glance/tests/functional/test_schema.py b/glance/tests/functional/test_schema.py index 9208fab3..35835665 100644 --- a/glance/tests/functional/test_schema.py +++ b/glance/tests/functional/test_schema.py @@ -13,16 +13,15 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - import glance.schema -import glance.tests.utils +from glance.tests import utils -class TestSchemaAPI(unittest.TestCase): +class TestSchemaAPI(utils.BaseTestCase): def setUp(self): - conf = glance.tests.utils.TestConfigOpts() + super(TestSchemaAPI, self).setUp() + conf = utils.TestConfigOpts() self.schema_api = glance.schema.API(conf) def test_load_image_schema(self): diff --git a/glance/tests/unit/base.py b/glance/tests/unit/base.py index 6276c4c2..8753275b 100644 --- a/glance/tests/unit/base.py +++ b/glance/tests/unit/base.py @@ -18,7 +18,6 @@ import json import os import shutil -import unittest import stubout @@ -28,14 +27,16 @@ from glance.tests import stubs from glance.tests import utils as test_utils -class StoreClearingUnitTest(unittest.TestCase): +class StoreClearingUnitTest(test_utils.BaseTestCase): def setUp(self): + super(StoreClearingUnitTest, self).setUp() # Ensure stores + locations cleared store.STORES = {} location.SCHEME_TO_CLS_MAP = {} def tearDown(self): + super(StoreClearingUnitTest, self).tearDown() # Ensure stores + locations cleared store.STORES = {} location.SCHEME_TO_CLS_MAP = {} diff --git a/glance/tests/unit/test_auth.py b/glance/tests/unit/test_auth.py index 3b442cc1..9d3cbd0d 100644 --- a/glance/tests/unit/test_auth.py +++ b/glance/tests/unit/test_auth.py @@ -17,11 +17,11 @@ import json import stubout -import unittest import webob from glance.common import auth from glance.common import exception +from glance.tests import utils class FakeResponse(object): @@ -104,13 +104,15 @@ class V2Token(object): } -class TestKeystoneAuthPlugin(unittest.TestCase): +class TestKeystoneAuthPlugin(utils.BaseTestCase): """Test that the Keystone auth plugin works properly""" def setUp(self): + super(TestKeystoneAuthPlugin, self).setUp() self.stubs = stubout.StubOutForTesting() def tearDown(self): + super(TestKeystoneAuthPlugin, self).tearDown() self.stubs.UnsetAll() def test_required_creds(self): diff --git a/glance/tests/unit/test_clients.py b/glance/tests/unit/test_clients.py index 2c984a36..cf154b18 100644 --- a/glance/tests/unit/test_clients.py +++ b/glance/tests/unit/test_clients.py @@ -18,7 +18,6 @@ import datetime import os import tempfile -import unittest from glance import client from glance.common import client as base_client @@ -39,7 +38,7 @@ UUID1 = _gen_uuid() UUID2 = _gen_uuid() -class TestBadClients(unittest.TestCase): +class TestBadClients(test_utils.BaseTestCase): """Test exceptions raised for bad clients""" @@ -2061,8 +2060,10 @@ class TestClient(base.IsolatedUnitTest): self.client.delete_member, UUID2, 'pattieblack') -class TestConfigureClientFromURL(unittest.TestCase): +class TestConfigureClientFromURL(test_utils.BaseTestCase): + def setUp(self): + super(TestConfigureClientFromURL, self).setUp() self.client = client.Client("0.0.0.0") def assertConfiguration(self, url, host, port, use_ssl, doc_root): diff --git a/glance/tests/unit/test_config.py b/glance/tests/unit/test_config.py index 0044e1b8..228c2f23 100644 --- a/glance/tests/unit/test_config.py +++ b/glance/tests/unit/test_config.py @@ -17,7 +17,6 @@ import os.path import shutil -import unittest import stubout @@ -27,12 +26,14 @@ from glance.image_cache import pruner from glance.tests import utils as test_utils -class TestPasteApp(unittest.TestCase): +class TestPasteApp(test_utils.BaseTestCase): def setUp(self): + super(TestPasteApp, self).setUp() self.stubs = stubout.StubOutForTesting() def tearDown(self): + super(TestPasteApp, self).tearDown() self.stubs.UnsetAll() def _do_test_load_paste_app(self, diff --git a/glance/tests/unit/test_context.py b/glance/tests/unit/test_context.py index 6945ea27..f99c20af 100644 --- a/glance/tests/unit/test_context.py +++ b/glance/tests/unit/test_context.py @@ -15,9 +15,8 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - from glance.common import context +from glance.tests import utils def _fake_image(owner, is_public): @@ -32,7 +31,7 @@ def _fake_membership(can_share=False): return {'can_share': can_share} -class TestContext(unittest.TestCase): +class TestContext(utils.BaseTestCase): def do_visible(self, exp_res, img_owner, img_public, **kwargs): """ Perform a context visibility test. Creates a (fake) image diff --git a/glance/tests/unit/test_http_store.py b/glance/tests/unit/test_http_store.py index a68705d6..3908ff03 100644 --- a/glance/tests/unit/test_http_store.py +++ b/glance/tests/unit/test_http_store.py @@ -16,7 +16,6 @@ # under the License. import StringIO -import unittest import stubout diff --git a/glance/tests/unit/test_image_cache.py b/glance/tests/unit/test_image_cache.py index 9661bb64..8ed1bb6b 100644 --- a/glance/tests/unit/test_image_cache.py +++ b/glance/tests/unit/test_image_cache.py @@ -20,7 +20,6 @@ import os import random import shutil import StringIO -import unittest import stubout @@ -224,7 +223,7 @@ class ImageCacheTestCase(object): ['0', '1', '2']) -class TestImageCacheXattr(unittest.TestCase, +class TestImageCacheXattr(test_utils.BaseTestCase, ImageCacheTestCase): """Tests image caching when xattr is used in cache""" @@ -235,6 +234,8 @@ class TestImageCacheXattr(unittest.TestCase, are working (python-xattr installed and xattr support on the filesystem) """ + super(TestImageCacheXattr, self).setUp() + if getattr(self, 'disable', False): return @@ -268,11 +269,12 @@ class TestImageCacheXattr(unittest.TestCase, return def tearDown(self): + super(TestImageCacheXattr, self).tearDown() if os.path.exists(self.cache_dir): shutil.rmtree(self.cache_dir) -class TestImageCacheSqlite(unittest.TestCase, +class TestImageCacheSqlite(test_utils.BaseTestCase, ImageCacheTestCase): """Tests image caching when SQLite is used in cache""" @@ -282,6 +284,8 @@ class TestImageCacheSqlite(unittest.TestCase, Test to see if the pre-requisites for the image cache are working (python-sqlite3 installed) """ + super(TestImageCacheSqlite, self).setUp() + if getattr(self, 'disable', False): return @@ -307,13 +311,16 @@ class TestImageCacheSqlite(unittest.TestCase, self.cache = image_cache.ImageCache(self.conf) def tearDown(self): + super(TestImageCacheSqlite, self).tearDown() if os.path.exists(self.cache_dir): shutil.rmtree(self.cache_dir) -class TestImageCacheNoDep(unittest.TestCase): +class TestImageCacheNoDep(test_utils.BaseTestCase): def setUp(self): + super(TestImageCacheNoDep, self).setUp() + self.driver = None def init_driver(self2): @@ -323,6 +330,7 @@ class TestImageCacheNoDep(unittest.TestCase): self.stubs.Set(image_cache.ImageCache, 'init_driver', init_driver) def tearDown(self): + super(TestImageCacheNoDep, self).tearDown() self.stubs.UnsetAll() def test_get_caching_iter_when_write_fails(self): diff --git a/glance/tests/unit/test_migrations.py b/glance/tests/unit/test_migrations.py index ab93ece6..876c66cd 100644 --- a/glance/tests/unit/test_migrations.py +++ b/glance/tests/unit/test_migrations.py @@ -27,7 +27,6 @@ if possible. import ConfigParser import datetime import os -import unittest import urlparse from migrate.versioning.repository import Repository @@ -41,7 +40,7 @@ from glance.registry.db import models from glance.tests import utils -class TestMigrations(unittest.TestCase): +class TestMigrations(utils.BaseTestCase): """Test sqlalchemy-migrate migrations""" @@ -58,6 +57,7 @@ class TestMigrations(unittest.TestCase): super(TestMigrations, self).__init__(*args, **kwargs) def setUp(self): + super(TestMigrations, self).setUp() # Load test databases from the config file. Only do this # once. No need to re-run this on each test... if not TestMigrations.TEST_DATABASES: @@ -83,6 +83,7 @@ class TestMigrations(unittest.TestCase): self._reset_databases() def tearDown(self): + super(TestMigrations, self).tearDown() # We destroy the test data store between each test case, # and recreate it, which ensures that we have no side-effects # from the tests diff --git a/glance/tests/unit/test_misc.py b/glance/tests/unit/test_misc.py index 7080d900..3364a079 100644 --- a/glance/tests/unit/test_misc.py +++ b/glance/tests/unit/test_misc.py @@ -19,14 +19,14 @@ import os import commands import datetime import re -import unittest from glance.common import crypt from glance.common import exception from glance.common import utils +from glance.tests import utils as test_utils -class UtilsTestCase(unittest.TestCase): +class UtilsTestCase(test_utils.BaseTestCase): def test_bool_from_string(self): true_values = ['True', True, 'true', 'TRUE', '1', 1, 'on', diff --git a/glance/tests/unit/test_notifier.py b/glance/tests/unit/test_notifier.py index b952ee36..dee13615 100644 --- a/glance/tests/unit/test_notifier.py +++ b/glance/tests/unit/test_notifier.py @@ -17,7 +17,6 @@ import json import logging -import unittest import kombu.entity import mox @@ -35,7 +34,7 @@ import glance.notifier.notify_kombu from glance.tests import utils -class TestInvalidNotifier(unittest.TestCase): +class TestInvalidNotifier(utils.BaseTestCase): """Test that notifications are generated appropriately""" def test_cannot_create(self): @@ -45,10 +44,11 @@ class TestInvalidNotifier(unittest.TestCase): conf) -class TestLoggingNotifier(unittest.TestCase): +class TestLoggingNotifier(utils.BaseTestCase): """Test the logging notifier is selected and works properly.""" def setUp(self): + super(TestLoggingNotifier, self).setUp() conf = utils.TestConfigOpts({"notifier_strategy": "logging"}) self.called = False self.logger = logging.getLogger("glance.notifier.logging_notifier") @@ -76,10 +76,11 @@ class TestLoggingNotifier(unittest.TestCase): self.fail("Did not call logging library correctly.") -class TestNoopNotifier(unittest.TestCase): +class TestNoopNotifier(utils.BaseTestCase): """Test that the noop notifier works...and does nothing?""" def setUp(self): + super(TestNoopNotifier, self).setUp() conf = utils.TestConfigOpts({"notifier_strategy": "noop"}) self.notifier = notifier.Notifier(conf) @@ -93,10 +94,12 @@ class TestNoopNotifier(unittest.TestCase): self.notifier.error("test_event", "test_message") -class TestRabbitNotifier(unittest.TestCase): +class TestRabbitNotifier(utils.BaseTestCase): """Test AMQP/Rabbit notifier works.""" def setUp(self): + super(TestRabbitNotifier, self).setUp() + def _fake_connect(rabbit_self): rabbit_self.connection_errors = () rabbit_self.connection = 'fake_connection' @@ -274,10 +277,12 @@ class TestRabbitNotifier(unittest.TestCase): self.assertEquals(info['conn_called'], 2) -class TestQpidNotifier(unittest.TestCase): +class TestQpidNotifier(utils.BaseTestCase): """Test Qpid notifier.""" def setUp(self): + super(TestQpidNotifier, self).setUp() + if not qpid: return @@ -300,9 +305,9 @@ class TestQpidNotifier(unittest.TestCase): self.notify_qpid = common_utils.import_object( "glance.notifier.notify_qpid") - super(TestQpidNotifier, self).setUp() - def tearDown(self): + super(TestQpidNotifier, self).tearDown() + if not qpid: return @@ -313,8 +318,6 @@ class TestQpidNotifier(unittest.TestCase): self.mocker.ResetAll() - super(TestQpidNotifier, self).tearDown() - def _test_notify(self, priority): test_msg = {'a': 'b'} @@ -359,10 +362,11 @@ class TestQpidNotifier(unittest.TestCase): self._test_notify('error') -class TestRabbitContentType(unittest.TestCase): +class TestRabbitContentType(utils.BaseTestCase): """Test AMQP/Rabbit notifier works.""" def setUp(self): + super(TestRabbitContentType, self).setUp() self.stubs = stubout.StubOutForTesting() def _fake_connect(rabbit_self): @@ -383,7 +387,6 @@ class TestRabbitContentType(unittest.TestCase): "rabbit_notification_topic": "fake_topic"}) self.notifier = notifier.Notifier(self.conf) - super(TestRabbitContentType, self).setUp() def _fake_exchange(self): class Dummy(object): diff --git a/glance/tests/unit/test_s3_store.py b/glance/tests/unit/test_s3_store.py index 29f398ab..026092ad 100644 --- a/glance/tests/unit/test_s3_store.py +++ b/glance/tests/unit/test_s3_store.py @@ -19,7 +19,6 @@ import StringIO import hashlib -import unittest import stubout import boto.s3.connection diff --git a/glance/tests/unit/test_schema.py b/glance/tests/unit/test_schema.py index ba84dc1f..4dd96acf 100644 --- a/glance/tests/unit/test_schema.py +++ b/glance/tests/unit/test_schema.py @@ -13,8 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - from glance.common import exception import glance.schema from glance.tests import utils as test_utils @@ -49,8 +47,10 @@ FAKE_BASE_PROPERTIES = { } -class TestSchemaAPI(unittest.TestCase): +class TestSchemaAPI(test_utils.BaseTestCase): + def setUp(self): + super(TestSchemaAPI, self).setUp() self.conf = test_utils.TestConfigOpts() self.schema_api = glance.schema.API(self.conf, FAKE_BASE_PROPERTIES) diff --git a/glance/tests/unit/test_skip_examples.py b/glance/tests/unit/test_skip_examples.py index 21d55990..fa2c60ca 100644 --- a/glance/tests/unit/test_skip_examples.py +++ b/glance/tests/unit/test_skip_examples.py @@ -15,12 +15,10 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - from glance.tests import utils -class TestSkipExamples(unittest.TestCase): +class TestSkipExamples(utils.BaseTestCase): test_counter = 0 @utils.skip_test("Example usage of @utils.skip_test()") diff --git a/glance/tests/unit/test_store_location.py b/glance/tests/unit/test_store_location.py index 5e92ecdd..20138947 100644 --- a/glance/tests/unit/test_store_location.py +++ b/glance/tests/unit/test_store_location.py @@ -15,8 +15,6 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - from glance.common import exception import glance.store import glance.store.location as location diff --git a/glance/tests/unit/test_swift_store.py b/glance/tests/unit/test_swift_store.py index 49c72a50..a274c3c2 100644 --- a/glance/tests/unit/test_swift_store.py +++ b/glance/tests/unit/test_swift_store.py @@ -21,7 +21,6 @@ import StringIO import hashlib import httplib import tempfile -import unittest import urllib import stubout diff --git a/glance/tests/unit/test_utils.py b/glance/tests/unit/test_utils.py index 85131116..d2f7240c 100644 --- a/glance/tests/unit/test_utils.py +++ b/glance/tests/unit/test_utils.py @@ -15,14 +15,13 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - import iso8601 from glance.common import utils +from glance.tests import utils as test_utils -class TestUtils(unittest.TestCase): +class TestUtils(test_utils.BaseTestCase): """Test routines in glance.utils""" def test_generate_uuid_format(self): @@ -49,7 +48,7 @@ class TestUtils(unittest.TestCase): self.assertFalse(utils.is_uuid_like(fixture)) -class TestIso8601Time(unittest.TestCase): +class TestIso8601Time(test_utils.BaseTestCase): def _instaneous(self, timestamp, yr, mon, day, hr, min, sec, micro): self.assertEquals(timestamp.year, yr) diff --git a/glance/tests/unit/test_wsgi.py b/glance/tests/unit/test_wsgi.py index 96aa25c0..65891142 100644 --- a/glance/tests/unit/test_wsgi.py +++ b/glance/tests/unit/test_wsgi.py @@ -15,15 +15,16 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest import webob from glance.common import wsgi from glance.common import utils from glance.common import exception +from glance.tests import utils as test_utils -class RequestTest(unittest.TestCase): +class RequestTest(test_utils.BaseTestCase): + def test_content_type_missing(self): request = wsgi.Request.blank('/tests/123') self.assertRaises(exception.InvalidContentType, @@ -73,7 +74,8 @@ class RequestTest(unittest.TestCase): self.assertEqual(result, "application/json") -class ResourceTest(unittest.TestCase): +class ResourceTest(test_utils.BaseTestCase): + def test_get_action_args(self): env = { 'wsgiorg.routing_args': [ @@ -122,7 +124,8 @@ class ResourceTest(unittest.TestCase): 'index', 'on', pants='off') -class JSONResponseSerializerTest(unittest.TestCase): +class JSONResponseSerializerTest(test_utils.BaseTestCase): + def test_to_json(self): fixture = {"key": "value"} expected = '{"key": "value"}' @@ -141,7 +144,8 @@ class JSONResponseSerializerTest(unittest.TestCase): self.assertEqual(response.body, '{"key": "value"}') -class JSONRequestDeserializerTest(unittest.TestCase): +class JSONRequestDeserializerTest(test_utils.BaseTestCase): + def test_has_body_no_content_length(self): request = wsgi.Request.blank('/') request.method = 'POST' @@ -188,7 +192,7 @@ class JSONRequestDeserializerTest(unittest.TestCase): self.assertEqual(actual, expected) -class TestHelpers(unittest.TestCase): +class TestHelpers(test_utils.BaseTestCase): def test_headers_are_unicode(self): """ diff --git a/glance/tests/unit/v1/test_api.py b/glance/tests/unit/v1/test_api.py index 33b4e800..c768de0f 100644 --- a/glance/tests/unit/v1/test_api.py +++ b/glance/tests/unit/v1/test_api.py @@ -19,7 +19,6 @@ import datetime import hashlib import httplib import json -import unittest import stubout import webob @@ -42,10 +41,11 @@ UUID1 = _gen_uuid() UUID2 = _gen_uuid() -class TestRegistryDb(unittest.TestCase): +class TestRegistryDb(test_utils.BaseTestCase): def setUp(self): """Establish a clean test environment""" + super(TestRegistryDb, self).setUp() self.stubs = stubout.StubOutForTesting() self.orig_engine = db_api._ENGINE @@ -85,6 +85,7 @@ class TestRegistryDb(unittest.TestCase): def tearDown(self): """Clear the test environment""" + super(TestRegistryDb, self).setUp() db_api._ENGINE = self.orig_engine self.stubs.UnsetAll() diff --git a/glance/tests/unit/v2/test_image_access_resource.py b/glance/tests/unit/v2/test_image_access_resource.py index 73b012ae..ac2bfd51 100644 --- a/glance/tests/unit/v2/test_image_access_resource.py +++ b/glance/tests/unit/v2/test_image_access_resource.py @@ -14,7 +14,6 @@ # under the License. import json -import unittest import webob @@ -22,30 +21,30 @@ from glance.api.v2 import image_access from glance.common import exception from glance.common import utils import glance.schema -import glance.tests.unit.utils as test_utils -import glance.tests.utils +import glance.tests.unit.utils as unit_test_utils +import glance.tests.utils as test_utils -class TestImageAccessController(unittest.TestCase): +class TestImageAccessController(test_utils.BaseTestCase): def setUp(self): super(TestImageAccessController, self).setUp() - self.db = test_utils.FakeDB() + self.db = unit_test_utils.FakeDB() self.controller = image_access.Controller({}, self.db) def test_index(self): - req = test_utils.FakeRequest() - output = self.controller.index(req, test_utils.UUID1) + req = unit_test_utils.FakeRequest() + output = self.controller.index(req, unit_test_utils.UUID1) expected = [ { - 'image_id': test_utils.UUID1, - 'member': test_utils.TENANT1, + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT1, 'can_share': True, 'deleted': False, }, { - 'image_id': test_utils.UUID1, - 'member': test_utils.TENANT2, + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT2, 'can_share': False, 'deleted': False, }, @@ -53,21 +52,21 @@ class TestImageAccessController(unittest.TestCase): self.assertEqual(expected, output) def test_index_zero_records(self): - req = test_utils.FakeRequest() - output = self.controller.index(req, test_utils.UUID2) + req = unit_test_utils.FakeRequest() + output = self.controller.index(req, unit_test_utils.UUID2) expected = [] self.assertEqual(expected, output) def test_index_nonexistant_image(self): - req = test_utils.FakeRequest() + req = unit_test_utils.FakeRequest() image_id = utils.generate_uuid() self.assertRaises(exception.NotFound, self.controller.index, req, image_id) def test_show(self): - req = test_utils.FakeRequest() - image_id = test_utils.UUID1 - tenant_id = test_utils.TENANT1 + req = unit_test_utils.FakeRequest() + image_id = unit_test_utils.UUID1 + tenant_id = unit_test_utils.TENANT1 output = self.controller.show(req, image_id, tenant_id) expected = { 'image_id': image_id, @@ -78,15 +77,15 @@ class TestImageAccessController(unittest.TestCase): self.assertEqual(expected, output) def test_show_nonexistant_image(self): - req = test_utils.FakeRequest() + req = unit_test_utils.FakeRequest() image_id = utils.generate_uuid() - tenant_id = test_utils.TENANT1 + tenant_id = unit_test_utils.TENANT1 self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, req, image_id, tenant_id) def test_show_nonexistant_tenant(self): - req = test_utils.FakeRequest() - image_id = test_utils.UUID1 + req = unit_test_utils.FakeRequest() + image_id = unit_test_utils.UUID1 tenant_id = utils.generate_uuid() self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, req, image_id, tenant_id) @@ -98,42 +97,46 @@ class TestImageAccessController(unittest.TestCase): 'can_share': True, } expected = { - 'image_id': test_utils.UUID1, + 'image_id': unit_test_utils.UUID1, 'member': member, 'can_share': True, 'deleted': False, } - req = test_utils.FakeRequest() - output = self.controller.create(req, test_utils.UUID1, fixture) + req = unit_test_utils.FakeRequest() + output = self.controller.create(req, unit_test_utils.UUID1, fixture) self.assertEqual(expected, output) -class TestImageAccessDeserializer(unittest.TestCase): +class TestImageAccessDeserializer(test_utils.BaseTestCase): + def setUp(self): - conf = glance.tests.utils.TestConfigOpts() + super(TestImageAccessDeserializer, self).setUp() + conf = test_utils.TestConfigOpts() schema_api = glance.schema.API(conf) self.deserializer = image_access.RequestDeserializer({}, schema_api) def test_create(self): fixture = { - 'tenant_id': test_utils.TENANT1, + 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, } expected = { 'access_record': { - 'member': test_utils.TENANT1, + 'member': unit_test_utils.TENANT1, 'can_share': False, }, } - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps(fixture) output = self.deserializer.create(request) self.assertEqual(expected, output) -class TestImageAccessDeserializerWithExtendedSchema(unittest.TestCase): +class TestImageAccessDeserializerWithExtendedSchema(test_utils.BaseTestCase): + def setUp(self): - conf = glance.tests.utils.TestConfigOpts() + super(TestImageAccessDeserializerWithExtendedSchema, self).setUp() + conf = test_utils.TestConfigOpts() schema_api = glance.schema.API(conf) props = { 'color': { @@ -147,48 +150,48 @@ class TestImageAccessDeserializerWithExtendedSchema(unittest.TestCase): def test_create(self): fixture = { - 'tenant_id': test_utils.TENANT1, + 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, 'color': 'blue', } expected = { 'access_record': { - 'member': test_utils.TENANT1, + 'member': unit_test_utils.TENANT1, 'can_share': False, 'color': 'blue', }, } - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps(fixture) output = self.deserializer.create(request) self.assertEqual(expected, output) def test_create_bad_data(self): fixture = { - 'tenant_id': test_utils.TENANT1, + 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, 'color': 'purple', } - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps(fixture) self.assertRaises(exception.InvalidObject, self.deserializer.create, request) -class TestImageAccessSerializer(unittest.TestCase): +class TestImageAccessSerializer(test_utils.BaseTestCase): serializer = image_access.ResponseSerializer() def test_show(self): fixture = { - 'image_id': test_utils.UUID1, - 'member': test_utils.TENANT1, + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT1, 'can_share': False, } self_href = ('/v2/images/%s/access/%s' % - (test_utils.UUID1, test_utils.TENANT1)) + (unit_test_utils.UUID1, unit_test_utils.TENANT1)) expected = { 'access_record': { - 'tenant_id': test_utils.TENANT1, + 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, 'links': [ {'rel': 'self', 'href': self_href}, @@ -203,26 +206,27 @@ class TestImageAccessSerializer(unittest.TestCase): def test_index(self): fixtures = [ { - 'image_id': test_utils.UUID1, - 'member': test_utils.TENANT1, + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT1, 'can_share': False, }, { - 'image_id': test_utils.UUID1, - 'member': test_utils.TENANT2, + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT2, 'can_share': True, }, ] expected = { 'access_records': [ { - 'tenant_id': test_utils.TENANT1, + 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, 'links': [ { 'rel': 'self', 'href': ('/v2/images/%s/access/%s' % - (test_utils.UUID1, test_utils.TENANT1)) + (unit_test_utils.UUID1, + unit_test_utils.TENANT1)) }, { 'rel': 'describedby', @@ -231,13 +235,14 @@ class TestImageAccessSerializer(unittest.TestCase): ], }, { - 'tenant_id': test_utils.TENANT2, + 'tenant_id': unit_test_utils.TENANT2, 'can_share': True, 'links': [ { 'rel': 'self', 'href': ('/v2/images/%s/access/%s' % - (test_utils.UUID1, test_utils.TENANT2)) + (unit_test_utils.UUID1, + unit_test_utils.TENANT2)) }, { 'rel': 'describedby', @@ -254,15 +259,15 @@ class TestImageAccessSerializer(unittest.TestCase): def test_create(self): fixture = { - 'image_id': test_utils.UUID1, - 'member': test_utils.TENANT1, + 'image_id': unit_test_utils.UUID1, + 'member': unit_test_utils.TENANT1, 'can_share': False, } self_href = ('/v2/images/%s/access/%s' % - (test_utils.UUID1, test_utils.TENANT1)) + (unit_test_utils.UUID1, unit_test_utils.TENANT1)) expected = { 'access': { - 'tenant_id': test_utils.TENANT1, + 'tenant_id': unit_test_utils.TENANT1, 'can_share': False, 'links': [ {'rel': 'self', 'href': self_href}, diff --git a/glance/tests/unit/v2/test_image_data_resource.py b/glance/tests/unit/v2/test_image_data_resource.py index 7f1ddb5b..12d79950 100644 --- a/glance/tests/unit/v2/test_image_data_resource.py +++ b/glance/tests/unit/v2/test_image_data_resource.py @@ -14,15 +14,14 @@ # under the License. import StringIO -import unittest import webob import glance.api.v2.image_data from glance.common import utils -import glance.tests.unit.utils as test_utils +import glance.tests.unit.utils as unit_test_utils from glance.tests.unit import base -import glance.tests.utils +import glance.tests.utils as test_utils class TestImagesController(base.StoreClearingUnitTest): @@ -34,56 +33,58 @@ class TestImagesController(base.StoreClearingUnitTest): 'debug': True, }) self.controller = glance.api.v2.image_data.ImageDataController(conf, - db_api=test_utils.FakeDB(), - store_api=test_utils.FakeStoreAPI()) + db_api=unit_test_utils.FakeDB(), + store_api=unit_test_utils.FakeStoreAPI()) def test_download(self): - request = test_utils.FakeRequest() - output = self.controller.download(request, test_utils.UUID1) + request = unit_test_utils.FakeRequest() + output = self.controller.download(request, unit_test_utils.UUID1) expected = {'data': 'XXX', 'size': 3} self.assertEqual(expected, output) def test_download_no_data(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() self.assertRaises(webob.exc.HTTPNotFound, self.controller.download, - request, test_utils.UUID2) + request, unit_test_utils.UUID2) def test_download_non_existant_image(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() self.assertRaises(webob.exc.HTTPNotFound, self.controller.download, request, utils.generate_uuid()) def test_upload_download(self): - request = test_utils.FakeRequest() - self.controller.upload(request, test_utils.UUID2, 'YYYY', 4) - output = self.controller.download(request, test_utils.UUID2) + request = unit_test_utils.FakeRequest() + self.controller.upload(request, unit_test_utils.UUID2, 'YYYY', 4) + output = self.controller.download(request, unit_test_utils.UUID2) expected = {'data': 'YYYY', 'size': 4} self.assertEqual(expected, output) def test_upload_non_existant_image(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() self.assertRaises(webob.exc.HTTPNotFound, self.controller.upload, request, utils.generate_uuid(), 'YYYY', 4) def test_upload_data_exists(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() self.assertRaises(webob.exc.HTTPConflict, self.controller.upload, - request, test_utils.UUID1, 'YYYY', 4) + request, unit_test_utils.UUID1, 'YYYY', 4) def test_upload_download_no_size(self): - request = test_utils.FakeRequest() - self.controller.upload(request, test_utils.UUID2, 'YYYY', None) - output = self.controller.download(request, test_utils.UUID2) + request = unit_test_utils.FakeRequest() + self.controller.upload(request, unit_test_utils.UUID2, 'YYYY', None) + output = self.controller.download(request, unit_test_utils.UUID2) expected = {'data': 'YYYY', 'size': 4} self.assertEqual(expected, output) -class TestImageDataDeserializer(unittest.TestCase): +class TestImageDataDeserializer(test_utils.BaseTestCase): + def setUp(self): + super(TestImageDataDeserializer, self).setUp() self.deserializer = glance.api.v2.image_data.RequestDeserializer() def test_upload(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.headers['Content-Type'] = 'application/octet-stream' request.body = 'YYY' request.headers['Content-Length'] = 3 @@ -94,7 +95,7 @@ class TestImageDataDeserializer(unittest.TestCase): self.assertEqual(expected, output) def test_upload_chunked(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.headers['Content-Type'] = 'application/octet-stream' # If we use body_file, webob assumes we want to do a chunked upload, # ignoring the Content-Length header @@ -106,7 +107,7 @@ class TestImageDataDeserializer(unittest.TestCase): self.assertEqual(expected, output) def test_upload_chunked_with_content_length(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.headers['Content-Type'] = 'application/octet-stream' request.body_file = StringIO.StringIO('YYY') # The deserializer shouldn't care if the Content-Length is @@ -119,7 +120,7 @@ class TestImageDataDeserializer(unittest.TestCase): self.assertEqual(expected, output) def test_upload_with_incorrect_content_length(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.headers['Content-Type'] = 'application/octet-stream' # The deserializer shouldn't care if the Content-Length and # actual request body length differ. That job is left up @@ -133,15 +134,17 @@ class TestImageDataDeserializer(unittest.TestCase): self.assertEqual(expected, output) def test_upload_wrong_content_type(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.headers['Content-Type'] = 'application/json' request.body = 'YYYYY' self.assertRaises(webob.exc.HTTPUnsupportedMediaType, self.deserializer.upload, request) -class TestImageDataSerializer(unittest.TestCase): +class TestImageDataSerializer(test_utils.BaseTestCase): + def setUp(self): + super(TestImageDataSerializer, self).setUp() self.serializer = glance.api.v2.image_data.ResponseSerializer() def test_download(self): diff --git a/glance/tests/unit/v2/test_image_tags_resource.py b/glance/tests/unit/v2/test_image_tags_resource.py index 4a9c3300..485811c0 100644 --- a/glance/tests/unit/v2/test_image_tags_resource.py +++ b/glance/tests/unit/v2/test_image_tags_resource.py @@ -14,43 +14,46 @@ # under the License. import json -import unittest import webob import glance.api.v2.image_tags -import glance.tests.unit.utils as test_utils +import glance.tests.unit.utils as unit_test_utils +import glance.tests.utils as test_utils -class TestImageTagsController(unittest.TestCase): +class TestImageTagsController(test_utils.BaseTestCase): + def setUp(self): super(TestImageTagsController, self).setUp() - self.db = test_utils.FakeDB() + self.db = unit_test_utils.FakeDB() conf = {} self.controller = glance.api.v2.image_tags.Controller(conf, self.db) def test_list_tags(self): - request = test_utils.FakeRequest() - tags = self.controller.index(request, test_utils.UUID1) + request = unit_test_utils.FakeRequest() + tags = self.controller.index(request, unit_test_utils.UUID1) expected = ['ping', 'pong'] self.assertEqual(expected, tags) def test_create_tag(self): - request = test_utils.FakeRequest() - self.controller.update(request, test_utils.UUID1, 'dink') + request = unit_test_utils.FakeRequest() + self.controller.update(request, unit_test_utils.UUID1, 'dink') def test_delete_tag(self): - request = test_utils.FakeRequest() - self.controller.delete(request, test_utils.UUID1, 'ping') + request = unit_test_utils.FakeRequest() + self.controller.delete(request, unit_test_utils.UUID1, 'ping') def test_delete_tag_not_found(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete, - request, test_utils.UUID1, 'what') + request, unit_test_utils.UUID1, 'what') -class TestImagesSerializer(unittest.TestCase): +class TestImagesSerializer(test_utils.BaseTestCase): + def setUp(self): + super(TestImagesSerializer, self).setUp() self.serializer = glance.api.v2.image_tags.ResponseSerializer() def test_list_tags(self): diff --git a/glance/tests/unit/v2/test_images_resource.py b/glance/tests/unit/v2/test_images_resource.py index c5a8eacf..f05c6535 100644 --- a/glance/tests/unit/v2/test_images_resource.py +++ b/glance/tests/unit/v2/test_images_resource.py @@ -15,7 +15,6 @@ import datetime import json -import unittest import webob @@ -23,42 +22,43 @@ import glance.api.v2.images from glance.common import exception from glance.common import utils import glance.schema -import glance.tests.unit.utils as test_utils -import glance.tests.utils +import glance.tests.unit.utils as unit_test_utils +import glance.tests.utils as test_utils DATETIME = datetime.datetime(2012, 5, 16, 15, 27, 36, 325355) ISOTIME = '2012-05-16T15:27:36Z' -class TestImagesController(unittest.TestCase): +class TestImagesController(test_utils.BaseTestCase): + def setUp(self): super(TestImagesController, self).setUp() - self.db = test_utils.FakeDB() + self.db = unit_test_utils.FakeDB() self.controller = glance.api.v2.images.ImagesController({}, self.db) def test_index(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() output = self.controller.index(request) self.assertEqual(2, len(output)) - self.assertEqual(output[0]['id'], test_utils.UUID1) - self.assertEqual(output[1]['id'], test_utils.UUID2) + self.assertEqual(output[0]['id'], unit_test_utils.UUID1) + self.assertEqual(output[1]['id'], unit_test_utils.UUID2) def test_index_zero_images(self): self.db.reset() - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() output = self.controller.index(request) self.assertEqual([], output) def test_show(self): - request = test_utils.FakeRequest() - output = self.controller.show(request, image_id=test_utils.UUID2) + request = unit_test_utils.FakeRequest() + output = self.controller.show(request, image_id=unit_test_utils.UUID2) for key in ['created_at', 'updated_at']: output.pop(key) expected = { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-name', - 'owner': test_utils.TENANT1, + 'owner': unit_test_utils.TENANT1, 'location': None, 'status': 'queued', 'is_public': False, @@ -70,17 +70,17 @@ class TestImagesController(unittest.TestCase): def test_show_non_existant(self): self.assertRaises(webob.exc.HTTPNotFound, self.controller.show, - test_utils.FakeRequest(), image_id=utils.generate_uuid()) + unit_test_utils.FakeRequest(), image_id=utils.generate_uuid()) def test_create(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() image = {'name': 'image-1'} output = self.controller.create(request, image) for key in ['id', 'created_at', 'updated_at']: output.pop(key) expected = { 'name': 'image-1', - 'owner': test_utils.TENANT1, + 'owner': unit_test_utils.TENANT1, 'location': None, 'status': 'queued', 'is_public': False, @@ -90,20 +90,20 @@ class TestImagesController(unittest.TestCase): self.assertEqual(expected, output) def test_create_with_owner_forbidden(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() image = {'name': 'image-1', 'owner': utils.generate_uuid()} self.assertRaises(webob.exc.HTTPForbidden, self.controller.create, request, image) def test_create_public_image_as_admin(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() image = {'name': 'image-1', 'is_public': True} output = self.controller.create(request, image) for key in ['id', 'created_at', 'updated_at']: output.pop(key) expected = { 'name': 'image-1', - 'owner': test_utils.TENANT1, + 'owner': unit_test_utils.TENANT1, 'location': None, 'status': 'queued', 'is_public': True, @@ -113,15 +113,15 @@ class TestImagesController(unittest.TestCase): self.assertEqual(expected, output) def test_update(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() image = {'name': 'image-2'} - output = self.controller.update(request, test_utils.UUID1, image) + output = self.controller.update(request, unit_test_utils.UUID1, image) for key in ['id', 'created_at', 'updated_at']: output.pop(key) expected = { 'name': 'image-2', - 'owner': test_utils.TENANT1, - 'location': test_utils.UUID1, + 'owner': unit_test_utils.TENANT1, + 'location': unit_test_utils.UUID1, 'status': 'queued', 'is_public': False, 'tags': ['ping', 'pong'], @@ -130,21 +130,23 @@ class TestImagesController(unittest.TestCase): self.assertEqual(expected, output) def test_update_non_existant(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() image = {'name': 'image-2'} self.assertRaises(webob.exc.HTTPNotFound, self.controller.update, request, utils.generate_uuid(), image) -class TestImagesDeserializer(unittest.TestCase): +class TestImagesDeserializer(test_utils.BaseTestCase): + def setUp(self): - self.conf = glance.tests.utils.TestConfigOpts() + super(TestImagesDeserializer, self).setUp() + self.conf = test_utils.TestConfigOpts() schema_api = glance.schema.API(self.conf) self.deserializer = glance.api.v2.images.RequestDeserializer( {}, schema_api) def test_create_with_id(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() image_id = utils.generate_uuid() request.body = json.dumps({'id': image_id}) output = self.deserializer.create(request) @@ -152,21 +154,21 @@ class TestImagesDeserializer(unittest.TestCase): self.assertEqual(expected, output) def test_create_with_name(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'name': 'image-1'}) output = self.deserializer.create(request) expected = {'image': {'name': 'image-1', 'properties': {}}} self.assertEqual(expected, output) def test_create_public(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'visibility': 'public'}) output = self.deserializer.create(request) expected = {'image': {'is_public': True, 'properties': {}}} self.assertEqual(expected, output) def test_create_private(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'visibility': 'private'}) output = self.deserializer.create(request) expected = {'image': {'is_public': False, 'properties': {}}} @@ -174,21 +176,21 @@ class TestImagesDeserializer(unittest.TestCase): def test_create_readonly_attributes_ignored(self): for key in ['created_at', 'updated_at']: - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({key: ISOTIME}) output = self.deserializer.create(request) expected = {'image': {'properties': {}}} self.assertEqual(expected, output) def test_create_with_tags(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'tags': ['one', 'two']}) output = self.deserializer.create(request) expected = {'image': {'tags': ['one', 'two'], 'properties': {}}} self.assertEqual(expected, output) def test_update(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'name': 'image-1', 'visibility': 'public'}) output = self.deserializer.update(request) expected = { @@ -202,16 +204,18 @@ class TestImagesDeserializer(unittest.TestCase): def test_update_readonly_attributes_ignored(self): for key in ['created_at', 'updated_at']: - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({key: ISOTIME}) output = self.deserializer.update(request) expected = {'image': {'properties': {}}} self.assertEqual(expected, output) -class TestImagesDeserializerWithExtendedSchema(unittest.TestCase): +class TestImagesDeserializerWithExtendedSchema(test_utils.BaseTestCase): + def setUp(self): - conf = glance.tests.utils.TestConfigOpts() + super(TestImagesDeserializerWithExtendedSchema, self).setUp() + conf = test_utils.TestConfigOpts() schema_api = glance.schema.API(conf) props = { 'pants': { @@ -225,7 +229,7 @@ class TestImagesDeserializerWithExtendedSchema(unittest.TestCase): {}, schema_api) def test_create(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'name': 'image-1', 'pants': 'on'}) output = self.deserializer.create(request) expected = { @@ -237,13 +241,13 @@ class TestImagesDeserializerWithExtendedSchema(unittest.TestCase): self.assertEqual(expected, output) def test_create_bad_data(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'name': 'image-1', 'pants': 'borked'}) self.assertRaises(exception.InvalidObject, self.deserializer.create, request) def test_update(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'name': 'image-1', 'pants': 'off'}) output = self.deserializer.update(request) expected = { @@ -255,22 +259,24 @@ class TestImagesDeserializerWithExtendedSchema(unittest.TestCase): self.assertEqual(expected, output) def test_update_bad_data(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'name': 'image-1', 'pants': 'borked'}) self.assertRaises(exception.InvalidObject, self.deserializer.update, request) -class TestImagesDeserializerWithAdditionalProperties(unittest.TestCase): +class TestImagesDeserializerWithAdditionalProperties(test_utils.BaseTestCase): + def setUp(self): - self.conf = glance.tests.utils.TestConfigOpts() + super(TestImagesDeserializerWithAdditionalProperties, self).setUp() + self.conf = test_utils.TestConfigOpts() self.conf.allow_additional_image_properties = True schema_api = glance.schema.API(self.conf) self.deserializer = glance.api.v2.images.RequestDeserializer( {}, schema_api) def test_create(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'foo': 'bar'}) output = self.deserializer.create(request) expected = {'image': {'properties': {'foo': 'bar'}}} @@ -278,25 +284,25 @@ class TestImagesDeserializerWithAdditionalProperties(unittest.TestCase): def test_create_with_additional_properties_disallowed(self): self.conf.allow_additional_image_properties = False - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'foo': 'bar'}) self.assertRaises(exception.InvalidObject, self.deserializer.create, request) def test_create_with_numeric_property(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'abc': 123}) self.assertRaises(exception.InvalidObject, self.deserializer.create, request) def test_create_with_list_property(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'foo': ['bar']}) self.assertRaises(exception.InvalidObject, self.deserializer.create, request) def test_update(self): - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'foo': 'bar'}) output = self.deserializer.update(request) expected = {'image': {'properties': {'foo': 'bar'}}} @@ -304,22 +310,24 @@ class TestImagesDeserializerWithAdditionalProperties(unittest.TestCase): def test_update_with_additional_properties_disallowed(self): self.conf.allow_additional_image_properties = False - request = test_utils.FakeRequest() + request = unit_test_utils.FakeRequest() request.body = json.dumps({'foo': 'bar'}) self.assertRaises(exception.InvalidObject, self.deserializer.update, request) -class TestImagesSerializer(unittest.TestCase): +class TestImagesSerializer(test_utils.BaseTestCase): + def setUp(self): - conf = glance.tests.utils.TestConfigOpts() + super(TestImagesSerializer, self).setUp() + conf = test_utils.TestConfigOpts() schema_api = glance.schema.API(conf) self.serializer = glance.api.v2.images.ResponseSerializer(schema_api) def test_index(self): fixtures = [ { - 'id': test_utils.UUID1, + 'id': unit_test_utils.UUID1, 'name': 'image-1', 'is_public': True, 'properties': {}, @@ -328,7 +336,7 @@ class TestImagesSerializer(unittest.TestCase): 'tags': ['one', 'two'], }, { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'is_public': False, 'properties': {}, @@ -340,7 +348,7 @@ class TestImagesSerializer(unittest.TestCase): expected = { 'images': [ { - 'id': test_utils.UUID1, + 'id': unit_test_utils.UUID1, 'name': 'image-1', 'visibility': 'public', 'created_at': ISOTIME, @@ -349,17 +357,19 @@ class TestImagesSerializer(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID1, + 'href': + '/v2/images/%s' % unit_test_utils.UUID1, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID1, + 'href': + '/v2/images/%s/file' % unit_test_utils.UUID1, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], }, { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -368,11 +378,13 @@ class TestImagesSerializer(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': + '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': + '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], @@ -386,7 +398,7 @@ class TestImagesSerializer(unittest.TestCase): def test_show(self): fixture = { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'is_public': True, 'properties': {}, @@ -396,7 +408,7 @@ class TestImagesSerializer(unittest.TestCase): } expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'public', 'created_at': ISOTIME, @@ -405,11 +417,11 @@ class TestImagesSerializer(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], @@ -421,7 +433,7 @@ class TestImagesSerializer(unittest.TestCase): def test_create(self): fixture = { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'is_public': False, 'properties': {}, @@ -429,10 +441,10 @@ class TestImagesSerializer(unittest.TestCase): 'updated_at': DATETIME, 'tags': [], } - self_link = '/v2/images/%s' % test_utils.UUID2 + self_link = '/v2/images/%s' % unit_test_utils.UUID2 expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -452,7 +464,7 @@ class TestImagesSerializer(unittest.TestCase): def test_update(self): fixture = { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'is_public': True, 'properties': {}, @@ -460,10 +472,10 @@ class TestImagesSerializer(unittest.TestCase): 'updated_at': DATETIME, 'tags': ['five'], } - self_link = '/v2/images/%s' % test_utils.UUID2 + self_link = '/v2/images/%s' % unit_test_utils.UUID2 expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'public', 'created_at': ISOTIME, @@ -481,9 +493,11 @@ class TestImagesSerializer(unittest.TestCase): self.assertEqual(expected, json.loads(response.body)) -class TestImagesSerializerWithExtendedSchema(unittest.TestCase): +class TestImagesSerializerWithExtendedSchema(test_utils.BaseTestCase): + def setUp(self): - self.conf = glance.tests.utils.TestConfigOpts() + super(TestImagesSerializerWithExtendedSchema, self).setUp() + self.conf = test_utils.TestConfigOpts() self.conf.allow_additional_image_properties = False self.schema_api = glance.schema.API(self.conf) props = { @@ -495,7 +509,7 @@ class TestImagesSerializerWithExtendedSchema(unittest.TestCase): } self.schema_api.set_custom_schema_properties('image', props) self.fixture = { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'is_public': False, 'created_at': DATETIME, @@ -508,7 +522,7 @@ class TestImagesSerializerWithExtendedSchema(unittest.TestCase): serializer = glance.api.v2.images.ResponseSerializer(self.schema_api) expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -518,11 +532,11 @@ class TestImagesSerializerWithExtendedSchema(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], @@ -537,7 +551,7 @@ class TestImagesSerializerWithExtendedSchema(unittest.TestCase): self.fixture['properties']['color'] = 'invalid' expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -547,11 +561,11 @@ class TestImagesSerializerWithExtendedSchema(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], @@ -562,13 +576,15 @@ class TestImagesSerializerWithExtendedSchema(unittest.TestCase): self.assertEqual(expected, json.loads(response.body)) -class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): +class TestImagesSerializerWithAdditionalProperties(test_utils.BaseTestCase): + def setUp(self): - self.conf = glance.tests.utils.TestConfigOpts() + super(TestImagesSerializerWithAdditionalProperties, self).setUp() + self.conf = test_utils.TestConfigOpts() self.conf.allow_additional_image_properties = True self.schema_api = glance.schema.API(self.conf) self.fixture = { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'is_public': False, 'created_at': DATETIME, @@ -583,7 +599,7 @@ class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): serializer = glance.api.v2.images.ResponseSerializer(self.schema_api) expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -593,11 +609,11 @@ class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], @@ -615,7 +631,7 @@ class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): self.fixture['properties']['marx'] = 123 expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -625,11 +641,11 @@ class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], @@ -644,7 +660,7 @@ class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): serializer = glance.api.v2.images.ResponseSerializer(self.schema_api) expected = { 'image': { - 'id': test_utils.UUID2, + 'id': unit_test_utils.UUID2, 'name': 'image-2', 'visibility': 'private', 'created_at': ISOTIME, @@ -653,11 +669,11 @@ class TestImagesSerializerWithAdditionalProperties(unittest.TestCase): 'links': [ { 'rel': 'self', - 'href': '/v2/images/%s' % test_utils.UUID2, + 'href': '/v2/images/%s' % unit_test_utils.UUID2, }, { 'rel': 'file', - 'href': '/v2/images/%s/file' % test_utils.UUID2, + 'href': '/v2/images/%s/file' % unit_test_utils.UUID2, }, {'rel': 'describedby', 'href': '/v2/schemas/image'} ], diff --git a/glance/tests/unit/v2/test_root_resource.py b/glance/tests/unit/v2/test_root_resource.py index 36d8944a..667bfe4b 100644 --- a/glance/tests/unit/v2/test_root_resource.py +++ b/glance/tests/unit/v2/test_root_resource.py @@ -13,20 +13,19 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - import glance.api.v2.root -import glance.tests.unit.utils as test_utils +import glance.tests.unit.utils as unit_test_utils +import glance.tests.utils as test_utils -class TestRootController(unittest.TestCase): +class TestRootController(test_utils.BaseTestCase): def setUp(self): super(TestRootController, self).setUp() self.controller = glance.api.v2.root.RootController() def test_index(self): - req = test_utils.FakeRequest() + req = unit_test_utils.FakeRequest() output = self.controller.index(req) expected = { 'links': [ diff --git a/glance/tests/unit/v2/test_schemas_resource.py b/glance/tests/unit/v2/test_schemas_resource.py index 4f4da7df..a7975e00 100644 --- a/glance/tests/unit/v2/test_schemas_resource.py +++ b/glance/tests/unit/v2/test_schemas_resource.py @@ -13,24 +13,22 @@ # License for the specific language governing permissions and limitations # under the License. -import unittest - from glance.api.v2 import schemas import glance.schema -import glance.tests.unit.utils as test_utils -import glance.tests.utils +import glance.tests.unit.utils as unit_test_utils +import glance.tests.utils as test_utils -class TestSchemasController(unittest.TestCase): +class TestSchemasController(test_utils.BaseTestCase): def setUp(self): super(TestSchemasController, self).setUp() - conf = glance.tests.utils.TestConfigOpts() + conf = test_utils.TestConfigOpts() self.schema_api = glance.schema.API(conf) self.controller = schemas.Controller({}, self.schema_api) def test_index(self): - req = test_utils.FakeRequest() + req = unit_test_utils.FakeRequest() output = self.controller.index(req) expected = {'links': [ {'rel': 'image', 'href': '/v2/schemas/image'}, @@ -39,11 +37,11 @@ class TestSchemasController(unittest.TestCase): self.assertEqual(expected, output) def test_image(self): - req = test_utils.FakeRequest() + req = unit_test_utils.FakeRequest() output = self.controller.image(req) self.assertEqual(self.schema_api.get_schema('image'), output) def test_access(self): - req = test_utils.FakeRequest() + req = unit_test_utils.FakeRequest() output = self.controller.access(req) self.assertEqual(self.schema_api.get_schema('access'), output) diff --git a/glance/tests/utils.py b/glance/tests/utils.py index bb635928..8b5862c3 100644 --- a/glance/tests/utils.py +++ b/glance/tests/utils.py @@ -24,6 +24,7 @@ import random import socket import subprocess import tempfile +import unittest import nose.plugins.skip @@ -45,6 +46,15 @@ def get_isolated_test_env(): return test_id, test_dir +class BaseTestCase(unittest.TestCase): + + def setUp(self): + super(BaseTestCase, self).setUp() + + def tearDown(self): + super(BaseTestCase, self).tearDown() + + class TestConfigOpts(config.GlanceConfigOpts): """ Support easily controllable config for unit tests, avoiding the