Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / livetest / image.py @ 058ee9a8

History | View | Annotate | Download (9.2 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.astakos import AstakosClient as AstakosCachedClient
38
from kamaki.clients.cyclades import CycladesClient
39
from kamaki.clients.image import ImageClient
40
from kamaki.clients import ClientError
41

    
42

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

    
48

    
49
class Image(livetest.Generic):
50
    def setUp(self):
51
        self.now = time.mktime(time.gmtime())
52
        self.cloud = 'cloud.%s' % self['testcloud']
53
        aurl, self.token = self[self.cloud, 'url'], self[self.cloud, 'token']
54
        self.auth_base = AstakosCachedClient(aurl, self.token)
55
        self.imgname = 'img_%s' % self.now
56
        url = self.auth_base.get_service_endpoints('image')['publicURL']
57
        self.token = self.auth_base.token
58
        self.client = ImageClient(url, self.token)
59
        cyclades_url = self.auth_base.get_service_endpoints(
60
            'compute')['publicURL']
61
        self.cyclades = CycladesClient(cyclades_url, self.token)
62
        self._imglist = {}
63
        self._imgdetails = {}
64

    
65
    def test_000(self):
66
        self._prepare_img()
67
        super(self.__class__, self).test_000()
68

    
69
    def _prepare_img(self):
70
        f = open(self['image', 'local_path'], 'rb')
71
        (token, uuid) = (self.token, self.auth_base.user_term('id'))
72
        purl = self.auth_base.get_service_endpoints(
73
            'object-store')['publicURL']
74
        from kamaki.clients.pithos import PithosClient
75
        self.pithcli = PithosClient(purl, token, uuid)
76
        cont = 'cont_%s' % self.now
77
        self.pithcli.container = cont
78
        self.obj = 'obj_%s' % self.now
79
        print('\t- Create container %s on Pithos server' % cont)
80
        self.pithcli.container_put()
81
        self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
82
        print('\t- Upload an image at %s...\n' % self.location)
83
        self.pithcli.upload_object(self.obj, f)
84
        print('\t- ok')
85
        f.close()
86

    
87
        r = self.client.register(
88
            self.imgname, self.location, params=dict(is_public=True))
89
        self._imglist[self.imgname] = dict(
90
            name=r['name'], id=r['id'])
91
        self._imgdetails[self.imgname] = r
92

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

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

    
112
    def test_list_public(self):
113
        """Test list_public"""
114
        self._test_list_public()
115

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

    
160
    def test_get_meta(self):
161
        """Test get_meta"""
162
        self._test_get_meta()
163

    
164
    def _test_get_meta(self):
165
        r = self.client.get_meta(self['image', 'id'])
166
        self.assertEqual(r['id'], self['image', 'id'])
167
        for term in (
168
                'status',
169
                'name',
170
                'checksum',
171
                'updated-at',
172
                'created-at',
173
                'deleted-at',
174
                'location',
175
                'is-public',
176
                'owner',
177
                'disk-format',
178
                'size',
179
                'container-format'):
180
            self.assertTrue(term in r)
181
            for interm in (
182
                    'OSFAMILY',
183
                    'USERS',
184
                    'ROOT_PARTITION',
185
                    'OS',
186
                    'DESCRIPTION'):
187
                self.assertTrue(interm in r['properties'])
188

    
189
    def test_register(self):
190
        """Test register"""
191
        self._prepare_img()
192
        self._test_register()
193

    
194
    def _test_register(self):
195
        self.assertTrue(self._imglist)
196
        for img in self._imglist.values():
197
            self.assertTrue(img is not None)
198
            r = set(self._imgdetails[img['name']].keys())
199
            self.assertTrue(r.issubset(IMGMETA.union(['properties'])))
200

    
201
    def test_unregister(self):
202
        """Test unregister"""
203
        self._prepare_img()
204
        self._test_unregister()
205

    
206
    def _test_unregister(self):
207
        try:
208
            for img in self._imglist.values():
209
                self.client.unregister(img['id'])
210
                self._prepare_img()
211
                break
212
        except ClientError as ce:
213
            if ce.status in (405,):
214
                print 'IMAGE UNREGISTER is not supported by server: %s' % ce
215
            else:
216
                raise
217

    
218
    def test_set_members(self):
219
        """Test set_members"""
220
        self._prepare_img()
221
        self._test_set_members()
222

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

    
230
    def test_list_members(self):
231
        """Test list_members"""
232
        self._test_list_members()
233

    
234
    def _test_list_members(self):
235
        self._test_set_members()
236

    
237
    def test_remove_members(self):
238
        """Test remove_members - NO CHECK"""
239
        self._prepare_img()
240
        self._test_remove_members()
241

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

    
254
    def test_list_shared(self):
255
        """Test list_shared - NOT CHECKED"""
256
        self._test_list_shared()
257

    
258
    def _test_list_shared(self):
259
        #No way to test this, if I dont have member images
260
        pass