From e2555e1665a8d12d2699928a692d1ef62ae41ed6 Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Tue, 11 Oct 2011 12:57:40 -0400 Subject: [PATCH] Increases test coverage for the common utils Adds test case for the isotime stuff and removes unused legacy utility functions Change-Id: I6fa2bb6e2699b1050039fa03d1c0a5ecbd651d73 --- glance/common/utils.py | 56 ---------------------------------- glance/tests/unit/test_misc.py | 25 +++++++++++++++ 2 files changed, 25 insertions(+), 56 deletions(-) diff --git a/glance/common/utils.py b/glance/common/utils.py index 168842744a..0655db6d94 100644 --- a/glance/common/utils.py +++ b/glance/common/utils.py @@ -76,31 +76,6 @@ def import_object(import_str): return cls() -def abspath(s): - return os.path.join(os.path.dirname(__file__), s) - - -def debug(arg): - logging.debug('debug in callback: %s', arg) - return arg - - -def generate_uid(topic, size=8): - return '%s-%s' % (topic, ''.join( - [random.choice('01234567890abcdefghijklmnopqrstuvwxyz') - for x in xrange(size)])) - - -def generate_mac(): - mac = [0x02, 0x16, 0x3e, random.randint(0x00, 0x7f), - random.randint(0x00, 0xff), random.randint(0x00, 0xff)] - return ':'.join(map(lambda x: "%02x" % x, mac)) - - -def last_octet(address): - return int(address.split(".")[-1]) - - def isotime(at=None): if not at: at = datetime.datetime.utcnow() @@ -125,34 +100,3 @@ def safe_remove(path): except OSError, e: if e.errno != errno.ENOENT: raise - - -class LazyPluggable(object): - """A pluggable backend loaded lazily based on some value.""" - - def __init__(self, pivot, **backends): - self.__backends = backends - self.__pivot = pivot - self.__backend = None - - def __get_backend(self): - if not self.__backend: - backend_name = self.__pivot.value - if backend_name not in self.__backends: - raise exception.Error('Invalid backend: %s' % backend_name) - - backend = self.__backends[backend_name] - if type(backend) == type(tuple()): - name = backend[0] - fromlist = backend[1] - else: - name = backend - fromlist = backend - - self.__backend = __import__(name, None, None, fromlist) - logging.info('backend %s', self.__backend) - return self.__backend - - def __getattr__(self, key): - backend = self.__get_backend() - return getattr(backend, key) diff --git a/glance/tests/unit/test_misc.py b/glance/tests/unit/test_misc.py index f12bf0561b..646aca2ebf 100644 --- a/glance/tests/unit/test_misc.py +++ b/glance/tests/unit/test_misc.py @@ -17,6 +17,8 @@ import os import commands +import datetime +import re import unittest from glance.common import exception @@ -101,3 +103,26 @@ def test_import_class_or_object(self): self.assertRaises(exception.ImportFailure, utils.import_object, 'os.path.NONEXISTINGOBJECT') + + store_class = utils.import_class('glance.store.s3.Store') + + self.assertTrue(store_class.__name__ == 'Store') + + # Try importing an object by supplying a class and + # verify the object's class name is the same as that supplied + store_obj = utils.import_object('glance.store.s3.Store') + + self.assertTrue(store_obj.__class__.__name__ == 'Store') + + # Try importing a module itself + module_obj = utils.import_object('glance.registry') + + self.assertEqual('glance.registry', module_obj.__package__) + + def test_isotime(self): + dt1 = datetime.datetime(2001, 11, 10, 1, 2, 3) + self.assertEqual('2001-11-10T01:02:03Z', utils.isotime(dt1)) + + iso_re = re.compile(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z') + now_iso = utils.isotime() + self.assertTrue(iso_re.match(now_iso) is not None)