File: test_legacy_shell.py

package info (click to toggle)
python-glanceclient 1%3A0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 680 kB
  • ctags: 987
  • sloc: python: 5,914; sh: 47; makefile: 42
file content (585 lines) | stat: -rw-r--r-- 18,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# Copyright 2013 OpenStack Foundation
# Copyright (C) 2013 Yahoo! Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
# vim: tabstop=4 shiftwidth=4 softtabstop=4

import mock
import testtools

from glanceclient import client
from glanceclient import exc
from glanceclient.v1 import legacy_shell as test_shell


class LegacyShellV1Test(testtools.TestCase):
    def test_print_image_formatted(self):

        class FakeClient():
            endpoint = 'http://is.invalid'

        class FakeImage():
            id = 1
            name = 'fake_image'
            is_public = False
            protected = False
            status = 'active'
            size = '1024'
            min_ram = 512
            min_disk = 10
            properties = {'a': 'b', 'c': 'd'}
            created_at = '04.03.2013'
            owner = 'test'
            updated_at = '04.03.2013'
            deleted_at = '04.03.2013'

        test_shell.print_image_formatted(FakeClient(), FakeImage())

    def test_print_image(self):
        class FakeImage():
            id = 1
            name = 'fake_image'
            is_public = False
            protected = False
            status = 'active'
            size = '1024'
            min_ram = 512
            min_disk = 10
            properties = {'a': 'b', 'c': 'd'}
            created_at = '04.03.2013'
            owner = 'test'
            updated_at = '04.03.2013'
            deleted_at = '04.03.2013'

        gc = client.Client('1', 'http://is.invalid:8080')
        test_shell.print_image_formatted(gc, FakeImage())

    def test_get_image_fields_from_args(self):
        args = ["field=name"]
        actual = test_shell.get_image_fields_from_args(args)
        self.assertEqual({'field': 'name'}, actual)

    def test_get_image_fields_from_args_exception_raises(self):
        args = {"filed": "name"}
        self.assertRaises(
            RuntimeError, test_shell.get_image_fields_from_args, args)

    def test_get_filters_from_args(self):
        args = ["filter=name"]
        actual = test_shell.get_image_filters_from_args(args)
        self.assertEqual({'property-filter': 'name'}, actual)

    def test_get_image_filters_from_args_exception_raises(self):
        args = {"filter": "name"}
        actual = test_shell.get_image_filters_from_args(args)
        self.assertEqual(1, actual)

    def test_do_add_error(self):
        class FakeClient():
            endpoint = 'http://is.invalid'

        class args:
            fields = 'name'

        actual = test_shell.do_add(FakeClient(), args)
        self.assertEqual(1, actual)

    def test_do_add(self):
        gc = client.Client('1', 'http://is.invalid')

        class FakeImage():
            fields = ['name=test',
                      'status=active',
                      'id=test',
                      'is_public=True',
                      'protected=False',
                      'min_disk=10',
                      'container_format=ovi',
                      'status=active']
            dry_run = True

        test_args = FakeImage()
        actual = test_shell.do_add(gc, test_args)
        self.assertEqual(0, actual)

    def test_do_add_with_image_meta(self):
        gc = client.Client('1', 'http://is.invalid')

        class FakeImage():
            fields = ['name=test',
                      'status=active',
                      'is_public=True',
                      'id=test',
                      'protected=False',
                      'min_disk=10',
                      'container_format=ovi',
                      'status=active',
                      'size=256',
                      'location=test',
                      'checksum=1024',
                      'owner=test_user']
            dry_run = True

        test_args = FakeImage()
        actual = test_shell.do_add(gc, test_args)
        self.assertEqual(0, actual)

    def test_do_add_without_dry_run(self):
        gc = client.Client('1', 'http://is.invalid')

        class FakeImage():
            fields = ['name=test',
                      'status=active',
                      'is_public=True',
                      'id=test',
                      'protected=False',
                      'min_disk=10',
                      'container_format=ovi',
                      'status=active',
                      'size=256',
                      'location=test',
                      'checksum=1024',
                      'owner=test_user']
            dry_run = False
            id = 'test'
            verbose = False

        test_args = FakeImage()
        with mock.patch.object(gc.images, 'create') as mocked_create:
            mocked_create.return_value = FakeImage()
            actual = test_shell.do_add(gc, test_args)
            self.assertEqual(0, actual)

    def test_do_clear_force_true_error(self):
        class FakeImage1():
            id = 1
            name = 'fake_image'
            is_public = False
            protected = False
            status = 'active'
            size = '1024'
            min_ram = 512
            min_disk = 10
            properties = {'a': 'b', 'c': 'd'}
            created_at = '04.03.2013'
            owner = 'test'
            updated_at = '04.03.2013'
            deleted_at = '04.03.2013'
            force = True
            verbose = True

        class FakeImages():
            def __init__(self):
                self.id = 'test'
                self.name = 'test_image_name'

            def list(self):
                self.list = [FakeImage1(), FakeImage1()]
                return self.list

        class FakeClient():
            def __init__(self):
                self.images = FakeImages()

        test_args = FakeImage1()
        actual = test_shell.do_clear(FakeClient(), test_args)
        self.assertEqual(1, actual)

    def test_do_clear_force_true(self):
        class FakeImage1():
            def __init__(self):
                self.id = 1
                self.name = 'fake_image'
                self.is_public = False
                self.protected = False
                self.status = 'active'
                self.size = '1024'
                self.min_ram = 512
                self.min_disk = 10
                self.properties = {'a': 'b', 'c': 'd'}
                self.created_at = '04.03.2013'
                self.owner = 'test'
                self.updated_at = '04.03.2013'
                self.deleted_at = '04.03.2013'
                self.force = True
                self.verbose = True

            def delete(self):
                pass

        class FakeImages():
            def __init__(self):
                self.id = 'test'
                self.name = 'test_image_name'

            def list(self):
                self.list = [FakeImage1(), FakeImage1()]
                return self.list

        class FakeClient():
            def __init__(self):
                self.images = FakeImages()

        test_args = FakeImage1()
        actual = test_shell.do_clear(FakeClient(), test_args)
        self.assertEqual(0, actual)

    def test_do_update_error(self):
        class FakeClient():
            endpoint = 'http://is.invalid'

        class Image():
            fields = ['id', 'is_public', 'name']

        args = Image()
        fake_client = FakeClient()
        actual = test_shell.do_update(fake_client, args)
        self.assertEqual(1, actual)

    def test_do_update_invalid_endpoint(self):
        class Image():
            fields = ['id=test_updated', 'is_public=True', 'name=new_name']
            dry_run = False
            id = 'test'

        args = Image()
        gc = client.Client('1', 'http://is.invalid')
        self.assertRaises(
            exc.InvalidEndpoint, test_shell.do_update, gc, args)

    def test_do_update(self):
        class Image():
            fields = ['id=test_updated',
                      'status=active',
                      'is_public=True',
                      'name=new_name',
                      'protected=False']
            dry_run = True
            id = 'test'

        args = Image()
        gc = client.Client('1', 'http://is.invalid')
        actual = test_shell.do_update(gc, args)
        self.assertEqual(0, actual)

    def test_do_update_dry_run_false(self):
        class Image():
            fields = ['id=test_updated',
                      'status=active',
                      'is_public=True',
                      'name=new_name',
                      'protected=False',
                      'is_public=True']
            dry_run = False
            id = 'test'
            verbose = True
            is_public = True
            protected = False
            status = 'active'
            size = 1024
            min_ram = 512
            min_disk = 512
            properties = {'property': 'test'}
            created_at = '12.09.2013'

        args = Image()
        gc = client.Client('1', 'http://is.invalid')
        with mock.patch.object(gc.images, 'update') as mocked_update:
            mocked_update.return_value = Image()
            actual = test_shell.do_update(gc, args)
            self.assertEqual(0, actual)

    def test_do_delete(self):
        class FakeImage1():
            def __init__(self):
                self.id = 1
                self.name = 'fake_image'
                self.is_public = False
                self.protected = False
                self.status = 'active'
                self.size = '1024'
                self.min_ram = 512
                self.min_disk = 10
                self.properties = {'a': 'b', 'c': 'd'}
                self.created_at = '04.03.2013'
                self.owner = 'test'
                self.updated_at = '04.03.2013'
                self.deleted_at = '04.03.2013'
                self.force = True
                self.verbose = True

            def delete(self):
                pass

            def get(self, id):
                return FakeImage1()

        class FakeClient():
            def __init__(self):
                self.images = FakeImage1()

        actual = test_shell.do_delete(FakeClient(), FakeImage1())

    def test_show(self):
        class Image():
            fields = ['id=test_updated',
                      'status=active',
                      'is_public=True',
                      'name=new_name',
                      'protected=False']
            id = 'test_show'
            name = 'fake_image'
            is_public = False
            protected = False
            status = 'active'
            size = '1024'
            min_ram = 512
            min_disk = 10
            properties = {'a': 'b', 'c': 'd'}
            created_at = '04.03.2013'
            owner = 'test'
            updated_at = '04.03.2013'

        gc = client.Client('1', 'http://is.invalid')
        with mock.patch.object(gc.images, 'get') as mocked_get:
            mocked_get.return_value = Image()
            actual = test_shell.do_show(gc, Image())
            self.assertEqual(0, actual)

    def test_index(self):
        class Image():
            id = 'test'
            filters = {}
            limit = 18
            marker = False
            sort_key = 'test'
            kwarg = 'name'
            sort_dir = 'test'
            name = 'test'
            disk_format = 'ovi'
            container_format = 'ovi'
            size = 1024

        args = Image()
        gc = client.Client('1', 'http://is.invalid')
        with mock.patch.object(gc.images, 'list') as mocked_list:
            mocked_list.return_value = [Image(), Image()]
            actual = test_shell.do_index(gc, args)

    def test_index_return_empty(self):
        class Image():
            id = 'test'
            filters = {}
            limit = 18
            marker = False
            sort_key = 'test'
            kwarg = 'name'
            sort_dir = 'test'
            name = 'test'
            disk_format = 'ovi'
            container_format = 'ovi'
            size = 1024

        args = Image()
        gc = client.Client('1', 'http://is.invalid')
        with mock.patch.object(test_shell, '_get_images') as mocked_get:
            mocked_get.return_value = False
            actual = test_shell.do_index(gc, args)
            self.assertEqual(0, actual)

    def test_do_details(self):
        class Image():
            id = 'test'
            filters = {}
            limit = 18
            marker = False
            sort_key = 'test'
            kwarg = 'name'
            sort_dir = 'test'
            name = 'test'
            disk_format = 'ovi'
            container_format = 'ovi'
            size = 1024
            is_public = True
            protected = False
            status = 'active'
            min_ram = 512
            min_disk = 512
            properties = {}
            created_at = '12.12.12'

        args = Image()
        gc = client.Client('1', 'http://is.invalid')
        with mock.patch.object(gc.images, 'list') as mocked_list:
            mocked_list.return_value = [Image(), Image()]
            actual = test_shell.do_details(gc, args)

    def test_do_image_members(self):
        class FakeImage1():
            def __init__(self):
                self.image_id = 1
                self.name = 'fake_image'
                self.is_public = False
                self.protected = False
                self.status = 'active'
                self.size = '1024'
                self.min_ram = 512
                self.min_disk = 10
                self.properties = {'a': 'b', 'c': 'd'}
                self.created_at = '04.03.2013'
                self.owner = 'test'
                self.updated_at = '04.03.2013'
                self.deleted_at = '04.03.2013'
                self.force = True
                self.verbose = True

            def delete(self):
                pass

            def get(self, id):
                return FakeImage1()

        class FakeClient():
            def __init__(self):
                self.image_members = ImageMembers()

        class ImageMembers():
            def __init__(self):
                self.member_id = 'test'
                self.can_share = True

            def list(self, image):
                return [ImageMembers(), ImageMembers()]

        actual = test_shell.do_image_members(FakeClient(), FakeImage1())

    def test_do_member_add_error(self):
        class FakeClient():
            def __init__(self):
                self.image_members = ImageMembers()

        class FakeImage1():
            def __init__(self):
                self.member_id = 'test'
                self.fields = ["name", "id", "filter"]
                self.dry_run = True
                self.image_id = 'fake_image_id'
                self.can_share = True

            def delete(self):
                pass

            def get(self, id):
                return FakeImage1()

        class ImageMembers():
            def __init__(self):
                self.member_id = 'test'
                self.can_share = True

            def list(self, image):
                return [ImageMembers(), ImageMembers()]

        actual = test_shell.do_member_add(FakeClient(), FakeImage1())

    def test_do_member_images_empty_result(self):
        class FakeImage1():
            def __init__(self):
                self.member_id = 'test'

        gc = client.Client('1', 'http://is.invalid')
        with mock.patch.object(gc.image_members, 'list') as mocked_list:
            mocked_list.return_value = []
            actual = test_shell.do_member_images(gc, FakeImage1())
            self.assertEqual(0, actual)

    def test_do_member_replace(self):
        class FakeClient():
            def __init__(self):
                self.image_members = ImageMembers()

        class ImageMembers():
            def __init__(self):
                self.member_id = 'test'
                self.can_share = True
                self.dry_run = True
                self.image_id = "fake_image_id"

            def list(self, image):
                return [ImageMembers(), ImageMembers()]

        actual = test_shell.do_member_add(FakeClient(), ImageMembers())

    def test_do_members_replace_dry_run_true(self):
        class Fake():
            def __init__(self):
                self.dry_run = True
                self.can_share = True
                self.image_id = 'fake_id'
                self.member_id = 'test'

        gc = client.Client('1', 'http://is.invalid')
        actual = test_shell.do_members_replace(gc, Fake())

    def test_do_members_replace_dry_run_false(self):
        class Fake():
            def __init__(self):
                self.dry_run = False
                self.can_share = True
                self.image_id = 'fake_id'
                self.member_id = 'test'

        gc = client.Client('1', 'http://is.invalid')

        with mock.patch.object(gc.image_members, 'list') as mocked_list:
            mocked_list.return_value = []
            with mock.patch.object(gc.image_members, 'create'):
                actual = test_shell.do_members_replace(gc, Fake())

    def test_do_member_images(self):
        class FakeClient():
            def __init__(self):
                self.image_members = ImageMembers()

        class ImageMembers():
            def __init__(self):
                self.member_id = 'test'
                self.can_share = True
                self.dry_run = True
                self.image_id = "fake_image_id"

            def list(self, member):
                return [ImageMembers(), ImageMembers()]

        actual = test_shell.do_member_images(FakeClient(), ImageMembers())

    def test_create_pretty_table(self):
        class MyPrettyTable(test_shell.PrettyTable):
            def __init__(self):
                self.columns = []

        # Test add column
        my_pretty_table = MyPrettyTable()
        my_pretty_table.add_column(1, label='test')

        # Test make header
        test_res = my_pretty_table.make_header()
        self.assertEqual('t\n-', test_res)

        # Test make row
        result = my_pretty_table.make_row('t')
        self.assertEqual("t", result)
        result = my_pretty_table._clip_and_justify(
            data='test', width=4, just=1)
        self.assertEqual("test", result)