Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / tests / image.py @ d2e1b032

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
        r0.reverse()
124
        for i, img in enumerate(r):
125
            self.assert_dicts_are_deeply_equal(img, r0[i])
126
        r1 = self.client.list_public(detail=True)
127
        for img in r1:
128
            for term in ('status',
129
                'name',
130
                'checksum',
131
                'created_at',
132
                'disk_format',
133
                'updated_at',
134
                'id',
135
                'location',
136
                'container_format',
137
                'owner',
138
                'is_public',
139
                'deleted_at',
140
                'properties',
141
                'size'):
142
                self.assertTrue(term in img)
143
                if img['properties']:
144
                    for interm in (
145
                        'osfamily',
146
                        'users',
147
                        'os',
148
                        'root_partition',
149
                        'description'):
150
                        self.assertTrue(interm in img['properties'])
151
        size_max = 1000000000
152
        r2 = self.client.list_public(filters=dict(size_max=size_max))
153
        self.assertTrue(len(r2) <= len(r))
154
        for img in r2:
155
            self.assertTrue(int(img['size']) <= size_max)
156

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

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

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

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

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

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

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

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

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

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

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

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

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

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