~ttx/nova/d4-merge

« back to all changes in this revision

Viewing changes to nova/tests/test_image.py

  • Committer: Thierry Carrez
  • Date: 2011-08-23 12:23:07 UTC
  • mfrom: (1130.75.258 nova)
  • Revision ID: thierry@openstack.org-20110823122307-f0vtuyg1ikc14n87
Merge diablo-4 development from trunk (rev1479)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
#    Copyright 2011 OpenStack LLC
 
4
#    Author: Soren Hansen
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
import datetime
 
19
 
 
20
from nova import context
 
21
from nova import exception
 
22
from nova import test
 
23
import nova.image
 
24
 
 
25
 
 
26
class _ImageTestCase(test.TestCase):
 
27
    def setUp(self):
 
28
        super(_ImageTestCase, self).setUp()
 
29
        self.context = context.get_admin_context()
 
30
 
 
31
    def test_index(self):
 
32
        res = self.image_service.index(self.context)
 
33
        for image in res:
 
34
            self.assertEquals(set(image.keys()), set(['id', 'name']))
 
35
 
 
36
    def test_detail(self):
 
37
        res = self.image_service.detail(self.context)
 
38
        for image in res:
 
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))
 
45
 
 
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')
 
50
 
 
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))
 
56
 
 
57
            check_is_bool(image, 'deleted')
 
58
            check_is_bool(image, 'is_public')
 
59
 
 
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)
 
66
 
 
67
    def test_show_raises_imagenotfound_for_invalid_id(self):
 
68
        self.assertRaises(exception.ImageNotFound,
 
69
                          self.image_service.show,
 
70
                          self.context,
 
71
                          'this image does not exist')
 
72
 
 
73
    def test_show_by_name(self):
 
74
        self.assertRaises(exception.ImageNotFound,
 
75
                          self.image_service.show_by_name,
 
76
                          self.context,
 
77
                          'this image does not exist')
 
78
 
 
79
    def test_create_adds_id(self):
 
80
        index = self.image_service.index(self.context)
 
81
        image_count = len(index)
 
82
 
 
83
        self.image_service.create(self.context, {})
 
84
 
 
85
        index = self.image_service.index(self.context)
 
86
        self.assertEquals(len(index), image_count + 1)
 
87
 
 
88
        self.assertTrue(index[0]['id'])
 
89
 
 
90
    def test_create_keeps_id(self):
 
91
        self.image_service.create(self.context, {'id': '34'})
 
92
        self.image_service.show(self.context, '34')
 
93
 
 
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,
 
98
                          self.context,
 
99
                          {'id': '34'})
 
100
 
 
101
        # Make sure there's still one left
 
102
        self.image_service.show(self.context, '34')
 
103
 
 
104
    def test_update(self):
 
105
        self.image_service.create(self.context,
 
106
                                  {'id': '34', 'foo': 'bar'})
 
107
 
 
108
        self.image_service.update(self.context, '34',
 
109
                                  {'id': '34', 'foo': 'baz'})
 
110
 
 
111
        img = self.image_service.show(self.context, '34')
 
112
        self.assertEquals(img['foo'], 'baz')
 
113
 
 
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,
 
119
                          self.context,
 
120
                          '34')
 
121
 
 
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)
 
129
 
 
130
 
 
131
class FakeImageTestCase(_ImageTestCase):
 
132
    def setUp(self):
 
133
        super(FakeImageTestCase, self).setUp()
 
134
        self.image_service = nova.image.fake.FakeImageService()