Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / livetest / image.py @ fe5173b9

History | View | Annotate | Download (8.6 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
class Image(livetest.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
        (token, uuid) = (self['token'], self['store', 'account'])
60
        if not uuid:
61
            from kamaki.clients.astakos import AstakosClient
62
            uuid = AstakosClient(self['astakos', 'url'], token).term('uuid')
63
        from kamaki.clients.pithos import PithosClient
64
        self.pithcli = PithosClient(self['store', 'url'], token, uuid)
65
        cont = 'cont_%s' % self.now
66
        self.pithcli.container = cont
67
        self.obj = 'obj_%s' % self.now
68
        print('\t- Create container %s on Pithos server' % cont)
69
        self.pithcli.container_put()
70
        self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
71
        print('\t- Upload an image at %s...\n' % self.location)
72
        self.pithcli.upload_object(self.obj, f)
73
        print('\t- ok')
74
        f.close()
75

    
76
        self.client.register(
77
            self.imgname,
78
            self.location,
79
            params=dict(is_public=True))
80
        img = self._get_img_by_name(self.imgname)
81
        self._imglist[self.imgname] = img
82

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

    
95
    def _get_img_by_name(self, name):
96
        r = self.cyclades.list_images()
97
        for img in r:
98
            if img['name'] == name:
99
                return img
100
        return None
101

    
102
    def assert_dicts_are_deeply_equal(self, d1, d2):
103
        for k, v in d1.items():
104
            self.assertTrue(k in d2)
105
            if isinstance(v, dict):
106
                self.assert_dicts_are_deeply_equal(v, d2[k])
107
            else:
108
                self.assertEqual(unicode(v), unicode(d2[k]))
109

    
110
    def test_list_public(self):
111
        """Test list_public"""
112
        self._test_list_public()
113

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

    
163
    def test_get_meta(self):
164
        """Test get_meta"""
165
        self._test_get_meta()
166

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

    
194
    def test_register(self):
195
        """Test register"""
196
        self._prepare_img()
197
        self._test_register()
198

    
199
    def _test_register(self):
200
        self.assertTrue(self._imglist)
201
        for img in self._imglist.values():
202
            self.assertTrue(img is not None)
203

    
204
    def test_set_members(self):
205
        """Test set_members"""
206
        self._prepare_img()
207
        self._test_set_members()
208

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

    
216
    def test_list_members(self):
217
        """Test list_members"""
218
        self._test_list_members()
219

    
220
    def _test_list_members(self):
221
        self._test_set_members()
222

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

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

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

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