~mdragon/nova/system-usages

« back to all changes in this revision

Viewing changes to nova/image/fake.py

  • Committer: Monsyne Dragon
  • Date: 2011-06-28 10:23:00 UTC
  • mfrom: (1077.1.141 nova)
  • Revision ID: mdragon@rackspace.com-20110628102300-lnkdr13k8uuyp30i
remergedĀ trunkĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import copy
21
21
import datetime
 
22
import random
22
23
 
23
24
from nova import exception
24
25
from nova import flags
32
33
FLAGS = flags.FLAGS
33
34
 
34
35
 
35
 
class FakeImageService(service.BaseImageService):
 
36
class _FakeImageService(service.BaseImageService):
36
37
    """Mock (fake) image service for unit testing."""
37
38
 
38
39
    def __init__(self):
40
41
        # NOTE(justinsb): The OpenStack API can't upload an image?
41
42
        # So, make sure we've got one..
42
43
        timestamp = datetime.datetime(2011, 01, 01, 01, 02, 03)
43
 
        image = {'id': '123456',
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
 
        self.create(None, image)
53
 
        super(FakeImageService, self).__init__()
54
 
 
55
 
    def index(self, context):
 
44
        image1 = {'id': '123456',
 
45
                 'name': 'fakeimage123456',
 
46
                 'created_at': timestamp,
 
47
                 'updated_at': timestamp,
 
48
                 'status': 'active',
 
49
                 'container_format': 'ami',
 
50
                 'disk_format': 'raw',
 
51
                 'properties': {'kernel_id': FLAGS.null_kernel,
 
52
                                'ramdisk_id': FLAGS.null_kernel,
 
53
                                'architecture': 'x86_64'}}
 
54
 
 
55
        image2 = {'id': 'fake',
 
56
                 'name': 'fakeimage123456',
 
57
                 'created_at': timestamp,
 
58
                 'updated_at': timestamp,
 
59
                 'status': 'active',
 
60
                 'container_format': 'ami',
 
61
                 'disk_format': 'raw',
 
62
                 'properties': {'kernel_id': FLAGS.null_kernel,
 
63
                                'ramdisk_id': FLAGS.null_kernel}}
 
64
 
 
65
        image3 = {'id': '2',
 
66
                 'name': 'fakeimage123456',
 
67
                 'created_at': timestamp,
 
68
                 'updated_at': timestamp,
 
69
                 'status': 'active',
 
70
                 'container_format': 'ami',
 
71
                 'disk_format': 'raw',
 
72
                 'properties': {'kernel_id': FLAGS.null_kernel,
 
73
                                'ramdisk_id': FLAGS.null_kernel}}
 
74
 
 
75
        image4 = {'id': '1',
 
76
                 'name': 'fakeimage123456',
 
77
                 'created_at': timestamp,
 
78
                 'updated_at': timestamp,
 
79
                 'status': 'active',
 
80
                 'container_format': 'ami',
 
81
                 'disk_format': 'raw',
 
82
                 'properties': {'kernel_id': FLAGS.null_kernel,
 
83
                                'ramdisk_id': FLAGS.null_kernel}}
 
84
 
 
85
        image5 = {'id': '3',
 
86
                 'name': 'fakeimage123456',
 
87
                 'created_at': timestamp,
 
88
                 'updated_at': timestamp,
 
89
                 'status': 'active',
 
90
                 'container_format': 'ami',
 
91
                 'disk_format': 'raw',
 
92
                 'properties': {'kernel_id': FLAGS.null_kernel,
 
93
                                'ramdisk_id': FLAGS.null_kernel}}
 
94
 
 
95
        self.create(None, image1)
 
96
        self.create(None, image2)
 
97
        self.create(None, image3)
 
98
        self.create(None, image4)
 
99
        self.create(None, image5)
 
100
        super(_FakeImageService, self).__init__()
 
101
 
 
102
    def index(self, context, filters=None, marker=None, limit=None):
56
103
        """Returns list of images."""
57
104
        return copy.deepcopy(self.images.values())
58
105
 
59
 
    def detail(self, context):
 
106
    def detail(self, context, filters=None, marker=None, limit=None):
60
107
        """Return list of detailed image information."""
61
108
        return copy.deepcopy(self.images.values())
62
109
 
66
113
        Returns a dict containing image data for the given opaque image id.
67
114
 
68
115
        """
69
 
        image_id = int(image_id)
70
 
        image = self.images.get(image_id)
 
116
        image = self.images.get(str(image_id))
71
117
        if image:
72
118
            return copy.deepcopy(image)
73
119
        LOG.warn('Unable to find image id %s.  Have images: %s',
74
120
                 image_id, self.images)
75
121
        raise exception.ImageNotFound(image_id=image_id)
76
122
 
77
 
    def create(self, context, data):
 
123
    def show_by_name(self, context, name):
 
124
        """Returns a dict containing image data for the given name."""
 
125
        images = copy.deepcopy(self.images.values())
 
126
        for image in images:
 
127
            if name == image.get('name'):
 
128
                return image
 
129
        raise exception.ImageNotFound(image_id=name)
 
130
 
 
131
    def create(self, context, metadata, data=None):
78
132
        """Store the image data and return the new image id.
79
133
 
80
134
        :raises: Duplicate if the image already exist.
81
135
 
82
136
        """
83
 
        image_id = int(data['id'])
 
137
        try:
 
138
            image_id = metadata['id']
 
139
        except KeyError:
 
140
            image_id = random.randint(0, 2 ** 31 - 1)
 
141
        image_id = str(image_id)
 
142
 
84
143
        if self.images.get(image_id):
85
144
            raise exception.Duplicate()
86
145
 
87
 
        self.images[image_id] = copy.deepcopy(data)
 
146
        metadata['id'] = image_id
 
147
        self.images[image_id] = copy.deepcopy(metadata)
 
148
        return self.images[image_id]
88
149
 
89
 
    def update(self, context, image_id, data):
 
150
    def update(self, context, image_id, metadata, data=None):
90
151
        """Replace the contents of the given image with the new data.
91
152
 
92
153
        :raises: ImageNotFound if the image does not exist.
93
154
 
94
155
        """
95
 
        image_id = int(image_id)
96
156
        if not self.images.get(image_id):
97
157
            raise exception.ImageNotFound(image_id=image_id)
98
 
        self.images[image_id] = copy.deepcopy(data)
 
158
        self.images[image_id] = copy.deepcopy(metadata)
99
159
 
100
160
    def delete(self, context, image_id):
101
161
        """Delete the given image.
103
163
        :raises: ImageNotFound if the image does not exist.
104
164
 
105
165
        """
106
 
        image_id = int(image_id)
107
166
        removed = self.images.pop(image_id, None)
108
167
        if not removed:
109
168
            raise exception.ImageNotFound(image_id=image_id)
111
170
    def delete_all(self):
112
171
        """Clears out all images."""
113
172
        self.images.clear()
 
173
 
 
174
_fakeImageService = _FakeImageService()
 
175
 
 
176
 
 
177
def FakeImageService():
 
178
    return _fakeImageService