|
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 _prepare_img(self):
|
|
54 |
f = open(self['image', 'local_path'], 'rb')
|
|
55 |
uuid = self['store', 'account']
|
|
56 |
from kamaki.clients.pithos import PithosClient
|
|
57 |
self.pithcli = PithosClient(self['store', 'url'], self['token'], uuid)
|
|
58 |
cont = 'cont_%s' % self.now
|
|
59 |
self.pithcli.container = cont
|
|
60 |
self.obj = 'obj_%s' % self.now
|
|
61 |
print('\t- Create container %s on Pithos server' % cont)
|
|
62 |
self.pithcli.container_put()
|
|
63 |
self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
|
|
64 |
print('\t- Upload an image at %s...' % self.location)
|
|
65 |
self.pithcli.upload_object(self.obj, f)
|
|
66 |
print('\t- ok')
|
|
67 |
f.close()
|
|
68 |
|
|
69 |
self.client.register(self.imgname,
|
|
70 |
self.location,
|
|
71 |
params=dict(is_public=True))
|
|
72 |
img = self._get_img_by_name(self.imgname)
|
|
73 |
self._imglist[self.imgname] = img
|
|
74 |
|
|
75 |
def tearDown(self):
|
|
76 |
for img in self._imglist.values():
|
|
77 |
print('\tDeleting image %s' % img['id'])
|
|
78 |
self.cyclades.delete_image(img['id'])
|
|
79 |
if hasattr(self, 'pithcli'):
|
|
80 |
print('\tDeleting container %s' % self.pithcli.container)
|
|
81 |
try:
|
|
82 |
self.pithcli.del_container(delimiter='/')
|
|
83 |
self.pithcli.purge_container()
|
|
84 |
except ClientError:
|
|
85 |
pass
|
|
86 |
|
|
87 |
def _get_img_by_name(self, name):
|
|
88 |
r = self.cyclades.list_images()
|
|
89 |
for img in r:
|
|
90 |
if img['name'] == name:
|
|
91 |
return img
|
|
92 |
return None
|
|
93 |
|
|
94 |
def assert_dicts_are_deeply_equal(self, d1, d2):
|
|
95 |
for k, v in d1.items():
|
|
96 |
self.assertTrue(k in d2)
|
|
97 |
if isinstance(v, dict):
|
|
98 |
self.assert_dicts_are_deeply_equal(v, d2[k])
|
|
99 |
else:
|
|
100 |
self.assertEqual(unicode(v), unicode(d2[k]))
|
|
101 |
|
|
102 |
def test_list_public(self):
|
|
103 |
"""Test list_public"""
|
|
104 |
r = self.client.list_public()
|
|
105 |
r0 = self.client.list_public(order='-')
|
|
106 |
self.assertTrue(len(r) > 0)
|
|
107 |
for img in r:
|
|
108 |
for term in ('status',
|
|
109 |
'name',
|
|
110 |
'container_format',
|
|
111 |
'disk_format',
|
|
112 |
'id',
|
|
113 |
'size'):
|
|
114 |
self.assertTrue(term in img)
|
|
115 |
self.assertTrue(len(r), len(r0))
|
|
116 |
r0.reverse()
|
|
117 |
for i, img in enumerate(r):
|
|
118 |
self.assert_dicts_are_deeply_equal(img, r0[i])
|
|
119 |
r1 = self.client.list_public(detail=True)
|
|
120 |
for img in r1:
|
|
121 |
for term in ('status',
|
|
122 |
'name',
|
|
123 |
'checksum',
|
|
124 |
'created_at',
|
|
125 |
'disk_format',
|
|
126 |
'updated_at',
|
|
127 |
'id',
|
|
128 |
'location',
|
|
129 |
'container_format',
|
|
130 |
'owner',
|
|
131 |
'is_public',
|
|
132 |
'deleted_at',
|
|
133 |
'properties',
|
|
134 |
'size'):
|
|
135 |
self.assertTrue(term in img)
|
|
136 |
for interm in (
|
|
137 |
'osfamily',
|
|
138 |
'users',
|
|
139 |
'os',
|
|
140 |
'root_partition',
|
|
141 |
'description'):
|
|
142 |
self.assertTrue(interm in img['properties'])
|
|
143 |
size_max = 1000000000
|
|
144 |
r2 = self.client.list_public(filters=dict(size_max=size_max))
|
|
145 |
self.assertTrue(len(r2) <= len(r))
|
|
146 |
for img in r2:
|
|
147 |
self.assertTrue(int(img['size']) <= size_max)
|
|
148 |
|
|
149 |
def test_get_meta(self):
|
|
150 |
"""Test get_meta"""
|
|
151 |
r = self.client.get_meta(self['image', 'id'])
|
|
152 |
self.assertEqual(r['id'], self['image', 'id'])
|
|
153 |
for term in ('status',
|
|
154 |
'name',
|
|
155 |
'checksum',
|
|
156 |
'updated-at',
|
|
157 |
'created-at',
|
|
158 |
'deleted-at',
|
|
159 |
'location',
|
|
160 |
'is-public',
|
|
161 |
'owner',
|
|
162 |
'disk-format',
|
|
163 |
'size',
|
|
164 |
'container-format'):
|
|
165 |
self.assertTrue(term in r)
|
|
166 |
for interm in ('kernel',
|
|
167 |
'osfamily',
|
|
168 |
'users',
|
|
169 |
'gui', 'sortorder',
|
|
170 |
'root-partition',
|
|
171 |
'os',
|
|
172 |
'description'):
|
|
173 |
self.assertTrue(interm in r['properties'])
|
|
174 |
|
|
175 |
def test_register(self):
|
|
176 |
"""Test register"""
|
|
177 |
self._prepare_img()
|
|
178 |
self.assertTrue(len(self._imglist) > 0)
|
|
179 |
for img in self._imglist.values():
|
|
180 |
self.assertTrue(img != None)
|
|
181 |
|
|
182 |
def test_reregister(self):
|
|
183 |
"""Test reregister"""
|
|
184 |
self._prepare_img()
|
|
185 |
self.client.reregister(
|
|
186 |
self.location,
|
|
187 |
properties=dict(my_property='some_value'))
|
|
188 |
|
|
189 |
def test_set_members(self):
|
|
190 |
"""Test set_members"""
|
|
191 |
self._prepare_img()
|
|
192 |
members = ['%s@fake.net' % self.now]
|
|
193 |
for img in self._imglist.values():
|
|
194 |
self.client.set_members(img['id'], members)
|
|
195 |
r = self.client.list_members(img['id'])
|
|
196 |
self.assertEqual(r[0]['member_id'], members[0])
|
|
197 |
|
|
198 |
def test_list_members(self):
|
|
199 |
"""Test list_members"""
|
|
200 |
self.test_set_members()
|
|
201 |
|
|
202 |
def test_remove_members(self):
|
|
203 |
"""Test remove_members - NO CHECK"""
|
|
204 |
return
|
|
205 |
self._prepare_img()
|
|
206 |
members = ['%s@fake.net' % self.now, '%s_v2@fake.net' % self.now]
|
|
207 |
for img in self._imglist.values():
|
|
208 |
self.client.set_members(img['id'], members)
|
|
209 |
r = self.client.list_members(img['id'])
|
|
210 |
self.assertTrue(len(r) > 1)
|
|
211 |
self.client.remove_member(img['id'], members[0])
|
|
212 |
r0 = self.client.list_members(img['id'])
|
|
213 |
self.assertEqual(len(r), 1 + len(r0))
|
|
214 |
self.assertEqual(r0[0]['member_id'], members[1])
|
|
215 |
|
|
216 |
def test_list_shared(self):
|
|
217 |
"""Test list_shared - NOT CHECKED"""
|
|
218 |
#No way to test this, if I dont have member images
|
|
219 |
pass
|