diff options
| author | Justin Santa Barbara <justin@fathomdb.com> | 2011-03-24 01:13:20 -0700 |
|---|---|---|
| committer | Justin Santa Barbara <justin@fathomdb.com> | 2011-03-24 01:13:20 -0700 |
| commit | 694c2cfd2afdc0ed293f205890bda977968dc079 (patch) | |
| tree | a750c989e2a1daa97cf022234f34a59cc8bb19a6 /nova/image | |
| parent | 143a8387fcbec34fd919e322a3fed7e9505c8a7c (diff) | |
Created simple test case for server creation, so that we can have something to attach to...
Diffstat (limited to 'nova/image')
| -rw-r--r-- | nova/image/fake.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/nova/image/fake.py b/nova/image/fake.py new file mode 100644 index 000000000..bc63780d3 --- /dev/null +++ b/nova/image/fake.py @@ -0,0 +1,109 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 Justin Santa Barbara +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Implementation of an fake image service""" + +from nova import exception +from nova import flags +from nova import log as logging +from nova.image import service + + +LOG = logging.getLogger('nova.image.fake') + +FLAGS = flags.FLAGS + + +class MockImageService(service.BaseImageService): + """Mock (fake) image service for unit testing""" + + def __init__(self): + self.images = {} + # NOTE(justinsb): The OpenStack API can't upload an image??? + # So, make sure we've got one.. + image = {'id': '123456', + 'status': 'active', + 'type': 'machine', + 'disk_format': 'ami', + 'properties': {'kernel_id': FLAGS.null_kernel, + 'ramdisk_id': FLAGS.null_kernel, + } + } + self.create(None, image) + super(MockImageService, self).__init__() + + def index(self, context): + """Returns list of images""" + return self.images.values() + + def detail(self, context): + """Return list of detailed image information""" + return self.images.values() + + def show(self, context, image_id): + """ + Returns a dict containing image data for the given opaque image id. + """ + image_id = int(image_id) + image = self.images.get(image_id) + if image: + return image + LOG.warn("Unable to find image id %s. Have images: %s", + image_id, self.images) + raise exception.NotFound + + def create(self, context, data): + """ + Store the image data and return the new image id. + + :raises AlreadyExists if the image already exist. + + """ + image_id = int(data['id']) + if self.images.get(image_id): + #TODO(justinsb): Where is this AlreadyExists exception?? + raise exception.Error("AlreadyExists") + + self.images[image_id] = data + + def update(self, context, image_id, data): + """Replace the contents of the given image with the new data. + + :raises NotFound if the image does not exist. + + """ + image_id = int(image_id) + if not self.images.get(image_id): + raise exception.NotFound + self.images[image_id] = data + + def delete(self, context, image_id): + """ + Delete the given image. + + :raises NotFound if the image does not exist. + + """ + image_id = int(image_id) + removed = self.images.pop(image_id, None) + if not removed: + raise exception.NotFound + + def delete_all(self): + """ + Clears out all images + """ + self.images.clear() |
