From 6d984c3097252f9f97ef10e48be390fdf756b391 Mon Sep 17 00:00:00 2001 From: Ken Pepple Date: Tue, 15 Mar 2011 16:08:22 -0700 Subject: wrap errors getting image ids from local image store --- nova/image/local.py | 14 ++++++++++++-- nova/tests/api/openstack/test_images.py | 7 +++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/nova/image/local.py b/nova/image/local.py index c4ac3baaa..ef92a35b5 100644 --- a/nova/image/local.py +++ b/nova/image/local.py @@ -20,8 +20,9 @@ import os.path import random import shutil -from nova import flags from nova import exception +from nova import flags +from nova import log as logging from nova.image import service @@ -29,6 +30,8 @@ FLAGS = flags.FLAGS flags.DEFINE_string('images_path', '$state_path/images', 'path to decrypted images') +LOG = logging.getLogger('nova.image.local') + class LocalImageService(service.BaseImageService): """Image service storing images to local disk. @@ -47,7 +50,14 @@ class LocalImageService(service.BaseImageService): def _ids(self): """The list of all image ids.""" - return [int(i, 16) for i in os.listdir(self._path)] + images = [] + for i in os.listdir(self._path): + try: + images.append(int(i, 16)) + except: + LOG.debug( + _("%s is not in correct directory naming format" % i)) + return images def index(self, context): return [dict(image_id=i['id'], name=i.get('name')) diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py index 76f758929..2c4918117 100644 --- a/nova/tests/api/openstack/test_images.py +++ b/nova/tests/api/openstack/test_images.py @@ -151,6 +151,13 @@ class LocalImageServiceTest(test.TestCase, self.stubs.UnsetAll() super(LocalImageServiceTest, self).tearDown() + def test_get_all_ids_with_incorrect_directory_formats(self): + # create some old-style image directories (starting with 'ami-') + for x in [1, 2, 3]: + tempfile.mkstemp(prefix='ami-', dir=self.tempdir) + found_images = self.service._ids() + self.assertEqual(True, isinstance(found_images, list)) + class GlanceImageServiceTest(test.TestCase, BaseImageServiceTests): -- cgit From 60c7ce60826becb1ebe7f75a0a0d28b2893d70c0 Mon Sep 17 00:00:00 2001 From: Ken Pepple Date: Tue, 15 Mar 2011 18:54:51 -0700 Subject: revised per code review --- nova/image/local.py | 11 +++++++---- nova/tests/api/openstack/test_images.py | 9 +++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/nova/image/local.py b/nova/image/local.py index ef92a35b5..c304a2212 100644 --- a/nova/image/local.py +++ b/nova/image/local.py @@ -51,12 +51,15 @@ class LocalImageService(service.BaseImageService): def _ids(self): """The list of all image ids.""" images = [] - for i in os.listdir(self._path): + for image_dir in os.listdir(self._path): try: - images.append(int(i, 16)) + images.append(int(image_dir, 16)) + except ValueError: + LOG.error( + _("%s is not in correct directory naming format"\ + % image_dir)) except: - LOG.debug( - _("%s is not in correct directory naming format" % i)) + raise return images def index(self, context): diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py index 2c4918117..a674ccefe 100644 --- a/nova/tests/api/openstack/test_images.py +++ b/nova/tests/api/openstack/test_images.py @@ -22,6 +22,7 @@ and as a WSGI layer import json import datetime +import os import shutil import tempfile @@ -155,8 +156,12 @@ class LocalImageServiceTest(test.TestCase, # create some old-style image directories (starting with 'ami-') for x in [1, 2, 3]: tempfile.mkstemp(prefix='ami-', dir=self.tempdir) - found_images = self.service._ids() - self.assertEqual(True, isinstance(found_images, list)) + # create some valid image directories names + for x in ["1485baed", "1a60f0ee", "3123a73d"]: + os.makedirs(os.path.join(self.tempdir, x)) + found_image_ids = self.service._ids() + self.assertEqual(True, isinstance(found_image_ids, list)) + self.assertEqual(3, len(found_image_ids), len(found_image_ids)) class GlanceImageServiceTest(test.TestCase, -- cgit From 6d6d0f686a7f8d47263b7ed725bdae0f322b2a4e Mon Sep 17 00:00:00 2001 From: Ken Pepple Date: Thu, 17 Mar 2011 11:34:14 -0700 Subject: better implementation of try..except..else --- nova/image/local.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nova/image/local.py b/nova/image/local.py index c304a2212..609d6c42a 100644 --- a/nova/image/local.py +++ b/nova/image/local.py @@ -53,13 +53,13 @@ class LocalImageService(service.BaseImageService): images = [] for image_dir in os.listdir(self._path): try: - images.append(int(image_dir, 16)) + unhexed_image_id = int(image_dir, 16) except ValueError: LOG.error( _("%s is not in correct directory naming format"\ % image_dir)) - except: - raise + else: + images.append(unhexed_image_id) return images def index(self, context): -- cgit