1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 OpenStack LLC
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
20
from nova import context
21
from nova import exception
26
class _ImageTestCase(test.TestCase):
28
super(_ImageTestCase, self).setUp()
29
self.context = context.get_admin_context()
32
res = self.image_service.index(self.context)
34
self.assertEquals(set(image.keys()), set(['id', 'name']))
36
def test_detail(self):
37
res = self.image_service.detail(self.context)
39
keys = set(image.keys())
40
self.assertEquals(keys, set(['id', 'name', 'created_at',
41
'updated_at', 'deleted_at', 'deleted',
42
'status', 'is_public', 'properties']))
43
self.assertTrue(isinstance(image['created_at'], datetime.datetime))
44
self.assertTrue(isinstance(image['updated_at'], datetime.datetime))
46
if not (isinstance(image['deleted_at'], datetime.datetime) or
47
image['deleted_at'] is None):
48
self.fail('image\'s "deleted_at" attribute was neither a '
49
'datetime object nor None')
51
def check_is_bool(image, key):
52
val = image.get('deleted')
53
if not isinstance(val, bool):
54
self.fail('image\'s "%s" attribute wasn\'t '
55
'a bool: %r' % (key, val))
57
check_is_bool(image, 'deleted')
58
check_is_bool(image, 'is_public')
60
def test_index_and_detail_have_same_results(self):
61
index = self.image_service.index(self.context)
62
detail = self.image_service.detail(self.context)
63
index_set = set([(i['id'], i['name']) for i in index])
64
detail_set = set([(i['id'], i['name']) for i in detail])
65
self.assertEqual(index_set, detail_set)
67
def test_show_raises_imagenotfound_for_invalid_id(self):
68
self.assertRaises(exception.ImageNotFound,
69
self.image_service.show,
71
'this image does not exist')
73
def test_show_by_name(self):
74
self.assertRaises(exception.ImageNotFound,
75
self.image_service.show_by_name,
77
'this image does not exist')
79
def test_create_adds_id(self):
80
index = self.image_service.index(self.context)
81
image_count = len(index)
83
self.image_service.create(self.context, {})
85
index = self.image_service.index(self.context)
86
self.assertEquals(len(index), image_count + 1)
88
self.assertTrue(index[0]['id'])
90
def test_create_keeps_id(self):
91
self.image_service.create(self.context, {'id': '34'})
92
self.image_service.show(self.context, '34')
94
def test_create_rejects_duplicate_ids(self):
95
self.image_service.create(self.context, {'id': '34'})
96
self.assertRaises(exception.Duplicate,
97
self.image_service.create,
101
# Make sure there's still one left
102
self.image_service.show(self.context, '34')
104
def test_update(self):
105
self.image_service.create(self.context,
106
{'id': '34', 'foo': 'bar'})
108
self.image_service.update(self.context, '34',
109
{'id': '34', 'foo': 'baz'})
111
img = self.image_service.show(self.context, '34')
112
self.assertEquals(img['foo'], 'baz')
114
def test_delete(self):
115
self.image_service.create(self.context, {'id': '34', 'foo': 'bar'})
116
self.image_service.delete(self.context, '34')
117
self.assertRaises(exception.NotFound,
118
self.image_service.show,
122
def test_delete_all(self):
123
self.image_service.create(self.context, {'id': '32', 'foo': 'bar'})
124
self.image_service.create(self.context, {'id': '33', 'foo': 'bar'})
125
self.image_service.create(self.context, {'id': '34', 'foo': 'bar'})
126
self.image_service.delete_all()
127
index = self.image_service.index(self.context)
128
self.assertEquals(len(index), 0)
131
class FakeImageTestCase(_ImageTestCase):
133
super(FakeImageTestCase, self).setUp()
134
self.image_service = nova.image.fake.FakeImageService()