OpenStack Image Management (Glance)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_images_resource.py 192KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405
  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. import datetime
  16. import eventlet
  17. import hashlib
  18. import uuid
  19. import glance_store as store
  20. import mock
  21. from oslo_serialization import jsonutils
  22. import six
  23. from six.moves import http_client as http
  24. # NOTE(jokke): simplified transition to py3, behaves like py2 xrange
  25. from six.moves import range
  26. import testtools
  27. import webob
  28. import glance.api.v2.image_actions
  29. import glance.api.v2.images
  30. from glance.common import exception
  31. from glance import domain
  32. import glance.schema
  33. from glance.tests.unit import base
  34. import glance.tests.unit.utils as unit_test_utils
  35. import glance.tests.utils as test_utils
  36. DATETIME = datetime.datetime(2012, 5, 16, 15, 27, 36, 325355)
  37. ISOTIME = '2012-05-16T15:27:36Z'
  38. BASE_URI = unit_test_utils.BASE_URI
  39. UUID1 = 'c80a1a6c-bd1f-41c5-90ee-81afedb1d58d'
  40. UUID2 = 'a85abd86-55b3-4d5b-b0b4-5d0a6e6042fc'
  41. UUID3 = '971ec09a-8067-4bc8-a91f-ae3557f1c4c7'
  42. UUID4 = '6bbe7cc2-eae7-4c0f-b50d-a7160b0c6a86'
  43. TENANT1 = '6838eb7b-6ded-434a-882c-b344c77fe8df'
  44. TENANT2 = '2c014f32-55eb-467d-8fcb-4bd706012f81'
  45. TENANT3 = '5a3e60e8-cfa9-4a9e-a90a-62b42cea92b8'
  46. TENANT4 = 'c6c87f25-8a94-47ed-8c83-053c25f42df4'
  47. CHKSUM = '93264c3edf5972c9f1cb309543d38a5c'
  48. CHKSUM1 = '43254c3edf6972c9f1cb309543d38a8c'
  49. FAKEHASHALGO = 'fake-name-for-sha512'
  50. MULTIHASH1 = hashlib.sha512(b'glance').hexdigest()
  51. MULTIHASH2 = hashlib.sha512(b'image_service').hexdigest()
  52. def _db_fixture(id, **kwargs):
  53. obj = {
  54. 'id': id,
  55. 'name': None,
  56. 'visibility': 'shared',
  57. 'properties': {},
  58. 'checksum': None,
  59. 'os_hash_algo': FAKEHASHALGO,
  60. 'os_hash_value': None,
  61. 'owner': None,
  62. 'status': 'queued',
  63. 'tags': [],
  64. 'size': None,
  65. 'virtual_size': None,
  66. 'locations': [],
  67. 'protected': False,
  68. 'disk_format': None,
  69. 'container_format': None,
  70. 'deleted': False,
  71. 'min_ram': None,
  72. 'min_disk': None,
  73. }
  74. obj.update(kwargs)
  75. return obj
  76. def _domain_fixture(id, **kwargs):
  77. properties = {
  78. 'image_id': id,
  79. 'name': None,
  80. 'visibility': 'private',
  81. 'checksum': None,
  82. 'os_hash_algo': None,
  83. 'os_hash_value': None,
  84. 'owner': None,
  85. 'status': 'queued',
  86. 'size': None,
  87. 'virtual_size': None,
  88. 'locations': [],
  89. 'protected': False,
  90. 'disk_format': None,
  91. 'container_format': None,
  92. 'min_ram': None,
  93. 'min_disk': None,
  94. 'tags': [],
  95. }
  96. properties.update(kwargs)
  97. return glance.domain.Image(**properties)
  98. def _db_image_member_fixture(image_id, member_id, **kwargs):
  99. obj = {
  100. 'image_id': image_id,
  101. 'member': member_id,
  102. }
  103. obj.update(kwargs)
  104. return obj
  105. class FakeImage(object):
  106. def __init__(self, status='active', container_format='ami',
  107. disk_format='ami'):
  108. self.id = UUID4
  109. self.status = status
  110. self.container_format = container_format
  111. self.disk_format = disk_format
  112. class TestImagesController(base.IsolatedUnitTest):
  113. def setUp(self):
  114. super(TestImagesController, self).setUp()
  115. self.db = unit_test_utils.FakeDB(initialize=False)
  116. self.policy = unit_test_utils.FakePolicyEnforcer()
  117. self.notifier = unit_test_utils.FakeNotifier()
  118. self.store = unit_test_utils.FakeStoreAPI()
  119. for i in range(1, 4):
  120. self.store.data['%s/fake_location_%i' % (BASE_URI, i)] = ('Z', 1)
  121. self.store_utils = unit_test_utils.FakeStoreUtils(self.store)
  122. self._create_images()
  123. self._create_image_members()
  124. self.controller = glance.api.v2.images.ImagesController(self.db,
  125. self.policy,
  126. self.notifier,
  127. self.store)
  128. self.action_controller = (glance.api.v2.image_actions.
  129. ImageActionsController(self.db,
  130. self.policy,
  131. self.notifier,
  132. self.store))
  133. self.controller.gateway.store_utils = self.store_utils
  134. store.create_stores()
  135. def _create_images(self):
  136. self.images = [
  137. _db_fixture(UUID1, owner=TENANT1, checksum=CHKSUM,
  138. os_hash_algo=FAKEHASHALGO, os_hash_value=MULTIHASH1,
  139. name='1', size=256, virtual_size=1024,
  140. visibility='public',
  141. locations=[{'url': '%s/%s' % (BASE_URI, UUID1),
  142. 'metadata': {}, 'status': 'active'}],
  143. disk_format='raw',
  144. container_format='bare',
  145. status='active'),
  146. _db_fixture(UUID2, owner=TENANT1, checksum=CHKSUM1,
  147. os_hash_algo=FAKEHASHALGO, os_hash_value=MULTIHASH2,
  148. name='2', size=512, virtual_size=2048,
  149. visibility='public',
  150. disk_format='raw',
  151. container_format='bare',
  152. status='active',
  153. tags=['redhat', '64bit', 'power'],
  154. properties={'hypervisor_type': 'kvm', 'foo': 'bar',
  155. 'bar': 'foo'}),
  156. _db_fixture(UUID3, owner=TENANT3, checksum=CHKSUM1,
  157. os_hash_algo=FAKEHASHALGO, os_hash_value=MULTIHASH2,
  158. name='3', size=512, virtual_size=2048,
  159. visibility='public', tags=['windows', '64bit', 'x86']),
  160. _db_fixture(UUID4, owner=TENANT4, name='4',
  161. size=1024, virtual_size=3072),
  162. ]
  163. [self.db.image_create(None, image) for image in self.images]
  164. self.db.image_tag_set_all(None, UUID1, ['ping', 'pong'])
  165. def _create_image_members(self):
  166. self.image_members = [
  167. _db_image_member_fixture(UUID4, TENANT2),
  168. _db_image_member_fixture(UUID4, TENANT3,
  169. status='accepted'),
  170. ]
  171. [self.db.image_member_create(None, image_member)
  172. for image_member in self.image_members]
  173. def test_index(self):
  174. self.config(limit_param_default=1, api_limit_max=3)
  175. request = unit_test_utils.get_fake_request()
  176. output = self.controller.index(request)
  177. self.assertEqual(1, len(output['images']))
  178. actual = set([image.image_id for image in output['images']])
  179. expected = set([UUID3])
  180. self.assertEqual(expected, actual)
  181. def test_index_member_status_accepted(self):
  182. self.config(limit_param_default=5, api_limit_max=5)
  183. request = unit_test_utils.get_fake_request(tenant=TENANT2)
  184. output = self.controller.index(request)
  185. self.assertEqual(3, len(output['images']))
  186. actual = set([image.image_id for image in output['images']])
  187. expected = set([UUID1, UUID2, UUID3])
  188. # can see only the public image
  189. self.assertEqual(expected, actual)
  190. request = unit_test_utils.get_fake_request(tenant=TENANT3)
  191. output = self.controller.index(request)
  192. self.assertEqual(4, len(output['images']))
  193. actual = set([image.image_id for image in output['images']])
  194. expected = set([UUID1, UUID2, UUID3, UUID4])
  195. self.assertEqual(expected, actual)
  196. def test_index_admin(self):
  197. request = unit_test_utils.get_fake_request(is_admin=True)
  198. output = self.controller.index(request)
  199. self.assertEqual(4, len(output['images']))
  200. def test_index_admin_deleted_images_hidden(self):
  201. request = unit_test_utils.get_fake_request(is_admin=True)
  202. self.controller.delete(request, UUID1)
  203. output = self.controller.index(request)
  204. self.assertEqual(3, len(output['images']))
  205. actual = set([image.image_id for image in output['images']])
  206. expected = set([UUID2, UUID3, UUID4])
  207. self.assertEqual(expected, actual)
  208. def test_index_return_parameters(self):
  209. self.config(limit_param_default=1, api_limit_max=3)
  210. request = unit_test_utils.get_fake_request()
  211. output = self.controller.index(request, marker=UUID3, limit=1,
  212. sort_key=['created_at'],
  213. sort_dir=['desc'])
  214. self.assertEqual(1, len(output['images']))
  215. actual = set([image.image_id for image in output['images']])
  216. expected = set([UUID2])
  217. self.assertEqual(actual, expected)
  218. self.assertEqual(UUID2, output['next_marker'])
  219. def test_index_next_marker(self):
  220. self.config(limit_param_default=1, api_limit_max=3)
  221. request = unit_test_utils.get_fake_request()
  222. output = self.controller.index(request, marker=UUID3, limit=2)
  223. self.assertEqual(2, len(output['images']))
  224. actual = set([image.image_id for image in output['images']])
  225. expected = set([UUID2, UUID1])
  226. self.assertEqual(expected, actual)
  227. self.assertEqual(UUID1, output['next_marker'])
  228. def test_index_no_next_marker(self):
  229. self.config(limit_param_default=1, api_limit_max=3)
  230. request = unit_test_utils.get_fake_request()
  231. output = self.controller.index(request, marker=UUID1, limit=2)
  232. self.assertEqual(0, len(output['images']))
  233. actual = set([image.image_id for image in output['images']])
  234. expected = set([])
  235. self.assertEqual(expected, actual)
  236. self.assertNotIn('next_marker', output)
  237. def test_index_with_id_filter(self):
  238. request = unit_test_utils.get_fake_request('/images?id=%s' % UUID1)
  239. output = self.controller.index(request, filters={'id': UUID1})
  240. self.assertEqual(1, len(output['images']))
  241. actual = set([image.image_id for image in output['images']])
  242. expected = set([UUID1])
  243. self.assertEqual(expected, actual)
  244. def test_index_with_invalid_hidden_filter(self):
  245. request = unit_test_utils.get_fake_request('/images?os_hidden=abcd')
  246. self.assertRaises(webob.exc.HTTPBadRequest,
  247. self.controller.index, request,
  248. filters={'os_hidden': 'abcd'})
  249. def test_index_with_checksum_filter_single_image(self):
  250. req = unit_test_utils.get_fake_request('/images?checksum=%s' % CHKSUM)
  251. output = self.controller.index(req, filters={'checksum': CHKSUM})
  252. self.assertEqual(1, len(output['images']))
  253. actual = list([image.image_id for image in output['images']])
  254. expected = [UUID1]
  255. self.assertEqual(expected, actual)
  256. def test_index_with_checksum_filter_multiple_images(self):
  257. req = unit_test_utils.get_fake_request('/images?checksum=%s' % CHKSUM1)
  258. output = self.controller.index(req, filters={'checksum': CHKSUM1})
  259. self.assertEqual(2, len(output['images']))
  260. actual = list([image.image_id for image in output['images']])
  261. expected = [UUID3, UUID2]
  262. self.assertEqual(expected, actual)
  263. def test_index_with_non_existent_checksum(self):
  264. req = unit_test_utils.get_fake_request('/images?checksum=236231827')
  265. output = self.controller.index(req, filters={'checksum': '236231827'})
  266. self.assertEqual(0, len(output['images']))
  267. def test_index_with_os_hash_value_filter_single_image(self):
  268. req = unit_test_utils.get_fake_request(
  269. '/images?os_hash_value=%s' % MULTIHASH1)
  270. output = self.controller.index(req,
  271. filters={'os_hash_value': MULTIHASH1})
  272. self.assertEqual(1, len(output['images']))
  273. actual = list([image.image_id for image in output['images']])
  274. expected = [UUID1]
  275. self.assertEqual(expected, actual)
  276. def test_index_with_os_hash_value_filter_multiple_images(self):
  277. req = unit_test_utils.get_fake_request(
  278. '/images?os_hash_value=%s' % MULTIHASH2)
  279. output = self.controller.index(req,
  280. filters={'os_hash_value': MULTIHASH2})
  281. self.assertEqual(2, len(output['images']))
  282. actual = list([image.image_id for image in output['images']])
  283. expected = [UUID3, UUID2]
  284. self.assertEqual(expected, actual)
  285. def test_index_with_non_existent_os_hash_value(self):
  286. fake_hash_value = hashlib.sha512(b'not_used_in_fixtures').hexdigest()
  287. req = unit_test_utils.get_fake_request(
  288. '/images?os_hash_value=%s' % fake_hash_value)
  289. output = self.controller.index(req,
  290. filters={'checksum': fake_hash_value})
  291. self.assertEqual(0, len(output['images']))
  292. def test_index_size_max_filter(self):
  293. request = unit_test_utils.get_fake_request('/images?size_max=512')
  294. output = self.controller.index(request, filters={'size_max': 512})
  295. self.assertEqual(3, len(output['images']))
  296. actual = set([image.image_id for image in output['images']])
  297. expected = set([UUID1, UUID2, UUID3])
  298. self.assertEqual(expected, actual)
  299. def test_index_size_min_filter(self):
  300. request = unit_test_utils.get_fake_request('/images?size_min=512')
  301. output = self.controller.index(request, filters={'size_min': 512})
  302. self.assertEqual(2, len(output['images']))
  303. actual = set([image.image_id for image in output['images']])
  304. expected = set([UUID2, UUID3])
  305. self.assertEqual(expected, actual)
  306. def test_index_size_range_filter(self):
  307. path = '/images?size_min=512&size_max=512'
  308. request = unit_test_utils.get_fake_request(path)
  309. output = self.controller.index(request,
  310. filters={'size_min': 512,
  311. 'size_max': 512})
  312. self.assertEqual(2, len(output['images']))
  313. actual = set([image.image_id for image in output['images']])
  314. expected = set([UUID2, UUID3])
  315. self.assertEqual(expected, actual)
  316. def test_index_virtual_size_max_filter(self):
  317. ref = '/images?virtual_size_max=2048'
  318. request = unit_test_utils.get_fake_request(ref)
  319. output = self.controller.index(request,
  320. filters={'virtual_size_max': 2048})
  321. self.assertEqual(3, len(output['images']))
  322. actual = set([image.image_id for image in output['images']])
  323. expected = set([UUID1, UUID2, UUID3])
  324. self.assertEqual(expected, actual)
  325. def test_index_virtual_size_min_filter(self):
  326. ref = '/images?virtual_size_min=2048'
  327. request = unit_test_utils.get_fake_request(ref)
  328. output = self.controller.index(request,
  329. filters={'virtual_size_min': 2048})
  330. self.assertEqual(2, len(output['images']))
  331. actual = set([image.image_id for image in output['images']])
  332. expected = set([UUID2, UUID3])
  333. self.assertEqual(expected, actual)
  334. def test_index_virtual_size_range_filter(self):
  335. path = '/images?virtual_size_min=512&virtual_size_max=2048'
  336. request = unit_test_utils.get_fake_request(path)
  337. output = self.controller.index(request,
  338. filters={'virtual_size_min': 2048,
  339. 'virtual_size_max': 2048})
  340. self.assertEqual(2, len(output['images']))
  341. actual = set([image.image_id for image in output['images']])
  342. expected = set([UUID2, UUID3])
  343. self.assertEqual(expected, actual)
  344. def test_index_with_invalid_max_range_filter_value(self):
  345. request = unit_test_utils.get_fake_request('/images?size_max=blah')
  346. self.assertRaises(webob.exc.HTTPBadRequest,
  347. self.controller.index,
  348. request,
  349. filters={'size_max': 'blah'})
  350. def test_index_with_filters_return_many(self):
  351. path = '/images?status=queued'
  352. request = unit_test_utils.get_fake_request(path)
  353. output = self.controller.index(request, filters={'status': 'queued'})
  354. self.assertEqual(1, len(output['images']))
  355. actual = set([image.image_id for image in output['images']])
  356. expected = set([UUID3])
  357. self.assertEqual(expected, actual)
  358. def test_index_with_nonexistent_name_filter(self):
  359. request = unit_test_utils.get_fake_request('/images?name=%s' % 'blah')
  360. images = self.controller.index(request,
  361. filters={'name': 'blah'})['images']
  362. self.assertEqual(0, len(images))
  363. def test_index_with_non_default_is_public_filter(self):
  364. private_uuid = str(uuid.uuid4())
  365. new_image = _db_fixture(private_uuid,
  366. visibility='private',
  367. owner=TENANT3)
  368. self.db.image_create(None, new_image)
  369. path = '/images?visibility=private'
  370. request = unit_test_utils.get_fake_request(path, is_admin=True)
  371. output = self.controller.index(request,
  372. filters={'visibility': 'private'})
  373. self.assertEqual(1, len(output['images']))
  374. actual = set([image.image_id for image in output['images']])
  375. expected = set([private_uuid])
  376. self.assertEqual(expected, actual)
  377. path = '/images?visibility=shared'
  378. request = unit_test_utils.get_fake_request(path, is_admin=True)
  379. output = self.controller.index(request,
  380. filters={'visibility': 'shared'})
  381. self.assertEqual(1, len(output['images']))
  382. actual = set([image.image_id for image in output['images']])
  383. expected = set([UUID4])
  384. self.assertEqual(expected, actual)
  385. def test_index_with_many_filters(self):
  386. url = '/images?status=queued&name=3'
  387. request = unit_test_utils.get_fake_request(url)
  388. output = self.controller.index(request,
  389. filters={
  390. 'status': 'queued',
  391. 'name': '3',
  392. })
  393. self.assertEqual(1, len(output['images']))
  394. actual = set([image.image_id for image in output['images']])
  395. expected = set([UUID3])
  396. self.assertEqual(expected, actual)
  397. def test_index_with_marker(self):
  398. self.config(limit_param_default=1, api_limit_max=3)
  399. path = '/images'
  400. request = unit_test_utils.get_fake_request(path)
  401. output = self.controller.index(request, marker=UUID3)
  402. actual = set([image.image_id for image in output['images']])
  403. self.assertEqual(1, len(actual))
  404. self.assertIn(UUID2, actual)
  405. def test_index_with_limit(self):
  406. path = '/images'
  407. limit = 2
  408. request = unit_test_utils.get_fake_request(path)
  409. output = self.controller.index(request, limit=limit)
  410. actual = set([image.image_id for image in output['images']])
  411. self.assertEqual(limit, len(actual))
  412. self.assertIn(UUID3, actual)
  413. self.assertIn(UUID2, actual)
  414. def test_index_greater_than_limit_max(self):
  415. self.config(limit_param_default=1, api_limit_max=3)
  416. path = '/images'
  417. request = unit_test_utils.get_fake_request(path)
  418. output = self.controller.index(request, limit=4)
  419. actual = set([image.image_id for image in output['images']])
  420. self.assertEqual(3, len(actual))
  421. self.assertNotIn(output['next_marker'], output)
  422. def test_index_default_limit(self):
  423. self.config(limit_param_default=1, api_limit_max=3)
  424. path = '/images'
  425. request = unit_test_utils.get_fake_request(path)
  426. output = self.controller.index(request)
  427. actual = set([image.image_id for image in output['images']])
  428. self.assertEqual(1, len(actual))
  429. def test_index_with_sort_dir(self):
  430. path = '/images'
  431. request = unit_test_utils.get_fake_request(path)
  432. output = self.controller.index(request, sort_dir=['asc'], limit=3)
  433. actual = [image.image_id for image in output['images']]
  434. self.assertEqual(3, len(actual))
  435. self.assertEqual(UUID1, actual[0])
  436. self.assertEqual(UUID2, actual[1])
  437. self.assertEqual(UUID3, actual[2])
  438. def test_index_with_sort_key(self):
  439. path = '/images'
  440. request = unit_test_utils.get_fake_request(path)
  441. output = self.controller.index(request, sort_key=['created_at'],
  442. limit=3)
  443. actual = [image.image_id for image in output['images']]
  444. self.assertEqual(3, len(actual))
  445. self.assertEqual(UUID3, actual[0])
  446. self.assertEqual(UUID2, actual[1])
  447. self.assertEqual(UUID1, actual[2])
  448. def test_index_with_multiple_sort_keys(self):
  449. path = '/images'
  450. request = unit_test_utils.get_fake_request(path)
  451. output = self.controller.index(request,
  452. sort_key=['created_at', 'name'],
  453. limit=3)
  454. actual = [image.image_id for image in output['images']]
  455. self.assertEqual(3, len(actual))
  456. self.assertEqual(UUID3, actual[0])
  457. self.assertEqual(UUID2, actual[1])
  458. self.assertEqual(UUID1, actual[2])
  459. def test_index_with_marker_not_found(self):
  460. fake_uuid = str(uuid.uuid4())
  461. path = '/images'
  462. request = unit_test_utils.get_fake_request(path)
  463. self.assertRaises(webob.exc.HTTPBadRequest,
  464. self.controller.index, request, marker=fake_uuid)
  465. def test_index_invalid_sort_key(self):
  466. path = '/images'
  467. request = unit_test_utils.get_fake_request(path)
  468. self.assertRaises(webob.exc.HTTPBadRequest,
  469. self.controller.index, request, sort_key=['foo'])
  470. def test_index_zero_images(self):
  471. self.db.reset()
  472. request = unit_test_utils.get_fake_request()
  473. output = self.controller.index(request)
  474. self.assertEqual([], output['images'])
  475. def test_index_with_tags(self):
  476. path = '/images?tag=64bit'
  477. request = unit_test_utils.get_fake_request(path)
  478. output = self.controller.index(request, filters={'tags': ['64bit']})
  479. actual = [image.tags for image in output['images']]
  480. self.assertEqual(2, len(actual))
  481. self.assertIn('64bit', actual[0])
  482. self.assertIn('64bit', actual[1])
  483. def test_index_with_multi_tags(self):
  484. path = '/images?tag=power&tag=64bit'
  485. request = unit_test_utils.get_fake_request(path)
  486. output = self.controller.index(request,
  487. filters={'tags': ['power', '64bit']})
  488. actual = [image.tags for image in output['images']]
  489. self.assertEqual(1, len(actual))
  490. self.assertIn('64bit', actual[0])
  491. self.assertIn('power', actual[0])
  492. def test_index_with_multi_tags_and_nonexistent(self):
  493. path = '/images?tag=power&tag=fake'
  494. request = unit_test_utils.get_fake_request(path)
  495. output = self.controller.index(request,
  496. filters={'tags': ['power', 'fake']})
  497. actual = [image.tags for image in output['images']]
  498. self.assertEqual(0, len(actual))
  499. def test_index_with_tags_and_properties(self):
  500. path = '/images?tag=64bit&hypervisor_type=kvm'
  501. request = unit_test_utils.get_fake_request(path)
  502. output = self.controller.index(request,
  503. filters={'tags': ['64bit'],
  504. 'hypervisor_type': 'kvm'})
  505. tags = [image.tags for image in output['images']]
  506. properties = [image.extra_properties for image in output['images']]
  507. self.assertEqual(len(tags), len(properties))
  508. self.assertIn('64bit', tags[0])
  509. self.assertEqual('kvm', properties[0]['hypervisor_type'])
  510. def test_index_with_multiple_properties(self):
  511. path = '/images?foo=bar&hypervisor_type=kvm'
  512. request = unit_test_utils.get_fake_request(path)
  513. output = self.controller.index(request,
  514. filters={'foo': 'bar',
  515. 'hypervisor_type': 'kvm'})
  516. properties = [image.extra_properties for image in output['images']]
  517. self.assertEqual('kvm', properties[0]['hypervisor_type'])
  518. self.assertEqual('bar', properties[0]['foo'])
  519. def test_index_with_core_and_extra_property(self):
  520. path = '/images?disk_format=raw&foo=bar'
  521. request = unit_test_utils.get_fake_request(path)
  522. output = self.controller.index(request,
  523. filters={'foo': 'bar',
  524. 'disk_format': 'raw'})
  525. properties = [image.extra_properties for image in output['images']]
  526. self.assertEqual(1, len(output['images']))
  527. self.assertEqual('raw', output['images'][0].disk_format)
  528. self.assertEqual('bar', properties[0]['foo'])
  529. def test_index_with_nonexistent_properties(self):
  530. path = '/images?abc=xyz&pudding=banana'
  531. request = unit_test_utils.get_fake_request(path)
  532. output = self.controller.index(request,
  533. filters={'abc': 'xyz',
  534. 'pudding': 'banana'})
  535. self.assertEqual(0, len(output['images']))
  536. def test_index_with_non_existent_tags(self):
  537. path = '/images?tag=fake'
  538. request = unit_test_utils.get_fake_request(path)
  539. output = self.controller.index(request,
  540. filters={'tags': ['fake']})
  541. actual = [image.tags for image in output['images']]
  542. self.assertEqual(0, len(actual))
  543. def test_show(self):
  544. request = unit_test_utils.get_fake_request()
  545. output = self.controller.show(request, image_id=UUID2)
  546. self.assertEqual(UUID2, output.image_id)
  547. self.assertEqual('2', output.name)
  548. def test_show_deleted_properties(self):
  549. """Ensure that the api filters out deleted image properties."""
  550. # get the image properties into the odd state
  551. image = {
  552. 'id': str(uuid.uuid4()),
  553. 'status': 'active',
  554. 'properties': {'poo': 'bear'},
  555. }
  556. self.db.image_create(None, image)
  557. self.db.image_update(None, image['id'],
  558. {'properties': {'yin': 'yang'}},
  559. purge_props=True)
  560. request = unit_test_utils.get_fake_request()
  561. output = self.controller.show(request, image['id'])
  562. self.assertEqual('yang', output.extra_properties['yin'])
  563. def test_show_non_existent(self):
  564. request = unit_test_utils.get_fake_request()
  565. image_id = str(uuid.uuid4())
  566. self.assertRaises(webob.exc.HTTPNotFound,
  567. self.controller.show, request, image_id)
  568. def test_show_deleted_image_admin(self):
  569. request = unit_test_utils.get_fake_request(is_admin=True)
  570. self.controller.delete(request, UUID1)
  571. self.assertRaises(webob.exc.HTTPNotFound,
  572. self.controller.show, request, UUID1)
  573. def test_show_not_allowed(self):
  574. request = unit_test_utils.get_fake_request()
  575. self.assertEqual(TENANT1, request.context.tenant)
  576. self.assertRaises(webob.exc.HTTPNotFound,
  577. self.controller.show, request, UUID4)
  578. def test_image_import_raises_conflict_if_container_format_is_none(self):
  579. request = unit_test_utils.get_fake_request()
  580. with mock.patch.object(
  581. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  582. mock_get.return_value = FakeImage(container_format=None)
  583. self.assertRaises(webob.exc.HTTPConflict,
  584. self.controller.import_image, request, UUID4,
  585. {'method': {'name': 'glance-direct'}})
  586. def test_image_import_raises_conflict_if_disk_format_is_none(self):
  587. request = unit_test_utils.get_fake_request()
  588. with mock.patch.object(
  589. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  590. mock_get.return_value = FakeImage(disk_format=None)
  591. self.assertRaises(webob.exc.HTTPConflict,
  592. self.controller.import_image, request, UUID4,
  593. {'method': {'name': 'glance-direct'}})
  594. def test_image_import_raises_conflict(self):
  595. request = unit_test_utils.get_fake_request()
  596. with mock.patch.object(
  597. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  598. mock_get.return_value = FakeImage(status='queued')
  599. self.assertRaises(webob.exc.HTTPConflict,
  600. self.controller.import_image, request, UUID4,
  601. {'method': {'name': 'glance-direct'}})
  602. def test_image_import_raises_conflict_for_web_download(self):
  603. request = unit_test_utils.get_fake_request()
  604. with mock.patch.object(
  605. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  606. mock_get.return_value = FakeImage()
  607. self.assertRaises(webob.exc.HTTPConflict,
  608. self.controller.import_image, request, UUID4,
  609. {'method': {'name': 'web-download'}})
  610. def test_image_import_raises_conflict_for_invalid_status_change(self):
  611. request = unit_test_utils.get_fake_request()
  612. with mock.patch.object(
  613. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  614. mock_get.return_value = FakeImage()
  615. self.assertRaises(webob.exc.HTTPConflict,
  616. self.controller.import_image, request, UUID4,
  617. {'method': {'name': 'glance-direct'}})
  618. def test_image_import_raises_bad_request(self):
  619. request = unit_test_utils.get_fake_request()
  620. with mock.patch.object(
  621. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  622. mock_get.return_value = FakeImage(status='uploading')
  623. # NOTE(abhishekk): Due to
  624. # https://bugs.launchpad.net/glance/+bug/1712463 taskflow is not
  625. # executing. Once it is fixed instead of mocking spawn_n method
  626. # we should mock execute method of _ImportToStore task.
  627. with mock.patch.object(eventlet.GreenPool, 'spawn_n',
  628. side_effect=ValueError):
  629. self.assertRaises(webob.exc.HTTPBadRequest,
  630. self.controller.import_image, request, UUID4,
  631. {'method': {'name': 'glance-direct'}})
  632. def test_image_import_invalid_uri_filtering(self):
  633. request = unit_test_utils.get_fake_request()
  634. with mock.patch.object(
  635. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  636. mock_get.return_value = FakeImage(status='queued')
  637. self.assertRaises(webob.exc.HTTPBadRequest,
  638. self.controller.import_image, request, UUID4,
  639. {'method': {'name': 'web-download',
  640. 'uri': 'fake_uri'}})
  641. def test_create(self):
  642. request = unit_test_utils.get_fake_request()
  643. image = {'name': 'image-1'}
  644. output = self.controller.create(request, image=image,
  645. extra_properties={},
  646. tags=[])
  647. self.assertEqual('image-1', output.name)
  648. self.assertEqual({}, output.extra_properties)
  649. self.assertEqual(set([]), output.tags)
  650. self.assertEqual('shared', output.visibility)
  651. output_logs = self.notifier.get_logs()
  652. self.assertEqual(1, len(output_logs))
  653. output_log = output_logs[0]
  654. self.assertEqual('INFO', output_log['notification_type'])
  655. self.assertEqual('image.create', output_log['event_type'])
  656. self.assertEqual('image-1', output_log['payload']['name'])
  657. def test_create_disabled_notification(self):
  658. self.config(disabled_notifications=["image.create"])
  659. request = unit_test_utils.get_fake_request()
  660. image = {'name': 'image-1'}
  661. output = self.controller.create(request, image=image,
  662. extra_properties={},
  663. tags=[])
  664. self.assertEqual('image-1', output.name)
  665. self.assertEqual({}, output.extra_properties)
  666. self.assertEqual(set([]), output.tags)
  667. self.assertEqual('shared', output.visibility)
  668. output_logs = self.notifier.get_logs()
  669. self.assertEqual(0, len(output_logs))
  670. def test_create_with_properties(self):
  671. request = unit_test_utils.get_fake_request()
  672. image_properties = {'foo': 'bar'}
  673. image = {'name': 'image-1'}
  674. output = self.controller.create(request, image=image,
  675. extra_properties=image_properties,
  676. tags=[])
  677. self.assertEqual('image-1', output.name)
  678. self.assertEqual(image_properties, output.extra_properties)
  679. self.assertEqual(set([]), output.tags)
  680. self.assertEqual('shared', output.visibility)
  681. output_logs = self.notifier.get_logs()
  682. self.assertEqual(1, len(output_logs))
  683. output_log = output_logs[0]
  684. self.assertEqual('INFO', output_log['notification_type'])
  685. self.assertEqual('image.create', output_log['event_type'])
  686. self.assertEqual('image-1', output_log['payload']['name'])
  687. def test_create_with_too_many_properties(self):
  688. self.config(image_property_quota=1)
  689. request = unit_test_utils.get_fake_request()
  690. image_properties = {'foo': 'bar', 'foo2': 'bar'}
  691. image = {'name': 'image-1'}
  692. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  693. self.controller.create, request,
  694. image=image,
  695. extra_properties=image_properties,
  696. tags=[])
  697. def test_create_with_bad_min_disk_size(self):
  698. request = unit_test_utils.get_fake_request()
  699. image = {'min_disk': -42, 'name': 'image-1'}
  700. self.assertRaises(webob.exc.HTTPBadRequest,
  701. self.controller.create, request,
  702. image=image,
  703. extra_properties={},
  704. tags=[])
  705. def test_create_with_bad_min_ram_size(self):
  706. request = unit_test_utils.get_fake_request()
  707. image = {'min_ram': -42, 'name': 'image-1'}
  708. self.assertRaises(webob.exc.HTTPBadRequest,
  709. self.controller.create, request,
  710. image=image,
  711. extra_properties={},
  712. tags=[])
  713. def test_create_public_image_as_admin(self):
  714. request = unit_test_utils.get_fake_request()
  715. image = {'name': 'image-1', 'visibility': 'public'}
  716. output = self.controller.create(request, image=image,
  717. extra_properties={}, tags=[])
  718. self.assertEqual('public', output.visibility)
  719. output_logs = self.notifier.get_logs()
  720. self.assertEqual(1, len(output_logs))
  721. output_log = output_logs[0]
  722. self.assertEqual('INFO', output_log['notification_type'])
  723. self.assertEqual('image.create', output_log['event_type'])
  724. self.assertEqual(output.image_id, output_log['payload']['id'])
  725. def test_create_dup_id(self):
  726. request = unit_test_utils.get_fake_request()
  727. image = {'image_id': UUID4}
  728. self.assertRaises(webob.exc.HTTPConflict,
  729. self.controller.create,
  730. request,
  731. image=image,
  732. extra_properties={},
  733. tags=[])
  734. def test_create_duplicate_tags(self):
  735. request = unit_test_utils.get_fake_request()
  736. tags = ['ping', 'ping']
  737. output = self.controller.create(request, image={},
  738. extra_properties={}, tags=tags)
  739. self.assertEqual(set(['ping']), output.tags)
  740. output_logs = self.notifier.get_logs()
  741. self.assertEqual(1, len(output_logs))
  742. output_log = output_logs[0]
  743. self.assertEqual('INFO', output_log['notification_type'])
  744. self.assertEqual('image.create', output_log['event_type'])
  745. self.assertEqual(output.image_id, output_log['payload']['id'])
  746. def test_create_with_too_many_tags(self):
  747. self.config(image_tag_quota=1)
  748. request = unit_test_utils.get_fake_request()
  749. tags = ['ping', 'pong']
  750. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  751. self.controller.create,
  752. request, image={}, extra_properties={},
  753. tags=tags)
  754. def test_create_with_owner_non_admin(self):
  755. request = unit_test_utils.get_fake_request()
  756. request.context.is_admin = False
  757. image = {'owner': '12345'}
  758. self.assertRaises(webob.exc.HTTPForbidden,
  759. self.controller.create,
  760. request, image=image, extra_properties={},
  761. tags=[])
  762. request = unit_test_utils.get_fake_request()
  763. request.context.is_admin = False
  764. image = {'owner': TENANT1}
  765. output = self.controller.create(request, image=image,
  766. extra_properties={}, tags=[])
  767. self.assertEqual(TENANT1, output.owner)
  768. def test_create_with_owner_admin(self):
  769. request = unit_test_utils.get_fake_request()
  770. request.context.is_admin = True
  771. image = {'owner': '12345'}
  772. output = self.controller.create(request, image=image,
  773. extra_properties={}, tags=[])
  774. self.assertEqual('12345', output.owner)
  775. def test_create_with_duplicate_location(self):
  776. request = unit_test_utils.get_fake_request()
  777. location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  778. image = {'name': 'image-1', 'locations': [location, location]}
  779. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create,
  780. request, image=image, extra_properties={},
  781. tags=[])
  782. def test_create_unexpected_property(self):
  783. request = unit_test_utils.get_fake_request()
  784. image_properties = {'unexpected': 'unexpected'}
  785. image = {'name': 'image-1'}
  786. with mock.patch.object(domain.ImageFactory, 'new_image',
  787. side_effect=TypeError):
  788. self.assertRaises(webob.exc.HTTPBadRequest,
  789. self.controller.create, request, image=image,
  790. extra_properties=image_properties, tags=[])
  791. def test_create_reserved_property(self):
  792. request = unit_test_utils.get_fake_request()
  793. image_properties = {'reserved': 'reserved'}
  794. image = {'name': 'image-1'}
  795. with mock.patch.object(domain.ImageFactory, 'new_image',
  796. side_effect=exception.ReservedProperty(
  797. property='reserved')):
  798. self.assertRaises(webob.exc.HTTPForbidden,
  799. self.controller.create, request, image=image,
  800. extra_properties=image_properties, tags=[])
  801. def test_create_readonly_property(self):
  802. request = unit_test_utils.get_fake_request()
  803. image_properties = {'readonly': 'readonly'}
  804. image = {'name': 'image-1'}
  805. with mock.patch.object(domain.ImageFactory, 'new_image',
  806. side_effect=exception.ReadonlyProperty(
  807. property='readonly')):
  808. self.assertRaises(webob.exc.HTTPForbidden,
  809. self.controller.create, request, image=image,
  810. extra_properties=image_properties, tags=[])
  811. def test_update_no_changes(self):
  812. request = unit_test_utils.get_fake_request()
  813. output = self.controller.update(request, UUID1, changes=[])
  814. self.assertEqual(UUID1, output.image_id)
  815. self.assertEqual(output.created_at, output.updated_at)
  816. self.assertEqual(2, len(output.tags))
  817. self.assertIn('ping', output.tags)
  818. self.assertIn('pong', output.tags)
  819. output_logs = self.notifier.get_logs()
  820. # NOTE(markwash): don't send a notification if nothing is updated
  821. self.assertEqual(0, len(output_logs))
  822. def test_update_queued_image_with_hidden(self):
  823. request = unit_test_utils.get_fake_request()
  824. changes = [{'op': 'replace', 'path': ['os_hidden'], 'value': 'true'}]
  825. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  826. request, UUID3, changes=changes)
  827. def test_update_with_bad_min_disk(self):
  828. request = unit_test_utils.get_fake_request()
  829. changes = [{'op': 'replace', 'path': ['min_disk'], 'value': -42}]
  830. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  831. request, UUID1, changes=changes)
  832. def test_update_with_bad_min_ram(self):
  833. request = unit_test_utils.get_fake_request()
  834. changes = [{'op': 'replace', 'path': ['min_ram'], 'value': -42}]
  835. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  836. request, UUID1, changes=changes)
  837. def test_update_image_doesnt_exist(self):
  838. request = unit_test_utils.get_fake_request()
  839. self.assertRaises(webob.exc.HTTPNotFound, self.controller.update,
  840. request, str(uuid.uuid4()), changes=[])
  841. def test_update_deleted_image_admin(self):
  842. request = unit_test_utils.get_fake_request(is_admin=True)
  843. self.controller.delete(request, UUID1)
  844. self.assertRaises(webob.exc.HTTPNotFound, self.controller.update,
  845. request, UUID1, changes=[])
  846. def test_update_with_too_many_properties(self):
  847. self.config(show_multiple_locations=True)
  848. self.config(user_storage_quota='1')
  849. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  850. request = unit_test_utils.get_fake_request()
  851. changes = [{'op': 'add', 'path': ['locations', '-'],
  852. 'value': new_location}]
  853. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  854. self.controller.update,
  855. request, UUID1, changes=changes)
  856. def test_update_replace_base_attribute(self):
  857. self.db.image_update(None, UUID1, {'properties': {'foo': 'bar'}})
  858. request = unit_test_utils.get_fake_request()
  859. request.context.is_admin = True
  860. changes = [{'op': 'replace', 'path': ['name'], 'value': 'fedora'},
  861. {'op': 'replace', 'path': ['owner'], 'value': TENANT3}]
  862. output = self.controller.update(request, UUID1, changes)
  863. self.assertEqual(UUID1, output.image_id)
  864. self.assertEqual('fedora', output.name)
  865. self.assertEqual(TENANT3, output.owner)
  866. self.assertEqual({'foo': 'bar'}, output.extra_properties)
  867. self.assertNotEqual(output.created_at, output.updated_at)
  868. def test_update_replace_onwer_non_admin(self):
  869. request = unit_test_utils.get_fake_request()
  870. request.context.is_admin = False
  871. changes = [{'op': 'replace', 'path': ['owner'], 'value': TENANT3}]
  872. self.assertRaises(webob.exc.HTTPForbidden,
  873. self.controller.update, request, UUID1, changes)
  874. def test_update_replace_tags(self):
  875. request = unit_test_utils.get_fake_request()
  876. changes = [
  877. {'op': 'replace', 'path': ['tags'], 'value': ['king', 'kong']},
  878. ]
  879. output = self.controller.update(request, UUID1, changes)
  880. self.assertEqual(UUID1, output.image_id)
  881. self.assertEqual(2, len(output.tags))
  882. self.assertIn('king', output.tags)
  883. self.assertIn('kong', output.tags)
  884. self.assertNotEqual(output.created_at, output.updated_at)
  885. def test_update_replace_property(self):
  886. request = unit_test_utils.get_fake_request()
  887. properties = {'foo': 'bar', 'snitch': 'golden'}
  888. self.db.image_update(None, UUID1, {'properties': properties})
  889. output = self.controller.show(request, UUID1)
  890. self.assertEqual('bar', output.extra_properties['foo'])
  891. self.assertEqual('golden', output.extra_properties['snitch'])
  892. changes = [
  893. {'op': 'replace', 'path': ['foo'], 'value': 'baz'},
  894. ]
  895. output = self.controller.update(request, UUID1, changes)
  896. self.assertEqual(UUID1, output.image_id)
  897. self.assertEqual('baz', output.extra_properties['foo'])
  898. self.assertEqual('golden', output.extra_properties['snitch'])
  899. self.assertNotEqual(output.created_at, output.updated_at)
  900. def test_update_add_too_many_properties(self):
  901. self.config(image_property_quota=1)
  902. request = unit_test_utils.get_fake_request()
  903. changes = [
  904. {'op': 'add', 'path': ['foo'], 'value': 'baz'},
  905. {'op': 'add', 'path': ['snitch'], 'value': 'golden'},
  906. ]
  907. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  908. self.controller.update, request,
  909. UUID1, changes)
  910. def test_update_add_and_remove_too_many_properties(self):
  911. request = unit_test_utils.get_fake_request()
  912. changes = [
  913. {'op': 'add', 'path': ['foo'], 'value': 'baz'},
  914. {'op': 'add', 'path': ['snitch'], 'value': 'golden'},
  915. ]
  916. self.controller.update(request, UUID1, changes)
  917. self.config(image_property_quota=1)
  918. # We must remove two properties to avoid being
  919. # over the limit of 1 property
  920. changes = [
  921. {'op': 'remove', 'path': ['foo']},
  922. {'op': 'add', 'path': ['fizz'], 'value': 'buzz'},
  923. ]
  924. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  925. self.controller.update, request,
  926. UUID1, changes)
  927. def test_update_add_unlimited_properties(self):
  928. self.config(image_property_quota=-1)
  929. request = unit_test_utils.get_fake_request()
  930. output = self.controller.show(request, UUID1)
  931. changes = [{'op': 'add',
  932. 'path': ['foo'],
  933. 'value': 'bar'}]
  934. output = self.controller.update(request, UUID1, changes)
  935. self.assertEqual(UUID1, output.image_id)
  936. self.assertNotEqual(output.created_at, output.updated_at)
  937. def test_update_format_properties(self):
  938. statuses_for_immutability = ['active', 'saving', 'killed']
  939. request = unit_test_utils.get_fake_request(is_admin=True)
  940. for status in statuses_for_immutability:
  941. image = {
  942. 'id': str(uuid.uuid4()),
  943. 'status': status,
  944. 'disk_format': 'ari',
  945. 'container_format': 'ari',
  946. }
  947. self.db.image_create(None, image)
  948. changes = [
  949. {'op': 'replace', 'path': ['disk_format'], 'value': 'ami'},
  950. ]
  951. self.assertRaises(webob.exc.HTTPForbidden,
  952. self.controller.update,
  953. request, image['id'], changes)
  954. changes = [
  955. {'op': 'replace',
  956. 'path': ['container_format'],
  957. 'value': 'ami'},
  958. ]
  959. self.assertRaises(webob.exc.HTTPForbidden,
  960. self.controller.update,
  961. request, image['id'], changes)
  962. self.db.image_update(None, image['id'], {'status': 'queued'})
  963. changes = [
  964. {'op': 'replace', 'path': ['disk_format'], 'value': 'raw'},
  965. {'op': 'replace', 'path': ['container_format'], 'value': 'bare'},
  966. ]
  967. resp = self.controller.update(request, image['id'], changes)
  968. self.assertEqual('raw', resp.disk_format)
  969. self.assertEqual('bare', resp.container_format)
  970. def test_update_remove_property_while_over_limit(self):
  971. """Ensure that image properties can be removed.
  972. Image properties should be able to be removed as long as the image has
  973. fewer than the limited number of image properties after the
  974. transaction.
  975. """
  976. request = unit_test_utils.get_fake_request()
  977. changes = [
  978. {'op': 'add', 'path': ['foo'], 'value': 'baz'},
  979. {'op': 'add', 'path': ['snitch'], 'value': 'golden'},
  980. {'op': 'add', 'path': ['fizz'], 'value': 'buzz'},
  981. ]
  982. self.controller.update(request, UUID1, changes)
  983. self.config(image_property_quota=1)
  984. # We must remove two properties to avoid being
  985. # over the limit of 1 property
  986. changes = [
  987. {'op': 'remove', 'path': ['foo']},
  988. {'op': 'remove', 'path': ['snitch']},
  989. ]
  990. output = self.controller.update(request, UUID1, changes)
  991. self.assertEqual(UUID1, output.image_id)
  992. self.assertEqual(1, len(output.extra_properties))
  993. self.assertEqual('buzz', output.extra_properties['fizz'])
  994. self.assertNotEqual(output.created_at, output.updated_at)
  995. def test_update_add_and_remove_property_under_limit(self):
  996. """Ensure that image properties can be removed.
  997. Image properties should be able to be added and removed simultaneously
  998. as long as the image has fewer than the limited number of image
  999. properties after the transaction.
  1000. """
  1001. request = unit_test_utils.get_fake_request()
  1002. changes = [
  1003. {'op': 'add', 'path': ['foo'], 'value': 'baz'},
  1004. {'op': 'add', 'path': ['snitch'], 'value': 'golden'},
  1005. ]
  1006. self.controller.update(request, UUID1, changes)
  1007. self.config(image_property_quota=1)
  1008. # We must remove two properties to avoid being
  1009. # over the limit of 1 property
  1010. changes = [
  1011. {'op': 'remove', 'path': ['foo']},
  1012. {'op': 'remove', 'path': ['snitch']},
  1013. {'op': 'add', 'path': ['fizz'], 'value': 'buzz'},
  1014. ]
  1015. output = self.controller.update(request, UUID1, changes)
  1016. self.assertEqual(UUID1, output.image_id)
  1017. self.assertEqual(1, len(output.extra_properties))
  1018. self.assertEqual('buzz', output.extra_properties['fizz'])
  1019. self.assertNotEqual(output.created_at, output.updated_at)
  1020. def test_update_replace_missing_property(self):
  1021. request = unit_test_utils.get_fake_request()
  1022. changes = [
  1023. {'op': 'replace', 'path': 'foo', 'value': 'baz'},
  1024. ]
  1025. self.assertRaises(webob.exc.HTTPConflict,
  1026. self.controller.update, request, UUID1, changes)
  1027. def test_prop_protection_with_create_and_permitted_role(self):
  1028. enforcer = glance.api.policy.Enforcer()
  1029. self.controller = glance.api.v2.images.ImagesController(self.db,
  1030. enforcer,
  1031. self.notifier,
  1032. self.store)
  1033. self.set_property_protections()
  1034. request = unit_test_utils.get_fake_request(roles=['admin'])
  1035. image = {'name': 'image-1'}
  1036. created_image = self.controller.create(request, image=image,
  1037. extra_properties={},
  1038. tags=[])
  1039. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1040. changes = [
  1041. {'op': 'add', 'path': ['x_owner_foo'], 'value': 'bar'},
  1042. ]
  1043. output = self.controller.update(another_request,
  1044. created_image.image_id, changes)
  1045. self.assertEqual('bar', output.extra_properties['x_owner_foo'])
  1046. def test_prop_protection_with_update_and_permitted_policy(self):
  1047. self.set_property_protections(use_policies=True)
  1048. enforcer = glance.api.policy.Enforcer()
  1049. self.controller = glance.api.v2.images.ImagesController(self.db,
  1050. enforcer,
  1051. self.notifier,
  1052. self.store)
  1053. request = unit_test_utils.get_fake_request(roles=['spl_role'])
  1054. image = {'name': 'image-1'}
  1055. extra_props = {'spl_creator_policy': 'bar'}
  1056. created_image = self.controller.create(request, image=image,
  1057. extra_properties=extra_props,
  1058. tags=[])
  1059. self.assertEqual('bar',
  1060. created_image.extra_properties['spl_creator_policy'])
  1061. another_request = unit_test_utils.get_fake_request(roles=['spl_role'])
  1062. changes = [
  1063. {'op': 'replace', 'path': ['spl_creator_policy'], 'value': 'par'},
  1064. ]
  1065. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  1066. another_request, created_image.image_id, changes)
  1067. another_request = unit_test_utils.get_fake_request(roles=['admin'])
  1068. output = self.controller.update(another_request,
  1069. created_image.image_id, changes)
  1070. self.assertEqual('par',
  1071. output.extra_properties['spl_creator_policy'])
  1072. def test_prop_protection_with_create_with_patch_and_policy(self):
  1073. self.set_property_protections(use_policies=True)
  1074. enforcer = glance.api.policy.Enforcer()
  1075. self.controller = glance.api.v2.images.ImagesController(self.db,
  1076. enforcer,
  1077. self.notifier,
  1078. self.store)
  1079. request = unit_test_utils.get_fake_request(roles=['spl_role', 'admin'])
  1080. image = {'name': 'image-1'}
  1081. extra_props = {'spl_default_policy': 'bar'}
  1082. created_image = self.controller.create(request, image=image,
  1083. extra_properties=extra_props,
  1084. tags=[])
  1085. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1086. changes = [
  1087. {'op': 'add', 'path': ['spl_creator_policy'], 'value': 'bar'},
  1088. ]
  1089. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  1090. another_request, created_image.image_id, changes)
  1091. another_request = unit_test_utils.get_fake_request(roles=['spl_role'])
  1092. output = self.controller.update(another_request,
  1093. created_image.image_id, changes)
  1094. self.assertEqual('bar',
  1095. output.extra_properties['spl_creator_policy'])
  1096. def test_prop_protection_with_create_and_unpermitted_role(self):
  1097. enforcer = glance.api.policy.Enforcer()
  1098. self.controller = glance.api.v2.images.ImagesController(self.db,
  1099. enforcer,
  1100. self.notifier,
  1101. self.store)
  1102. self.set_property_protections()
  1103. request = unit_test_utils.get_fake_request(roles=['admin'])
  1104. image = {'name': 'image-1'}
  1105. created_image = self.controller.create(request, image=image,
  1106. extra_properties={},
  1107. tags=[])
  1108. roles = ['fake_member']
  1109. another_request = unit_test_utils.get_fake_request(roles=roles)
  1110. changes = [
  1111. {'op': 'add', 'path': ['x_owner_foo'], 'value': 'bar'},
  1112. ]
  1113. self.assertRaises(webob.exc.HTTPForbidden,
  1114. self.controller.update, another_request,
  1115. created_image.image_id, changes)
  1116. def test_prop_protection_with_show_and_permitted_role(self):
  1117. enforcer = glance.api.policy.Enforcer()
  1118. self.controller = glance.api.v2.images.ImagesController(self.db,
  1119. enforcer,
  1120. self.notifier,
  1121. self.store)
  1122. self.set_property_protections()
  1123. request = unit_test_utils.get_fake_request(roles=['admin'])
  1124. image = {'name': 'image-1'}
  1125. extra_props = {'x_owner_foo': 'bar'}
  1126. created_image = self.controller.create(request, image=image,
  1127. extra_properties=extra_props,
  1128. tags=[])
  1129. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1130. output = self.controller.show(another_request, created_image.image_id)
  1131. self.assertEqual('bar', output.extra_properties['x_owner_foo'])
  1132. def test_prop_protection_with_show_and_unpermitted_role(self):
  1133. enforcer = glance.api.policy.Enforcer()
  1134. self.controller = glance.api.v2.images.ImagesController(self.db,
  1135. enforcer,
  1136. self.notifier,
  1137. self.store)
  1138. self.set_property_protections()
  1139. request = unit_test_utils.get_fake_request(roles=['member'])
  1140. image = {'name': 'image-1'}
  1141. extra_props = {'x_owner_foo': 'bar'}
  1142. created_image = self.controller.create(request, image=image,
  1143. extra_properties=extra_props,
  1144. tags=[])
  1145. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1146. output = self.controller.show(another_request, created_image.image_id)
  1147. self.assertRaises(KeyError, output.extra_properties.__getitem__,
  1148. 'x_owner_foo')
  1149. def test_prop_protection_with_update_and_permitted_role(self):
  1150. enforcer = glance.api.policy.Enforcer()
  1151. self.controller = glance.api.v2.images.ImagesController(self.db,
  1152. enforcer,
  1153. self.notifier,
  1154. self.store)
  1155. self.set_property_protections()
  1156. request = unit_test_utils.get_fake_request(roles=['admin'])
  1157. image = {'name': 'image-1'}
  1158. extra_props = {'x_owner_foo': 'bar'}
  1159. created_image = self.controller.create(request, image=image,
  1160. extra_properties=extra_props,
  1161. tags=[])
  1162. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1163. changes = [
  1164. {'op': 'replace', 'path': ['x_owner_foo'], 'value': 'baz'},
  1165. ]
  1166. output = self.controller.update(another_request,
  1167. created_image.image_id, changes)
  1168. self.assertEqual('baz', output.extra_properties['x_owner_foo'])
  1169. def test_prop_protection_with_update_and_unpermitted_role(self):
  1170. enforcer = glance.api.policy.Enforcer()
  1171. self.controller = glance.api.v2.images.ImagesController(self.db,
  1172. enforcer,
  1173. self.notifier,
  1174. self.store)
  1175. self.set_property_protections()
  1176. request = unit_test_utils.get_fake_request(roles=['admin'])
  1177. image = {'name': 'image-1'}
  1178. extra_props = {'x_owner_foo': 'bar'}
  1179. created_image = self.controller.create(request, image=image,
  1180. extra_properties=extra_props,
  1181. tags=[])
  1182. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1183. changes = [
  1184. {'op': 'replace', 'path': ['x_owner_foo'], 'value': 'baz'},
  1185. ]
  1186. self.assertRaises(webob.exc.HTTPConflict, self.controller.update,
  1187. another_request, created_image.image_id, changes)
  1188. def test_prop_protection_with_delete_and_permitted_role(self):
  1189. enforcer = glance.api.policy.Enforcer()
  1190. self.controller = glance.api.v2.images.ImagesController(self.db,
  1191. enforcer,
  1192. self.notifier,
  1193. self.store)
  1194. self.set_property_protections()
  1195. request = unit_test_utils.get_fake_request(roles=['admin'])
  1196. image = {'name': 'image-1'}
  1197. extra_props = {'x_owner_foo': 'bar'}
  1198. created_image = self.controller.create(request, image=image,
  1199. extra_properties=extra_props,
  1200. tags=[])
  1201. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1202. changes = [
  1203. {'op': 'remove', 'path': ['x_owner_foo']}
  1204. ]
  1205. output = self.controller.update(another_request,
  1206. created_image.image_id, changes)
  1207. self.assertRaises(KeyError, output.extra_properties.__getitem__,
  1208. 'x_owner_foo')
  1209. def test_prop_protection_with_delete_and_unpermitted_role(self):
  1210. enforcer = glance.api.policy.Enforcer()
  1211. self.controller = glance.api.v2.images.ImagesController(self.db,
  1212. enforcer,
  1213. self.notifier,
  1214. self.store)
  1215. self.set_property_protections()
  1216. request = unit_test_utils.get_fake_request(roles=['admin'])
  1217. image = {'name': 'image-1'}
  1218. extra_props = {'x_owner_foo': 'bar'}
  1219. created_image = self.controller.create(request, image=image,
  1220. extra_properties=extra_props,
  1221. tags=[])
  1222. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1223. changes = [
  1224. {'op': 'remove', 'path': ['x_owner_foo']}
  1225. ]
  1226. self.assertRaises(webob.exc.HTTPConflict, self.controller.update,
  1227. another_request, created_image.image_id, changes)
  1228. def test_create_protected_prop_case_insensitive(self):
  1229. enforcer = glance.api.policy.Enforcer()
  1230. self.controller = glance.api.v2.images.ImagesController(self.db,
  1231. enforcer,
  1232. self.notifier,
  1233. self.store)
  1234. self.set_property_protections()
  1235. request = unit_test_utils.get_fake_request(roles=['admin'])
  1236. image = {'name': 'image-1'}
  1237. created_image = self.controller.create(request, image=image,
  1238. extra_properties={},
  1239. tags=[])
  1240. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1241. changes = [
  1242. {'op': 'add', 'path': ['x_case_insensitive'], 'value': '1'},
  1243. ]
  1244. output = self.controller.update(another_request,
  1245. created_image.image_id, changes)
  1246. self.assertEqual('1', output.extra_properties['x_case_insensitive'])
  1247. def test_read_protected_prop_case_insensitive(self):
  1248. enforcer = glance.api.policy.Enforcer()
  1249. self.controller = glance.api.v2.images.ImagesController(self.db,
  1250. enforcer,
  1251. self.notifier,
  1252. self.store)
  1253. self.set_property_protections()
  1254. request = unit_test_utils.get_fake_request(roles=['admin'])
  1255. image = {'name': 'image-1'}
  1256. extra_props = {'x_case_insensitive': '1'}
  1257. created_image = self.controller.create(request, image=image,
  1258. extra_properties=extra_props,
  1259. tags=[])
  1260. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1261. output = self.controller.show(another_request, created_image.image_id)
  1262. self.assertEqual('1', output.extra_properties['x_case_insensitive'])
  1263. def test_update_protected_prop_case_insensitive(self):
  1264. enforcer = glance.api.policy.Enforcer()
  1265. self.controller = glance.api.v2.images.ImagesController(self.db,
  1266. enforcer,
  1267. self.notifier,
  1268. self.store)
  1269. self.set_property_protections()
  1270. request = unit_test_utils.get_fake_request(roles=['admin'])
  1271. image = {'name': 'image-1'}
  1272. extra_props = {'x_case_insensitive': '1'}
  1273. created_image = self.controller.create(request, image=image,
  1274. extra_properties=extra_props,
  1275. tags=[])
  1276. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1277. changes = [
  1278. {'op': 'replace', 'path': ['x_case_insensitive'], 'value': '2'},
  1279. ]
  1280. output = self.controller.update(another_request,
  1281. created_image.image_id, changes)
  1282. self.assertEqual('2', output.extra_properties['x_case_insensitive'])
  1283. def test_delete_protected_prop_case_insensitive(self):
  1284. enforcer = glance.api.policy.Enforcer()
  1285. self.controller = glance.api.v2.images.ImagesController(self.db,
  1286. enforcer,
  1287. self.notifier,
  1288. self.store)
  1289. self.set_property_protections()
  1290. request = unit_test_utils.get_fake_request(roles=['admin'])
  1291. image = {'name': 'image-1'}
  1292. extra_props = {'x_case_insensitive': 'bar'}
  1293. created_image = self.controller.create(request, image=image,
  1294. extra_properties=extra_props,
  1295. tags=[])
  1296. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1297. changes = [
  1298. {'op': 'remove', 'path': ['x_case_insensitive']}
  1299. ]
  1300. output = self.controller.update(another_request,
  1301. created_image.image_id, changes)
  1302. self.assertRaises(KeyError, output.extra_properties.__getitem__,
  1303. 'x_case_insensitive')
  1304. def test_create_non_protected_prop(self):
  1305. """Property marked with special char @ creatable by an unknown role"""
  1306. self.set_property_protections()
  1307. request = unit_test_utils.get_fake_request(roles=['admin'])
  1308. image = {'name': 'image-1'}
  1309. extra_props = {'x_all_permitted_1': '1'}
  1310. created_image = self.controller.create(request, image=image,
  1311. extra_properties=extra_props,
  1312. tags=[])
  1313. self.assertEqual('1',
  1314. created_image.extra_properties['x_all_permitted_1'])
  1315. another_request = unit_test_utils.get_fake_request(roles=['joe_soap'])
  1316. extra_props = {'x_all_permitted_2': '2'}
  1317. created_image = self.controller.create(another_request, image=image,
  1318. extra_properties=extra_props,
  1319. tags=[])
  1320. self.assertEqual('2',
  1321. created_image.extra_properties['x_all_permitted_2'])
  1322. def test_read_non_protected_prop(self):
  1323. """Property marked with special char @ readable by an unknown role"""
  1324. self.set_property_protections()
  1325. request = unit_test_utils.get_fake_request(roles=['admin'])
  1326. image = {'name': 'image-1'}
  1327. extra_props = {'x_all_permitted': '1'}
  1328. created_image = self.controller.create(request, image=image,
  1329. extra_properties=extra_props,
  1330. tags=[])
  1331. another_request = unit_test_utils.get_fake_request(roles=['joe_soap'])
  1332. output = self.controller.show(another_request, created_image.image_id)
  1333. self.assertEqual('1', output.extra_properties['x_all_permitted'])
  1334. def test_update_non_protected_prop(self):
  1335. """Property marked with special char @ updatable by an unknown role"""
  1336. self.set_property_protections()
  1337. request = unit_test_utils.get_fake_request(roles=['admin'])
  1338. image = {'name': 'image-1'}
  1339. extra_props = {'x_all_permitted': 'bar'}
  1340. created_image = self.controller.create(request, image=image,
  1341. extra_properties=extra_props,
  1342. tags=[])
  1343. another_request = unit_test_utils.get_fake_request(roles=['joe_soap'])
  1344. changes = [
  1345. {'op': 'replace', 'path': ['x_all_permitted'], 'value': 'baz'},
  1346. ]
  1347. output = self.controller.update(another_request,
  1348. created_image.image_id, changes)
  1349. self.assertEqual('baz', output.extra_properties['x_all_permitted'])
  1350. def test_delete_non_protected_prop(self):
  1351. """Property marked with special char @ deletable by an unknown role"""
  1352. self.set_property_protections()
  1353. request = unit_test_utils.get_fake_request(roles=['admin'])
  1354. image = {'name': 'image-1'}
  1355. extra_props = {'x_all_permitted': 'bar'}
  1356. created_image = self.controller.create(request, image=image,
  1357. extra_properties=extra_props,
  1358. tags=[])
  1359. another_request = unit_test_utils.get_fake_request(roles=['member'])
  1360. changes = [
  1361. {'op': 'remove', 'path': ['x_all_permitted']}
  1362. ]
  1363. output = self.controller.update(another_request,
  1364. created_image.image_id, changes)
  1365. self.assertRaises(KeyError, output.extra_properties.__getitem__,
  1366. 'x_all_permitted')
  1367. def test_create_locked_down_protected_prop(self):
  1368. """Property marked with special char ! creatable by no one"""
  1369. self.set_property_protections()
  1370. request = unit_test_utils.get_fake_request(roles=['admin'])
  1371. image = {'name': 'image-1'}
  1372. created_image = self.controller.create(request, image=image,
  1373. extra_properties={},
  1374. tags=[])
  1375. roles = ['fake_member']
  1376. another_request = unit_test_utils.get_fake_request(roles=roles)
  1377. changes = [
  1378. {'op': 'add', 'path': ['x_none_permitted'], 'value': 'bar'},
  1379. ]
  1380. self.assertRaises(webob.exc.HTTPForbidden,
  1381. self.controller.update, another_request,
  1382. created_image.image_id, changes)
  1383. def test_read_locked_down_protected_prop(self):
  1384. """Property marked with special char ! readable by no one"""
  1385. self.set_property_protections()
  1386. request = unit_test_utils.get_fake_request(roles=['member'])
  1387. image = {'name': 'image-1'}
  1388. extra_props = {'x_none_read': 'bar'}
  1389. created_image = self.controller.create(request, image=image,
  1390. extra_properties=extra_props,
  1391. tags=[])
  1392. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1393. output = self.controller.show(another_request, created_image.image_id)
  1394. self.assertRaises(KeyError, output.extra_properties.__getitem__,
  1395. 'x_none_read')
  1396. def test_update_locked_down_protected_prop(self):
  1397. """Property marked with special char ! updatable by no one"""
  1398. self.set_property_protections()
  1399. request = unit_test_utils.get_fake_request(roles=['admin'])
  1400. image = {'name': 'image-1'}
  1401. extra_props = {'x_none_update': 'bar'}
  1402. created_image = self.controller.create(request, image=image,
  1403. extra_properties=extra_props,
  1404. tags=[])
  1405. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1406. changes = [
  1407. {'op': 'replace', 'path': ['x_none_update'], 'value': 'baz'},
  1408. ]
  1409. self.assertRaises(webob.exc.HTTPConflict, self.controller.update,
  1410. another_request, created_image.image_id, changes)
  1411. def test_delete_locked_down_protected_prop(self):
  1412. """Property marked with special char ! deletable by no one"""
  1413. self.set_property_protections()
  1414. request = unit_test_utils.get_fake_request(roles=['admin'])
  1415. image = {'name': 'image-1'}
  1416. extra_props = {'x_none_delete': 'bar'}
  1417. created_image = self.controller.create(request, image=image,
  1418. extra_properties=extra_props,
  1419. tags=[])
  1420. another_request = unit_test_utils.get_fake_request(roles=['fake_role'])
  1421. changes = [
  1422. {'op': 'remove', 'path': ['x_none_delete']}
  1423. ]
  1424. self.assertRaises(webob.exc.HTTPConflict, self.controller.update,
  1425. another_request, created_image.image_id, changes)
  1426. def test_update_replace_locations_non_empty(self):
  1427. self.config(show_multiple_locations=True)
  1428. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1429. request = unit_test_utils.get_fake_request()
  1430. changes = [{'op': 'replace', 'path': ['locations'],
  1431. 'value': [new_location]}]
  1432. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1433. request, UUID1, changes)
  1434. def test_update_replace_locations_metadata_update(self):
  1435. self.config(show_multiple_locations=True)
  1436. location = {'url': '%s/%s' % (BASE_URI, UUID1),
  1437. 'metadata': {'a': 1}}
  1438. request = unit_test_utils.get_fake_request()
  1439. changes = [{'op': 'replace', 'path': ['locations'],
  1440. 'value': [location]}]
  1441. output = self.controller.update(request, UUID1, changes)
  1442. self.assertEqual({'a': 1}, output.locations[0]['metadata'])
  1443. def test_locations_actions_with_locations_invisible(self):
  1444. self.config(show_multiple_locations=False)
  1445. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1446. request = unit_test_utils.get_fake_request()
  1447. changes = [{'op': 'replace', 'path': ['locations'],
  1448. 'value': [new_location]}]
  1449. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  1450. request, UUID1, changes)
  1451. def test_update_replace_locations_invalid(self):
  1452. request = unit_test_utils.get_fake_request()
  1453. changes = [{'op': 'replace', 'path': ['locations'], 'value': []}]
  1454. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  1455. request, UUID1, changes)
  1456. def test_update_add_property(self):
  1457. request = unit_test_utils.get_fake_request()
  1458. changes = [
  1459. {'op': 'add', 'path': ['foo'], 'value': 'baz'},
  1460. {'op': 'add', 'path': ['snitch'], 'value': 'golden'},
  1461. ]
  1462. output = self.controller.update(request, UUID1, changes)
  1463. self.assertEqual(UUID1, output.image_id)
  1464. self.assertEqual('baz', output.extra_properties['foo'])
  1465. self.assertEqual('golden', output.extra_properties['snitch'])
  1466. self.assertNotEqual(output.created_at, output.updated_at)
  1467. def test_update_add_base_property_json_schema_version_4(self):
  1468. request = unit_test_utils.get_fake_request()
  1469. changes = [{
  1470. 'json_schema_version': 4, 'op': 'add',
  1471. 'path': ['name'], 'value': 'fedora'
  1472. }]
  1473. self.assertRaises(webob.exc.HTTPConflict, self.controller.update,
  1474. request, UUID1, changes)
  1475. def test_update_add_extra_property_json_schema_version_4(self):
  1476. self.db.image_update(None, UUID1, {'properties': {'foo': 'bar'}})
  1477. request = unit_test_utils.get_fake_request()
  1478. changes = [{
  1479. 'json_schema_version': 4, 'op': 'add',
  1480. 'path': ['foo'], 'value': 'baz'
  1481. }]
  1482. self.assertRaises(webob.exc.HTTPConflict, self.controller.update,
  1483. request, UUID1, changes)
  1484. def test_update_add_base_property_json_schema_version_10(self):
  1485. request = unit_test_utils.get_fake_request()
  1486. changes = [{
  1487. 'json_schema_version': 10, 'op': 'add',
  1488. 'path': ['name'], 'value': 'fedora'
  1489. }]
  1490. output = self.controller.update(request, UUID1, changes)
  1491. self.assertEqual(UUID1, output.image_id)
  1492. self.assertEqual('fedora', output.name)
  1493. def test_update_add_extra_property_json_schema_version_10(self):
  1494. self.db.image_update(None, UUID1, {'properties': {'foo': 'bar'}})
  1495. request = unit_test_utils.get_fake_request()
  1496. changes = [{
  1497. 'json_schema_version': 10, 'op': 'add',
  1498. 'path': ['foo'], 'value': 'baz'
  1499. }]
  1500. output = self.controller.update(request, UUID1, changes)
  1501. self.assertEqual(UUID1, output.image_id)
  1502. self.assertEqual({'foo': 'baz'}, output.extra_properties)
  1503. def test_update_add_property_already_present_json_schema_version_4(self):
  1504. request = unit_test_utils.get_fake_request()
  1505. properties = {'foo': 'bar'}
  1506. self.db.image_update(None, UUID1, {'properties': properties})
  1507. output = self.controller.show(request, UUID1)
  1508. self.assertEqual('bar', output.extra_properties['foo'])
  1509. changes = [
  1510. {'json_schema_version': 4, 'op': 'add',
  1511. 'path': ['foo'], 'value': 'baz'},
  1512. ]
  1513. self.assertRaises(webob.exc.HTTPConflict,
  1514. self.controller.update, request, UUID1, changes)
  1515. def test_update_add_property_already_present_json_schema_version_10(self):
  1516. request = unit_test_utils.get_fake_request()
  1517. properties = {'foo': 'bar'}
  1518. self.db.image_update(None, UUID1, {'properties': properties})
  1519. output = self.controller.show(request, UUID1)
  1520. self.assertEqual('bar', output.extra_properties['foo'])
  1521. changes = [
  1522. {'json_schema_version': 10, 'op': 'add',
  1523. 'path': ['foo'], 'value': 'baz'},
  1524. ]
  1525. output = self.controller.update(request, UUID1, changes)
  1526. self.assertEqual(UUID1, output.image_id)
  1527. self.assertEqual({'foo': 'baz'}, output.extra_properties)
  1528. def test_update_add_locations(self):
  1529. self.config(show_multiple_locations=True)
  1530. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1531. request = unit_test_utils.get_fake_request()
  1532. changes = [{'op': 'add', 'path': ['locations', '-'],
  1533. 'value': new_location}]
  1534. output = self.controller.update(request, UUID1, changes)
  1535. self.assertEqual(UUID1, output.image_id)
  1536. self.assertEqual(2, len(output.locations))
  1537. self.assertEqual(new_location, output.locations[1])
  1538. @mock.patch.object(glance.quota, '_calc_required_size')
  1539. @mock.patch.object(glance.location, '_check_image_location')
  1540. @mock.patch.object(glance.location.ImageRepoProxy, '_set_acls')
  1541. @mock.patch.object(store, 'get_size_from_uri_and_backend')
  1542. @mock.patch.object(store, 'get_size_from_backend')
  1543. def test_replace_location_on_queued(self,
  1544. mock_get_size,
  1545. mock_get_size_uri,
  1546. mock_set_acls,
  1547. mock_check_loc,
  1548. mock_calc):
  1549. mock_calc.return_value = 1
  1550. mock_get_size.return_value = 1
  1551. mock_get_size_uri.return_value = 1
  1552. self.config(show_multiple_locations=True)
  1553. self.images = [
  1554. _db_fixture('1', owner=TENANT1, checksum=CHKSUM,
  1555. name='1',
  1556. disk_format='raw',
  1557. container_format='bare',
  1558. status='queued'),
  1559. ]
  1560. self.db.image_create(None, self.images[0])
  1561. request = unit_test_utils.get_fake_request()
  1562. new_location = {'url': '%s/fake_location_1' % BASE_URI, 'metadata': {}}
  1563. changes = [{'op': 'replace', 'path': ['locations'],
  1564. 'value': [new_location]}]
  1565. output = self.controller.update(request, '1', changes)
  1566. self.assertEqual('1', output.image_id)
  1567. self.assertEqual(1, len(output.locations))
  1568. self.assertEqual(new_location, output.locations[0])
  1569. self.assertEqual('active', output.status)
  1570. @mock.patch.object(glance.quota, '_calc_required_size')
  1571. @mock.patch.object(glance.location, '_check_image_location')
  1572. @mock.patch.object(glance.location.ImageRepoProxy, '_set_acls')
  1573. @mock.patch.object(store, 'get_size_from_uri_and_backend')
  1574. @mock.patch.object(store, 'get_size_from_backend')
  1575. def test_add_location_on_queued(self,
  1576. mock_get_size,
  1577. mock_get_size_uri,
  1578. mock_set_acls,
  1579. mock_check_loc,
  1580. mock_calc):
  1581. mock_calc.return_value = 1
  1582. mock_get_size.return_value = 1
  1583. mock_get_size_uri.return_value = 1
  1584. self.config(show_multiple_locations=True)
  1585. self.images = [
  1586. _db_fixture('1', owner=TENANT1, checksum=CHKSUM,
  1587. name='1',
  1588. disk_format='raw',
  1589. container_format='bare',
  1590. status='queued'),
  1591. ]
  1592. self.db.image_create(None, self.images[0])
  1593. request = unit_test_utils.get_fake_request()
  1594. new_location = {'url': '%s/fake_location_1' % BASE_URI, 'metadata': {}}
  1595. changes = [{'op': 'add', 'path': ['locations', '-'],
  1596. 'value': new_location}]
  1597. output = self.controller.update(request, '1', changes)
  1598. self.assertEqual('1', output.image_id)
  1599. self.assertEqual(1, len(output.locations))
  1600. self.assertEqual(new_location, output.locations[0])
  1601. self.assertEqual('active', output.status)
  1602. def _test_update_locations_status(self, image_status, update):
  1603. self.config(show_multiple_locations=True)
  1604. self.images = [
  1605. _db_fixture('1', owner=TENANT1, checksum=CHKSUM,
  1606. name='1',
  1607. disk_format='raw',
  1608. container_format='bare',
  1609. status=image_status),
  1610. ]
  1611. request = unit_test_utils.get_fake_request()
  1612. if image_status == 'deactivated':
  1613. self.db.image_create(request.context, self.images[0])
  1614. else:
  1615. self.db.image_create(None, self.images[0])
  1616. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1617. changes = [{'op': update, 'path': ['locations', '-'],
  1618. 'value': new_location}]
  1619. self.assertRaises(webob.exc.HTTPConflict,
  1620. self.controller.update, request, '1', changes)
  1621. def test_location_add_not_permitted_status_saving(self):
  1622. self._test_update_locations_status('saving', 'add')
  1623. def test_location_add_not_permitted_status_deactivated(self):
  1624. self._test_update_locations_status('deactivated', 'add')
  1625. def test_location_add_not_permitted_status_deleted(self):
  1626. self._test_update_locations_status('deleted', 'add')
  1627. def test_location_add_not_permitted_status_pending_delete(self):
  1628. self._test_update_locations_status('pending_delete', 'add')
  1629. def test_location_add_not_permitted_status_killed(self):
  1630. self._test_update_locations_status('killed', 'add')
  1631. def test_location_remove_not_permitted_status_saving(self):
  1632. self._test_update_locations_status('saving', 'remove')
  1633. def test_location_remove_not_permitted_status_deactivated(self):
  1634. self._test_update_locations_status('deactivated', 'remove')
  1635. def test_location_remove_not_permitted_status_deleted(self):
  1636. self._test_update_locations_status('deleted', 'remove')
  1637. def test_location_remove_not_permitted_status_pending_delete(self):
  1638. self._test_update_locations_status('pending_delete', 'remove')
  1639. def test_location_remove_not_permitted_status_killed(self):
  1640. self._test_update_locations_status('killed', 'remove')
  1641. def test_location_remove_not_permitted_status_queued(self):
  1642. self._test_update_locations_status('queued', 'remove')
  1643. def test_location_replace_not_permitted_status_saving(self):
  1644. self._test_update_locations_status('saving', 'replace')
  1645. def test_location_replace_not_permitted_status_deactivated(self):
  1646. self._test_update_locations_status('deactivated', 'replace')
  1647. def test_location_replace_not_permitted_status_deleted(self):
  1648. self._test_update_locations_status('deleted', 'replace')
  1649. def test_location_replace_not_permitted_status_pending_delete(self):
  1650. self._test_update_locations_status('pending_delete', 'replace')
  1651. def test_location_replace_not_permitted_status_killed(self):
  1652. self._test_update_locations_status('killed', 'replace')
  1653. def test_update_add_locations_insertion(self):
  1654. self.config(show_multiple_locations=True)
  1655. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1656. request = unit_test_utils.get_fake_request()
  1657. changes = [{'op': 'add', 'path': ['locations', '0'],
  1658. 'value': new_location}]
  1659. output = self.controller.update(request, UUID1, changes)
  1660. self.assertEqual(UUID1, output.image_id)
  1661. self.assertEqual(2, len(output.locations))
  1662. self.assertEqual(new_location, output.locations[0])
  1663. def test_update_add_locations_list(self):
  1664. self.config(show_multiple_locations=True)
  1665. request = unit_test_utils.get_fake_request()
  1666. changes = [{'op': 'add', 'path': ['locations', '-'],
  1667. 'value': {'url': 'foo', 'metadata': {}}}]
  1668. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1669. request, UUID1, changes)
  1670. def test_update_add_locations_invalid(self):
  1671. self.config(show_multiple_locations=True)
  1672. request = unit_test_utils.get_fake_request()
  1673. changes = [{'op': 'add', 'path': ['locations', '-'],
  1674. 'value': {'url': 'unknow://foo', 'metadata': {}}}]
  1675. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1676. request, UUID1, changes)
  1677. changes = [{'op': 'add', 'path': ['locations', None],
  1678. 'value': {'url': 'unknow://foo', 'metadata': {}}}]
  1679. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1680. request, UUID1, changes)
  1681. def test_update_add_duplicate_locations(self):
  1682. self.config(show_multiple_locations=True)
  1683. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1684. request = unit_test_utils.get_fake_request()
  1685. changes = [{'op': 'add', 'path': ['locations', '-'],
  1686. 'value': new_location}]
  1687. output = self.controller.update(request, UUID1, changes)
  1688. self.assertEqual(UUID1, output.image_id)
  1689. self.assertEqual(2, len(output.locations))
  1690. self.assertEqual(new_location, output.locations[1])
  1691. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1692. request, UUID1, changes)
  1693. def test_update_add_too_many_locations(self):
  1694. self.config(show_multiple_locations=True)
  1695. self.config(image_location_quota=1)
  1696. request = unit_test_utils.get_fake_request()
  1697. changes = [
  1698. {'op': 'add', 'path': ['locations', '-'],
  1699. 'value': {'url': '%s/fake_location_1' % BASE_URI,
  1700. 'metadata': {}}},
  1701. {'op': 'add', 'path': ['locations', '-'],
  1702. 'value': {'url': '%s/fake_location_2' % BASE_URI,
  1703. 'metadata': {}}},
  1704. ]
  1705. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  1706. self.controller.update, request,
  1707. UUID1, changes)
  1708. def test_update_add_and_remove_too_many_locations(self):
  1709. self.config(show_multiple_locations=True)
  1710. request = unit_test_utils.get_fake_request()
  1711. changes = [
  1712. {'op': 'add', 'path': ['locations', '-'],
  1713. 'value': {'url': '%s/fake_location_1' % BASE_URI,
  1714. 'metadata': {}}},
  1715. {'op': 'add', 'path': ['locations', '-'],
  1716. 'value': {'url': '%s/fake_location_2' % BASE_URI,
  1717. 'metadata': {}}},
  1718. ]
  1719. self.controller.update(request, UUID1, changes)
  1720. self.config(image_location_quota=1)
  1721. # We must remove two properties to avoid being
  1722. # over the limit of 1 property
  1723. changes = [
  1724. {'op': 'remove', 'path': ['locations', '0']},
  1725. {'op': 'add', 'path': ['locations', '-'],
  1726. 'value': {'url': '%s/fake_location_3' % BASE_URI,
  1727. 'metadata': {}}},
  1728. ]
  1729. self.assertRaises(webob.exc.HTTPRequestEntityTooLarge,
  1730. self.controller.update, request,
  1731. UUID1, changes)
  1732. def test_update_add_unlimited_locations(self):
  1733. self.config(show_multiple_locations=True)
  1734. self.config(image_location_quota=-1)
  1735. request = unit_test_utils.get_fake_request()
  1736. changes = [
  1737. {'op': 'add', 'path': ['locations', '-'],
  1738. 'value': {'url': '%s/fake_location_1' % BASE_URI,
  1739. 'metadata': {}}},
  1740. ]
  1741. output = self.controller.update(request, UUID1, changes)
  1742. self.assertEqual(UUID1, output.image_id)
  1743. self.assertNotEqual(output.created_at, output.updated_at)
  1744. def test_update_remove_location_while_over_limit(self):
  1745. """Ensure that image locations can be removed.
  1746. Image locations should be able to be removed as long as the image has
  1747. fewer than the limited number of image locations after the
  1748. transaction.
  1749. """
  1750. self.config(show_multiple_locations=True)
  1751. request = unit_test_utils.get_fake_request()
  1752. changes = [
  1753. {'op': 'add', 'path': ['locations', '-'],
  1754. 'value': {'url': '%s/fake_location_1' % BASE_URI,
  1755. 'metadata': {}}},
  1756. {'op': 'add', 'path': ['locations', '-'],
  1757. 'value': {'url': '%s/fake_location_2' % BASE_URI,
  1758. 'metadata': {}}},
  1759. ]
  1760. self.controller.update(request, UUID1, changes)
  1761. self.config(image_location_quota=1)
  1762. self.config(show_multiple_locations=True)
  1763. # We must remove two locations to avoid being over
  1764. # the limit of 1 location
  1765. changes = [
  1766. {'op': 'remove', 'path': ['locations', '0']},
  1767. {'op': 'remove', 'path': ['locations', '0']},
  1768. ]
  1769. output = self.controller.update(request, UUID1, changes)
  1770. self.assertEqual(UUID1, output.image_id)
  1771. self.assertEqual(1, len(output.locations))
  1772. self.assertIn('fake_location_2', output.locations[0]['url'])
  1773. self.assertNotEqual(output.created_at, output.updated_at)
  1774. def test_update_add_and_remove_location_under_limit(self):
  1775. """Ensure that image locations can be removed.
  1776. Image locations should be able to be added and removed simultaneously
  1777. as long as the image has fewer than the limited number of image
  1778. locations after the transaction.
  1779. """
  1780. self.mock_object(store, 'get_size_from_backend',
  1781. unit_test_utils.fake_get_size_from_backend)
  1782. self.config(show_multiple_locations=True)
  1783. request = unit_test_utils.get_fake_request()
  1784. changes = [
  1785. {'op': 'add', 'path': ['locations', '-'],
  1786. 'value': {'url': '%s/fake_location_1' % BASE_URI,
  1787. 'metadata': {}}},
  1788. {'op': 'add', 'path': ['locations', '-'],
  1789. 'value': {'url': '%s/fake_location_2' % BASE_URI,
  1790. 'metadata': {}}},
  1791. ]
  1792. self.controller.update(request, UUID1, changes)
  1793. self.config(image_location_quota=2)
  1794. # We must remove two properties to avoid being
  1795. # over the limit of 1 property
  1796. changes = [
  1797. {'op': 'remove', 'path': ['locations', '0']},
  1798. {'op': 'remove', 'path': ['locations', '0']},
  1799. {'op': 'add', 'path': ['locations', '-'],
  1800. 'value': {'url': '%s/fake_location_3' % BASE_URI,
  1801. 'metadata': {}}},
  1802. ]
  1803. output = self.controller.update(request, UUID1, changes)
  1804. self.assertEqual(UUID1, output.image_id)
  1805. self.assertEqual(2, len(output.locations))
  1806. self.assertIn('fake_location_3', output.locations[1]['url'])
  1807. self.assertNotEqual(output.created_at, output.updated_at)
  1808. def test_update_remove_base_property(self):
  1809. self.db.image_update(None, UUID1, {'properties': {'foo': 'bar'}})
  1810. request = unit_test_utils.get_fake_request()
  1811. changes = [{'op': 'remove', 'path': ['name']}]
  1812. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  1813. request, UUID1, changes)
  1814. def test_update_remove_property(self):
  1815. request = unit_test_utils.get_fake_request()
  1816. properties = {'foo': 'bar', 'snitch': 'golden'}
  1817. self.db.image_update(None, UUID1, {'properties': properties})
  1818. output = self.controller.show(request, UUID1)
  1819. self.assertEqual('bar', output.extra_properties['foo'])
  1820. self.assertEqual('golden', output.extra_properties['snitch'])
  1821. changes = [
  1822. {'op': 'remove', 'path': ['snitch']},
  1823. ]
  1824. output = self.controller.update(request, UUID1, changes)
  1825. self.assertEqual(UUID1, output.image_id)
  1826. self.assertEqual({'foo': 'bar'}, output.extra_properties)
  1827. self.assertNotEqual(output.created_at, output.updated_at)
  1828. def test_update_remove_missing_property(self):
  1829. request = unit_test_utils.get_fake_request()
  1830. changes = [
  1831. {'op': 'remove', 'path': ['foo']},
  1832. ]
  1833. self.assertRaises(webob.exc.HTTPConflict,
  1834. self.controller.update, request, UUID1, changes)
  1835. def test_update_remove_location(self):
  1836. self.config(show_multiple_locations=True)
  1837. self.mock_object(store, 'get_size_from_backend',
  1838. unit_test_utils.fake_get_size_from_backend)
  1839. request = unit_test_utils.get_fake_request()
  1840. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  1841. changes = [{'op': 'add', 'path': ['locations', '-'],
  1842. 'value': new_location}]
  1843. self.controller.update(request, UUID1, changes)
  1844. changes = [{'op': 'remove', 'path': ['locations', '0']}]
  1845. output = self.controller.update(request, UUID1, changes)
  1846. self.assertEqual(UUID1, output.image_id)
  1847. self.assertEqual(1, len(output.locations))
  1848. self.assertEqual('active', output.status)
  1849. def test_update_remove_location_invalid_pos(self):
  1850. self.config(show_multiple_locations=True)
  1851. request = unit_test_utils.get_fake_request()
  1852. changes = [
  1853. {'op': 'add', 'path': ['locations', '-'],
  1854. 'value': {'url': '%s/fake_location' % BASE_URI,
  1855. 'metadata': {}}}]
  1856. self.controller.update(request, UUID1, changes)
  1857. changes = [{'op': 'remove', 'path': ['locations', None]}]
  1858. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1859. request, UUID1, changes)
  1860. changes = [{'op': 'remove', 'path': ['locations', '-1']}]
  1861. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1862. request, UUID1, changes)
  1863. changes = [{'op': 'remove', 'path': ['locations', '99']}]
  1864. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1865. request, UUID1, changes)
  1866. changes = [{'op': 'remove', 'path': ['locations', 'x']}]
  1867. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
  1868. request, UUID1, changes)
  1869. def test_update_remove_location_store_exception(self):
  1870. self.config(show_multiple_locations=True)
  1871. def fake_delete_image_location_from_backend(self, *args, **kwargs):
  1872. raise Exception('fake_backend_exception')
  1873. self.mock_object(self.store_utils,
  1874. 'delete_image_location_from_backend',
  1875. fake_delete_image_location_from_backend)
  1876. request = unit_test_utils.get_fake_request()
  1877. changes = [
  1878. {'op': 'add', 'path': ['locations', '-'],
  1879. 'value': {'url': '%s/fake_location' % BASE_URI,
  1880. 'metadata': {}}}]
  1881. self.controller.update(request, UUID1, changes)
  1882. changes = [{'op': 'remove', 'path': ['locations', '0']}]
  1883. self.assertRaises(webob.exc.HTTPInternalServerError,
  1884. self.controller.update, request, UUID1, changes)
  1885. def test_update_multiple_changes(self):
  1886. request = unit_test_utils.get_fake_request()
  1887. properties = {'foo': 'bar', 'snitch': 'golden'}
  1888. self.db.image_update(None, UUID1, {'properties': properties})
  1889. changes = [
  1890. {'op': 'replace', 'path': ['min_ram'], 'value': 128},
  1891. {'op': 'replace', 'path': ['foo'], 'value': 'baz'},
  1892. {'op': 'remove', 'path': ['snitch']},
  1893. {'op': 'add', 'path': ['kb'], 'value': 'dvorak'},
  1894. ]
  1895. output = self.controller.update(request, UUID1, changes)
  1896. self.assertEqual(UUID1, output.image_id)
  1897. self.assertEqual(128, output.min_ram)
  1898. self.addDetail('extra_properties',
  1899. testtools.content.json_content(
  1900. jsonutils.dumps(output.extra_properties)))
  1901. self.assertEqual(2, len(output.extra_properties))
  1902. self.assertEqual('baz', output.extra_properties['foo'])
  1903. self.assertEqual('dvorak', output.extra_properties['kb'])
  1904. self.assertNotEqual(output.created_at, output.updated_at)
  1905. def test_update_invalid_operation(self):
  1906. request = unit_test_utils.get_fake_request()
  1907. change = {'op': 'test', 'path': 'options', 'value': 'puts'}
  1908. try:
  1909. self.controller.update(request, UUID1, [change])
  1910. except AttributeError:
  1911. pass # AttributeError is the desired behavior
  1912. else:
  1913. self.fail('Failed to raise AssertionError on %s' % change)
  1914. def test_update_duplicate_tags(self):
  1915. request = unit_test_utils.get_fake_request()
  1916. changes = [
  1917. {'op': 'replace', 'path': ['tags'], 'value': ['ping', 'ping']},
  1918. ]
  1919. output = self.controller.update(request, UUID1, changes)
  1920. self.assertEqual(1, len(output.tags))
  1921. self.assertIn('ping', output.tags)
  1922. output_logs = self.notifier.get_logs()
  1923. self.assertEqual(1, len(output_logs))
  1924. output_log = output_logs[0]
  1925. self.assertEqual('INFO', output_log['notification_type'])
  1926. self.assertEqual('image.update', output_log['event_type'])
  1927. self.assertEqual(UUID1, output_log['payload']['id'])
  1928. def test_update_disabled_notification(self):
  1929. self.config(disabled_notifications=["image.update"])
  1930. request = unit_test_utils.get_fake_request()
  1931. changes = [
  1932. {'op': 'replace', 'path': ['name'], 'value': 'Ping Pong'},
  1933. ]
  1934. output = self.controller.update(request, UUID1, changes)
  1935. self.assertEqual('Ping Pong', output.name)
  1936. output_logs = self.notifier.get_logs()
  1937. self.assertEqual(0, len(output_logs))
  1938. def test_delete(self):
  1939. request = unit_test_utils.get_fake_request()
  1940. self.assertIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  1941. try:
  1942. self.controller.delete(request, UUID1)
  1943. output_logs = self.notifier.get_logs()
  1944. self.assertEqual(1, len(output_logs))
  1945. output_log = output_logs[0]
  1946. self.assertEqual('INFO', output_log['notification_type'])
  1947. self.assertEqual("image.delete", output_log['event_type'])
  1948. except Exception as e:
  1949. self.fail("Delete raised exception: %s" % e)
  1950. deleted_img = self.db.image_get(request.context, UUID1,
  1951. force_show_deleted=True)
  1952. self.assertTrue(deleted_img['deleted'])
  1953. self.assertEqual('deleted', deleted_img['status'])
  1954. self.assertNotIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  1955. def test_delete_with_tags(self):
  1956. request = unit_test_utils.get_fake_request()
  1957. changes = [
  1958. {'op': 'replace', 'path': ['tags'],
  1959. 'value': ['many', 'cool', 'new', 'tags']},
  1960. ]
  1961. self.controller.update(request, UUID1, changes)
  1962. self.assertIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  1963. self.controller.delete(request, UUID1)
  1964. output_logs = self.notifier.get_logs()
  1965. # Get `delete` event from logs
  1966. output_delete_logs = [output_log for output_log in output_logs
  1967. if output_log['event_type'] == 'image.delete']
  1968. self.assertEqual(1, len(output_delete_logs))
  1969. output_log = output_delete_logs[0]
  1970. self.assertEqual('INFO', output_log['notification_type'])
  1971. deleted_img = self.db.image_get(request.context, UUID1,
  1972. force_show_deleted=True)
  1973. self.assertTrue(deleted_img['deleted'])
  1974. self.assertEqual('deleted', deleted_img['status'])
  1975. self.assertNotIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  1976. def test_delete_disabled_notification(self):
  1977. self.config(disabled_notifications=["image.delete"])
  1978. request = unit_test_utils.get_fake_request()
  1979. self.assertIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  1980. try:
  1981. self.controller.delete(request, UUID1)
  1982. output_logs = self.notifier.get_logs()
  1983. self.assertEqual(0, len(output_logs))
  1984. except Exception as e:
  1985. self.fail("Delete raised exception: %s" % e)
  1986. deleted_img = self.db.image_get(request.context, UUID1,
  1987. force_show_deleted=True)
  1988. self.assertTrue(deleted_img['deleted'])
  1989. self.assertEqual('deleted', deleted_img['status'])
  1990. self.assertNotIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  1991. def test_delete_queued_updates_status(self):
  1992. """Ensure status of queued image is updated (LP bug #1048851)"""
  1993. request = unit_test_utils.get_fake_request(is_admin=True)
  1994. image = self.db.image_create(request.context, {'status': 'queued'})
  1995. image_id = image['id']
  1996. self.controller.delete(request, image_id)
  1997. image = self.db.image_get(request.context, image_id,
  1998. force_show_deleted=True)
  1999. self.assertTrue(image['deleted'])
  2000. self.assertEqual('deleted', image['status'])
  2001. def test_delete_queued_updates_status_delayed_delete(self):
  2002. """Ensure status of queued image is updated (LP bug #1048851).
  2003. Must be set to 'deleted' when delayed_delete isenabled.
  2004. """
  2005. self.config(delayed_delete=True)
  2006. request = unit_test_utils.get_fake_request(is_admin=True)
  2007. image = self.db.image_create(request.context, {'status': 'queued'})
  2008. image_id = image['id']
  2009. self.controller.delete(request, image_id)
  2010. image = self.db.image_get(request.context, image_id,
  2011. force_show_deleted=True)
  2012. self.assertTrue(image['deleted'])
  2013. self.assertEqual('deleted', image['status'])
  2014. def test_delete_not_in_store(self):
  2015. request = unit_test_utils.get_fake_request()
  2016. self.assertIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  2017. for k in self.store.data:
  2018. if UUID1 in k:
  2019. del self.store.data[k]
  2020. break
  2021. self.controller.delete(request, UUID1)
  2022. deleted_img = self.db.image_get(request.context, UUID1,
  2023. force_show_deleted=True)
  2024. self.assertTrue(deleted_img['deleted'])
  2025. self.assertEqual('deleted', deleted_img['status'])
  2026. self.assertNotIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  2027. def test_delayed_delete(self):
  2028. self.config(delayed_delete=True)
  2029. request = unit_test_utils.get_fake_request()
  2030. self.assertIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  2031. self.controller.delete(request, UUID1)
  2032. deleted_img = self.db.image_get(request.context, UUID1,
  2033. force_show_deleted=True)
  2034. self.assertTrue(deleted_img['deleted'])
  2035. self.assertEqual('pending_delete', deleted_img['status'])
  2036. self.assertIn('%s/%s' % (BASE_URI, UUID1), self.store.data)
  2037. def test_delete_non_existent(self):
  2038. request = unit_test_utils.get_fake_request()
  2039. self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
  2040. request, str(uuid.uuid4()))
  2041. def test_delete_already_deleted_image_admin(self):
  2042. request = unit_test_utils.get_fake_request(is_admin=True)
  2043. self.controller.delete(request, UUID1)
  2044. self.assertRaises(webob.exc.HTTPNotFound,
  2045. self.controller.delete, request, UUID1)
  2046. def test_delete_not_allowed(self):
  2047. request = unit_test_utils.get_fake_request()
  2048. self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
  2049. request, UUID4)
  2050. def test_delete_in_use(self):
  2051. def fake_safe_delete_from_backend(self, *args, **kwargs):
  2052. raise store.exceptions.InUseByStore()
  2053. self.mock_object(self.store_utils, 'safe_delete_from_backend',
  2054. fake_safe_delete_from_backend)
  2055. request = unit_test_utils.get_fake_request()
  2056. self.assertRaises(webob.exc.HTTPConflict, self.controller.delete,
  2057. request, UUID1)
  2058. def test_delete_has_snapshot(self):
  2059. def fake_safe_delete_from_backend(self, *args, **kwargs):
  2060. raise store.exceptions.HasSnapshot()
  2061. self.mock_object(self.store_utils, 'safe_delete_from_backend',
  2062. fake_safe_delete_from_backend)
  2063. request = unit_test_utils.get_fake_request()
  2064. self.assertRaises(webob.exc.HTTPConflict, self.controller.delete,
  2065. request, UUID1)
  2066. def test_delete_to_unallowed_status(self):
  2067. # from deactivated to pending-delete
  2068. self.config(delayed_delete=True)
  2069. request = unit_test_utils.get_fake_request(is_admin=True)
  2070. self.action_controller.deactivate(request, UUID1)
  2071. self.assertRaises(webob.exc.HTTPBadRequest, self.controller.delete,
  2072. request, UUID1)
  2073. def test_delete_uploading_status_image(self):
  2074. """Ensure status of uploading image is updated (LP bug #1733289)"""
  2075. request = unit_test_utils.get_fake_request(is_admin=True)
  2076. image = self.db.image_create(request.context, {'status': 'uploading'})
  2077. image_id = image['id']
  2078. with mock.patch.object(self.store,
  2079. 'delete_from_backend') as mock_store:
  2080. self.controller.delete(request, image_id)
  2081. # Ensure delete_from_backend is called
  2082. self.assertEqual(1, mock_store.call_count)
  2083. image = self.db.image_get(request.context, image_id,
  2084. force_show_deleted=True)
  2085. self.assertTrue(image['deleted'])
  2086. self.assertEqual('deleted', image['status'])
  2087. def test_index_with_invalid_marker(self):
  2088. fake_uuid = str(uuid.uuid4())
  2089. request = unit_test_utils.get_fake_request()
  2090. self.assertRaises(webob.exc.HTTPBadRequest,
  2091. self.controller.index, request, marker=fake_uuid)
  2092. def test_invalid_locations_op_pos(self):
  2093. pos = self.controller._get_locations_op_pos(None, 2, True)
  2094. self.assertIsNone(pos)
  2095. pos = self.controller._get_locations_op_pos('1', None, True)
  2096. self.assertIsNone(pos)
  2097. def test_image_import(self):
  2098. request = unit_test_utils.get_fake_request()
  2099. with mock.patch.object(
  2100. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  2101. mock_get.return_value = FakeImage(status='uploading')
  2102. output = self.controller.import_image(
  2103. request, UUID4, {'method': {'name': 'glance-direct'}})
  2104. self.assertEqual(UUID4, output)
  2105. def test_image_import_not_allowed(self):
  2106. request = unit_test_utils.get_fake_request()
  2107. # NOTE(abhishekk): For coverage purpose setting tenant to
  2108. # None. It is not expected to do in normal scenarios.
  2109. request.context.tenant = None
  2110. with mock.patch.object(
  2111. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  2112. mock_get.return_value = FakeImage(status='uploading')
  2113. self.assertRaises(webob.exc.HTTPForbidden,
  2114. self.controller.import_image,
  2115. request, UUID4, {'method': {'name':
  2116. 'glance-direct'}})
  2117. class TestImagesControllerPolicies(base.IsolatedUnitTest):
  2118. def setUp(self):
  2119. super(TestImagesControllerPolicies, self).setUp()
  2120. self.db = unit_test_utils.FakeDB()
  2121. self.policy = unit_test_utils.FakePolicyEnforcer()
  2122. self.controller = glance.api.v2.images.ImagesController(self.db,
  2123. self.policy)
  2124. store = unit_test_utils.FakeStoreAPI()
  2125. self.store_utils = unit_test_utils.FakeStoreUtils(store)
  2126. def test_index_unauthorized(self):
  2127. rules = {"get_images": False}
  2128. self.policy.set_rules(rules)
  2129. request = unit_test_utils.get_fake_request()
  2130. self.assertRaises(webob.exc.HTTPForbidden, self.controller.index,
  2131. request)
  2132. def test_show_unauthorized(self):
  2133. rules = {"get_image": False}
  2134. self.policy.set_rules(rules)
  2135. request = unit_test_utils.get_fake_request()
  2136. self.assertRaises(webob.exc.HTTPForbidden, self.controller.show,
  2137. request, image_id=UUID2)
  2138. def test_create_image_unauthorized(self):
  2139. rules = {"add_image": False}
  2140. self.policy.set_rules(rules)
  2141. request = unit_test_utils.get_fake_request()
  2142. image = {'name': 'image-1'}
  2143. extra_properties = {}
  2144. tags = []
  2145. self.assertRaises(webob.exc.HTTPForbidden, self.controller.create,
  2146. request, image, extra_properties, tags)
  2147. def test_create_public_image_unauthorized(self):
  2148. rules = {"publicize_image": False}
  2149. self.policy.set_rules(rules)
  2150. request = unit_test_utils.get_fake_request()
  2151. image = {'name': 'image-1', 'visibility': 'public'}
  2152. extra_properties = {}
  2153. tags = []
  2154. self.assertRaises(webob.exc.HTTPForbidden, self.controller.create,
  2155. request, image, extra_properties, tags)
  2156. def test_create_community_image_unauthorized(self):
  2157. rules = {"communitize_image": False}
  2158. self.policy.set_rules(rules)
  2159. request = unit_test_utils.get_fake_request()
  2160. image = {'name': 'image-c1', 'visibility': 'community'}
  2161. extra_properties = {}
  2162. tags = []
  2163. self.assertRaises(webob.exc.HTTPForbidden, self.controller.create,
  2164. request, image, extra_properties, tags)
  2165. def test_update_unauthorized(self):
  2166. rules = {"modify_image": False}
  2167. self.policy.set_rules(rules)
  2168. request = unit_test_utils.get_fake_request()
  2169. changes = [{'op': 'replace', 'path': ['name'], 'value': 'image-2'}]
  2170. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  2171. request, UUID1, changes)
  2172. def test_update_publicize_image_unauthorized(self):
  2173. rules = {"publicize_image": False}
  2174. self.policy.set_rules(rules)
  2175. request = unit_test_utils.get_fake_request()
  2176. changes = [{'op': 'replace', 'path': ['visibility'],
  2177. 'value': 'public'}]
  2178. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  2179. request, UUID1, changes)
  2180. def test_update_communitize_image_unauthorized(self):
  2181. rules = {"communitize_image": False}
  2182. self.policy.set_rules(rules)
  2183. request = unit_test_utils.get_fake_request()
  2184. changes = [{'op': 'replace', 'path': ['visibility'],
  2185. 'value': 'community'}]
  2186. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  2187. request, UUID1, changes)
  2188. def test_update_depublicize_image_unauthorized(self):
  2189. rules = {"publicize_image": False}
  2190. self.policy.set_rules(rules)
  2191. request = unit_test_utils.get_fake_request()
  2192. changes = [{'op': 'replace', 'path': ['visibility'],
  2193. 'value': 'private'}]
  2194. output = self.controller.update(request, UUID1, changes)
  2195. self.assertEqual('private', output.visibility)
  2196. def test_update_decommunitize_image_unauthorized(self):
  2197. rules = {"communitize_image": False}
  2198. self.policy.set_rules(rules)
  2199. request = unit_test_utils.get_fake_request()
  2200. changes = [{'op': 'replace', 'path': ['visibility'],
  2201. 'value': 'private'}]
  2202. output = self.controller.update(request, UUID1, changes)
  2203. self.assertEqual('private', output.visibility)
  2204. def test_update_get_image_location_unauthorized(self):
  2205. rules = {"get_image_location": False}
  2206. self.policy.set_rules(rules)
  2207. request = unit_test_utils.get_fake_request()
  2208. changes = [{'op': 'replace', 'path': ['locations'], 'value': []}]
  2209. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  2210. request, UUID1, changes)
  2211. def test_update_set_image_location_unauthorized(self):
  2212. def fake_delete_image_location_from_backend(self, *args, **kwargs):
  2213. pass
  2214. rules = {"set_image_location": False}
  2215. self.policy.set_rules(rules)
  2216. new_location = {'url': '%s/fake_location' % BASE_URI, 'metadata': {}}
  2217. request = unit_test_utils.get_fake_request()
  2218. changes = [{'op': 'add', 'path': ['locations', '-'],
  2219. 'value': new_location}]
  2220. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  2221. request, UUID1, changes)
  2222. def test_update_delete_image_location_unauthorized(self):
  2223. rules = {"delete_image_location": False}
  2224. self.policy.set_rules(rules)
  2225. request = unit_test_utils.get_fake_request()
  2226. changes = [{'op': 'replace', 'path': ['locations'], 'value': []}]
  2227. self.assertRaises(webob.exc.HTTPForbidden, self.controller.update,
  2228. request, UUID1, changes)
  2229. def test_delete_unauthorized(self):
  2230. rules = {"delete_image": False}
  2231. self.policy.set_rules(rules)
  2232. request = unit_test_utils.get_fake_request()
  2233. self.assertRaises(webob.exc.HTTPForbidden, self.controller.delete,
  2234. request, UUID1)
  2235. class TestImagesDeserializer(test_utils.BaseTestCase):
  2236. def setUp(self):
  2237. super(TestImagesDeserializer, self).setUp()
  2238. self.deserializer = glance.api.v2.images.RequestDeserializer()
  2239. def test_create_minimal(self):
  2240. request = unit_test_utils.get_fake_request()
  2241. request.body = jsonutils.dump_as_bytes({})
  2242. output = self.deserializer.create(request)
  2243. expected = {'image': {}, 'extra_properties': {}, 'tags': []}
  2244. self.assertEqual(expected, output)
  2245. def test_create_invalid_id(self):
  2246. request = unit_test_utils.get_fake_request()
  2247. request.body = jsonutils.dump_as_bytes({'id': 'gabe'})
  2248. self.assertRaises(webob.exc.HTTPBadRequest, self.deserializer.create,
  2249. request)
  2250. def test_create_id_to_image_id(self):
  2251. request = unit_test_utils.get_fake_request()
  2252. request.body = jsonutils.dump_as_bytes({'id': UUID4})
  2253. output = self.deserializer.create(request)
  2254. expected = {'image': {'image_id': UUID4},
  2255. 'extra_properties': {},
  2256. 'tags': []}
  2257. self.assertEqual(expected, output)
  2258. def test_create_no_body(self):
  2259. request = unit_test_utils.get_fake_request()
  2260. self.assertRaises(webob.exc.HTTPBadRequest, self.deserializer.create,
  2261. request)
  2262. def test_create_full(self):
  2263. request = unit_test_utils.get_fake_request()
  2264. request.body = jsonutils.dump_as_bytes({
  2265. 'id': UUID3,
  2266. 'name': 'image-1',
  2267. 'visibility': 'public',
  2268. 'tags': ['one', 'two'],
  2269. 'container_format': 'ami',
  2270. 'disk_format': 'ami',
  2271. 'min_ram': 128,
  2272. 'min_disk': 10,
  2273. 'foo': 'bar',
  2274. 'protected': True,
  2275. })
  2276. output = self.deserializer.create(request)
  2277. properties = {
  2278. 'image_id': UUID3,
  2279. 'name': 'image-1',
  2280. 'visibility': 'public',
  2281. 'container_format': 'ami',
  2282. 'disk_format': 'ami',
  2283. 'min_ram': 128,
  2284. 'min_disk': 10,
  2285. 'protected': True,
  2286. }
  2287. self.maxDiff = None
  2288. expected = {'image': properties,
  2289. 'extra_properties': {'foo': 'bar'},
  2290. 'tags': ['one', 'two']}
  2291. self.assertEqual(expected, output)
  2292. def test_create_invalid_property_key(self):
  2293. request = unit_test_utils.get_fake_request()
  2294. request.body = jsonutils.dump_as_bytes({
  2295. 'id': UUID3,
  2296. 'name': 'image-1',
  2297. 'visibility': 'public',
  2298. 'tags': ['one', 'two'],
  2299. 'container_format': 'ami',
  2300. 'disk_format': 'ami',
  2301. 'min_ram': 128,
  2302. 'min_disk': 10,
  2303. 'f' * 256: 'bar',
  2304. 'protected': True,
  2305. })
  2306. self.assertRaises(webob.exc.HTTPBadRequest, self.deserializer.create,
  2307. request)
  2308. def test_create_readonly_attributes_forbidden(self):
  2309. bodies = [
  2310. {'direct_url': 'http://example.com'},
  2311. {'self': 'http://example.com'},
  2312. {'file': 'http://example.com'},
  2313. {'schema': 'http://example.com'},
  2314. ]
  2315. for body in bodies:
  2316. request = unit_test_utils.get_fake_request()
  2317. request.body = jsonutils.dump_as_bytes(body)
  2318. self.assertRaises(webob.exc.HTTPForbidden,
  2319. self.deserializer.create, request)
  2320. def _get_fake_patch_request(self, content_type_minor_version=1):
  2321. request = unit_test_utils.get_fake_request()
  2322. template = 'application/openstack-images-v2.%d-json-patch'
  2323. request.content_type = template % content_type_minor_version
  2324. return request
  2325. def test_update_empty_body(self):
  2326. request = self._get_fake_patch_request()
  2327. request.body = jsonutils.dump_as_bytes([])
  2328. output = self.deserializer.update(request)
  2329. expected = {'changes': []}
  2330. self.assertEqual(expected, output)
  2331. def test_update_unsupported_content_type(self):
  2332. request = unit_test_utils.get_fake_request()
  2333. request.content_type = 'application/json-patch'
  2334. request.body = jsonutils.dump_as_bytes([])
  2335. try:
  2336. self.deserializer.update(request)
  2337. except webob.exc.HTTPUnsupportedMediaType as e:
  2338. # desired result, but must have correct Accept-Patch header
  2339. accept_patch = ['application/openstack-images-v2.1-json-patch',
  2340. 'application/openstack-images-v2.0-json-patch']
  2341. expected = ', '.join(sorted(accept_patch))
  2342. self.assertEqual(expected, e.headers['Accept-Patch'])
  2343. else:
  2344. self.fail('Did not raise HTTPUnsupportedMediaType')
  2345. def test_update_body_not_a_list(self):
  2346. bodies = [
  2347. {'op': 'add', 'path': '/someprop', 'value': 'somevalue'},
  2348. 'just some string',
  2349. 123,
  2350. True,
  2351. False,
  2352. None,
  2353. ]
  2354. for body in bodies:
  2355. request = self._get_fake_patch_request()
  2356. request.body = jsonutils.dump_as_bytes(body)
  2357. self.assertRaises(webob.exc.HTTPBadRequest,
  2358. self.deserializer.update, request)
  2359. def test_update_invalid_changes(self):
  2360. changes = [
  2361. ['a', 'list', 'of', 'stuff'],
  2362. 'just some string',
  2363. 123,
  2364. True,
  2365. False,
  2366. None,
  2367. {'op': 'invalid', 'path': '/name', 'value': 'fedora'}
  2368. ]
  2369. for change in changes:
  2370. request = self._get_fake_patch_request()
  2371. request.body = jsonutils.dump_as_bytes([change])
  2372. self.assertRaises(webob.exc.HTTPBadRequest,
  2373. self.deserializer.update, request)
  2374. def test_update(self):
  2375. request = self._get_fake_patch_request()
  2376. body = [
  2377. {'op': 'replace', 'path': '/name', 'value': 'fedora'},
  2378. {'op': 'replace', 'path': '/tags', 'value': ['king', 'kong']},
  2379. {'op': 'replace', 'path': '/foo', 'value': 'bar'},
  2380. {'op': 'add', 'path': '/bebim', 'value': 'bap'},
  2381. {'op': 'remove', 'path': '/sparks'},
  2382. {'op': 'add', 'path': '/locations/-',
  2383. 'value': {'url': 'scheme3://path3', 'metadata': {}}},
  2384. {'op': 'add', 'path': '/locations/10',
  2385. 'value': {'url': 'scheme4://path4', 'metadata': {}}},
  2386. {'op': 'remove', 'path': '/locations/2'},
  2387. {'op': 'replace', 'path': '/locations', 'value': []},
  2388. {'op': 'replace', 'path': '/locations',
  2389. 'value': [{'url': 'scheme5://path5', 'metadata': {}},
  2390. {'url': 'scheme6://path6', 'metadata': {}}]},
  2391. ]
  2392. request.body = jsonutils.dump_as_bytes(body)
  2393. output = self.deserializer.update(request)
  2394. expected = {'changes': [
  2395. {'json_schema_version': 10, 'op': 'replace',
  2396. 'path': ['name'], 'value': 'fedora'},
  2397. {'json_schema_version': 10, 'op': 'replace',
  2398. 'path': ['tags'], 'value': ['king', 'kong']},
  2399. {'json_schema_version': 10, 'op': 'replace',
  2400. 'path': ['foo'], 'value': 'bar'},
  2401. {'json_schema_version': 10, 'op': 'add',
  2402. 'path': ['bebim'], 'value': 'bap'},
  2403. {'json_schema_version': 10, 'op': 'remove',
  2404. 'path': ['sparks']},
  2405. {'json_schema_version': 10, 'op': 'add',
  2406. 'path': ['locations', '-'],
  2407. 'value': {'url': 'scheme3://path3', 'metadata': {}}},
  2408. {'json_schema_version': 10, 'op': 'add',
  2409. 'path': ['locations', '10'],
  2410. 'value': {'url': 'scheme4://path4', 'metadata': {}}},
  2411. {'json_schema_version': 10, 'op': 'remove',
  2412. 'path': ['locations', '2']},
  2413. {'json_schema_version': 10, 'op': 'replace',
  2414. 'path': ['locations'], 'value': []},
  2415. {'json_schema_version': 10, 'op': 'replace',
  2416. 'path': ['locations'],
  2417. 'value': [{'url': 'scheme5://path5', 'metadata': {}},
  2418. {'url': 'scheme6://path6', 'metadata': {}}]},
  2419. ]}
  2420. self.assertEqual(expected, output)
  2421. def test_update_v2_0_compatibility(self):
  2422. request = self._get_fake_patch_request(content_type_minor_version=0)
  2423. body = [
  2424. {'replace': '/name', 'value': 'fedora'},
  2425. {'replace': '/tags', 'value': ['king', 'kong']},
  2426. {'replace': '/foo', 'value': 'bar'},
  2427. {'add': '/bebim', 'value': 'bap'},
  2428. {'remove': '/sparks'},
  2429. {'add': '/locations/-', 'value': {'url': 'scheme3://path3',
  2430. 'metadata': {}}},
  2431. {'add': '/locations/10', 'value': {'url': 'scheme4://path4',
  2432. 'metadata': {}}},
  2433. {'remove': '/locations/2'},
  2434. {'replace': '/locations', 'value': []},
  2435. {'replace': '/locations',
  2436. 'value': [{'url': 'scheme5://path5', 'metadata': {}},
  2437. {'url': 'scheme6://path6', 'metadata': {}}]},
  2438. ]
  2439. request.body = jsonutils.dump_as_bytes(body)
  2440. output = self.deserializer.update(request)
  2441. expected = {'changes': [
  2442. {'json_schema_version': 4, 'op': 'replace',
  2443. 'path': ['name'], 'value': 'fedora'},
  2444. {'json_schema_version': 4, 'op': 'replace',
  2445. 'path': ['tags'], 'value': ['king', 'kong']},
  2446. {'json_schema_version': 4, 'op': 'replace',
  2447. 'path': ['foo'], 'value': 'bar'},
  2448. {'json_schema_version': 4, 'op': 'add',
  2449. 'path': ['bebim'], 'value': 'bap'},
  2450. {'json_schema_version': 4, 'op': 'remove', 'path': ['sparks']},
  2451. {'json_schema_version': 4, 'op': 'add',
  2452. 'path': ['locations', '-'],
  2453. 'value': {'url': 'scheme3://path3', 'metadata': {}}},
  2454. {'json_schema_version': 4, 'op': 'add',
  2455. 'path': ['locations', '10'],
  2456. 'value': {'url': 'scheme4://path4', 'metadata': {}}},
  2457. {'json_schema_version': 4, 'op': 'remove',
  2458. 'path': ['locations', '2']},
  2459. {'json_schema_version': 4, 'op': 'replace',
  2460. 'path': ['locations'], 'value': []},
  2461. {'json_schema_version': 4, 'op': 'replace', 'path': ['locations'],
  2462. 'value': [{'url': 'scheme5://path5', 'metadata': {}},
  2463. {'url': 'scheme6://path6', 'metadata': {}}]},
  2464. ]}
  2465. self.assertEqual(expected, output)
  2466. def test_update_base_attributes(self):
  2467. request = self._get_fake_patch_request()
  2468. body = [
  2469. {'op': 'replace', 'path': '/name', 'value': 'fedora'},
  2470. {'op': 'replace', 'path': '/visibility', 'value': 'public'},
  2471. {'op': 'replace', 'path': '/tags', 'value': ['king', 'kong']},
  2472. {'op': 'replace', 'path': '/protected', 'value': True},
  2473. {'op': 'replace', 'path': '/container_format', 'value': 'bare'},
  2474. {'op': 'replace', 'path': '/disk_format', 'value': 'raw'},
  2475. {'op': 'replace', 'path': '/min_ram', 'value': 128},
  2476. {'op': 'replace', 'path': '/min_disk', 'value': 10},
  2477. {'op': 'replace', 'path': '/locations', 'value': []},
  2478. {'op': 'replace', 'path': '/locations',
  2479. 'value': [{'url': 'scheme5://path5', 'metadata': {}},
  2480. {'url': 'scheme6://path6', 'metadata': {}}]}
  2481. ]
  2482. request.body = jsonutils.dump_as_bytes(body)
  2483. output = self.deserializer.update(request)
  2484. expected = {'changes': [
  2485. {'json_schema_version': 10, 'op': 'replace',
  2486. 'path': ['name'], 'value': 'fedora'},
  2487. {'json_schema_version': 10, 'op': 'replace',
  2488. 'path': ['visibility'], 'value': 'public'},
  2489. {'json_schema_version': 10, 'op': 'replace',
  2490. 'path': ['tags'], 'value': ['king', 'kong']},
  2491. {'json_schema_version': 10, 'op': 'replace',
  2492. 'path': ['protected'], 'value': True},
  2493. {'json_schema_version': 10, 'op': 'replace',
  2494. 'path': ['container_format'], 'value': 'bare'},
  2495. {'json_schema_version': 10, 'op': 'replace',
  2496. 'path': ['disk_format'], 'value': 'raw'},
  2497. {'json_schema_version': 10, 'op': 'replace',
  2498. 'path': ['min_ram'], 'value': 128},
  2499. {'json_schema_version': 10, 'op': 'replace',
  2500. 'path': ['min_disk'], 'value': 10},
  2501. {'json_schema_version': 10, 'op': 'replace',
  2502. 'path': ['locations'], 'value': []},
  2503. {'json_schema_version': 10, 'op': 'replace', 'path': ['locations'],
  2504. 'value': [{'url': 'scheme5://path5', 'metadata': {}},
  2505. {'url': 'scheme6://path6', 'metadata': {}}]}
  2506. ]}
  2507. self.assertEqual(expected, output)
  2508. def test_update_disallowed_attributes(self):
  2509. samples = {
  2510. 'direct_url': '/a/b/c/d',
  2511. 'self': '/e/f/g/h',
  2512. 'file': '/e/f/g/h/file',
  2513. 'schema': '/i/j/k',
  2514. }
  2515. for key, value in samples.items():
  2516. request = self._get_fake_patch_request()
  2517. body = [{'op': 'replace', 'path': '/%s' % key, 'value': value}]
  2518. request.body = jsonutils.dump_as_bytes(body)
  2519. try:
  2520. self.deserializer.update(request)
  2521. except webob.exc.HTTPForbidden:
  2522. pass # desired behavior
  2523. else:
  2524. self.fail("Updating %s did not result in HTTPForbidden" % key)
  2525. def test_update_readonly_attributes(self):
  2526. samples = {
  2527. 'id': '00000000-0000-0000-0000-000000000000',
  2528. 'status': 'active',
  2529. 'checksum': 'abcdefghijklmnopqrstuvwxyz012345',
  2530. 'os_hash_algo': 'supersecure',
  2531. 'os_hash_value': 'a' * 32 + 'b' * 32 + 'c' * 32 + 'd' * 32,
  2532. 'size': 9001,
  2533. 'virtual_size': 9001,
  2534. 'created_at': ISOTIME,
  2535. 'updated_at': ISOTIME,
  2536. }
  2537. for key, value in samples.items():
  2538. request = self._get_fake_patch_request()
  2539. body = [{'op': 'replace', 'path': '/%s' % key, 'value': value}]
  2540. request.body = jsonutils.dump_as_bytes(body)
  2541. try:
  2542. self.deserializer.update(request)
  2543. except webob.exc.HTTPForbidden:
  2544. pass # desired behavior
  2545. else:
  2546. self.fail("Updating %s did not result in HTTPForbidden" % key)
  2547. def test_update_reserved_attributes(self):
  2548. samples = {
  2549. 'deleted': False,
  2550. 'deleted_at': ISOTIME,
  2551. }
  2552. for key, value in samples.items():
  2553. request = self._get_fake_patch_request()
  2554. body = [{'op': 'replace', 'path': '/%s' % key, 'value': value}]
  2555. request.body = jsonutils.dump_as_bytes(body)
  2556. try:
  2557. self.deserializer.update(request)
  2558. except webob.exc.HTTPForbidden:
  2559. pass # desired behavior
  2560. else:
  2561. self.fail("Updating %s did not result in HTTPForbidden" % key)
  2562. def test_update_invalid_attributes(self):
  2563. keys = [
  2564. 'noslash',
  2565. '///twoslash',
  2566. '/two/ /slash',
  2567. '/ / ',
  2568. '/trailingslash/',
  2569. '/lone~tilde',
  2570. '/trailingtilde~'
  2571. ]
  2572. for key in keys:
  2573. request = self._get_fake_patch_request()
  2574. body = [{'op': 'replace', 'path': '%s' % key, 'value': 'dummy'}]
  2575. request.body = jsonutils.dump_as_bytes(body)
  2576. try:
  2577. self.deserializer.update(request)
  2578. except webob.exc.HTTPBadRequest:
  2579. pass # desired behavior
  2580. else:
  2581. self.fail("Updating %s did not result in HTTPBadRequest" % key)
  2582. def test_update_pointer_encoding(self):
  2583. samples = {
  2584. '/keywith~1slash': [u'keywith/slash'],
  2585. '/keywith~0tilde': [u'keywith~tilde'],
  2586. '/tricky~01': [u'tricky~1'],
  2587. }
  2588. for encoded, decoded in samples.items():
  2589. request = self._get_fake_patch_request()
  2590. doc = [{'op': 'replace', 'path': '%s' % encoded, 'value': 'dummy'}]
  2591. request.body = jsonutils.dump_as_bytes(doc)
  2592. output = self.deserializer.update(request)
  2593. self.assertEqual(decoded, output['changes'][0]['path'])
  2594. def test_update_deep_limited_attributes(self):
  2595. samples = {
  2596. 'locations/1/2': [],
  2597. }
  2598. for key, value in samples.items():
  2599. request = self._get_fake_patch_request()
  2600. body = [{'op': 'replace', 'path': '/%s' % key, 'value': value}]
  2601. request.body = jsonutils.dump_as_bytes(body)
  2602. try:
  2603. self.deserializer.update(request)
  2604. except webob.exc.HTTPBadRequest:
  2605. pass # desired behavior
  2606. else:
  2607. self.fail("Updating %s did not result in HTTPBadRequest" % key)
  2608. def test_update_v2_1_missing_operations(self):
  2609. request = self._get_fake_patch_request()
  2610. body = [{'path': '/colburn', 'value': 'arcata'}]
  2611. request.body = jsonutils.dump_as_bytes(body)
  2612. self.assertRaises(webob.exc.HTTPBadRequest,
  2613. self.deserializer.update, request)
  2614. def test_update_v2_1_missing_value(self):
  2615. request = self._get_fake_patch_request()
  2616. body = [{'op': 'replace', 'path': '/colburn'}]
  2617. request.body = jsonutils.dump_as_bytes(body)
  2618. self.assertRaises(webob.exc.HTTPBadRequest,
  2619. self.deserializer.update, request)
  2620. def test_update_v2_1_missing_path(self):
  2621. request = self._get_fake_patch_request()
  2622. body = [{'op': 'replace', 'value': 'arcata'}]
  2623. request.body = jsonutils.dump_as_bytes(body)
  2624. self.assertRaises(webob.exc.HTTPBadRequest,
  2625. self.deserializer.update, request)
  2626. def test_update_v2_0_multiple_operations(self):
  2627. request = self._get_fake_patch_request(content_type_minor_version=0)
  2628. body = [{'replace': '/foo', 'add': '/bar', 'value': 'snore'}]
  2629. request.body = jsonutils.dump_as_bytes(body)
  2630. self.assertRaises(webob.exc.HTTPBadRequest,
  2631. self.deserializer.update, request)
  2632. def test_update_v2_0_missing_operations(self):
  2633. request = self._get_fake_patch_request(content_type_minor_version=0)
  2634. body = [{'value': 'arcata'}]
  2635. request.body = jsonutils.dump_as_bytes(body)
  2636. self.assertRaises(webob.exc.HTTPBadRequest,
  2637. self.deserializer.update, request)
  2638. def test_update_v2_0_missing_value(self):
  2639. request = self._get_fake_patch_request(content_type_minor_version=0)
  2640. body = [{'replace': '/colburn'}]
  2641. request.body = jsonutils.dump_as_bytes(body)
  2642. self.assertRaises(webob.exc.HTTPBadRequest,
  2643. self.deserializer.update, request)
  2644. def test_index(self):
  2645. marker = str(uuid.uuid4())
  2646. path = '/images?limit=1&marker=%s&member_status=pending' % marker
  2647. request = unit_test_utils.get_fake_request(path)
  2648. expected = {'limit': 1,
  2649. 'marker': marker,
  2650. 'sort_key': ['created_at'],
  2651. 'sort_dir': ['desc'],
  2652. 'member_status': 'pending',
  2653. 'filters': {}}
  2654. output = self.deserializer.index(request)
  2655. self.assertEqual(expected, output)
  2656. def test_index_with_filter(self):
  2657. name = 'My Little Image'
  2658. path = '/images?name=%s' % name
  2659. request = unit_test_utils.get_fake_request(path)
  2660. output = self.deserializer.index(request)
  2661. self.assertEqual(name, output['filters']['name'])
  2662. def test_index_strip_params_from_filters(self):
  2663. name = 'My Little Image'
  2664. path = '/images?name=%s' % name
  2665. request = unit_test_utils.get_fake_request(path)
  2666. output = self.deserializer.index(request)
  2667. self.assertEqual(name, output['filters']['name'])
  2668. self.assertEqual(1, len(output['filters']))
  2669. def test_index_with_many_filter(self):
  2670. name = 'My Little Image'
  2671. instance_id = str(uuid.uuid4())
  2672. path = ('/images?name=%(name)s&id=%(instance_id)s' %
  2673. {'name': name, 'instance_id': instance_id})
  2674. request = unit_test_utils.get_fake_request(path)
  2675. output = self.deserializer.index(request)
  2676. self.assertEqual(name, output['filters']['name'])
  2677. self.assertEqual(instance_id, output['filters']['id'])
  2678. def test_index_with_filter_and_limit(self):
  2679. name = 'My Little Image'
  2680. path = '/images?name=%s&limit=1' % name
  2681. request = unit_test_utils.get_fake_request(path)
  2682. output = self.deserializer.index(request)
  2683. self.assertEqual(name, output['filters']['name'])
  2684. self.assertEqual(1, output['limit'])
  2685. def test_index_non_integer_limit(self):
  2686. request = unit_test_utils.get_fake_request('/images?limit=blah')
  2687. self.assertRaises(webob.exc.HTTPBadRequest,
  2688. self.deserializer.index, request)
  2689. def test_index_zero_limit(self):
  2690. request = unit_test_utils.get_fake_request('/images?limit=0')
  2691. expected = {'limit': 0,
  2692. 'sort_key': ['created_at'],
  2693. 'member_status': 'accepted',
  2694. 'sort_dir': ['desc'],
  2695. 'filters': {}}
  2696. output = self.deserializer.index(request)
  2697. self.assertEqual(expected, output)
  2698. def test_index_negative_limit(self):
  2699. request = unit_test_utils.get_fake_request('/images?limit=-1')
  2700. self.assertRaises(webob.exc.HTTPBadRequest,
  2701. self.deserializer.index, request)
  2702. def test_index_fraction(self):
  2703. request = unit_test_utils.get_fake_request('/images?limit=1.1')
  2704. self.assertRaises(webob.exc.HTTPBadRequest,
  2705. self.deserializer.index, request)
  2706. def test_index_invalid_status(self):
  2707. path = '/images?member_status=blah'
  2708. request = unit_test_utils.get_fake_request(path)
  2709. self.assertRaises(webob.exc.HTTPBadRequest,
  2710. self.deserializer.index, request)
  2711. def test_index_marker(self):
  2712. marker = str(uuid.uuid4())
  2713. path = '/images?marker=%s' % marker
  2714. request = unit_test_utils.get_fake_request(path)
  2715. output = self.deserializer.index(request)
  2716. self.assertEqual(marker, output.get('marker'))
  2717. def test_index_marker_not_specified(self):
  2718. request = unit_test_utils.get_fake_request('/images')
  2719. output = self.deserializer.index(request)
  2720. self.assertNotIn('marker', output)
  2721. def test_index_limit_not_specified(self):
  2722. request = unit_test_utils.get_fake_request('/images')
  2723. output = self.deserializer.index(request)
  2724. self.assertNotIn('limit', output)
  2725. def test_index_sort_key_id(self):
  2726. request = unit_test_utils.get_fake_request('/images?sort_key=id')
  2727. output = self.deserializer.index(request)
  2728. expected = {
  2729. 'sort_key': ['id'],
  2730. 'sort_dir': ['desc'],
  2731. 'member_status': 'accepted',
  2732. 'filters': {}
  2733. }
  2734. self.assertEqual(expected, output)
  2735. def test_index_multiple_sort_keys(self):
  2736. request = unit_test_utils.get_fake_request('/images?'
  2737. 'sort_key=name&'
  2738. 'sort_key=size')
  2739. output = self.deserializer.index(request)
  2740. expected = {
  2741. 'sort_key': ['name', 'size'],
  2742. 'sort_dir': ['desc'],
  2743. 'member_status': 'accepted',
  2744. 'filters': {}
  2745. }
  2746. self.assertEqual(expected, output)
  2747. def test_index_invalid_multiple_sort_keys(self):
  2748. # blah is an invalid sort key
  2749. request = unit_test_utils.get_fake_request('/images?'
  2750. 'sort_key=name&'
  2751. 'sort_key=blah')
  2752. self.assertRaises(webob.exc.HTTPBadRequest,
  2753. self.deserializer.index, request)
  2754. def test_index_sort_dir_asc(self):
  2755. request = unit_test_utils.get_fake_request('/images?sort_dir=asc')
  2756. output = self.deserializer.index(request)
  2757. expected = {
  2758. 'sort_key': ['created_at'],
  2759. 'sort_dir': ['asc'],
  2760. 'member_status': 'accepted',
  2761. 'filters': {}}
  2762. self.assertEqual(expected, output)
  2763. def test_index_multiple_sort_dirs(self):
  2764. req_string = ('/images?sort_key=name&sort_dir=asc&'
  2765. 'sort_key=id&sort_dir=desc')
  2766. request = unit_test_utils.get_fake_request(req_string)
  2767. output = self.deserializer.index(request)
  2768. expected = {
  2769. 'sort_key': ['name', 'id'],
  2770. 'sort_dir': ['asc', 'desc'],
  2771. 'member_status': 'accepted',
  2772. 'filters': {}}
  2773. self.assertEqual(expected, output)
  2774. def test_index_new_sorting_syntax_single_key_default_dir(self):
  2775. req_string = '/images?sort=name'
  2776. request = unit_test_utils.get_fake_request(req_string)
  2777. output = self.deserializer.index(request)
  2778. expected = {
  2779. 'sort_key': ['name'],
  2780. 'sort_dir': ['desc'],
  2781. 'member_status': 'accepted',
  2782. 'filters': {}}
  2783. self.assertEqual(expected, output)
  2784. def test_index_new_sorting_syntax_single_key_desc_dir(self):
  2785. req_string = '/images?sort=name:desc'
  2786. request = unit_test_utils.get_fake_request(req_string)
  2787. output = self.deserializer.index(request)
  2788. expected = {
  2789. 'sort_key': ['name'],
  2790. 'sort_dir': ['desc'],
  2791. 'member_status': 'accepted',
  2792. 'filters': {}}
  2793. self.assertEqual(expected, output)
  2794. def test_index_new_sorting_syntax_multiple_keys_default_dir(self):
  2795. req_string = '/images?sort=name,size'
  2796. request = unit_test_utils.get_fake_request(req_string)
  2797. output = self.deserializer.index(request)
  2798. expected = {
  2799. 'sort_key': ['name', 'size'],
  2800. 'sort_dir': ['desc', 'desc'],
  2801. 'member_status': 'accepted',
  2802. 'filters': {}}
  2803. self.assertEqual(expected, output)
  2804. def test_index_new_sorting_syntax_multiple_keys_asc_dir(self):
  2805. req_string = '/images?sort=name:asc,size:asc'
  2806. request = unit_test_utils.get_fake_request(req_string)
  2807. output = self.deserializer.index(request)
  2808. expected = {
  2809. 'sort_key': ['name', 'size'],
  2810. 'sort_dir': ['asc', 'asc'],
  2811. 'member_status': 'accepted',
  2812. 'filters': {}}
  2813. self.assertEqual(expected, output)
  2814. def test_index_new_sorting_syntax_multiple_keys_different_dirs(self):
  2815. req_string = '/images?sort=name:desc,size:asc'
  2816. request = unit_test_utils.get_fake_request(req_string)
  2817. output = self.deserializer.index(request)
  2818. expected = {
  2819. 'sort_key': ['name', 'size'],
  2820. 'sort_dir': ['desc', 'asc'],
  2821. 'member_status': 'accepted',
  2822. 'filters': {}}
  2823. self.assertEqual(expected, output)
  2824. def test_index_new_sorting_syntax_multiple_keys_optional_dir(self):
  2825. req_string = '/images?sort=name:asc,size'
  2826. request = unit_test_utils.get_fake_request(req_string)
  2827. output = self.deserializer.index(request)
  2828. expected = {
  2829. 'sort_key': ['name', 'size'],
  2830. 'sort_dir': ['asc', 'desc'],
  2831. 'member_status': 'accepted',
  2832. 'filters': {}}
  2833. self.assertEqual(expected, output)
  2834. req_string = '/images?sort=name,size:asc'
  2835. request = unit_test_utils.get_fake_request(req_string)
  2836. output = self.deserializer.index(request)
  2837. expected = {
  2838. 'sort_key': ['name', 'size'],
  2839. 'sort_dir': ['desc', 'asc'],
  2840. 'member_status': 'accepted',
  2841. 'filters': {}}
  2842. self.assertEqual(expected, output)
  2843. req_string = '/images?sort=name,id:asc,size'
  2844. request = unit_test_utils.get_fake_request(req_string)
  2845. output = self.deserializer.index(request)
  2846. expected = {
  2847. 'sort_key': ['name', 'id', 'size'],
  2848. 'sort_dir': ['desc', 'asc', 'desc'],
  2849. 'member_status': 'accepted',
  2850. 'filters': {}}
  2851. self.assertEqual(expected, output)
  2852. req_string = '/images?sort=name:asc,id,size:asc'
  2853. request = unit_test_utils.get_fake_request(req_string)
  2854. output = self.deserializer.index(request)
  2855. expected = {
  2856. 'sort_key': ['name', 'id', 'size'],
  2857. 'sort_dir': ['asc', 'desc', 'asc'],
  2858. 'member_status': 'accepted',
  2859. 'filters': {}}
  2860. self.assertEqual(expected, output)
  2861. def test_index_sort_wrong_sort_dirs_number(self):
  2862. req_string = '/images?sort_key=name&sort_dir=asc&sort_dir=desc'
  2863. request = unit_test_utils.get_fake_request(req_string)
  2864. self.assertRaises(webob.exc.HTTPBadRequest,
  2865. self.deserializer.index, request)
  2866. def test_index_sort_dirs_fewer_than_keys(self):
  2867. req_string = ('/images?sort_key=name&sort_dir=asc&sort_key=id&'
  2868. 'sort_dir=asc&sort_key=created_at')
  2869. request = unit_test_utils.get_fake_request(req_string)
  2870. self.assertRaises(webob.exc.HTTPBadRequest,
  2871. self.deserializer.index, request)
  2872. def test_index_sort_wrong_sort_dirs_number_without_key(self):
  2873. req_string = '/images?sort_dir=asc&sort_dir=desc'
  2874. request = unit_test_utils.get_fake_request(req_string)
  2875. self.assertRaises(webob.exc.HTTPBadRequest,
  2876. self.deserializer.index, request)
  2877. def test_index_sort_private_key(self):
  2878. request = unit_test_utils.get_fake_request('/images?sort_key=min_ram')
  2879. self.assertRaises(webob.exc.HTTPBadRequest,
  2880. self.deserializer.index, request)
  2881. def test_index_sort_key_invalid_value(self):
  2882. # blah is an invalid sort key
  2883. request = unit_test_utils.get_fake_request('/images?sort_key=blah')
  2884. self.assertRaises(webob.exc.HTTPBadRequest,
  2885. self.deserializer.index, request)
  2886. def test_index_sort_dir_invalid_value(self):
  2887. # foo is an invalid sort dir
  2888. request = unit_test_utils.get_fake_request('/images?sort_dir=foo')
  2889. self.assertRaises(webob.exc.HTTPBadRequest,
  2890. self.deserializer.index, request)
  2891. def test_index_new_sorting_syntax_invalid_request(self):
  2892. # 'blah' is not a supported sorting key
  2893. req_string = '/images?sort=blah'
  2894. request = unit_test_utils.get_fake_request(req_string)
  2895. self.assertRaises(webob.exc.HTTPBadRequest,
  2896. self.deserializer.index, request)
  2897. req_string = '/images?sort=name,blah'
  2898. request = unit_test_utils.get_fake_request(req_string)
  2899. self.assertRaises(webob.exc.HTTPBadRequest,
  2900. self.deserializer.index, request)
  2901. # 'foo' isn't a valid sort direction
  2902. req_string = '/images?sort=name:foo'
  2903. request = unit_test_utils.get_fake_request(req_string)
  2904. self.assertRaises(webob.exc.HTTPBadRequest,
  2905. self.deserializer.index, request)
  2906. # 'asc:desc' isn't a valid sort direction
  2907. req_string = '/images?sort=name:asc:desc'
  2908. request = unit_test_utils.get_fake_request(req_string)
  2909. self.assertRaises(webob.exc.HTTPBadRequest,
  2910. self.deserializer.index, request)
  2911. def test_index_combined_sorting_syntax(self):
  2912. req_string = '/images?sort_dir=name&sort=name'
  2913. request = unit_test_utils.get_fake_request(req_string)
  2914. self.assertRaises(webob.exc.HTTPBadRequest,
  2915. self.deserializer.index, request)
  2916. def test_index_with_tag(self):
  2917. path = '/images?tag=%s&tag=%s' % ('x86', '64bit')
  2918. request = unit_test_utils.get_fake_request(path)
  2919. output = self.deserializer.index(request)
  2920. self.assertEqual(sorted(['x86', '64bit']),
  2921. sorted(output['filters']['tags']))
  2922. def test_image_import(self):
  2923. # Bug 1754634: make sure that what's considered valid
  2924. # is determined by the config option
  2925. self.config(enabled_import_methods=['party-time'])
  2926. request = unit_test_utils.get_fake_request()
  2927. import_body = {
  2928. "method": {
  2929. "name": "party-time"
  2930. }
  2931. }
  2932. request.body = jsonutils.dump_as_bytes(import_body)
  2933. output = self.deserializer.import_image(request)
  2934. expected = {"body": import_body}
  2935. self.assertEqual(expected, output)
  2936. def test_import_image_invalid_body(self):
  2937. request = unit_test_utils.get_fake_request()
  2938. import_body = {
  2939. "method1": {
  2940. "name": "glance-direct"
  2941. }
  2942. }
  2943. request.body = jsonutils.dump_as_bytes(import_body)
  2944. self.assertRaises(webob.exc.HTTPBadRequest,
  2945. self.deserializer.import_image,
  2946. request)
  2947. def test_import_image_invalid_input(self):
  2948. request = unit_test_utils.get_fake_request()
  2949. import_body = {
  2950. "method": {
  2951. "abcd": "glance-direct"
  2952. }
  2953. }
  2954. request.body = jsonutils.dump_as_bytes(import_body)
  2955. self.assertRaises(webob.exc.HTTPBadRequest,
  2956. self.deserializer.import_image,
  2957. request)
  2958. def _get_request_for_method(self, method_name):
  2959. request = unit_test_utils.get_fake_request()
  2960. import_body = {
  2961. "method": {
  2962. "name": method_name
  2963. }
  2964. }
  2965. request.body = jsonutils.dump_as_bytes(import_body)
  2966. return request
  2967. KNOWN_IMPORT_METHODS = ['glance-direct', 'web-download']
  2968. def test_import_image_invalid_import_method(self):
  2969. # Bug 1754634: make sure that what's considered valid
  2970. # is determined by the config option. So put known bad
  2971. # name in config, and known good name in request
  2972. self.config(enabled_import_methods=['bad-method-name'])
  2973. for m in self.KNOWN_IMPORT_METHODS:
  2974. request = self._get_request_for_method(m)
  2975. self.assertRaises(webob.exc.HTTPBadRequest,
  2976. self.deserializer.import_image,
  2977. request)
  2978. class TestImagesDeserializerWithExtendedSchema(test_utils.BaseTestCase):
  2979. def setUp(self):
  2980. super(TestImagesDeserializerWithExtendedSchema, self).setUp()
  2981. self.config(allow_additional_image_properties=False)
  2982. custom_image_properties = {
  2983. 'pants': {
  2984. 'type': 'string',
  2985. 'enum': ['on', 'off'],
  2986. },
  2987. }
  2988. schema = glance.api.v2.images.get_schema(custom_image_properties)
  2989. self.deserializer = glance.api.v2.images.RequestDeserializer(schema)
  2990. def test_create(self):
  2991. request = unit_test_utils.get_fake_request()
  2992. request.body = jsonutils.dump_as_bytes({
  2993. 'name': 'image-1',
  2994. 'pants': 'on'
  2995. })
  2996. output = self.deserializer.create(request)
  2997. expected = {
  2998. 'image': {'name': 'image-1'},
  2999. 'extra_properties': {'pants': 'on'},
  3000. 'tags': [],
  3001. }
  3002. self.assertEqual(expected, output)
  3003. def test_create_bad_data(self):
  3004. request = unit_test_utils.get_fake_request()
  3005. request.body = jsonutils.dump_as_bytes({
  3006. 'name': 'image-1',
  3007. 'pants': 'borked'
  3008. })
  3009. self.assertRaises(webob.exc.HTTPBadRequest,
  3010. self.deserializer.create, request)
  3011. def test_update(self):
  3012. request = unit_test_utils.get_fake_request()
  3013. request.content_type = 'application/openstack-images-v2.1-json-patch'
  3014. doc = [{'op': 'add', 'path': '/pants', 'value': 'off'}]
  3015. request.body = jsonutils.dump_as_bytes(doc)
  3016. output = self.deserializer.update(request)
  3017. expected = {'changes': [
  3018. {'json_schema_version': 10, 'op': 'add',
  3019. 'path': ['pants'], 'value': 'off'},
  3020. ]}
  3021. self.assertEqual(expected, output)
  3022. def test_update_bad_data(self):
  3023. request = unit_test_utils.get_fake_request()
  3024. request.content_type = 'application/openstack-images-v2.1-json-patch'
  3025. doc = [{'op': 'add', 'path': '/pants', 'value': 'cutoffs'}]
  3026. request.body = jsonutils.dump_as_bytes(doc)
  3027. self.assertRaises(webob.exc.HTTPBadRequest,
  3028. self.deserializer.update,
  3029. request)
  3030. class TestImagesDeserializerWithAdditionalProperties(test_utils.BaseTestCase):
  3031. def setUp(self):
  3032. super(TestImagesDeserializerWithAdditionalProperties, self).setUp()
  3033. self.config(allow_additional_image_properties=True)
  3034. self.deserializer = glance.api.v2.images.RequestDeserializer()
  3035. def test_create(self):
  3036. request = unit_test_utils.get_fake_request()
  3037. request.body = jsonutils.dump_as_bytes({'foo': 'bar'})
  3038. output = self.deserializer.create(request)
  3039. expected = {'image': {},
  3040. 'extra_properties': {'foo': 'bar'},
  3041. 'tags': []}
  3042. self.assertEqual(expected, output)
  3043. def test_create_with_numeric_property(self):
  3044. request = unit_test_utils.get_fake_request()
  3045. request.body = jsonutils.dump_as_bytes({'abc': 123})
  3046. self.assertRaises(webob.exc.HTTPBadRequest,
  3047. self.deserializer.create, request)
  3048. def test_update_with_numeric_property(self):
  3049. request = unit_test_utils.get_fake_request()
  3050. request.content_type = 'application/openstack-images-v2.1-json-patch'
  3051. doc = [{'op': 'add', 'path': '/foo', 'value': 123}]
  3052. request.body = jsonutils.dump_as_bytes(doc)
  3053. self.assertRaises(webob.exc.HTTPBadRequest,
  3054. self.deserializer.update, request)
  3055. def test_create_with_list_property(self):
  3056. request = unit_test_utils.get_fake_request()
  3057. request.body = jsonutils.dump_as_bytes({'foo': ['bar']})
  3058. self.assertRaises(webob.exc.HTTPBadRequest,
  3059. self.deserializer.create, request)
  3060. def test_update_with_list_property(self):
  3061. request = unit_test_utils.get_fake_request()
  3062. request.content_type = 'application/openstack-images-v2.1-json-patch'
  3063. doc = [{'op': 'add', 'path': '/foo', 'value': ['bar', 'baz']}]
  3064. request.body = jsonutils.dump_as_bytes(doc)
  3065. self.assertRaises(webob.exc.HTTPBadRequest,
  3066. self.deserializer.update, request)
  3067. def test_update(self):
  3068. request = unit_test_utils.get_fake_request()
  3069. request.content_type = 'application/openstack-images-v2.1-json-patch'
  3070. doc = [{'op': 'add', 'path': '/foo', 'value': 'bar'}]
  3071. request.body = jsonutils.dump_as_bytes(doc)
  3072. output = self.deserializer.update(request)
  3073. change = {
  3074. 'json_schema_version': 10, 'op': 'add',
  3075. 'path': ['foo'], 'value': 'bar'
  3076. }
  3077. self.assertEqual({'changes': [change]}, output)
  3078. class TestImagesDeserializerNoAdditionalProperties(test_utils.BaseTestCase):
  3079. def setUp(self):
  3080. super(TestImagesDeserializerNoAdditionalProperties, self).setUp()
  3081. self.config(allow_additional_image_properties=False)
  3082. self.deserializer = glance.api.v2.images.RequestDeserializer()
  3083. def test_create_with_additional_properties_disallowed(self):
  3084. self.config(allow_additional_image_properties=False)
  3085. request = unit_test_utils.get_fake_request()
  3086. request.body = jsonutils.dump_as_bytes({'foo': 'bar'})
  3087. self.assertRaises(webob.exc.HTTPBadRequest,
  3088. self.deserializer.create, request)
  3089. def test_update(self):
  3090. request = unit_test_utils.get_fake_request()
  3091. request.content_type = 'application/openstack-images-v2.1-json-patch'
  3092. doc = [{'op': 'add', 'path': '/foo', 'value': 'bar'}]
  3093. request.body = jsonutils.dump_as_bytes(doc)
  3094. self.assertRaises(webob.exc.HTTPBadRequest,
  3095. self.deserializer.update, request)
  3096. class TestImagesSerializer(test_utils.BaseTestCase):
  3097. def setUp(self):
  3098. super(TestImagesSerializer, self).setUp()
  3099. self.serializer = glance.api.v2.images.ResponseSerializer()
  3100. self.fixtures = [
  3101. # NOTE(bcwaldon): This first fixture has every property defined
  3102. _domain_fixture(UUID1, name='image-1', size=1024,
  3103. virtual_size=3072, created_at=DATETIME,
  3104. updated_at=DATETIME, owner=TENANT1,
  3105. visibility='public', container_format='ami',
  3106. tags=['one', 'two'], disk_format='ami',
  3107. min_ram=128, min_disk=10,
  3108. checksum='ca425b88f047ce8ec45ee90e813ada91',
  3109. os_hash_algo=FAKEHASHALGO,
  3110. os_hash_value=MULTIHASH1),
  3111. # NOTE(bcwaldon): This second fixture depends on default behavior
  3112. # and sets most values to None
  3113. _domain_fixture(UUID2, created_at=DATETIME, updated_at=DATETIME),
  3114. ]
  3115. def test_index(self):
  3116. expected = {
  3117. 'images': [
  3118. {
  3119. 'id': UUID1,
  3120. 'name': 'image-1',
  3121. 'status': 'queued',
  3122. 'visibility': 'public',
  3123. 'protected': False,
  3124. 'os_hidden': False,
  3125. 'tags': set(['one', 'two']),
  3126. 'size': 1024,
  3127. 'virtual_size': 3072,
  3128. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3129. 'os_hash_algo': FAKEHASHALGO,
  3130. 'os_hash_value': MULTIHASH1,
  3131. 'container_format': 'ami',
  3132. 'disk_format': 'ami',
  3133. 'min_ram': 128,
  3134. 'min_disk': 10,
  3135. 'created_at': ISOTIME,
  3136. 'updated_at': ISOTIME,
  3137. 'self': '/v2/images/%s' % UUID1,
  3138. 'file': '/v2/images/%s/file' % UUID1,
  3139. 'schema': '/v2/schemas/image',
  3140. 'owner': '6838eb7b-6ded-434a-882c-b344c77fe8df',
  3141. },
  3142. {
  3143. 'id': UUID2,
  3144. 'status': 'queued',
  3145. 'visibility': 'private',
  3146. 'protected': False,
  3147. 'os_hidden': False,
  3148. 'tags': set([]),
  3149. 'created_at': ISOTIME,
  3150. 'updated_at': ISOTIME,
  3151. 'self': '/v2/images/%s' % UUID2,
  3152. 'file': '/v2/images/%s/file' % UUID2,
  3153. 'schema': '/v2/schemas/image',
  3154. 'size': None,
  3155. 'name': None,
  3156. 'owner': None,
  3157. 'min_ram': None,
  3158. 'min_disk': None,
  3159. 'checksum': None,
  3160. 'os_hash_algo': None,
  3161. 'os_hash_value': None,
  3162. 'disk_format': None,
  3163. 'virtual_size': None,
  3164. 'container_format': None,
  3165. },
  3166. ],
  3167. 'first': '/v2/images',
  3168. 'schema': '/v2/schemas/images',
  3169. }
  3170. request = webob.Request.blank('/v2/images')
  3171. response = webob.Response(request=request)
  3172. result = {'images': self.fixtures}
  3173. self.serializer.index(response, result)
  3174. actual = jsonutils.loads(response.body)
  3175. for image in actual['images']:
  3176. image['tags'] = set(image['tags'])
  3177. self.assertEqual(expected, actual)
  3178. self.assertEqual('application/json', response.content_type)
  3179. def test_index_next_marker(self):
  3180. request = webob.Request.blank('/v2/images')
  3181. response = webob.Response(request=request)
  3182. result = {'images': self.fixtures, 'next_marker': UUID2}
  3183. self.serializer.index(response, result)
  3184. output = jsonutils.loads(response.body)
  3185. self.assertEqual('/v2/images?marker=%s' % UUID2, output['next'])
  3186. def test_index_carries_query_parameters(self):
  3187. url = '/v2/images?limit=10&sort_key=id&sort_dir=asc'
  3188. request = webob.Request.blank(url)
  3189. response = webob.Response(request=request)
  3190. result = {'images': self.fixtures, 'next_marker': UUID2}
  3191. self.serializer.index(response, result)
  3192. output = jsonutils.loads(response.body)
  3193. expected_url = '/v2/images?limit=10&sort_dir=asc&sort_key=id'
  3194. self.assertEqual(unit_test_utils.sort_url_by_qs_keys(expected_url),
  3195. unit_test_utils.sort_url_by_qs_keys(output['first']))
  3196. expect_next = '/v2/images?limit=10&marker=%s&sort_dir=asc&sort_key=id'
  3197. self.assertEqual(unit_test_utils.sort_url_by_qs_keys(
  3198. expect_next % UUID2),
  3199. unit_test_utils.sort_url_by_qs_keys(output['next']))
  3200. def test_index_forbidden_get_image_location(self):
  3201. """Make sure the serializer works fine.
  3202. No mater if current user is authorized to get image location if the
  3203. show_multiple_locations is False.
  3204. """
  3205. class ImageLocations(object):
  3206. def __len__(self):
  3207. raise exception.Forbidden()
  3208. self.config(show_multiple_locations=False)
  3209. self.config(show_image_direct_url=False)
  3210. url = '/v2/images?limit=10&sort_key=id&sort_dir=asc'
  3211. request = webob.Request.blank(url)
  3212. response = webob.Response(request=request)
  3213. result = {'images': self.fixtures}
  3214. self.assertEqual(http.OK, response.status_int)
  3215. # The image index should work though the user is forbidden
  3216. result['images'][0].locations = ImageLocations()
  3217. self.serializer.index(response, result)
  3218. self.assertEqual(http.OK, response.status_int)
  3219. def test_show_full_fixture(self):
  3220. expected = {
  3221. 'id': UUID1,
  3222. 'name': 'image-1',
  3223. 'status': 'queued',
  3224. 'visibility': 'public',
  3225. 'protected': False,
  3226. 'os_hidden': False,
  3227. 'tags': set(['one', 'two']),
  3228. 'size': 1024,
  3229. 'virtual_size': 3072,
  3230. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3231. 'os_hash_algo': FAKEHASHALGO,
  3232. 'os_hash_value': MULTIHASH1,
  3233. 'container_format': 'ami',
  3234. 'disk_format': 'ami',
  3235. 'min_ram': 128,
  3236. 'min_disk': 10,
  3237. 'created_at': ISOTIME,
  3238. 'updated_at': ISOTIME,
  3239. 'self': '/v2/images/%s' % UUID1,
  3240. 'file': '/v2/images/%s/file' % UUID1,
  3241. 'schema': '/v2/schemas/image',
  3242. 'owner': '6838eb7b-6ded-434a-882c-b344c77fe8df',
  3243. }
  3244. response = webob.Response()
  3245. self.serializer.show(response, self.fixtures[0])
  3246. actual = jsonutils.loads(response.body)
  3247. actual['tags'] = set(actual['tags'])
  3248. self.assertEqual(expected, actual)
  3249. self.assertEqual('application/json', response.content_type)
  3250. def test_show_minimal_fixture(self):
  3251. expected = {
  3252. 'id': UUID2,
  3253. 'status': 'queued',
  3254. 'visibility': 'private',
  3255. 'protected': False,
  3256. 'os_hidden': False,
  3257. 'tags': [],
  3258. 'created_at': ISOTIME,
  3259. 'updated_at': ISOTIME,
  3260. 'self': '/v2/images/%s' % UUID2,
  3261. 'file': '/v2/images/%s/file' % UUID2,
  3262. 'schema': '/v2/schemas/image',
  3263. 'size': None,
  3264. 'name': None,
  3265. 'owner': None,
  3266. 'min_ram': None,
  3267. 'min_disk': None,
  3268. 'checksum': None,
  3269. 'os_hash_algo': None,
  3270. 'os_hash_value': None,
  3271. 'disk_format': None,
  3272. 'virtual_size': None,
  3273. 'container_format': None,
  3274. }
  3275. response = webob.Response()
  3276. self.serializer.show(response, self.fixtures[1])
  3277. self.assertEqual(expected, jsonutils.loads(response.body))
  3278. def test_create(self):
  3279. expected = {
  3280. 'id': UUID1,
  3281. 'name': 'image-1',
  3282. 'status': 'queued',
  3283. 'visibility': 'public',
  3284. 'protected': False,
  3285. 'os_hidden': False,
  3286. 'tags': ['one', 'two'],
  3287. 'size': 1024,
  3288. 'virtual_size': 3072,
  3289. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3290. 'os_hash_algo': FAKEHASHALGO,
  3291. 'os_hash_value': MULTIHASH1,
  3292. 'container_format': 'ami',
  3293. 'disk_format': 'ami',
  3294. 'min_ram': 128,
  3295. 'min_disk': 10,
  3296. 'created_at': ISOTIME,
  3297. 'updated_at': ISOTIME,
  3298. 'self': '/v2/images/%s' % UUID1,
  3299. 'file': '/v2/images/%s/file' % UUID1,
  3300. 'schema': '/v2/schemas/image',
  3301. 'owner': '6838eb7b-6ded-434a-882c-b344c77fe8df',
  3302. }
  3303. response = webob.Response()
  3304. self.serializer.create(response, self.fixtures[0])
  3305. self.assertEqual(http.CREATED, response.status_int)
  3306. actual = jsonutils.loads(response.body)
  3307. actual['tags'] = sorted(actual['tags'])
  3308. self.assertEqual(expected, actual)
  3309. self.assertEqual('application/json', response.content_type)
  3310. self.assertEqual('/v2/images/%s' % UUID1, response.location)
  3311. def test_create_has_import_methods_header(self):
  3312. # NOTE(rosmaita): enabled_import_methods is defined as type
  3313. # oslo.config.cfg.ListOpt, so it is stored internally as a list
  3314. # but is converted to a string for output in the HTTP header
  3315. header_name = 'OpenStack-image-import-methods'
  3316. # check multiple methods
  3317. enabled_methods = ['one', 'two', 'three']
  3318. self.config(enabled_import_methods=enabled_methods)
  3319. response = webob.Response()
  3320. self.serializer.create(response, self.fixtures[0])
  3321. self.assertEqual(http.CREATED, response.status_int)
  3322. header_value = response.headers.get(header_name)
  3323. self.assertIsNotNone(header_value)
  3324. self.assertItemsEqual(enabled_methods, header_value.split(','))
  3325. # check single method
  3326. self.config(enabled_import_methods=['swift-party-time'])
  3327. response = webob.Response()
  3328. self.serializer.create(response, self.fixtures[0])
  3329. self.assertEqual(http.CREATED, response.status_int)
  3330. header_value = response.headers.get(header_name)
  3331. self.assertIsNotNone(header_value)
  3332. self.assertEqual('swift-party-time', header_value)
  3333. # no header for empty config value
  3334. self.config(enabled_import_methods=[])
  3335. response = webob.Response()
  3336. self.serializer.create(response, self.fixtures[0])
  3337. self.assertEqual(http.CREATED, response.status_int)
  3338. headers = response.headers.keys()
  3339. self.assertNotIn(header_name, headers)
  3340. def test_update(self):
  3341. expected = {
  3342. 'id': UUID1,
  3343. 'name': 'image-1',
  3344. 'status': 'queued',
  3345. 'visibility': 'public',
  3346. 'protected': False,
  3347. 'os_hidden': False,
  3348. 'tags': set(['one', 'two']),
  3349. 'size': 1024,
  3350. 'virtual_size': 3072,
  3351. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3352. 'os_hash_algo': FAKEHASHALGO,
  3353. 'os_hash_value': MULTIHASH1,
  3354. 'container_format': 'ami',
  3355. 'disk_format': 'ami',
  3356. 'min_ram': 128,
  3357. 'min_disk': 10,
  3358. 'created_at': ISOTIME,
  3359. 'updated_at': ISOTIME,
  3360. 'self': '/v2/images/%s' % UUID1,
  3361. 'file': '/v2/images/%s/file' % UUID1,
  3362. 'schema': '/v2/schemas/image',
  3363. 'owner': '6838eb7b-6ded-434a-882c-b344c77fe8df',
  3364. }
  3365. response = webob.Response()
  3366. self.serializer.update(response, self.fixtures[0])
  3367. actual = jsonutils.loads(response.body)
  3368. actual['tags'] = set(actual['tags'])
  3369. self.assertEqual(expected, actual)
  3370. self.assertEqual('application/json', response.content_type)
  3371. def test_import_image(self):
  3372. response = webob.Response()
  3373. self.serializer.import_image(response, {})
  3374. self.assertEqual(http.ACCEPTED, response.status_int)
  3375. self.assertEqual('0', response.headers['Content-Length'])
  3376. class TestImagesSerializerWithUnicode(test_utils.BaseTestCase):
  3377. def setUp(self):
  3378. super(TestImagesSerializerWithUnicode, self).setUp()
  3379. self.serializer = glance.api.v2.images.ResponseSerializer()
  3380. self.fixtures = [
  3381. # NOTE(bcwaldon): This first fixture has every property defined
  3382. _domain_fixture(UUID1, **{
  3383. 'name': u'OpenStack\u2122-1',
  3384. 'size': 1024,
  3385. 'virtual_size': 3072,
  3386. 'tags': [u'\u2160', u'\u2161'],
  3387. 'created_at': DATETIME,
  3388. 'updated_at': DATETIME,
  3389. 'owner': TENANT1,
  3390. 'visibility': 'public',
  3391. 'container_format': 'ami',
  3392. 'disk_format': 'ami',
  3393. 'min_ram': 128,
  3394. 'min_disk': 10,
  3395. 'checksum': u'ca425b88f047ce8ec45ee90e813ada91',
  3396. 'os_hash_algo': FAKEHASHALGO,
  3397. 'os_hash_value': MULTIHASH1,
  3398. 'extra_properties': {'lang': u'Fran\u00E7ais',
  3399. u'dispos\u00E9': u'f\u00E2ch\u00E9'},
  3400. }),
  3401. ]
  3402. def test_index(self):
  3403. expected = {
  3404. u'images': [
  3405. {
  3406. u'id': UUID1,
  3407. u'name': u'OpenStack\u2122-1',
  3408. u'status': u'queued',
  3409. u'visibility': u'public',
  3410. u'protected': False,
  3411. u'os_hidden': False,
  3412. u'tags': [u'\u2160', u'\u2161'],
  3413. u'size': 1024,
  3414. u'virtual_size': 3072,
  3415. u'checksum': u'ca425b88f047ce8ec45ee90e813ada91',
  3416. u'os_hash_algo': six.text_type(FAKEHASHALGO),
  3417. u'os_hash_value': six.text_type(MULTIHASH1),
  3418. u'container_format': u'ami',
  3419. u'disk_format': u'ami',
  3420. u'min_ram': 128,
  3421. u'min_disk': 10,
  3422. u'created_at': six.text_type(ISOTIME),
  3423. u'updated_at': six.text_type(ISOTIME),
  3424. u'self': u'/v2/images/%s' % UUID1,
  3425. u'file': u'/v2/images/%s/file' % UUID1,
  3426. u'schema': u'/v2/schemas/image',
  3427. u'lang': u'Fran\u00E7ais',
  3428. u'dispos\u00E9': u'f\u00E2ch\u00E9',
  3429. u'owner': u'6838eb7b-6ded-434a-882c-b344c77fe8df',
  3430. },
  3431. ],
  3432. u'first': u'/v2/images',
  3433. u'schema': u'/v2/schemas/images',
  3434. }
  3435. request = webob.Request.blank('/v2/images')
  3436. response = webob.Response(request=request)
  3437. result = {u'images': self.fixtures}
  3438. self.serializer.index(response, result)
  3439. actual = jsonutils.loads(response.body)
  3440. actual['images'][0]['tags'] = sorted(actual['images'][0]['tags'])
  3441. self.assertEqual(expected, actual)
  3442. self.assertEqual('application/json', response.content_type)
  3443. def test_show_full_fixture(self):
  3444. expected = {
  3445. u'id': UUID1,
  3446. u'name': u'OpenStack\u2122-1',
  3447. u'status': u'queued',
  3448. u'visibility': u'public',
  3449. u'protected': False,
  3450. u'os_hidden': False,
  3451. u'tags': set([u'\u2160', u'\u2161']),
  3452. u'size': 1024,
  3453. u'virtual_size': 3072,
  3454. u'checksum': u'ca425b88f047ce8ec45ee90e813ada91',
  3455. u'os_hash_algo': six.text_type(FAKEHASHALGO),
  3456. u'os_hash_value': six.text_type(MULTIHASH1),
  3457. u'container_format': u'ami',
  3458. u'disk_format': u'ami',
  3459. u'min_ram': 128,
  3460. u'min_disk': 10,
  3461. u'created_at': six.text_type(ISOTIME),
  3462. u'updated_at': six.text_type(ISOTIME),
  3463. u'self': u'/v2/images/%s' % UUID1,
  3464. u'file': u'/v2/images/%s/file' % UUID1,
  3465. u'schema': u'/v2/schemas/image',
  3466. u'lang': u'Fran\u00E7ais',
  3467. u'dispos\u00E9': u'f\u00E2ch\u00E9',
  3468. u'owner': u'6838eb7b-6ded-434a-882c-b344c77fe8df',
  3469. }
  3470. response = webob.Response()
  3471. self.serializer.show(response, self.fixtures[0])
  3472. actual = jsonutils.loads(response.body)
  3473. actual['tags'] = set(actual['tags'])
  3474. self.assertEqual(expected, actual)
  3475. self.assertEqual('application/json', response.content_type)
  3476. def test_create(self):
  3477. expected = {
  3478. u'id': UUID1,
  3479. u'name': u'OpenStack\u2122-1',
  3480. u'status': u'queued',
  3481. u'visibility': u'public',
  3482. u'protected': False,
  3483. u'os_hidden': False,
  3484. u'tags': [u'\u2160', u'\u2161'],
  3485. u'size': 1024,
  3486. u'virtual_size': 3072,
  3487. u'checksum': u'ca425b88f047ce8ec45ee90e813ada91',
  3488. u'os_hash_algo': six.text_type(FAKEHASHALGO),
  3489. u'os_hash_value': six.text_type(MULTIHASH1),
  3490. u'container_format': u'ami',
  3491. u'disk_format': u'ami',
  3492. u'min_ram': 128,
  3493. u'min_disk': 10,
  3494. u'created_at': six.text_type(ISOTIME),
  3495. u'updated_at': six.text_type(ISOTIME),
  3496. u'self': u'/v2/images/%s' % UUID1,
  3497. u'file': u'/v2/images/%s/file' % UUID1,
  3498. u'schema': u'/v2/schemas/image',
  3499. u'lang': u'Fran\u00E7ais',
  3500. u'dispos\u00E9': u'f\u00E2ch\u00E9',
  3501. u'owner': u'6838eb7b-6ded-434a-882c-b344c77fe8df',
  3502. }
  3503. response = webob.Response()
  3504. self.serializer.create(response, self.fixtures[0])
  3505. self.assertEqual(http.CREATED, response.status_int)
  3506. actual = jsonutils.loads(response.body)
  3507. actual['tags'] = sorted(actual['tags'])
  3508. self.assertEqual(expected, actual)
  3509. self.assertEqual('application/json', response.content_type)
  3510. self.assertEqual('/v2/images/%s' % UUID1, response.location)
  3511. def test_update(self):
  3512. expected = {
  3513. u'id': UUID1,
  3514. u'name': u'OpenStack\u2122-1',
  3515. u'status': u'queued',
  3516. u'visibility': u'public',
  3517. u'protected': False,
  3518. u'os_hidden': False,
  3519. u'tags': set([u'\u2160', u'\u2161']),
  3520. u'size': 1024,
  3521. u'virtual_size': 3072,
  3522. u'checksum': u'ca425b88f047ce8ec45ee90e813ada91',
  3523. u'os_hash_algo': six.text_type(FAKEHASHALGO),
  3524. u'os_hash_value': six.text_type(MULTIHASH1),
  3525. u'container_format': u'ami',
  3526. u'disk_format': u'ami',
  3527. u'min_ram': 128,
  3528. u'min_disk': 10,
  3529. u'created_at': six.text_type(ISOTIME),
  3530. u'updated_at': six.text_type(ISOTIME),
  3531. u'self': u'/v2/images/%s' % UUID1,
  3532. u'file': u'/v2/images/%s/file' % UUID1,
  3533. u'schema': u'/v2/schemas/image',
  3534. u'lang': u'Fran\u00E7ais',
  3535. u'dispos\u00E9': u'f\u00E2ch\u00E9',
  3536. u'owner': u'6838eb7b-6ded-434a-882c-b344c77fe8df',
  3537. }
  3538. response = webob.Response()
  3539. self.serializer.update(response, self.fixtures[0])
  3540. actual = jsonutils.loads(response.body)
  3541. actual['tags'] = set(actual['tags'])
  3542. self.assertEqual(expected, actual)
  3543. self.assertEqual('application/json', response.content_type)
  3544. class TestImagesSerializerWithExtendedSchema(test_utils.BaseTestCase):
  3545. def setUp(self):
  3546. super(TestImagesSerializerWithExtendedSchema, self).setUp()
  3547. self.config(allow_additional_image_properties=False)
  3548. custom_image_properties = {
  3549. 'color': {
  3550. 'type': 'string',
  3551. 'enum': ['red', 'green'],
  3552. },
  3553. }
  3554. schema = glance.api.v2.images.get_schema(custom_image_properties)
  3555. self.serializer = glance.api.v2.images.ResponseSerializer(schema)
  3556. props = dict(color='green', mood='grouchy')
  3557. self.fixture = _domain_fixture(
  3558. UUID2, name='image-2', owner=TENANT2,
  3559. checksum='ca425b88f047ce8ec45ee90e813ada91',
  3560. os_hash_algo=FAKEHASHALGO, os_hash_value=MULTIHASH1,
  3561. created_at=DATETIME, updated_at=DATETIME, size=1024,
  3562. virtual_size=3072, extra_properties=props)
  3563. def test_show(self):
  3564. expected = {
  3565. 'id': UUID2,
  3566. 'name': 'image-2',
  3567. 'status': 'queued',
  3568. 'visibility': 'private',
  3569. 'protected': False,
  3570. 'os_hidden': False,
  3571. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3572. 'os_hash_algo': FAKEHASHALGO,
  3573. 'os_hash_value': MULTIHASH1,
  3574. 'tags': [],
  3575. 'size': 1024,
  3576. 'virtual_size': 3072,
  3577. 'owner': '2c014f32-55eb-467d-8fcb-4bd706012f81',
  3578. 'color': 'green',
  3579. 'created_at': ISOTIME,
  3580. 'updated_at': ISOTIME,
  3581. 'self': '/v2/images/%s' % UUID2,
  3582. 'file': '/v2/images/%s/file' % UUID2,
  3583. 'schema': '/v2/schemas/image',
  3584. 'min_ram': None,
  3585. 'min_disk': None,
  3586. 'disk_format': None,
  3587. 'container_format': None,
  3588. }
  3589. response = webob.Response()
  3590. self.serializer.show(response, self.fixture)
  3591. self.assertEqual(expected, jsonutils.loads(response.body))
  3592. def test_show_reports_invalid_data(self):
  3593. self.fixture.extra_properties['color'] = 'invalid'
  3594. expected = {
  3595. 'id': UUID2,
  3596. 'name': 'image-2',
  3597. 'status': 'queued',
  3598. 'visibility': 'private',
  3599. 'protected': False,
  3600. 'os_hidden': False,
  3601. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3602. 'os_hash_algo': FAKEHASHALGO,
  3603. 'os_hash_value': MULTIHASH1,
  3604. 'tags': [],
  3605. 'size': 1024,
  3606. 'virtual_size': 3072,
  3607. 'owner': '2c014f32-55eb-467d-8fcb-4bd706012f81',
  3608. 'color': 'invalid',
  3609. 'created_at': ISOTIME,
  3610. 'updated_at': ISOTIME,
  3611. 'self': '/v2/images/%s' % UUID2,
  3612. 'file': '/v2/images/%s/file' % UUID2,
  3613. 'schema': '/v2/schemas/image',
  3614. 'min_ram': None,
  3615. 'min_disk': None,
  3616. 'disk_format': None,
  3617. 'container_format': None,
  3618. }
  3619. response = webob.Response()
  3620. self.serializer.show(response, self.fixture)
  3621. self.assertEqual(expected, jsonutils.loads(response.body))
  3622. class TestImagesSerializerWithAdditionalProperties(test_utils.BaseTestCase):
  3623. def setUp(self):
  3624. super(TestImagesSerializerWithAdditionalProperties, self).setUp()
  3625. self.config(allow_additional_image_properties=True)
  3626. self.fixture = _domain_fixture(
  3627. UUID2, name='image-2', owner=TENANT2,
  3628. checksum='ca425b88f047ce8ec45ee90e813ada91',
  3629. os_hash_algo=FAKEHASHALGO, os_hash_value=MULTIHASH1,
  3630. created_at=DATETIME, updated_at=DATETIME, size=1024,
  3631. virtual_size=3072, extra_properties={'marx': 'groucho'})
  3632. def test_show(self):
  3633. serializer = glance.api.v2.images.ResponseSerializer()
  3634. expected = {
  3635. 'id': UUID2,
  3636. 'name': 'image-2',
  3637. 'status': 'queued',
  3638. 'visibility': 'private',
  3639. 'protected': False,
  3640. 'os_hidden': False,
  3641. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3642. 'os_hash_algo': FAKEHASHALGO,
  3643. 'os_hash_value': MULTIHASH1,
  3644. 'marx': 'groucho',
  3645. 'tags': [],
  3646. 'size': 1024,
  3647. 'virtual_size': 3072,
  3648. 'created_at': ISOTIME,
  3649. 'updated_at': ISOTIME,
  3650. 'self': '/v2/images/%s' % UUID2,
  3651. 'file': '/v2/images/%s/file' % UUID2,
  3652. 'schema': '/v2/schemas/image',
  3653. 'owner': '2c014f32-55eb-467d-8fcb-4bd706012f81',
  3654. 'min_ram': None,
  3655. 'min_disk': None,
  3656. 'disk_format': None,
  3657. 'container_format': None,
  3658. }
  3659. response = webob.Response()
  3660. serializer.show(response, self.fixture)
  3661. self.assertEqual(expected, jsonutils.loads(response.body))
  3662. def test_show_invalid_additional_property(self):
  3663. """Ensure that the serializer passes
  3664. through invalid additional properties.
  3665. It must not complains with i.e. non-string.
  3666. """
  3667. serializer = glance.api.v2.images.ResponseSerializer()
  3668. self.fixture.extra_properties['marx'] = 123
  3669. expected = {
  3670. 'id': UUID2,
  3671. 'name': 'image-2',
  3672. 'status': 'queued',
  3673. 'visibility': 'private',
  3674. 'protected': False,
  3675. 'os_hidden': False,
  3676. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3677. 'os_hash_algo': FAKEHASHALGO,
  3678. 'os_hash_value': MULTIHASH1,
  3679. 'marx': 123,
  3680. 'tags': [],
  3681. 'size': 1024,
  3682. 'virtual_size': 3072,
  3683. 'created_at': ISOTIME,
  3684. 'updated_at': ISOTIME,
  3685. 'self': '/v2/images/%s' % UUID2,
  3686. 'file': '/v2/images/%s/file' % UUID2,
  3687. 'schema': '/v2/schemas/image',
  3688. 'owner': '2c014f32-55eb-467d-8fcb-4bd706012f81',
  3689. 'min_ram': None,
  3690. 'min_disk': None,
  3691. 'disk_format': None,
  3692. 'container_format': None,
  3693. }
  3694. response = webob.Response()
  3695. serializer.show(response, self.fixture)
  3696. self.assertEqual(expected, jsonutils.loads(response.body))
  3697. def test_show_with_additional_properties_disabled(self):
  3698. self.config(allow_additional_image_properties=False)
  3699. serializer = glance.api.v2.images.ResponseSerializer()
  3700. expected = {
  3701. 'id': UUID2,
  3702. 'name': 'image-2',
  3703. 'status': 'queued',
  3704. 'visibility': 'private',
  3705. 'protected': False,
  3706. 'os_hidden': False,
  3707. 'checksum': 'ca425b88f047ce8ec45ee90e813ada91',
  3708. 'os_hash_algo': FAKEHASHALGO,
  3709. 'os_hash_value': MULTIHASH1,
  3710. 'tags': [],
  3711. 'size': 1024,
  3712. 'virtual_size': 3072,
  3713. 'owner': '2c014f32-55eb-467d-8fcb-4bd706012f81',
  3714. 'created_at': ISOTIME,
  3715. 'updated_at': ISOTIME,
  3716. 'self': '/v2/images/%s' % UUID2,
  3717. 'file': '/v2/images/%s/file' % UUID2,
  3718. 'schema': '/v2/schemas/image',
  3719. 'min_ram': None,
  3720. 'min_disk': None,
  3721. 'disk_format': None,
  3722. 'container_format': None,
  3723. }
  3724. response = webob.Response()
  3725. serializer.show(response, self.fixture)
  3726. self.assertEqual(expected, jsonutils.loads(response.body))
  3727. class TestImagesSerializerDirectUrl(test_utils.BaseTestCase):
  3728. def setUp(self):
  3729. super(TestImagesSerializerDirectUrl, self).setUp()
  3730. self.serializer = glance.api.v2.images.ResponseSerializer()
  3731. self.active_image = _domain_fixture(
  3732. UUID1, name='image-1', visibility='public',
  3733. status='active', size=1024, virtual_size=3072,
  3734. created_at=DATETIME, updated_at=DATETIME,
  3735. locations=[{'id': '1', 'url': 'http://some/fake/location',
  3736. 'metadata': {}, 'status': 'active'}])
  3737. self.queued_image = _domain_fixture(
  3738. UUID2, name='image-2', status='active',
  3739. created_at=DATETIME, updated_at=DATETIME,
  3740. checksum='ca425b88f047ce8ec45ee90e813ada91')
  3741. self.location_data_image_url = 'http://abc.com/somewhere'
  3742. self.location_data_image_meta = {'key': 98231}
  3743. self.location_data_image = _domain_fixture(
  3744. UUID2, name='image-2', status='active',
  3745. created_at=DATETIME, updated_at=DATETIME,
  3746. locations=[{'id': '2',
  3747. 'url': self.location_data_image_url,
  3748. 'metadata': self.location_data_image_meta,
  3749. 'status': 'active'}])
  3750. def _do_index(self):
  3751. request = webob.Request.blank('/v2/images')
  3752. response = webob.Response(request=request)
  3753. self.serializer.index(response,
  3754. {'images': [self.active_image,
  3755. self.queued_image]})
  3756. return jsonutils.loads(response.body)['images']
  3757. def _do_show(self, image):
  3758. request = webob.Request.blank('/v2/images')
  3759. response = webob.Response(request=request)
  3760. self.serializer.show(response, image)
  3761. return jsonutils.loads(response.body)
  3762. def test_index_store_location_enabled(self):
  3763. self.config(show_image_direct_url=True)
  3764. images = self._do_index()
  3765. # NOTE(markwash): ordering sanity check
  3766. self.assertEqual(UUID1, images[0]['id'])
  3767. self.assertEqual(UUID2, images[1]['id'])
  3768. self.assertEqual('http://some/fake/location', images[0]['direct_url'])
  3769. self.assertNotIn('direct_url', images[1])
  3770. def test_index_store_multiple_location_enabled(self):
  3771. self.config(show_multiple_locations=True)
  3772. request = webob.Request.blank('/v2/images')
  3773. response = webob.Response(request=request)
  3774. self.serializer.index(response,
  3775. {'images': [self.location_data_image]}),
  3776. images = jsonutils.loads(response.body)['images']
  3777. location = images[0]['locations'][0]
  3778. self.assertEqual(location['url'], self.location_data_image_url)
  3779. self.assertEqual(location['metadata'], self.location_data_image_meta)
  3780. def test_index_store_location_explicitly_disabled(self):
  3781. self.config(show_image_direct_url=False)
  3782. images = self._do_index()
  3783. self.assertNotIn('direct_url', images[0])
  3784. self.assertNotIn('direct_url', images[1])
  3785. def test_show_location_enabled(self):
  3786. self.config(show_image_direct_url=True)
  3787. image = self._do_show(self.active_image)
  3788. self.assertEqual('http://some/fake/location', image['direct_url'])
  3789. def test_show_location_enabled_but_not_set(self):
  3790. self.config(show_image_direct_url=True)
  3791. image = self._do_show(self.queued_image)
  3792. self.assertNotIn('direct_url', image)
  3793. def test_show_location_explicitly_disabled(self):
  3794. self.config(show_image_direct_url=False)
  3795. image = self._do_show(self.active_image)
  3796. self.assertNotIn('direct_url', image)
  3797. class TestImageSchemaFormatConfiguration(test_utils.BaseTestCase):
  3798. def test_default_disk_formats(self):
  3799. schema = glance.api.v2.images.get_schema()
  3800. expected = [None, 'ami', 'ari', 'aki', 'vhd', 'vhdx', 'vmdk',
  3801. 'raw', 'qcow2', 'vdi', 'iso', 'ploop']
  3802. actual = schema.properties['disk_format']['enum']
  3803. self.assertEqual(expected, actual)
  3804. def test_custom_disk_formats(self):
  3805. self.config(disk_formats=['gabe'], group="image_format")
  3806. schema = glance.api.v2.images.get_schema()
  3807. expected = [None, 'gabe']
  3808. actual = schema.properties['disk_format']['enum']
  3809. self.assertEqual(expected, actual)
  3810. def test_default_container_formats(self):
  3811. schema = glance.api.v2.images.get_schema()
  3812. expected = [None, 'ami', 'ari', 'aki', 'bare', 'ovf', 'ova', 'docker']
  3813. actual = schema.properties['container_format']['enum']
  3814. self.assertEqual(expected, actual)
  3815. def test_custom_container_formats(self):
  3816. self.config(container_formats=['mark'], group="image_format")
  3817. schema = glance.api.v2.images.get_schema()
  3818. expected = [None, 'mark']
  3819. actual = schema.properties['container_format']['enum']
  3820. self.assertEqual(expected, actual)
  3821. class TestImageSchemaDeterminePropertyBasis(test_utils.BaseTestCase):
  3822. def test_custom_property_marked_as_non_base(self):
  3823. self.config(allow_additional_image_properties=False)
  3824. custom_image_properties = {
  3825. 'pants': {
  3826. 'type': 'string',
  3827. },
  3828. }
  3829. schema = glance.api.v2.images.get_schema(custom_image_properties)
  3830. self.assertFalse(schema.properties['pants'].get('is_base', True))
  3831. def test_base_property_marked_as_base(self):
  3832. schema = glance.api.v2.images.get_schema()
  3833. self.assertTrue(schema.properties['disk_format'].get('is_base', True))
  3834. class TestMultiImagesController(base.MultiIsolatedUnitTest):
  3835. def setUp(self):
  3836. super(TestMultiImagesController, self).setUp()
  3837. self.db = unit_test_utils.FakeDB(initialize=False)
  3838. self.policy = unit_test_utils.FakePolicyEnforcer()
  3839. self.notifier = unit_test_utils.FakeNotifier()
  3840. self.store = store
  3841. self._create_images()
  3842. self._create_image_members()
  3843. self.controller = glance.api.v2.images.ImagesController(self.db,
  3844. self.policy,
  3845. self.notifier,
  3846. self.store)
  3847. def _create_images(self):
  3848. self.images = [
  3849. _db_fixture(UUID1, owner=TENANT1, checksum=CHKSUM,
  3850. name='1', size=256, virtual_size=1024,
  3851. visibility='public',
  3852. locations=[{'url': '%s/%s' % (BASE_URI, UUID1),
  3853. 'metadata': {}, 'status': 'active'}],
  3854. disk_format='raw',
  3855. container_format='bare',
  3856. status='active'),
  3857. _db_fixture(UUID2, owner=TENANT1, checksum=CHKSUM1,
  3858. name='2', size=512, virtual_size=2048,
  3859. visibility='public',
  3860. disk_format='raw',
  3861. container_format='bare',
  3862. status='active',
  3863. tags=['redhat', '64bit', 'power'],
  3864. properties={'hypervisor_type': 'kvm', 'foo': 'bar',
  3865. 'bar': 'foo'}),
  3866. _db_fixture(UUID3, owner=TENANT3, checksum=CHKSUM1,
  3867. name='3', size=512, virtual_size=2048,
  3868. visibility='public', tags=['windows', '64bit', 'x86']),
  3869. _db_fixture(UUID4, owner=TENANT4, name='4',
  3870. size=1024, virtual_size=3072),
  3871. ]
  3872. [self.db.image_create(None, image) for image in self.images]
  3873. self.db.image_tag_set_all(None, UUID1, ['ping', 'pong'])
  3874. def _create_image_members(self):
  3875. self.image_members = [
  3876. _db_image_member_fixture(UUID4, TENANT2),
  3877. _db_image_member_fixture(UUID4, TENANT3,
  3878. status='accepted'),
  3879. ]
  3880. [self.db.image_member_create(None, image_member)
  3881. for image_member in self.image_members]
  3882. def test_image_import_image_not_exist(self):
  3883. request = unit_test_utils.get_fake_request()
  3884. self.assertRaises(webob.exc.HTTPNotFound,
  3885. self.controller.import_image,
  3886. request, 'invalid_image',
  3887. {'method': {'name': 'glance-direct'}})
  3888. def test_image_import_with_active_image(self):
  3889. request = unit_test_utils.get_fake_request()
  3890. self.assertRaises(webob.exc.HTTPConflict,
  3891. self.controller.import_image,
  3892. request, UUID1,
  3893. {'method': {'name': 'glance-direct'}})
  3894. def test_image_import_invalid_backend_in_request_header(self):
  3895. request = unit_test_utils.get_fake_request()
  3896. request.headers['x-image-meta-store'] = 'dummy'
  3897. with mock.patch.object(
  3898. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  3899. mock_get.return_value = FakeImage(status='uploading')
  3900. self.assertRaises(webob.exc.HTTPConflict,
  3901. self.controller.import_image,
  3902. request, UUID4,
  3903. {'method': {'name': 'glance-direct'}})
  3904. def test_image_import_raises_conflict_if_disk_format_is_none(self):
  3905. request = unit_test_utils.get_fake_request()
  3906. with mock.patch.object(
  3907. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  3908. mock_get.return_value = FakeImage(disk_format=None)
  3909. self.assertRaises(webob.exc.HTTPConflict,
  3910. self.controller.import_image, request, UUID4,
  3911. {'method': {'name': 'glance-direct'}})
  3912. def test_image_import_raises_conflict(self):
  3913. request = unit_test_utils.get_fake_request()
  3914. with mock.patch.object(
  3915. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  3916. mock_get.return_value = FakeImage(status='queued')
  3917. self.assertRaises(webob.exc.HTTPConflict,
  3918. self.controller.import_image, request, UUID4,
  3919. {'method': {'name': 'glance-direct'}})
  3920. def test_image_import_raises_conflict_for_web_download(self):
  3921. request = unit_test_utils.get_fake_request()
  3922. with mock.patch.object(
  3923. glance.api.authorization.ImageRepoProxy, 'get') as mock_get:
  3924. mock_get.return_value = FakeImage()
  3925. self.assertRaises(webob.exc.HTTPConflict,
  3926. self.controller.import_image, request, UUID4,
  3927. {'method': {'name': 'web-download'}})