Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / test / image.py @ e2c53122

History | View | Annotate | Download (11.1 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
from mock import patch
35
import time
36

    
37
from unittest import TestCase
38
from kamaki.clients import ClientError
39

    
40
example_images = [
41
    {
42
        "status": "available",
43
        "name": "Archlinux",
44
        "disk_format": "diskdump",
45
        "container_format": "bare",
46
        "id": "b4713f20-3a41-4eaf-81ae-88698c18b3e8",
47
        "size": 752782848},
48
    {
49
        "status": "available",
50
        "name": "Debian_Wheezy_Base",
51
        "disk_format": "diskdump",
52
        "container_format": "bare",
53
        "id": "1f8454f0-8e3e-4b6c-ab8e-5236b728dffe",
54
        "size": 795107328},
55
    {
56
        "status": "available",
57
        "name": "maelstrom",
58
        "disk_format": "diskdump",
59
        "container_format": "bare",
60
        "id": "0fb03e45-7d5a-4515-bd4e-e6bbf6457f06",
61
        "size": 2583195644},
62
    {
63
        "status": "available",
64
        "name": "Gardenia",
65
        "disk_format": "diskdump",
66
        "container_format": "bare",
67
        "id": "5963020b-ab74-4e11-bc59-90c494bbdedb",
68
        "size": 2589802496}]
69

    
70
example_images_detailed = [
71
    {
72
        "status": "available",
73
        "name": "Archlinux",
74
        "checksum": "1a126aad07475b43cc1959b446344211be13974",
75
        "created_at": "2013-01-28 22:44:54",
76
        "disk_format": "diskdump",
77
        "updated_at": "2013-01-28 22:44:55",
78
        "properties": {
79
            "partition_table": "msdos",
80
            "osfamily": "linux",
81
            "users": "root",
82
            "exclude_task_assignhostname": "yes",
83
            "os": "archlinux",
84
            "root_partition": "1",
85
            "description": "Archlinux base install 2012.12.01"},
86
        "location": "pithos://us3r-I6E-1d/images/archlinux.12.2012",
87
        "container_format": "bare",
88
        "owner": "user163@mail.example.com",
89
        "is_public": True,
90
        "deleted_at": "",
91
        "id": "b4713f20-3a41-4eaf-81ae-88698c18b3e8",
92
        "size": 752782848},
93
    {
94
        "status": "available",
95
        "name": "Debian_Wheezy_Base",
96
        "checksum": "8f96e73ba8886a05de6f9b3705c981",
97
        "created_at": "2013-01-29 16:41:13",
98
        "disk_format": "diskdump",
99
        "updated_at": "2013-01-29 16:41:14",
100
        "properties": {
101
            "partition_table": "msdos",
102
            "osfamily": "linux",
103
            "users": "root",
104
            "swap": "5:259",
105
            "os": "debian",
106
            "root_partition": "1",
107
            "description": "Debian7.0(Wheezy)Base"},
108
        "location": "pithos://us3r-EO2-1d/images/Deb_Whz201301291840.diskdump",
109
        "container_format": "bare",
110
        "owner": "user302@mail.example.com",
111
        "is_public": True,
112
        "deleted_at": "",
113
        "id": "1f8454f0-8e3e-4b6c-ab8e-5236b728dffe",
114
        "size": 795107328},
115
    {
116
        "status": "available",
117
        "name": "maelstrom",
118
        "checksum": "b202b8c7030cb22f896c6664ac",
119
        "created_at": "2013-02-13 10:07:42",
120
        "disk_format": "diskdump",
121
        "updated_at": "2013-02-13 10:07:44",
122
        "properties": {
123
            "partition_table": "msdos",
124
            "osfamily": "linux",
125
            "description": "Ubuntu 12.04.1 LTS",
126
            "os": "ubuntu",
127
            "root_partition": "1",
128
            "users": "user"},
129
        "location": "pithos://us3r-@r3n@-1d/images/mls-201302131203.diskdump",
130
        "container_format": "bare",
131
        "owner": "user3@mail.example.com",
132
        "is_public": True,
133
        "deleted_at": "",
134
        "id": "0fb03e45-7d5a-4515-bd4e-e6bbf6457f06",
135
        "size": 2583195648},
136
    {
137
        "status": "available",
138
        "name": "Gardenia",
139
        "checksum": "06d3099815d1f6fada91e80107638b882",
140
        "created_at": "2013-02-13 12:35:21",
141
        "disk_format": "diskdump",
142
        "updated_at": "2013-02-13 12:35:23",
143
        "properties": {
144
            "partition_table": "msdos",
145
            "osfamily": "linux",
146
            "description": "Ubuntu 12.04.2 LTS",
147
            "os": "ubuntu",
148
            "root_partition": "1",
149
            "users": "user"},
150
        "location": "pithos://us3r-E-1d/images/Gardenia-201302131431.diskdump",
151
        "container_format": "bare",
152
        "owner": "user3@mail.example.com",
153
        "is_public": True,
154
        "deleted_at": "",
155
        "id": "5963020b-ab74-4e11-bc59-90c494bbdedb",
156
        "size": 2589802496}]
157

    
158
class Image(TestCase):
159

    
160
    class FR(object):
161
        json = example_images
162
        headers = {}
163
        content = json
164
        status = None
165
        status_code = 200
166

    
167
    def setUp(self):
168
        self.now = time.mktime(time.gmtime())
169
        self.imgname = 'img_%s' % self.now
170
        self.url = 'http://image.example.com'
171
        self.token = 'an1m@g370k3n=='
172
        from kamaki.clients.image import ImageClient
173
        self.client = ImageClient(self.url, self.token)
174
        self.cyclades_url = 'http://cyclades.example.com'
175
        from kamaki.clients.cyclades import CycladesClient
176
        self.cyclades = CycladesClient(self.cyclades_url, self.token)
177
        from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
178
        self.C = KamakiHTTPConnection
179

    
180
    def tearDown(self):
181
        self.FR.json = example_images
182

    
183
    def assert_dicts_are_deeply_equal(self, d1, d2):
184
        for k, v in d1.items():
185
            self.assertTrue(k in d2)
186
            if isinstance(v, dict):
187
                self.assert_dicts_are_deeply_equal(v, d2[k])
188
            else:
189
                self.assertEqual(unicode(v), unicode(d2[k]))
190

    
191
    def test_list_public(self):
192
        with patch.object(
193
            self.C,
194
            'perform_request',
195
            return_value=self.FR()) as perform_req:
196
            r = self.client.list_public()
197
            self.assertEqual(self.client.http_client.url, self.url)
198
            self.assertEqual(self.client.http_client.path, '/images/')
199
            params = perform_req.call_args[0][3]
200
            self.assertEqual(params['sort_dir'], 'asc')
201
            for i in range(len(r)):
202
                self.assert_dicts_are_deeply_equal(r[i], example_images[i])
203

    
204
            r = self.client.list_public(order='-')
205
            params = perform_req.call_args[0][3]
206
            self.assertEqual(params['sort_dir'], 'desc')
207
            self.assertEqual(self.client.http_client.url, self.url)
208
            self.assertEqual(self.client.http_client.path, '/images/')
209

    
210
            self.FR.json = example_images_detailed
211
            r = self.client.list_public(detail=True)
212
            self.assertEqual(self.client.http_client.url, self.url)
213
            self.assertEqual(self.client.http_client.path, '/images/detail')
214
            for i in range(len(r)):
215
                self.assert_dicts_are_deeply_equal(
216
                    r[i],
217
                    example_images_detailed[i])
218

    
219
            size_max = 1000000000
220
            r = self.client.list_public(filters=dict(size_max=size_max))
221
            params = perform_req.call_args[0][3]
222
            self.assertEqual(params['size_max'], size_max)
223
            self.assertEqual(self.client.http_client.url, self.url)
224
            self.assertEqual(self.client.http_client.path, '/images/')
225

    
226
    def test_get_meta(self):
227
        img0 = example_images[0]
228
        self.FR.json = img0
229
        img0_headers = {}
230
        for k, v in example_images_detailed[0].items():
231
            img0_headers['x-image-meta-%s' % k] = v
232
        self.FR.headers = img0_headers
233
        with patch.object(self.C, 'perform_request', return_value=self.FR()):
234
            r = self.client.get_meta(img0['id'])
235
            self.assertEqual(self.client.http_client.url, self.url)
236
            expected_path = '/images/%s' % img0['id']
237
            self.assertEqual(self.client.http_client.path, expected_path)
238

    
239
            self.assertEqual(r['id'], img0['id'])
240
            self.assert_dicts_are_deeply_equal(r, example_images_detailed[0])
241

    
242
    """
243
    def test_register(self):
244
        ""Test register""
245
        self._prepare_img()
246
        self._test_register()
247

248
    def _test_register(self):
249
        self.assertTrue(self._imglist)
250
        for img in self._imglist.values():
251
            self.assertTrue(img is not None)
252

253
    def test_reregister(self):
254
        ""Test reregister""
255
        self._prepare_img()
256
        self._test_reregister()
257

258
    def _test_reregister(self):
259
        self.client.reregister(
260
            self.location,
261
            properties=dict(my_property='some_value'))
262

263
    def test_set_members(self):
264
        ""Test set_members""
265
        self._prepare_img()
266
        self._test_set_members()
267

268
    def _test_set_members(self):
269
        members = ['%s@fake.net' % self.now]
270
        for img in self._imglist.values():
271
            self.client.set_members(img['id'], members)
272
            r = self.client.list_members(img['id'])
273
            self.assertEqual(r[0]['member_id'], members[0])
274

275
    def test_list_members(self):
276
        ""Test list_members""
277
        self._test_list_members()
278

279
    def _test_list_members(self):
280
        self._test_set_members()
281

282
    def test_remove_members(self):
283
        ""Test remove_members - NO CHECK""
284
        self._prepare_img()
285
        self._test_remove_members()
286

287
    def _test_remove_members(self):
288
        return
289
        members = ['%s@fake.net' % self.now, '%s_v2@fake.net' % self.now]
290
        for img in self._imglist.values():
291
            self.client.set_members(img['id'], members)
292
            r = self.client.list_members(img['id'])
293
            self.assertTrue(len(r) > 1)
294
            self.client.remove_member(img['id'], members[0])
295
            r0 = self.client.list_members(img['id'])
296
            self.assertEqual(len(r), 1 + len(r0))
297
            self.assertEqual(r0[0]['member_id'], members[1])
298

299
    def test_list_shared(self):
300
        ""Test list_shared - NOT CHECKED""
301
        self._test_list_shared()
302

303
    def _test_list_shared(self):
304
        #No way to test this, if I dont have member images
305
        pass
306
    """