Debian Codegraph @

glanceclient/tests/unit/v2/test_images.py

raw symbols SCIP
1 # Copyright 2012 OpenStack Foundation.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import errno
17 import hashlib
18 import testtools
19 from unittest import mock
20
21 import ddt
22
23 from glanceclient.common import utils as common_utils
24 from glanceclient import exc
25 from glanceclient.tests.unit.v2 import base
26 from glanceclient.tests import utils
27 from glanceclient.v2 import images
28
29 _CHKSUM = '93264c3edf5972c9f1cb309543d38a5c'
30 _CHKSUM1 = '54264c3edf5972c9f1cb309453d38a46'
31
32 _HASHVAL = '54264c3edf93264c3edf5972c9f1cb309543d38a5c5972c9f1cb309453d38a46'
33 _HASHVAL1 = 'cb309543d38a5c5972c9f1cb309453d38a4654264c3edf93264c3edf5972c9f1'
34 _HASHBAD = '93264c3edf597254264c3edf5972c9f1cb309453d38a46c9f1cb309543d38a5c'
35
36 _TAG1 = 'power'
37 _TAG2 = '64bit'
38
39 _BOGUS_ID = '63e7f218-29de-4477-abdc-8db7c9533188'
40 _EVERYTHING_ID = '802cbbb7-0379-4c38-853f-37302b5e3d29'
41 _OWNED_IMAGE_ID = 'a4963502-acc7-42ba-ad60-5aa0962b7faf'
42 _OWNER_ID = '6bd473f0-79ae-40ad-a927-e07ec37b642f'
43 _PRIVATE_ID = 'e33560a7-3964-4de5-8339-5a24559f99ab'
44 _PUBLIC_ID = '857806e7-05b6-48e0-9d40-cb0e6fb727b9'
45 _SHARED_ID = '331ac905-2a38-44c5-a83d-653db8f08313'
46 _COMMUNITY_ID = '609ec9fc-0ee4-44c4-854d-0480af576929'
47 _STATUS_REJECTED_ID = 'f3ea56ff-d7e4-4451-998c-1e3d33539c8e'
48
49 data_fixtures = {
50 '/v2/schemas/image': {
51 'GET': (
52 {},
53 {
54 'name': 'image',
55 'properties': {
56 'id': {},
57 'name': {},
58 'locations': {
59 'type': 'array',
60 'items': {
61 'type': 'object',
62 'properties': {
63 'metadata': {'type': 'object'},
64 'url': {'type': 'string'},
65 },
66 'required': ['url', 'metadata'],
67 },
68 },
69 'color': {'type': 'string', 'is_base': False},
70 },
71 'additionalProperties': {'type': 'string'},
72 },
73 ),
74 },
75 '/v2/images?limit=%d' % images.DEFAULT_PAGE_SIZE: {
76 'GET': (
77 {},
78 {'images': [
79 {
80 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
81 'name': 'image-1',
82 },
83 {
84 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
85 'name': 'image-2',
86 },
87 ]},
88 ),
89 },
90 '/v2/images?limit=2': {
91 'GET': (
92 {},
93 {
94 'images': [
95 {
96 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
97 'name': 'image-1',
98 },
99 {
100 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
101 'name': 'image-2',
102 },
103 ],
104 'next': ('/v2/images?limit=2&'
105 'marker=6f99bf80-2ee6-47cf-acfe-1f1fabb7e810'),
106 },
107 ),
108 },
109 '/v2/images?limit=1': {
110 'GET': (
111 {},
112 {
113 'images': [
114 {
115 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
116 'name': 'image-1',
117 },
118 ],
119 'next': ('/v2/images?limit=1&'
120 'marker=3a4560a1-e585-443e-9b39-553b46ec92d1'),
121 },
122 ),
123 },
124 ('/v2/images?limit=1&marker=3a4560a1-e585-443e-9b39-553b46ec92d1'): {
125 'GET': (
126 {},
127 {'images': [
128 {
129 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
130 'name': 'image-2',
131 },
132 ]},
133 ),
134 },
135 ('/v2/images?limit=1&marker=6f99bf80-2ee6-47cf-acfe-1f1fabb7e810'): {
136 'GET': (
137 {},
138 {'images': [
139 {
140 'id': '3f99bf80-2ee6-47cf-acfe-1f1fabb7e811',
141 'name': 'image-3',
142 },
143 ]},
144 ),
145 },
146 '/v2/images/3a4560a1-e585-443e-9b39-553b46ec92d1': {
147 'GET': (
148 {},
149 {
150 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
151 'name': 'image-1',
152 },
153 ),
154 'PATCH': (
155 {},
156 '',
157 ),
158 },
159 '/v2/images/e7e59ff6-fa2e-4075-87d3-1a1398a07dc3': {
160 'GET': (
161 {},
162 {
163 'id': 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3',
164 'name': 'image-3',
165 'barney': 'rubble',
166 'george': 'jetson',
167 'color': 'red',
168 },
169 ),
170 'PATCH': (
171 {},
172 '',
173 ),
174 },
175 '/v2/images': {
176 'POST': (
177 {},
178 {
179 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
180 'name': 'image-1',
181 },
182 ),
183 },
184 '/v2/images/87b634c1-f893-33c9-28a9-e5673c99239a': {
185 'DELETE': (
186 {},
187 {
188 'id': '87b634c1-f893-33c9-28a9-e5673c99239a',
189 },
190 ),
191 },
192 '/v2/images/606b0e88-7c5a-4d54-b5bb-046105d4de6f/file': {
193 'PUT': (
194 {},
195 '',
196 ),
197 },
198 '/v2/images/606b0e88-7c5a-4d54-b5bb-046105d4de6f/stage': {
199 'PUT': (
200 {},
201 '',
202 ),
203 },
204 '/v2/images/5cc4bebc-db27-11e1-a1eb-080027cbe205/file': {
205 'GET': (
206 {},
207 'A',
208 ),
209 },
210 '/v2/images/5cc4bebc-db27-11e1-a1eb-080027cbe205': {
211 'GET': (
212 {},
213 {},
214 ),
215 },
216 '/v2/images/headeronly-db27-11e1-a1eb-080027cbe205/file': {
217 'GET': (
218 {
219 'content-md5': 'wrong'
220 },
221 'BB',
222 ),
223 },
224 '/v2/images/headeronly-db27-11e1-a1eb-080027cbe205': {
225 'GET': (
226 {},
227 {},
228 ),
229 },
230 '/v2/images/chkonly-db27-11e1-a1eb-080027cbe205/file': {
231 'GET': (
232 {
233 'content-md5': 'wrong'
234 },
235 'BB',
236 ),
237 },
238 '/v2/images/chkonly-db27-11e1-a1eb-080027cbe205': {
239 'GET': (
240 {},
241 {
242 'checksum': 'wrong',
243 },
244 ),
245 },
246 '/v2/images/multihash-db27-11e1-a1eb-080027cbe205/file': {
247 'GET': (
248 {
249 'content-md5': 'wrong'
250 },
251 'BB',
252 ),
253 },
254 '/v2/images/multihash-db27-11e1-a1eb-080027cbe205': {
255 'GET': (
256 {},
257 {
258 'checksum': 'wrong',
259 'os_hash_algo': 'md5',
260 'os_hash_value': 'junk'
261 },
262 ),
263 },
264 '/v2/images/badalgo-db27-11e1-a1eb-080027cbe205/file': {
265 'GET': (
266 {
267 'content-md5': hashlib.md5(b'BB').hexdigest()
268 },
269 'BB',
270 ),
271 },
272 '/v2/images/badalgo-db27-11e1-a1eb-080027cbe205': {
273 'GET': (
274 {},
275 {
276 'checksum': hashlib.md5(b'BB').hexdigest(),
277 'os_hash_algo': 'not_an_algo',
278 'os_hash_value': 'whatever'
279 },
280 ),
281 },
282 '/v2/images/bad-multihash-value-good-checksum/file': {
283 'GET': (
284 {
285 'content-md5': hashlib.md5(b'GOODCHECKSUM').hexdigest()
286 },
287 'GOODCHECKSUM',
288 ),
289 },
290 '/v2/images/bad-multihash-value-good-checksum': {
291 'GET': (
292 {},
293 {
294 'checksum': hashlib.md5(b'GOODCHECKSUM').hexdigest(),
295 'os_hash_algo': 'sha512',
296 'os_hash_value': 'badmultihashvalue'
297 },
298 ),
299 },
300 '/v2/images/headeronly-dd57-11e1-af0f-02163e68b1d8/file': {
301 'GET': (
302 {
303 'content-md5': 'defb99e69a9f1f6e06f15006b1f166ae'
304 },
305 'CCC',
306 ),
307 },
308 '/v2/images/headeronly-dd57-11e1-af0f-02163e68b1d8': {
309 'GET': (
310 {},
311 {},
312 ),
313 },
314 '/v2/images/chkonly-dd57-11e1-af0f-02163e68b1d8/file': {
315 'GET': (
316 {
317 'content-md5': 'defb99e69a9f1f6e06f15006b1f166ae'
318 },
319 'CCC',
320 ),
321 },
322 '/v2/images/chkonly-dd57-11e1-af0f-02163e68b1d8': {
323 'GET': (
324 {},
325 {
326 'checksum': 'defb99e69a9f1f6e06f15006b1f166ae',
327 },
328 ),
329 },
330 '/v2/images/multihash-dd57-11e1-af0f-02163e68b1d8/file': {
331 'GET': (
332 {
333 'content-md5': 'defb99e69a9f1f6e06f15006b1f166ae'
334 },
335 'CCC',
336 ),
337 },
338 '/v2/images/multihash-dd57-11e1-af0f-02163e68b1d8': {
339 'GET': (
340 {},
341 {
342 'checksum': 'defb99e69a9f1f6e06f15006b1f166ae',
343 'os_hash_algo': 'sha384',
344 'os_hash_value': hashlib.sha384(b'CCC').hexdigest()
345 },
346 ),
347 },
348 '/v2/images/87b634c1-f893-33c9-28a9-e5673c99239a/actions/reactivate': {
349 'POST': ({}, None)
350 },
351 '/v2/images/87b634c1-f893-33c9-28a9-e5673c99239a/actions/deactivate': {
352 'POST': ({}, None)
353 },
354 '/v2/images/606b0e88-7c5a-4d54-b5bb-046105d4de6f/import': {
355 'POST': ({}, None)
356 },
357 '/v2/images?limit=%d&visibility=public' % images.DEFAULT_PAGE_SIZE: {
358 'GET': (
359 {},
360 {'images': [
361 {
362 'id': _PUBLIC_ID,
363 'harvey': 'lipshitz',
364 },
365 ]},
366 ),
367 },
368 '/v2/images?limit=%d&visibility=private' % images.DEFAULT_PAGE_SIZE: {
369 'GET': (
370 {},
371 {'images': [
372 {
373 'id': _PRIVATE_ID,
374 },
375 ]},
376 ),
377 },
378 '/v2/images?limit=%d&visibility=shared' % images.DEFAULT_PAGE_SIZE: {
379 'GET': (
380 {},
381 {'images': [
382 {
383 'id': _SHARED_ID,
384 },
385 ]},
386 ),
387 },
388 '/v2/images?limit=%d&visibility=community' % images.DEFAULT_PAGE_SIZE: {
389 'GET': (
390 {},
391 {'images': [
392 {
393 'id': _COMMUNITY_ID,
394 },
395 ]},
396 ),
397 },
398 '/v2/images?limit=%d&member_status=rejected' % images.DEFAULT_PAGE_SIZE: {
399 'GET': (
400 {},
401 {'images': [
402 {
403 'id': _STATUS_REJECTED_ID,
404 },
405 ]},
406 ),
407 },
408 '/v2/images?limit=%d&member_status=pending' % images.DEFAULT_PAGE_SIZE: {
409 'GET': (
410 {},
411 {'images': []},
412 ),
413 },
414 '/v2/images?limit=%d&owner=%s' % (images.DEFAULT_PAGE_SIZE, _OWNER_ID): {
415 'GET': (
416 {},
417 {'images': [
418 {
419 'id': _OWNED_IMAGE_ID,
420 },
421 ]},
422 ),
423 },
424 '/v2/images?limit=%d&owner=%s' % (images.DEFAULT_PAGE_SIZE, _BOGUS_ID): {
425 'GET': (
426 {},
427 {'images': []},
428 ),
429 },
430 '/v2/images?limit=%d&member_status=pending&owner=%s&visibility=shared'
431 % (images.DEFAULT_PAGE_SIZE, _BOGUS_ID): {
432 'GET': (
433 {},
434 {'images': [
435 {
436 'id': _EVERYTHING_ID,
437 },
438 ]},
439 ),
440 },
441 '/v2/images?checksum=%s&limit=%d' % (_CHKSUM, images.DEFAULT_PAGE_SIZE): {
442 'GET': (
443 {},
444 {'images': [
445 {
446 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
447 'name': 'image-1',
448 }
449 ]},
450 ),
451 },
452 '/v2/images?checksum=%s&limit=%d' % (_CHKSUM1, images.DEFAULT_PAGE_SIZE): {
453 'GET': (
454 {},
455 {'images': [
456 {
457 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
458 'name': 'image-1',
459 },
460 {
461 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
462 'name': 'image-2',
463 },
464 ]},
465 ),
466 },
467 '/v2/images?checksum=wrong&limit=%d' % images.DEFAULT_PAGE_SIZE: {
468 'GET': (
469 {},
470 {'images': []},
471 ),
472 },
473 '/v2/images?limit=%d&os_hash_value=%s' % (images.DEFAULT_PAGE_SIZE,
474 _HASHVAL): {
475 'GET': (
476 {},
477 {'images': [
478 {
479 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
480 'name': 'image-1',
481 }
482 ]},
483 ),
484 },
485 '/v2/images?limit=%d&os_hash_value=%s' % (images.DEFAULT_PAGE_SIZE,
486 _HASHVAL1): {
487 'GET': (
488 {},
489 {'images': [
490 {
491 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
492 'name': 'image-1',
493 },
494 {
495 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
496 'name': 'image-2',
497 },
498 ]},
499 ),
500 },
501 '/v2/images?limit=%d&os_hash_value=%s' % (images.DEFAULT_PAGE_SIZE,
502 _HASHBAD): {
503 'GET': (
504 {},
505 {'images': []},
506 ),
507 },
508 '/v2/images?limit=%d&tag=%s' % (images.DEFAULT_PAGE_SIZE, _TAG1): {
509 'GET': (
510 {},
511 {'images': [
512 {
513 'id': '3a4560a1-e585-443e-9b39-553b46ec92d1',
514 'name': 'image-1',
515 }
516 ]},
517 ),
518 },
519 '/v2/images?limit=%d&tag=%s' % (images.DEFAULT_PAGE_SIZE, _TAG2): {
520 'GET': (
521 {},
522 {'images': [
523 {
524 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
525 'name': 'image-1',
526 },
527 {
528 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
529 'name': 'image-2',
530 },
531 ]},
532 ),
533 },
534 '/v2/images?limit=%d&tag=%s&tag=%s' % (images.DEFAULT_PAGE_SIZE,
535 _TAG1, _TAG2):
536 {
537 'GET': (
538 {},
539 {'images': [
540 {
541 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
542 'name': 'image-1',
543 }
544 ]},
545 ),
546 },
547 '/v2/images?limit=%d&tag=fake' % images.DEFAULT_PAGE_SIZE: {
548 'GET': (
549 {},
550 {'images': []},
551 ),
552 },
553 '/v2/images/a2b83adc-888e-11e3-8872-78acc0b951d8': {
554 'GET': (
555 {},
556 {
557 'id': 'a2b83adc-888e-11e3-8872-78acc0b951d8',
558 'name': 'image-location-tests',
559 'locations': [{'url': 'http://foo.com/',
560 'metadata': {'foo': 'foometa'}},
561 {'url': 'http://bar.com/',
562 'metadata': {'bar': 'barmeta'}}],
563 },
564 ),
565 'PATCH': (
566 {},
567 '',
568 )
569 },
570 '/v2/images?limit=%d&os_distro=NixOS' % images.DEFAULT_PAGE_SIZE: {
571 'GET': (
572 {},
573 {'images': [
574 {
575 'id': '8b052954-c76c-4e02-8e90-be89a70183a8',
576 'name': 'image-5',
577 'os_distro': 'NixOS',
578 },
579 ]},
580 ),
581 },
582 '/v2/images?limit=%d&my_little_property=cant_be_this_cute' %
583 images.DEFAULT_PAGE_SIZE: {
584 'GET': (
585 {},
586 {'images': []},
587 ),
588 },
589 '/v2/images?limit=%d&sort_key=name' % images.DEFAULT_PAGE_SIZE: {
590 'GET': (
591 {},
592 {'images': [
593 {
594 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
595 'name': 'image-1',
596 },
597 {
598 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
599 'name': 'image-2',
600 },
601 ]},
602 ),
603 },
604 '/v2/images?limit=%d&sort_key=name&sort_key=id'
605 % images.DEFAULT_PAGE_SIZE: {
606 'GET': (
607 {},
608 {'images': [
609 {
610 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
611 'name': 'image',
612 },
613 {
614 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
615 'name': 'image',
616 },
617 ]},
618 ),
619 },
620 '/v2/images?limit=%d&sort_dir=desc&sort_key=id'
621 % images.DEFAULT_PAGE_SIZE: {
622 'GET': (
623 {},
624 {'images': [
625 {
626 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
627 'name': 'image-2',
628 },
629 {
630 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
631 'name': 'image-1',
632 },
633 ]},
634 ),
635 },
636 '/v2/images?limit=%d&sort_dir=desc&sort_key=name&sort_key=id'
637 % images.DEFAULT_PAGE_SIZE: {
638 'GET': (
639 {},
640 {'images': [
641 {
642 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
643 'name': 'image-2',
644 },
645 {
646 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
647 'name': 'image-1',
648 },
649 ]},
650 ),
651 },
652 '/v2/images?limit=%d&sort_dir=desc&sort_dir=asc&sort_key=name&sort_key=id'
653 % images.DEFAULT_PAGE_SIZE: {
654 'GET': (
655 {},
656 {'images': [
657 {
658 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
659 'name': 'image-2',
660 },
661 {
662 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
663 'name': 'image-1',
664 },
665 ]},
666 ),
667 },
668 '/v2/images?limit=%d&sort=name%%3Adesc%%2Csize%%3Aasc'
669 % images.DEFAULT_PAGE_SIZE: {
670 'GET': (
671 {},
672 {'images': [
673 {
674 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
675 'name': 'image-2',
676 },
677 {
678 'id': '2a4560b2-e585-443e-9b39-553b46ec92d1',
679 'name': 'image-1',
680 },
681 ]},
682 ),
683 },
684 '/v2/images/3a4560a1-e585-443e-9b39-553b46ec92d1/tasks': {
685 'GET': (
686 {},
687 {'tasks': [
688 {
689 'id': '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810',
690 'status': 'succeed',
691 'message': 'Copied 44 MiB',
692 'updated_at': '2021-03-01T18:28:26.000000'
693 }
694 ]},
695 ),
696 },
697 '/v2/images/3a4560a1-e585-443e-9b39-553b46ec92d1/locations': {
698 'POST': ({}, '')
699 },
700 '/v2/images/a2b83adc-888e-11e3-8872-78acc0b951d8/locations': {
701 'GET': (
702 {},
703 [{'url': 'http://foo.com/',
704 'metadata': {'store': 'cheap'}},
705 {'url': 'http://bar.com/',
706 'metadata': {'store': 'fast'}}], ''
707 ),
708 },
709 }
710
711 schema_fixtures = {
712 'image': {
713 'GET': (
714 {},
715 {
716 'name': 'image',
717 'properties': {
718 'id': {},
719 'name': {},
720 'locations': {
721 'type': 'array',
722 'items': {
723 'type': 'object',
724 'properties': {
725 'metadata': {'type': 'object'},
726 'url': {'type': 'string'},
727 },
728 'required': ['url', 'metadata'],
729 }
730 },
731 'color': {'type': 'string', 'is_base': False},
732 'tags': {'type': 'array'},
733 },
734 'additionalProperties': {'type': 'string'},
735 }
736 )
737 }
738 }
739
740
741 @ddt.ddt
742 class TestController(testtools.TestCase):
743 def setUp(self):
744 super(TestController, self).setUp()
745 self.api = utils.FakeAPI(data_fixtures)
746 self.schema_api = utils.FakeSchemaAPI(schema_fixtures)
747 self.controller = base.BaseController(self.api, self.schema_api,
748 images.Controller)
749
750 def test_image_tasks_supported(self):
751 with mock.patch.object(common_utils,
752 'has_version') as mock_has_version:
753 mock_has_version.return_value = True
754 image_tasks = self.controller.get_associated_image_tasks(
755 '3a4560a1-e585-443e-9b39-553b46ec92d1')
756 self.assertEqual(1, len(image_tasks['tasks']))
757
758 def test_image_tasks_not_supported(self):
759 with mock.patch.object(common_utils,
760 'has_version') as mock_has_version:
761 mock_has_version.return_value = False
762 self.assertRaises(exc.HTTPNotImplemented,
763 self.controller.get_associated_image_tasks,
764 '3a4560a1-e585-443e-9b39-553b46ec92d1')
765
766 def test_list_images(self):
767 images = self.controller.list()
768 self.assertEqual('3a4560a1-e585-443e-9b39-553b46ec92d1', images[0].id)
769 self.assertEqual('image-1', images[0].name)
770 self.assertEqual('6f99bf80-2ee6-47cf-acfe-1f1fabb7e810', images[1].id)
771 self.assertEqual('image-2', images[1].name)
772
773 def test_list_images_paginated(self):
774 images = self.controller.list(page_size=1)
775 self.assertEqual('3a4560a1-e585-443e-9b39-553b46ec92d1', images[0].id)
776 self.assertEqual('image-1', images[0].name)
777 self.assertEqual('6f99bf80-2ee6-47cf-acfe-1f1fabb7e810', images[1].id)
778 self.assertEqual('image-2', images[1].name)
779
780 def test_list_images_paginated_with_limit(self):
781 images = self.controller.list(limit=3, page_size=2)
782 self.assertEqual('3a4560a1-e585-443e-9b39-553b46ec92d1', images[0].id)
783 self.assertEqual('image-1', images[0].name)
784 self.assertEqual('6f99bf80-2ee6-47cf-acfe-1f1fabb7e810', images[1].id)
785 self.assertEqual('image-2', images[1].name)
786 self.assertEqual('3f99bf80-2ee6-47cf-acfe-1f1fabb7e811', images[2].id)
787 self.assertEqual('image-3', images[2].name)
788 self.assertEqual(3, len(images))
789
790 def test_list_images_with_marker(self):
791 images = self.controller.list(
792 limit=1, marker='3a4560a1-e585-443e-9b39-553b46ec92d1')
793 self.assertEqual('6f99bf80-2ee6-47cf-acfe-1f1fabb7e810', images[0].id)
794 self.assertEqual('image-2', images[0].name)
795
796 def test_list_images_visibility_public(self):
797 filters = {'filters': {'visibility': 'public'}}
798 images = self.controller.list(**filters)
799 self.assertEqual(_PUBLIC_ID, images[0].id)
800
801 def test_list_images_visibility_private(self):
802 filters = {'filters': {'visibility': 'private'}}
803 images = self.controller.list(**filters)
804 self.assertEqual(_PRIVATE_ID, images[0].id)
805
806 def test_list_images_visibility_shared(self):
807 filters = {'filters': {'visibility': 'shared'}}
808 images = self.controller.list(**filters)
809 self.assertEqual(_SHARED_ID, images[0].id)
810
811 def test_list_images_visibility_community(self):
812 filters = {'filters': {'visibility': 'community'}}
813 images = list(self.controller.list(**filters))
814 self.assertEqual(_COMMUNITY_ID, images[0].id)
815
816 def test_list_images_member_status_rejected(self):
817 filters = {'filters': {'member_status': 'rejected'}}
818 images = self.controller.list(**filters)
819 self.assertEqual(_STATUS_REJECTED_ID, images[0].id)
820
821 def test_list_images_for_owner(self):
822 filters = {'filters': {'owner': _OWNER_ID}}
823 images = self.controller.list(**filters)
824 self.assertEqual(_OWNED_IMAGE_ID, images[0].id)
825
826 def test_list_images_for_checksum_single_image(self):
827 fake_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
828 filters = {'filters': {'checksum': _CHKSUM}}
829 images = self.controller.list(**filters)
830 self.assertEqual(1, len(images))
831 self.assertEqual('%s' % fake_id, images[0].id)
832
833 def test_list_images_for_checksum_multiple_images(self):
834 fake_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
835 fake_id2 = '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810'
836 filters = {'filters': {'checksum': _CHKSUM1}}
837 images = self.controller.list(**filters)
838 self.assertEqual(2, len(images))
839 self.assertEqual('%s' % fake_id1, images[0].id)
840 self.assertEqual('%s' % fake_id2, images[1].id)
841
842 def test_list_images_for_wrong_checksum(self):
843 filters = {'filters': {'checksum': 'wrong'}}
844 images = self.controller.list(**filters)
845 self.assertEqual(0, len(images))
846
847 def test_list_images_for_hash_single_image(self):
848 fake_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
849 filters = {'filters': {'os_hash_value': _HASHVAL}}
850 images = self.controller.list(**filters)
851 self.assertEqual(1, len(images))
852 self.assertEqual('%s' % fake_id, images[0].id)
853
854 def test_list_images_for_hash_multiple_images(self):
855 fake_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
856 fake_id2 = '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810'
857 filters = {'filters': {'os_hash_value': _HASHVAL1}}
858 images = self.controller.list(**filters)
859 self.assertEqual(2, len(images))
860 self.assertEqual('%s' % fake_id1, images[0].id)
861 self.assertEqual('%s' % fake_id2, images[1].id)
862
863 def test_list_images_for_wrong_hash(self):
864 filters = {'filters': {'os_hash_value': _HASHBAD}}
865 images = self.controller.list(**filters)
866 self.assertEqual(0, len(images))
867
868 def test_list_images_for_bogus_owner(self):
869 filters = {'filters': {'owner': _BOGUS_ID}}
870 images = self.controller.list(**filters)
871 self.assertEqual([], images)
872
873 def test_list_images_for_bunch_of_filters(self):
874 filters = {'filters': {'owner': _BOGUS_ID,
875 'visibility': 'shared',
876 'member_status': 'pending'}}
877 images = self.controller.list(**filters)
878 self.assertEqual(_EVERYTHING_ID, images[0].id)
879
880 def test_list_images_filters_encoding(self):
881 filters = {"owner": u"ni\xf1o"}
882 try:
883 self.controller.list(filters=filters)
884 except KeyError:
885 # NOTE(flaper87): It raises KeyError because there's
886 # no fixture supporting this query:
887 # /v2/images?owner=ni%C3%B1o&limit=20
888 # We just want to make sure filters are correctly encoded.
889 pass
890 self.assertEqual(b"ni\xc3\xb1o", filters["owner"])
891
892 def test_list_images_for_tag_single_image(self):
893 img_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
894 filters = {'filters': {'tag': [_TAG1]}}
895 images = self.controller.list(**filters)
896 self.assertEqual(1, len(images))
897 self.assertEqual('%s' % img_id, images[0].id)
898
899 def test_list_images_for_tag_multiple_images(self):
900 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
901 img_id2 = '6f99bf80-2ee6-47cf-acfe-1f1fabb7e810'
902 filters = {'filters': {'tag': [_TAG2]}}
903 images = self.controller.list(**filters)
904 self.assertEqual(2, len(images))
905 self.assertEqual('%s' % img_id1, images[0].id)
906 self.assertEqual('%s' % img_id2, images[1].id)
907
908 def test_list_images_for_multi_tags(self):
909 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
910 filters = {'filters': {'tag': [_TAG1, _TAG2]}}
911 images = self.controller.list(**filters)
912 self.assertEqual(1, len(images))
913 self.assertEqual('%s' % img_id1, images[0].id)
914
915 def test_list_images_for_non_existent_tag(self):
916 filters = {'filters': {'tag': ['fake']}}
917 images = self.controller.list(**filters)
918 self.assertEqual(0, len(images))
919
920 def test_list_images_for_invalid_tag(self):
921 filters = {'filters': {'tag': [[]]}}
922
923 self.assertRaises(exc.HTTPBadRequest,
924 self.controller.list, **filters)
925
926 def test_list_images_with_single_sort_key(self):
927 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
928 sort_key = 'name'
929 images = self.controller.list(sort_key=sort_key)
930 self.assertEqual(2, len(images))
931 self.assertEqual('%s' % img_id1, images[0].id)
932
933 def test_list_with_multiple_sort_keys(self):
934 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
935 sort_key = ['name', 'id']
936 images = self.controller.list(sort_key=sort_key)
937 self.assertEqual(2, len(images))
938 self.assertEqual('%s' % img_id1, images[0].id)
939
940 def test_list_images_with_desc_sort_dir(self):
941 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
942 sort_key = 'id'
943 sort_dir = 'desc'
944 images = self.controller.list(sort_key=sort_key, sort_dir=sort_dir)
945 self.assertEqual(2, len(images))
946 self.assertEqual('%s' % img_id1, images[1].id)
947
948 def test_list_images_with_multiple_sort_keys_and_one_sort_dir(self):
949 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
950 sort_key = ['name', 'id']
951 sort_dir = 'desc'
952 images = self.controller.list(sort_key=sort_key, sort_dir=sort_dir)
953 self.assertEqual(2, len(images))
954 self.assertEqual('%s' % img_id1, images[1].id)
955
956 def test_list_images_with_multiple_sort_dirs(self):
957 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
958 sort_key = ['name', 'id']
959 sort_dir = ['desc', 'asc']
960 images = self.controller.list(sort_key=sort_key, sort_dir=sort_dir)
961 self.assertEqual(2, len(images))
962 self.assertEqual('%s' % img_id1, images[1].id)
963
964 def test_list_images_with_new_sorting_syntax(self):
965 img_id1 = '2a4560b2-e585-443e-9b39-553b46ec92d1'
966 sort = 'name:desc,size:asc'
967 images = self.controller.list(sort=sort)
968 self.assertEqual(2, len(images))
969 self.assertEqual('%s' % img_id1, images[1].id)
970
971 def test_list_images_sort_dirs_fewer_than_keys(self):
972 sort_key = ['name', 'id', 'created_at']
973 sort_dir = ['desc', 'asc']
974 self.assertRaises(exc.HTTPBadRequest, self.controller.list,
975 sort_key=sort_key, sort_dir=sort_dir)
976
977 def test_list_images_combined_syntax(self):
978 sort_key = ['name', 'id']
979 sort_dir = ['desc', 'asc']
980 sort = 'name:asc'
981 self.assertRaises(exc.HTTPBadRequest,
982 self.controller.list, sort=sort, sort_key=sort_key,
983 sort_dir=sort_dir)
984
985 def test_list_images_new_sorting_syntax_invalid_key(self):
986 sort = 'INVALID:asc'
987 self.assertRaises(exc.HTTPBadRequest, self.controller.list,
988 sort=sort)
989
990 def test_list_images_new_sorting_syntax_invalid_direction(self):
991 sort = 'name:INVALID'
992 self.assertRaises(exc.HTTPBadRequest, self.controller.list,
993 sort=sort)
994
995 def test_list_images_for_property(self):
996 filters = {'filters': dict([('os_distro', 'NixOS')])}
997 images = self.controller.list(**filters)
998 self.assertEqual(1, len(images))
999
1000 def test_list_images_for_non_existent_property(self):
1001 filters = {'filters': dict([('my_little_property',
1002 'cant_be_this_cute')])}
1003 images = self.controller.list(**filters)
1004 self.assertEqual(0, len(images))
1005
1006 def test_get_image(self):
1007 image = self.controller.get('3a4560a1-e585-443e-9b39-553b46ec92d1')
1008 self.assertEqual('3a4560a1-e585-443e-9b39-553b46ec92d1', image.id)
1009 self.assertEqual('image-1', image.name)
1010
1011 def test_create_image(self):
1012 properties = {
1013 'name': 'image-1'
1014 }
1015 image = self.controller.create(**properties)
1016 self.assertEqual('3a4560a1-e585-443e-9b39-553b46ec92d1', image.id)
1017 self.assertEqual('image-1', image.name)
1018
1019 def test_create_image_w_size(self):
1020 properties = {
1021 'name': 'image-1',
1022 'size': '4'
1023 }
1024 image = self.controller.create(**properties)
1025 self.assertEqual('3a4560a1-e585-443e-9b39-553b46ec92d1', image.id)
1026 self.assertEqual('image-1', image.name)
1027 self.assertIsNone(image.get('size'))
1028
1029 def test_create_bad_additionalProperty_type(self):
1030 properties = {
1031 'name': 'image-1',
1032 'bad_prop': True,
1033 }
1034 with testtools.ExpectedException(TypeError):
1035 self.controller.create(**properties)
1036
1037 def test_delete_image(self):
1038 self.controller.delete('87b634c1-f893-33c9-28a9-e5673c99239a')
1039 expect = [
1040 ('DELETE',
1041 '/v2/images/87b634c1-f893-33c9-28a9-e5673c99239a',
1042 {},
1043 None)]
1044 self.assertEqual(expect, self.api.calls)
1045
1046 def test_deactivate_image(self):
1047 id_image = '87b634c1-f893-33c9-28a9-e5673c99239a'
1048 self.controller.deactivate(id_image)
1049 expect = [('POST',
1050 '/v2/images/%s/actions/deactivate' % id_image,
1051 {},
1052 None)]
1053 self.assertEqual(expect, self.api.calls)
1054
1055 def test_reactivate_image(self):
1056 id_image = '87b634c1-f893-33c9-28a9-e5673c99239a'
1057 self.controller.reactivate(id_image)
1058 expect = [('POST',
1059 '/v2/images/%s/actions/reactivate' % id_image,
1060 {},
1061 None)]
1062 self.assertEqual(expect, self.api.calls)
1063
1064 def test_data_upload(self):
1065 image_data = 'CCC'
1066 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1067 self.controller.upload(image_id, image_data)
1068 expect = [('PUT', '/v2/images/%s/file' % image_id,
1069 {'Content-Type': 'application/octet-stream'},
1070 image_data)]
1071 self.assertEqual(expect, self.api.calls)
1072
1073 def test_data_upload_with_invalid_size(self):
1074 image_data = 'CCC'
1075 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1076 self.assertRaises(TypeError, self.controller.upload, image_id,
1077 image_data, image_size='invalid_size')
1078 expect = []
1079 self.assertEqual(expect, self.api.calls)
1080
1081 def test_data_upload_w_size_same_as_data(self):
1082 image_data = 'CCC'
1083 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1084 self.controller.upload(image_id, image_data, image_size=3)
1085 expect = [('PUT', '/v2/images/%s/file' % image_id,
1086 {'Content-Type': 'application/octet-stream',
1087 'x-openstack-image-size': str(len(image_data))},
1088 image_data)]
1089 self.assertEqual(expect, self.api.calls)
1090
1091 def test_data_upload_w_size_diff_than_data(self):
1092 image_data = 'CCCCCC'
1093 image_size = '3'
1094 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1095 self.controller.upload(image_id, image_data,
1096 image_size=int(image_size))
1097 expect = [('PUT', '/v2/images/%s/file' % image_id,
1098 {'Content-Type': 'application/octet-stream',
1099 'x-openstack-image-size': image_size},
1100 image_data)]
1101 self.assertEqual(expect, self.api.calls)
1102
1103 def test_data_stage_with_invalid_size(self):
1104 image_data = 'CCC'
1105 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1106 self.assertRaises(TypeError, self.controller.stage, image_id,
1107 image_data, image_size='invalid_size')
1108 expect = []
1109 self.assertEqual(expect, self.api.calls)
1110
1111 def test_data_stage_w_size_same_as_data(self):
1112 image_data = 'CCC'
1113 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1114 self.controller.stage(image_id, image_data, image_size=3)
1115 expect = [('PUT', '/v2/images/%s/stage' % image_id,
1116 {'Content-Type': 'application/octet-stream',
1117 'x-openstack-image-size': str(len(image_data))},
1118 image_data)]
1119 self.assertEqual(expect, self.api.calls)
1120
1121 def test_data_stage_w_size_diff_than_data(self):
1122 image_data = 'CCCCCC'
1123 image_size = '3'
1124 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1125 self.controller.stage(image_id, image_data,
1126 image_size=int(image_size))
1127 expect = [('PUT', '/v2/images/%s/stage' % image_id,
1128 {'Content-Type': 'application/octet-stream',
1129 'x-openstack-image-size': image_size},
1130 image_data)]
1131 self.assertEqual(expect, self.api.calls)
1132
1133 def test_data_without_checksum(self):
1134 body = self.controller.data('5cc4bebc-db27-11e1-a1eb-080027cbe205',
1135 do_checksum=False)
1136 body = ''.join([b for b in body])
1137 self.assertEqual('A', body)
1138
1139 body = self.controller.data('5cc4bebc-db27-11e1-a1eb-080027cbe205')
1140 body = ''.join([b for b in body])
1141 self.assertEqual('A', body)
1142
1143 def test_data_with_wrong_checksum(self):
1144 body = self.controller.data('headeronly-db27-11e1-a1eb-080027cbe205',
1145 do_checksum=False)
1146 body = ''.join([b for b in body])
1147 self.assertEqual('BB', body)
1148 body = self.controller.data('headeronly-db27-11e1-a1eb-080027cbe205')
1149 try:
1150 body = ''.join([b for b in body])
1151 self.fail('data did not raise an error.')
1152 except IOError as e:
1153 self.assertEqual(errno.EPIPE, e.errno)
1154 msg = 'was 9d3d9048db16a7eee539e93e3618cbe7 expected wrong'
1155 self.assertIn(msg, str(e))
1156
1157 body = self.controller.data('chkonly-db27-11e1-a1eb-080027cbe205',
1158 do_checksum=False)
1159 body = ''.join([b for b in body])
1160 self.assertEqual('BB', body)
1161 body = self.controller.data('chkonly-db27-11e1-a1eb-080027cbe205')
1162 try:
1163 body = ''.join([b for b in body])
1164 self.fail('data did not raise an error.')
1165 except IOError as e:
1166 self.assertEqual(errno.EPIPE, e.errno)
1167 msg = 'was 9d3d9048db16a7eee539e93e3618cbe7 expected wrong'
1168 self.assertIn(msg, str(e))
1169
1170 body = self.controller.data('multihash-db27-11e1-a1eb-080027cbe205',
1171 do_checksum=False)
1172 body = ''.join([b for b in body])
1173 self.assertEqual('BB', body)
1174 body = self.controller.data('multihash-db27-11e1-a1eb-080027cbe205')
1175 try:
1176 body = ''.join([b for b in body])
1177 self.fail('data did not raise an error.')
1178 except IOError as e:
1179 self.assertEqual(errno.EPIPE, e.errno)
1180 msg = 'was 9d3d9048db16a7eee539e93e3618cbe7 expected junk'
1181 self.assertIn(msg, str(e))
1182
1183 body = self.controller.data('badalgo-db27-11e1-a1eb-080027cbe205',
1184 do_checksum=False)
1185 body = ''.join([b for b in body])
1186 self.assertEqual('BB', body)
1187 try:
1188 body = self.controller.data('badalgo-db27-11e1-a1eb-080027cbe205')
1189 self.fail('bad os_hash_algo did not raise an error.')
1190 except ValueError as e:
1191 msg = 'unsupported hash type not_an_algo'
1192 self.assertIn(msg, str(e))
1193
1194 def test_data_with_checksum(self):
1195 for prefix in ['headeronly', 'chkonly', 'multihash']:
1196 body = self.controller.data(prefix +
1197 '-dd57-11e1-af0f-02163e68b1d8',
1198 do_checksum=False)
1199 body = ''.join([b for b in body])
1200 self.assertEqual('CCC', body)
1201
1202 body = self.controller.data(prefix +
1203 '-dd57-11e1-af0f-02163e68b1d8')
1204 body = ''.join([b for b in body])
1205 self.assertEqual('CCC', body)
1206
1207 @ddt.data('headeronly', 'chkonly', 'multihash')
1208 def test_data_with_checksum_but_no_md5_algo(self, prefix):
1209 with mock.patch('hashlib.new', mock.MagicMock(
1210 side_effect=ValueError('unsupported hash type'))):
1211 body = self.controller.data(prefix +
1212 '-dd57-11e1-af0f-02163e68b1d8',
1213 allow_md5_fallback=True)
1214 try:
1215 body = ''.join([b for b in body])
1216 self.fail('missing md5 algo did not raise an error')
1217 except IOError as e:
1218 self.assertEqual(errno.EPIPE, e.errno)
1219 msg = 'md5 algorithm is not available on the client'
1220 self.assertIn(msg, str(e))
1221
1222 def test_data_with_checksum_and_fallback(self):
1223 # make sure the allow_md5_fallback option does not cause any
1224 # incorrect behavior when fallback is not needed
1225 for prefix in ['headeronly', 'chkonly', 'multihash']:
1226 body = self.controller.data(prefix +
1227 '-dd57-11e1-af0f-02163e68b1d8',
1228 do_checksum=False,
1229 allow_md5_fallback=True)
1230 body = ''.join([b for b in body])
1231 self.assertEqual('CCC', body)
1232
1233 body = self.controller.data(prefix +
1234 '-dd57-11e1-af0f-02163e68b1d8',
1235 allow_md5_fallback=True)
1236 body = ''.join([b for b in body])
1237 self.assertEqual('CCC', body)
1238
1239 def test_data_with_bad_hash_algo_and_fallback(self):
1240 # shouldn't matter when do_checksum is False
1241 body = self.controller.data('badalgo-db27-11e1-a1eb-080027cbe205',
1242 do_checksum=False,
1243 allow_md5_fallback=True)
1244 body = ''.join([b for b in body])
1245 self.assertEqual('BB', body)
1246
1247 # default value for do_checksum is True
1248 body = self.controller.data('badalgo-db27-11e1-a1eb-080027cbe205',
1249 allow_md5_fallback=True)
1250 body = ''.join([b for b in body])
1251 self.assertEqual('BB', body)
1252
1253 def test_neg_data_with_bad_hash_value_and_fallback_enabled(self):
1254 # make sure download fails when good hash_algo but bad hash_value
1255 # even when correct checksum is present regardless of
1256 # allow_md5_fallback setting
1257 body = self.controller.data('bad-multihash-value-good-checksum',
1258 allow_md5_fallback=False)
1259 try:
1260 body = ''.join([b for b in body])
1261 self.fail('bad os_hash_value did not raise an error.')
1262 except IOError as e:
1263 self.assertEqual(errno.EPIPE, e.errno)
1264 msg = 'expected badmultihashvalue'
1265 self.assertIn(msg, str(e))
1266
1267 body = self.controller.data('bad-multihash-value-good-checksum',
1268 allow_md5_fallback=True)
1269 try:
1270 body = ''.join([b for b in body])
1271 self.fail('bad os_hash_value did not raise an error.')
1272 except IOError as e:
1273 self.assertEqual(errno.EPIPE, e.errno)
1274 msg = 'expected badmultihashvalue'
1275 self.assertIn(msg, str(e))
1276
1277 # download should succeed when do_checksum is off, though
1278 body = self.controller.data('bad-multihash-value-good-checksum',
1279 do_checksum=False)
1280 body = ''.join([b for b in body])
1281 self.assertEqual('GOODCHECKSUM', body)
1282
1283 def test_image_import_web_download(self):
1284 uri = 'http://example.com/image.qcow'
1285 data = [('method', {'name': 'web-download',
1286 'uri': uri})]
1287 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1288 self.controller.image_import(image_id, 'web-download', uri)
1289 expect = [('POST', '/v2/images/%s/import' % image_id, {},
1290 data)]
1291 self.assertEqual(expect, self.api.calls)
1292
1293 def test_image_import_glance_download(self):
1294 region = 'REGION2'
1295 remote_image_id = '75baf7b6-253a-11ed-8307-4b1057986a78'
1296 image_id = '606b0e88-7c5a-4d54-b5bb-046105d4de6f'
1297 service_interface = 'public'
1298 data = [('method',
1299 {'name': 'glance-download',
1300 'glance_region': region,
1301 'glance_image_id': remote_image_id,
1302 'glance_service_interface': service_interface})]
1303 self.controller.image_import(
1304 image_id, 'glance-download', remote_region=region,
1305 remote_image_id=remote_image_id,
1306 remote_service_interface=service_interface)
1307 expect = [('POST', '/v2/images/%s/import' % image_id, {},
1308 data)]
1309 self.assertEqual(expect, self.api.calls)
1310
1311 def test_download_no_data(self):
1312 resp = utils.FakeResponse(headers={}, status_code=204)
1313 self.controller.controller.http_client.get = mock.Mock(
1314 return_value=(resp, {}))
1315 self.controller.data('image_id')
1316
1317 def test_download_forbidden(self):
1318 self.controller.controller.http_client.get = mock.Mock(
1319 side_effect=exc.HTTPForbidden())
1320 try:
1321 self.controller.data('image_id')
1322 self.fail('No forbidden exception raised.')
1323 except exc.HTTPForbidden:
1324 pass
1325
1326 def test_update_replace_prop(self):
1327 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
1328 params = {'name': 'pong'}
1329 image = self.controller.update(image_id, **params)
1330 expect_hdrs = {
1331 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1332 }
1333 expect_body = [[('op', 'replace'), ('path', '/name'),
1334 ('value', 'pong')]]
1335 expect = [
1336 ('GET', '/v2/images/%s' % image_id, {}, None),
1337 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1338 ('GET', '/v2/images/%s' % image_id,
1339 {'x-openstack-request-id': 'req-1234'}, None),
1340 ]
1341 self.assertEqual(expect, self.api.calls)
1342 self.assertEqual(image_id, image.id)
1343 # NOTE(bcwaldon):due to limitations of our fake api framework, the name
1344 # will not actually change - yet in real life it will...
1345 self.assertEqual('image-1', image.name)
1346
1347 def test_update_add_prop(self):
1348 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
1349 params = {'finn': 'human'}
1350 image = self.controller.update(image_id, **params)
1351 expect_hdrs = {
1352 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1353 }
1354 expect_body = [[('op', 'add'), ('path', '/finn'), ('value', 'human')]]
1355 expect = [
1356 ('GET', '/v2/images/%s' % image_id, {}, None),
1357 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1358 ('GET', '/v2/images/%s' % image_id,
1359 {'x-openstack-request-id': 'req-1234'}, None),
1360 ]
1361 self.assertEqual(expect, self.api.calls)
1362 self.assertEqual(image_id, image.id)
1363 # NOTE(bcwaldon):due to limitations of our fake api framework, the name
1364 # will not actually change - yet in real life it will...
1365 self.assertEqual('image-1', image.name)
1366
1367 def test_update_remove_prop(self):
1368 image_id = 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3'
1369 remove_props = ['barney']
1370 image = self.controller.update(image_id, remove_props)
1371 expect_hdrs = {
1372 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1373 }
1374 expect_body = [[('op', 'remove'), ('path', '/barney')]]
1375 expect = [
1376 ('GET', '/v2/images/%s' % image_id, {}, None),
1377 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1378 ('GET', '/v2/images/%s' % image_id,
1379 {'x-openstack-request-id': 'req-1234'}, None),
1380 ]
1381 self.assertEqual(expect, self.api.calls)
1382 self.assertEqual(image_id, image.id)
1383 # NOTE(bcwaldon):due to limitations of our fake api framework, the name
1384 # will not actually change - yet in real life it will...
1385 self.assertEqual('image-3', image.name)
1386
1387 def test_update_replace_remove_same_prop(self):
1388 image_id = 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3'
1389 # Updating a property takes precedence over removing a property
1390 params = {'barney': 'miller'}
1391 remove_props = ['barney']
1392 image = self.controller.update(image_id, remove_props, **params)
1393 expect_hdrs = {
1394 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1395 }
1396 expect_body = ([[('op', 'replace'), ('path', '/barney'),
1397 ('value', 'miller')]])
1398 expect = [
1399 ('GET', '/v2/images/%s' % image_id, {}, None),
1400 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1401 ('GET', '/v2/images/%s' % image_id,
1402 {'x-openstack-request-id': 'req-1234'}, None),
1403 ]
1404 self.assertEqual(expect, self.api.calls)
1405 self.assertEqual(image_id, image.id)
1406 # NOTE(bcwaldon):due to limitations of our fake api framework, the name
1407 # will not actually change - yet in real life it will...
1408 self.assertEqual('image-3', image.name)
1409
1410 def test_update_add_remove_same_prop(self):
1411 image_id = 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3'
1412 # Adding a property takes precedence over removing a property
1413 params = {'finn': 'human'}
1414 remove_props = ['finn']
1415 image = self.controller.update(image_id, remove_props, **params)
1416 expect_hdrs = {
1417 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1418 }
1419 expect_body = [[('op', 'add'), ('path', '/finn'), ('value', 'human')]]
1420 expect = [
1421 ('GET', '/v2/images/%s' % image_id, {}, None),
1422 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1423 ('GET', '/v2/images/%s' % image_id,
1424 {'x-openstack-request-id': 'req-1234'}, None),
1425 ]
1426 self.assertEqual(expect, self.api.calls)
1427 self.assertEqual(image_id, image.id)
1428 # NOTE(bcwaldon):due to limitations of our fake api framework, the name
1429 # will not actually change - yet in real life it will...
1430 self.assertEqual('image-3', image.name)
1431
1432 def test_update_bad_additionalProperty_type(self):
1433 image_id = 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3'
1434 params = {'name': 'pong', 'bad_prop': False}
1435 with testtools.ExpectedException(TypeError):
1436 self.controller.update(image_id, **params)
1437
1438 def test_update_add_custom_property(self):
1439 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
1440 params = {'color': 'red'}
1441 image = self.controller.update(image_id, **params)
1442 expect_hdrs = {
1443 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1444 }
1445 expect_body = [[('op', 'add'), ('path', '/color'), ('value', 'red')]]
1446 expect = [
1447 ('GET', '/v2/images/%s' % image_id, {}, None),
1448 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1449 ('GET', '/v2/images/%s' % image_id,
1450 {'x-openstack-request-id': 'req-1234'}, None),
1451 ]
1452 self.assertEqual(expect, self.api.calls)
1453 self.assertEqual(image_id, image.id)
1454
1455 def test_update_replace_custom_property(self):
1456 image_id = 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3'
1457 params = {'color': 'blue'}
1458 image = self.controller.update(image_id, **params)
1459 expect_hdrs = {
1460 'Content-Type': 'application/openstack-images-v2.1-json-patch',
1461 }
1462 expect_body = [[('op', 'replace'), ('path', '/color'),
1463 ('value', 'blue')]]
1464 expect = [
1465 ('GET', '/v2/images/%s' % image_id, {}, None),
1466 ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body),
1467 ('GET', '/v2/images/%s' % image_id,
1468 {'x-openstack-request-id': 'req-1234'}, None),
1469 ]
1470 self.assertEqual(expect, self.api.calls)
1471 self.assertEqual(image_id, image.id)
1472
1473 def test_location_ops_when_server_disabled_location_ops(self):
1474 # Location operations should not be allowed if server has not
1475 # enabled location related operations. There is no need to check it
1476 # when do location add, because the check would be done in server side.
1477 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
1478 estr = 'The administrator has disabled API access to image locations'
1479 url = 'http://bar.com/'
1480 meta = {'bar': 'barmeta'}
1481
1482 e = self.assertRaises(exc.HTTPBadRequest,
1483 self.controller.delete_locations,
1484 image_id, set([url]))
1485 self.assertIn(estr, str(e))
1486
1487 e = self.assertRaises(exc.HTTPBadRequest,
1488 self.controller.update_location,
1489 image_id, url, meta)
1490 self.assertIn(estr, str(e))
1491
1492 def _empty_get(self, image_id, headers=None):
1493 return ('GET', '/v2/images/%s' % image_id,
1494 headers or {}, None)
1495
1496 def _patch_req(self, image_id, patch_body):
1497 c_type = 'application/openstack-images-v2.1-json-patch'
1498 data = [sorted(d.items()) for d in patch_body]
1499 return ('PATCH',
1500 '/v2/images/%s' % image_id,
1501 {'Content-Type': c_type},
1502 data)
1503
1504 def test_add_location(self):
1505 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1506 new_loc = {'url': 'http://spam.com/', 'metadata': {'spam': 'ham'}}
1507 add_patch = {'path': '/locations/-', 'value': new_loc, 'op': 'add'}
1508 headers = {'x-openstack-request-id': 'req-1234'}
1509 self.controller.add_location(image_id, **new_loc)
1510 self.assertEqual([self._patch_req(image_id, [add_patch]),
1511 self._empty_get(image_id, headers=headers)],
1512 self.api.calls)
1513
1514 @mock.patch.object(images.Controller, '_send_image_update_request',
1515 side_effect=exc.HTTPBadRequest)
1516 def test_add_duplicate_location(self, mock_request):
1517 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1518 new_loc = {'url': 'http://foo.com/', 'metadata': {'foo': 'newfoo'}}
1519
1520 self.assertRaises(exc.HTTPBadRequest,
1521 self.controller.add_location,
1522 image_id, **new_loc)
1523
1524 def test_remove_location(self):
1525 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1526 url_set = set(['http://foo.com/', 'http://bar.com/'])
1527 del_patches = [{'path': '/locations/1', 'op': 'remove'},
1528 {'path': '/locations/0', 'op': 'remove'}]
1529 self.controller.delete_locations(image_id, url_set)
1530 self.assertEqual([self._empty_get(image_id),
1531 self._patch_req(image_id, del_patches)],
1532 self.api.calls)
1533
1534 def test_remove_missing_location(self):
1535 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1536 url_set = set(['http://spam.ham/'])
1537 err_str = 'Unknown URL(s): %s' % list(url_set)
1538
1539 err = self.assertRaises(exc.HTTPNotFound,
1540 self.controller.delete_locations,
1541 image_id, url_set)
1542 self.assertIn(err_str, str(err))
1543
1544 def test_update_location(self):
1545 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1546 new_loc = {'url': 'http://foo.com/', 'metadata': {'spam': 'ham'}}
1547 headers = {'x-openstack-request-id': 'req-1234'}
1548 fixture_idx = '/v2/images/%s' % (image_id)
1549 orig_locations = data_fixtures[fixture_idx]['GET'][1]['locations']
1550 loc_map = dict([(loc['url'], loc) for loc in orig_locations])
1551 loc_map[new_loc['url']] = new_loc
1552 mod_patch = [{'path': '/locations', 'op': 'replace',
1553 'value': list(loc_map.values())}]
1554 self.controller.update_location(image_id, **new_loc)
1555 self.assertEqual([self._empty_get(image_id),
1556 self._patch_req(image_id, mod_patch),
1557 self._empty_get(image_id, headers=headers)],
1558 self.api.calls)
1559
1560 def test_update_tags(self):
1561 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1562 tag_map = {'tags': ['tag01', 'tag02', 'tag03']}
1563 headers = {'x-openstack-request-id': 'req-1234'}
1564
1565 image = self.controller.update(image_id, **tag_map)
1566
1567 expected_body = [{'path': '/tags', 'op': 'replace',
1568 'value': tag_map['tags']}]
1569 expected = [
1570 self._empty_get(image_id),
1571 self._patch_req(image_id, expected_body),
1572 self._empty_get(image_id, headers=headers)
1573 ]
1574 self.assertEqual(expected, self.api.calls)
1575 self.assertEqual(image_id, image.id)
1576
1577 def test_update_missing_location(self):
1578 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1579 new_loc = {'url': 'http://spam.com/', 'metadata': {'spam': 'ham'}}
1580 err_str = 'Unknown URL: %s' % new_loc['url']
1581 err = self.assertRaises(exc.HTTPNotFound,
1582 self.controller.update_location,
1583 image_id, **new_loc)
1584 self.assertIn(err_str, str(err))
1585
1586 def test_add_image_location(self):
1587 location_url = 'http://spam.com/'
1588 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
1589 expect = [
1590 ('POST',
1591 '/v2/images/%s/locations' % (image_id),
1592 {},
1593 [('url', location_url), ('validation_data', {})]),
1594 ('GET', '/v2/images/%s' % (image_id), {}, None)]
1595 with mock.patch.object(common_utils,
1596 'has_version') as mock_has_version:
1597 mock_has_version.return_value = True
1598 self.controller.add_image_location(image_id, location_url, {})
1599 self.assertEqual(expect, self.api.calls)
1600
1601 def test_add_image_location_with_validation_data(self):
1602 location_url = 'http://spam.com/'
1603 image_id = '3a4560a1-e585-443e-9b39-553b46ec92d1'
1604 expect = [
1605 ('POST',
1606 '/v2/images/%s/locations' % (image_id),
1607 {},
1608 [('url', location_url), ('validation_data',
1609 {'os_hash_algo': 'sha512'})]),
1610 ('GET', '/v2/images/%s' % (image_id), {}, None)]
1611 with mock.patch.object(common_utils,
1612 'has_version') as mock_has_version:
1613 mock_has_version.return_value = True
1614 self.controller.add_image_location(image_id, location_url,
1615 {'os_hash_algo': 'sha512'})
1616 self.assertEqual(expect, self.api.calls)
1617
1618 def test_add_image_location_not_supported(self):
1619 with mock.patch.object(common_utils,
1620 'has_version') as mock_has_version:
1621 mock_has_version.return_value = False
1622 self.assertRaises(exc.HTTPNotImplemented,
1623 self.controller.add_image_location,
1624 '3a4560a1-e585-443e-9b39-553b46ec92d1',
1625 'http://spam.com/')
1626
1627 def test_get_image_locations(self):
1628 image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8'
1629 with mock.patch.object(common_utils,
1630 'has_version') as mock_has_version:
1631 mock_has_version.return_value = True
1632 locations = self.controller.get_image_locations(image_id)
1633 self.assertEqual(2, len(locations))
1634
1635 def test_get_image_location_not_supported(self):
1636 with mock.patch.object(common_utils,
1637 'has_version') as mock_has_version:
1638 mock_has_version.return_value = False
1639 self.assertRaises(exc.HTTPNotImplemented,
1640 self.controller.get_image_locations,
1641 '3a4560a1-e585-443e-9b39-553b46ec92d1')