/nova/image/fake.py

https://github.com/santhoshkumartw/openstack-nova
Python | 187 lines | 112 code | 32 blank | 43 comment | 10 complexity | 6d587139623eba4ae35677f5860194fd MD5 | raw file
  1. # vim: tabstop=4 shiftwidth=4 softtabstop=4
  2. # Copyright 2011 Justin Santa Barbara
  3. # All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. """Implementation of an fake image service"""
  17. import copy
  18. import datetime
  19. import random
  20. from nova import exception
  21. from nova import flags
  22. from nova import log as logging
  23. from nova.image import service
  24. LOG = logging.getLogger('nova.image.fake')
  25. FLAGS = flags.FLAGS
  26. class _FakeImageService(service.BaseImageService):
  27. """Mock (fake) image service for unit testing."""
  28. def __init__(self):
  29. self.images = {}
  30. # NOTE(justinsb): The OpenStack API can't upload an image?
  31. # So, make sure we've got one..
  32. timestamp = datetime.datetime(2011, 01, 01, 01, 02, 03)
  33. image1 = {'id': '123456',
  34. 'name': 'fakeimage123456',
  35. 'created_at': timestamp,
  36. 'updated_at': timestamp,
  37. 'status': 'active',
  38. 'container_format': 'ami',
  39. 'disk_format': 'raw',
  40. 'properties': {'kernel_id': FLAGS.null_kernel,
  41. 'ramdisk_id': FLAGS.null_kernel,
  42. 'architecture': 'x86_64'}}
  43. image2 = {'id': 'fake',
  44. 'name': 'fakeimage123456',
  45. 'created_at': timestamp,
  46. 'updated_at': timestamp,
  47. 'status': 'active',
  48. 'container_format': 'ami',
  49. 'disk_format': 'raw',
  50. 'properties': {'kernel_id': FLAGS.null_kernel,
  51. 'ramdisk_id': FLAGS.null_kernel}}
  52. image3 = {'id': '2',
  53. 'name': 'fakeimage123456',
  54. 'created_at': timestamp,
  55. 'updated_at': timestamp,
  56. 'status': 'active',
  57. 'container_format': 'ami',
  58. 'disk_format': 'raw',
  59. 'properties': {'kernel_id': FLAGS.null_kernel,
  60. 'ramdisk_id': FLAGS.null_kernel}}
  61. image4 = {'id': '1',
  62. 'name': 'fakeimage123456',
  63. 'created_at': timestamp,
  64. 'updated_at': timestamp,
  65. 'status': 'active',
  66. 'container_format': 'ami',
  67. 'disk_format': 'raw',
  68. 'properties': {'kernel_id': FLAGS.null_kernel,
  69. 'ramdisk_id': FLAGS.null_kernel}}
  70. image5 = {'id': '3',
  71. 'name': 'fakeimage123456',
  72. 'created_at': timestamp,
  73. 'updated_at': timestamp,
  74. 'status': 'active',
  75. 'container_format': 'ami',
  76. 'disk_format': 'raw',
  77. 'properties': {'kernel_id': FLAGS.null_kernel,
  78. 'ramdisk_id': FLAGS.null_kernel}}
  79. self.create(None, image1)
  80. self.create(None, image2)
  81. self.create(None, image3)
  82. self.create(None, image4)
  83. self.create(None, image5)
  84. super(_FakeImageService, self).__init__()
  85. def index(self, context, filters=None, marker=None, limit=None):
  86. """Returns list of images."""
  87. return copy.deepcopy(self.images.values())
  88. def detail(self, context, filters=None, marker=None, limit=None):
  89. """Return list of detailed image information."""
  90. return copy.deepcopy(self.images.values())
  91. def show(self, context, image_id):
  92. """Get data about specified image.
  93. Returns a dict containing image data for the given opaque image id.
  94. """
  95. image = self.images.get(str(image_id))
  96. if image:
  97. return copy.deepcopy(image)
  98. LOG.warn('Unable to find image id %s. Have images: %s',
  99. image_id, self.images)
  100. raise exception.ImageNotFound(image_id=image_id)
  101. def show_by_name(self, context, name):
  102. """Returns a dict containing image data for the given name."""
  103. images = copy.deepcopy(self.images.values())
  104. for image in images:
  105. if name == image.get('name'):
  106. return image
  107. raise exception.ImageNotFound(image_id=name)
  108. def create(self, context, metadata, data=None):
  109. """Store the image data and return the new image id.
  110. :raises: Duplicate if the image already exist.
  111. """
  112. try:
  113. image_id = metadata['id']
  114. except KeyError:
  115. while True:
  116. image_id = random.randint(0, 2 ** 31 - 1)
  117. if not self.images.get(str(image_id)):
  118. break
  119. image_id = str(image_id)
  120. if self.images.get(image_id):
  121. raise exception.Duplicate()
  122. metadata['id'] = image_id
  123. self.images[image_id] = copy.deepcopy(metadata)
  124. return self.images[image_id]
  125. def update(self, context, image_id, metadata, data=None):
  126. """Replace the contents of the given image with the new data.
  127. :raises: ImageNotFound if the image does not exist.
  128. """
  129. if not self.images.get(image_id):
  130. raise exception.ImageNotFound(image_id=image_id)
  131. self.images[image_id] = copy.deepcopy(metadata)
  132. def delete(self, context, image_id):
  133. """Delete the given image.
  134. :raises: ImageNotFound if the image does not exist.
  135. """
  136. removed = self.images.pop(image_id, None)
  137. if not removed:
  138. raise exception.ImageNotFound(image_id=image_id)
  139. def delete_all(self):
  140. """Clears out all images."""
  141. self.images.clear()
  142. _fakeImageService = _FakeImageService()
  143. def FakeImageService():
  144. return _fakeImageService
  145. def FakeImageService_reset():
  146. global _fakeImageService
  147. _fakeImageService = _FakeImageService()