Skip to content

Commit

Permalink
Increases test coverage for the common utils
Browse files Browse the repository at this point in the history
Adds test case for the isotime stuff and removes
unused legacy utility functions

Change-Id: I6fa2bb6e2699b1050039fa03d1c0a5ecbd651d73
  • Loading branch information
jaypipes committed Oct 11, 2011
1 parent 53db059 commit e2555e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 56 deletions.
56 changes: 0 additions & 56 deletions glance/common/utils.py
Expand Up @@ -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()
Expand All @@ -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)
25 changes: 25 additions & 0 deletions glance/tests/unit/test_misc.py
Expand Up @@ -17,6 +17,8 @@

import os
import commands
import datetime
import re
import unittest

from glance.common import exception
Expand Down Expand Up @@ -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)

0 comments on commit e2555e1

Please sign in to comment.