Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / livetest / image.py @ 7b6e977c

History | View | Annotate | Download (9 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 livetest
37
from kamaki.clients.cyclades import CycladesClient
38
from kamaki.clients.image import ImageClient
39
from kamaki.clients import ClientError
40

    
41

    
42
IMGMETA = set([
43
    'id', 'name', 'checksum', 'container-format', 'location', 'disk-format',
44
    'is-public', 'status', 'deleted-at', 'updated-at', 'created-at', 'owner',
45
    'size'])
46

    
47

    
48
class Image(livetest.Generic):
49
    def setUp(self):
50
        self.now = time.mktime(time.gmtime())
51

    
52
        self.imgname = 'img_%s' % self.now
53
        url = self['image', 'url']
54
        self.client = ImageClient(url, self['token'])
55
        cyclades_url = self['compute', 'url']
56
        self.cyclades = CycladesClient(cyclades_url, self['token'])
57
        self._imglist = {}
58
        self._imgdetails = {}
59

    
60
    def test_000(self):
61
        self._prepare_img()
62
        super(self.__class__, self).test_000()
63

    
64
    def _prepare_img(self):
65
        f = open(self['image', 'local_path'], 'rb')
66
        (token, uuid) = (self['token'], self['store', 'account'])
67
        if not uuid:
68
            from kamaki.clients.astakos import AstakosClient
69
            uuid = AstakosClient(self['astakos', 'url'], token).term('uuid')
70
        from kamaki.clients.pithos import PithosClient
71
        self.pithcli = PithosClient(self['store', 'url'], token, uuid)
72
        cont = 'cont_%s' % self.now
73
        self.pithcli.container = cont
74
        self.obj = 'obj_%s' % self.now
75
        print('\t- Create container %s on Pithos server' % cont)
76
        self.pithcli.container_put()
77
        self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
78
        print('\t- Upload an image at %s...\n' % self.location)
79
        self.pithcli.upload_object(self.obj, f)
80
        print('\t- ok')
81
        f.close()
82

    
83
        r = self.client.register(
84
            self.imgname,
85
            self.location,
86
            params=dict(is_public=True))
87
        self._imglist[self.imgname] = dict(
88
            name=r['x-image-meta-name'], id=r['x-image-meta-id'])
89
        self._imgdetails[self.imgname] = r
90

    
91
    def tearDown(self):
92
        for img in self._imglist.values():
93
            print('\tDeleting image %s' % img['id'])
94
            self.cyclades.delete_image(img['id'])
95
        if hasattr(self, 'pithcli'):
96
            print('\tDeleting container %s' % self.pithcli.container)
97
            try:
98
                self.pithcli.del_container(delimiter='/')
99
                self.pithcli.purge_container()
100
            except ClientError:
101
                pass
102

    
103
    def _get_img_by_name(self, name):
104
        r = self.cyclades.list_images()
105
        for img in r:
106
            if img['name'] == name:
107
                return img
108
        return None
109

    
110
    def assert_dicts_are_deeply_equal(self, d1, d2):
111
        for k, v in d1.items():
112
            self.assertTrue(k in d2)
113
            if isinstance(v, dict):
114
                self.assert_dicts_are_deeply_equal(v, d2[k])
115
            else:
116
                self.assertEqual(unicode(v), unicode(d2[k]))
117

    
118
    def test_list_public(self):
119
        """Test list_public"""
120
        self._test_list_public()
121

    
122
    def _test_list_public(self):
123
        r = self.client.list_public()
124
        r0 = self.client.list_public(order='-')
125
        self.assertTrue(len(r) > 0)
126
        for img in r:
127
            for term in (
128
                    'status',
129
                    'name',
130
                    'container_format',
131
                    'disk_format',
132
                    'id',
133
                    'size'):
134
                self.assertTrue(term in img)
135
        self.assertTrue(r, r0)
136
        r0.reverse()
137
        for i, img in enumerate(r):
138
            self.assert_dicts_are_deeply_equal(img, r0[i])
139
        r1 = self.client.list_public(detail=True)
140
        for img in r1:
141
            for term in (
142
                    'status',
143
                    'name',
144
                    'checksum',
145
                    'created_at',
146
                    'disk_format',
147
                    'updated_at',
148
                    'id',
149
                    'location',
150
                    'container_format',
151
                    'owner',
152
                    'is_public',
153
                    'deleted_at',
154
                    'properties',
155
                    'size'):
156
                self.assertTrue(term in img)
157
                if img['properties']:
158
                    for interm in (
159
                            'osfamily',
160
                            'users',
161
                            'os',
162
                            'root_partition',
163
                            'description'):
164
                        self.assertTrue(interm in img['properties'])
165
        size_max = 1000000000
166
        r2 = self.client.list_public(filters=dict(size_max=size_max))
167
        self.assertTrue(len(r2) <= len(r))
168
        for img in r2:
169
            self.assertTrue(int(img['size']) <= size_max)
170

    
171
    def test_get_meta(self):
172
        """Test get_meta"""
173
        self._test_get_meta()
174

    
175
    def _test_get_meta(self):
176
        r = self.client.get_meta(self['image', 'id'])
177
        self.assertEqual(r['id'], self['image', 'id'])
178
        for term in (
179
                'status',
180
                'name',
181
                'checksum',
182
                'updated-at',
183
                'created-at',
184
                'deleted-at',
185
                'location',
186
                'is-public',
187
                'owner',
188
                'disk-format',
189
                'size',
190
                'container-format'):
191
            self.assertTrue(term in r)
192
            for interm in (
193
                    'kernel',
194
                    'osfamily',
195
                    'users',
196
                    'gui', 'sortorder',
197
                    'root-partition',
198
                    'os',
199
                    'description'):
200
                self.assertTrue(interm in r['properties'])
201

    
202
    def test_register(self):
203
        """Test register"""
204
        self._prepare_img()
205
        self._test_register()
206

    
207
    def _test_register(self):
208
        self.assertTrue(self._imglist)
209
        for img in self._imglist.values():
210
            self.assertTrue(img is not None)
211
            r = set(self._imgdetails[img['name']].keys())
212
            self.assertTrue(
213
                r.issubset(['x-image-meta-%s' % k for k in IMGMETA]))
214

    
215
    def test_set_members(self):
216
        """Test set_members"""
217
        self._prepare_img()
218
        self._test_set_members()
219

    
220
    def _test_set_members(self):
221
        members = ['%s@fake.net' % self.now]
222
        for img in self._imglist.values():
223
            self.client.set_members(img['id'], members)
224
            r = self.client.list_members(img['id'])
225
            self.assertEqual(r[0]['member_id'], members[0])
226

    
227
    def test_list_members(self):
228
        """Test list_members"""
229
        self._test_list_members()
230

    
231
    def _test_list_members(self):
232
        self._test_set_members()
233

    
234
    def test_remove_members(self):
235
        """Test remove_members - NO CHECK"""
236
        self._prepare_img()
237
        self._test_remove_members()
238

    
239
    def _test_remove_members(self):
240
        return
241
        members = ['%s@fake.net' % self.now, '%s_v2@fake.net' % self.now]
242
        for img in self._imglist.values():
243
            self.client.set_members(img['id'], members)
244
            r = self.client.list_members(img['id'])
245
            self.assertTrue(len(r) > 1)
246
            self.client.remove_member(img['id'], members[0])
247
            r0 = self.client.list_members(img['id'])
248
            self.assertEqual(len(r), 1 + len(r0))
249
            self.assertEqual(r0[0]['member_id'], members[1])
250

    
251
    def test_list_shared(self):
252
        """Test list_shared - NOT CHECKED"""
253
        self._test_list_shared()
254

    
255
    def _test_list_shared(self):
256
        #No way to test this, if I dont have member images
257
        pass