Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / tests / image.py @ 54d7c02a

History | View | Annotate | Download (8.4 kB)

1
# Copyright 2012-2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import time
35

    
36
from kamaki.clients import tests
37
from kamaki.clients.cyclades import CycladesClient
38
from kamaki.clients.image import ImageClient
39
from kamaki.clients import ClientError
40

    
41

    
42
class Image(tests.Generic):
43
    def setUp(self):
44
        self.now = time.mktime(time.gmtime())
45

    
46
        self.imgname = 'img_%s' % self.now
47
        url = self['image', 'url']
48
        self.client = ImageClient(url, self['token'])
49
        cyclades_url = self['compute', 'url']
50
        self.cyclades = CycladesClient(cyclades_url, self['token'])
51
        self._imglist = {}
52

    
53
    def test_000(self):
54
        self._prepare_img()
55
        super(self.__class__, self).test_000()
56

    
57
    def _prepare_img(self):
58
        f = open(self['image', 'local_path'], 'rb')
59
        uuid = self['store', 'account']
60
        from kamaki.clients.pithos import PithosClient
61
        self.pithcli = PithosClient(self['store', 'url'], self['token'], uuid)
62
        cont = 'cont_%s' % self.now
63
        self.pithcli.container = cont
64
        self.obj = 'obj_%s' % self.now
65
        print('\t- Create container %s on Pithos server' % cont)
66
        self.pithcli.container_put()
67
        self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
68
        print('\t- Upload an image at %s...' % self.location)
69
        self.pithcli.upload_object(self.obj, f)
70
        print('\t- ok')
71
        f.close()
72

    
73
        self.client.register(self.imgname,
74
            self.location,
75
            params=dict(is_public=True))
76
        img = self._get_img_by_name(self.imgname)
77
        self._imglist[self.imgname] = img
78

    
79
    def tearDown(self):
80
        for img in self._imglist.values():
81
            print('\tDeleting image %s' % img['id'])
82
            self.cyclades.delete_image(img['id'])
83
        if hasattr(self, 'pithcli'):
84
            print('\tDeleting container %s' % self.pithcli.container)
85
            try:
86
                self.pithcli.del_container(delimiter='/')
87
                self.pithcli.purge_container()
88
            except ClientError:
89
                pass
90

    
91
    def _get_img_by_name(self, name):
92
        r = self.cyclades.list_images()
93
        for img in r:
94
            if img['name'] == name:
95
                return img
96
        return None
97

    
98
    def assert_dicts_are_deeply_equal(self, d1, d2):
99
        for k, v in d1.items():
100
            self.assertTrue(k in d2)
101
            if isinstance(v, dict):
102
                self.assert_dicts_are_deeply_equal(v, d2[k])
103
            else:
104
                self.assertEqual(unicode(v), unicode(d2[k]))
105

    
106
    def test_list_public(self):
107
        """Test list_public"""
108
        self._test_list_public()
109

    
110
    def _test_list_public(self):
111
        r = self.client.list_public()
112
        r0 = self.client.list_public(order='-')
113
        self.assertTrue(len(r) > 0)
114
        for img in r:
115
            for term in ('status',
116
                'name',
117
                'container_format',
118
                'disk_format',
119
                'id',
120
                'size'):
121
                self.assertTrue(term in img)
122
        self.assertTrue(r, r0)
123
        self.assertTrue(0)
124
        r0.reverse()
125
        for i, img in enumerate(r):
126
            self.assert_dicts_are_deeply_equal(img, r0[i])
127
        r1 = self.client.list_public(detail=True)
128
        for img in r1:
129
            for term in ('status',
130
                'name',
131
                'checksum',
132
                'created_at',
133
                'disk_format',
134
                'updated_at',
135
                'id',
136
                'location',
137
                'container_format',
138
                'owner',
139
                'is_public',
140
                'deleted_at',
141
                'properties',
142
                'size'):
143
                self.assertTrue(term in img)
144
                if img['properties']:
145
                    for interm in (
146
                        'osfamily',
147
                        'users',
148
                        'os',
149
                        'root_partition',
150
                        'description'):
151
                        self.assertTrue(interm in img['properties'])
152
        size_max = 1000000000
153
        r2 = self.client.list_public(filters=dict(size_max=size_max))
154
        self.assertTrue(len(r2) <= len(r))
155
        for img in r2:
156
            self.assertTrue(int(img['size']) <= size_max)
157

    
158
    def test_get_meta(self):
159
        """Test get_meta"""
160
        self._test_get_meta()
161

    
162
    def _test_get_meta(self):
163
        r = self.client.get_meta(self['image', 'id'])
164
        self.assertEqual(r['id'], self['image', 'id'])
165
        for term in ('status',
166
            'name',
167
            'checksum',
168
            'updated-at',
169
            'created-at',
170
            'deleted-at',
171
            'location',
172
            'is-public',
173
            'owner',
174
            'disk-format',
175
            'size',
176
            'container-format'):
177
            self.assertTrue(term in r)
178
            for interm in ('kernel',
179
                'osfamily',
180
                'users',
181
                'gui', 'sortorder',
182
                'root-partition',
183
                'os',
184
                'description'):
185
                self.assertTrue(interm in r['properties'])
186

    
187
    def test_register(self):
188
        """Test register"""
189
        self._prepare_img()
190
        self._test_register()
191

    
192
    def _test_register(self):
193
        self.assertTrue(self._imglist)
194
        for img in self._imglist.values():
195
            self.assertTrue(img != None)
196

    
197
    def test_reregister(self):
198
        """Test reregister"""
199
        self._prepare_img()
200
        self._test_reregister()
201

    
202
    def _test_reregister(self):
203
        self.client.reregister(
204
            self.location,
205
            properties=dict(my_property='some_value'))
206

    
207
    def test_set_members(self):
208
        """Test set_members"""
209
        self._prepare_img()
210
        self._test_set_members()
211

    
212
    def _test_set_members(self):
213
        members = ['%s@fake.net' % self.now]
214
        for img in self._imglist.values():
215
            self.client.set_members(img['id'], members)
216
            r = self.client.list_members(img['id'])
217
            self.assertEqual(r[0]['member_id'], members[0])
218

    
219
    def test_list_members(self):
220
        """Test list_members"""
221
        self._test_list_members()
222

    
223
    def _test_list_members(self):
224
        self._test_set_members()
225

    
226
    def test_remove_members(self):
227
        """Test remove_members - NO CHECK"""
228
        self._prepare_img()
229
        self._test_remove_members()
230

    
231
    def _test_remove_members(self):
232
        return
233
        members = ['%s@fake.net' % self.now, '%s_v2@fake.net' % self.now]
234
        for img in self._imglist.values():
235
            self.client.set_members(img['id'], members)
236
            r = self.client.list_members(img['id'])
237
            self.assertTrue(len(r) > 1)
238
            self.client.remove_member(img['id'], members[0])
239
            r0 = self.client.list_members(img['id'])
240
            self.assertEqual(len(r), 1 + len(r0))
241
            self.assertEqual(r0[0]['member_id'], members[1])
242

    
243
    def test_list_shared(self):
244
        """Test list_shared - NOT CHECKED"""
245
        self._test_list_shared()
246

    
247
    def _test_list_shared(self):
248
        #No way to test this, if I dont have member images
249
        pass