1
0

config_test.py 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794
  1. # encoding: utf-8
  2. from __future__ import absolute_import
  3. from __future__ import print_function
  4. from __future__ import unicode_literals
  5. import os
  6. import shutil
  7. import tempfile
  8. from operator import itemgetter
  9. import py
  10. import pytest
  11. import yaml
  12. from ...helpers import build_config_details
  13. from compose.config import config
  14. from compose.config import types
  15. from compose.config.config import resolve_build_args
  16. from compose.config.config import resolve_environment
  17. from compose.config.environment import Environment
  18. from compose.config.errors import ConfigurationError
  19. from compose.config.errors import VERSION_EXPLANATION
  20. from compose.config.serialize import denormalize_service_dict
  21. from compose.config.serialize import serialize_config
  22. from compose.config.serialize import serialize_ns_time_value
  23. from compose.config.types import VolumeSpec
  24. from compose.const import COMPOSEFILE_V1 as V1
  25. from compose.const import COMPOSEFILE_V2_0 as V2_0
  26. from compose.const import COMPOSEFILE_V2_1 as V2_1
  27. from compose.const import COMPOSEFILE_V2_2 as V2_2
  28. from compose.const import COMPOSEFILE_V2_3 as V2_3
  29. from compose.const import COMPOSEFILE_V3_0 as V3_0
  30. from compose.const import COMPOSEFILE_V3_1 as V3_1
  31. from compose.const import COMPOSEFILE_V3_2 as V3_2
  32. from compose.const import COMPOSEFILE_V3_3 as V3_3
  33. from compose.const import IS_WINDOWS_PLATFORM
  34. from tests import mock
  35. from tests import unittest
  36. DEFAULT_VERSION = V2_0
  37. def make_service_dict(name, service_dict, working_dir, filename=None):
  38. """Test helper function to construct a ServiceExtendsResolver
  39. """
  40. resolver = config.ServiceExtendsResolver(
  41. config.ServiceConfig(
  42. working_dir=working_dir,
  43. filename=filename,
  44. name=name,
  45. config=service_dict),
  46. config.ConfigFile(filename=filename, config={}),
  47. environment=Environment.from_env_file(working_dir)
  48. )
  49. return config.process_service(resolver.run())
  50. def service_sort(services):
  51. return sorted(services, key=itemgetter('name'))
  52. def secret_sort(secrets):
  53. return sorted(secrets, key=itemgetter('source'))
  54. class ConfigTest(unittest.TestCase):
  55. def test_load(self):
  56. service_dicts = config.load(
  57. build_config_details(
  58. {
  59. 'foo': {'image': 'busybox'},
  60. 'bar': {'image': 'busybox', 'environment': ['FOO=1']},
  61. },
  62. 'tests/fixtures/extends',
  63. 'common.yml'
  64. )
  65. ).services
  66. self.assertEqual(
  67. service_sort(service_dicts),
  68. service_sort([
  69. {
  70. 'name': 'bar',
  71. 'image': 'busybox',
  72. 'environment': {'FOO': '1'},
  73. },
  74. {
  75. 'name': 'foo',
  76. 'image': 'busybox',
  77. }
  78. ])
  79. )
  80. def test_load_v2(self):
  81. config_data = config.load(
  82. build_config_details({
  83. 'version': '2',
  84. 'services': {
  85. 'foo': {'image': 'busybox'},
  86. 'bar': {'image': 'busybox', 'environment': ['FOO=1']},
  87. },
  88. 'volumes': {
  89. 'hello': {
  90. 'driver': 'default',
  91. 'driver_opts': {'beep': 'boop'}
  92. }
  93. },
  94. 'networks': {
  95. 'default': {
  96. 'driver': 'bridge',
  97. 'driver_opts': {'beep': 'boop'}
  98. },
  99. 'with_ipam': {
  100. 'ipam': {
  101. 'driver': 'default',
  102. 'config': [
  103. {'subnet': '172.28.0.0/16'}
  104. ]
  105. }
  106. },
  107. 'internal': {
  108. 'driver': 'bridge',
  109. 'internal': True
  110. }
  111. }
  112. }, 'working_dir', 'filename.yml')
  113. )
  114. service_dicts = config_data.services
  115. volume_dict = config_data.volumes
  116. networks_dict = config_data.networks
  117. self.assertEqual(
  118. service_sort(service_dicts),
  119. service_sort([
  120. {
  121. 'name': 'bar',
  122. 'image': 'busybox',
  123. 'environment': {'FOO': '1'},
  124. },
  125. {
  126. 'name': 'foo',
  127. 'image': 'busybox',
  128. }
  129. ])
  130. )
  131. self.assertEqual(volume_dict, {
  132. 'hello': {
  133. 'driver': 'default',
  134. 'driver_opts': {'beep': 'boop'}
  135. }
  136. })
  137. self.assertEqual(networks_dict, {
  138. 'default': {
  139. 'driver': 'bridge',
  140. 'driver_opts': {'beep': 'boop'}
  141. },
  142. 'with_ipam': {
  143. 'ipam': {
  144. 'driver': 'default',
  145. 'config': [
  146. {'subnet': '172.28.0.0/16'}
  147. ]
  148. }
  149. },
  150. 'internal': {
  151. 'driver': 'bridge',
  152. 'internal': True
  153. }
  154. })
  155. def test_valid_versions(self):
  156. for version in ['2', '2.0']:
  157. cfg = config.load(build_config_details({'version': version}))
  158. assert cfg.version == V2_0
  159. cfg = config.load(build_config_details({'version': '2.1'}))
  160. assert cfg.version == V2_1
  161. cfg = config.load(build_config_details({'version': '2.2'}))
  162. assert cfg.version == V2_2
  163. cfg = config.load(build_config_details({'version': '2.3'}))
  164. assert cfg.version == V2_3
  165. for version in ['3', '3.0']:
  166. cfg = config.load(build_config_details({'version': version}))
  167. assert cfg.version == V3_0
  168. cfg = config.load(build_config_details({'version': '3.1'}))
  169. assert cfg.version == V3_1
  170. def test_v1_file_version(self):
  171. cfg = config.load(build_config_details({'web': {'image': 'busybox'}}))
  172. assert cfg.version == V1
  173. assert list(s['name'] for s in cfg.services) == ['web']
  174. cfg = config.load(build_config_details({'version': {'image': 'busybox'}}))
  175. assert cfg.version == V1
  176. assert list(s['name'] for s in cfg.services) == ['version']
  177. def test_wrong_version_type(self):
  178. for version in [None, 1, 2, 2.0]:
  179. with pytest.raises(ConfigurationError) as excinfo:
  180. config.load(
  181. build_config_details(
  182. {'version': version},
  183. filename='filename.yml',
  184. )
  185. )
  186. assert 'Version in "filename.yml" is invalid - it should be a string.' \
  187. in excinfo.exconly()
  188. def test_unsupported_version(self):
  189. with pytest.raises(ConfigurationError) as excinfo:
  190. config.load(
  191. build_config_details(
  192. {'version': '2.18'},
  193. filename='filename.yml',
  194. )
  195. )
  196. assert 'Version in "filename.yml" is unsupported' in excinfo.exconly()
  197. assert VERSION_EXPLANATION in excinfo.exconly()
  198. def test_version_1_is_invalid(self):
  199. with pytest.raises(ConfigurationError) as excinfo:
  200. config.load(
  201. build_config_details(
  202. {
  203. 'version': '1',
  204. 'web': {'image': 'busybox'},
  205. },
  206. filename='filename.yml',
  207. )
  208. )
  209. assert 'Version in "filename.yml" is invalid' in excinfo.exconly()
  210. assert VERSION_EXPLANATION in excinfo.exconly()
  211. def test_v1_file_with_version_is_invalid(self):
  212. with pytest.raises(ConfigurationError) as excinfo:
  213. config.load(
  214. build_config_details(
  215. {
  216. 'version': '2',
  217. 'web': {'image': 'busybox'},
  218. },
  219. filename='filename.yml',
  220. )
  221. )
  222. assert 'Invalid top-level property "web"' in excinfo.exconly()
  223. assert VERSION_EXPLANATION in excinfo.exconly()
  224. def test_named_volume_config_empty(self):
  225. config_details = build_config_details({
  226. 'version': '2',
  227. 'services': {
  228. 'simple': {'image': 'busybox'}
  229. },
  230. 'volumes': {
  231. 'simple': None,
  232. 'other': {},
  233. }
  234. })
  235. config_result = config.load(config_details)
  236. volumes = config_result.volumes
  237. assert 'simple' in volumes
  238. assert volumes['simple'] == {}
  239. assert volumes['other'] == {}
  240. def test_named_volume_numeric_driver_opt(self):
  241. config_details = build_config_details({
  242. 'version': '2',
  243. 'services': {
  244. 'simple': {'image': 'busybox'}
  245. },
  246. 'volumes': {
  247. 'simple': {'driver_opts': {'size': 42}},
  248. }
  249. })
  250. cfg = config.load(config_details)
  251. assert cfg.volumes['simple']['driver_opts']['size'] == '42'
  252. def test_volume_invalid_driver_opt(self):
  253. config_details = build_config_details({
  254. 'version': '2',
  255. 'services': {
  256. 'simple': {'image': 'busybox'}
  257. },
  258. 'volumes': {
  259. 'simple': {'driver_opts': {'size': True}},
  260. }
  261. })
  262. with pytest.raises(ConfigurationError) as exc:
  263. config.load(config_details)
  264. assert 'driver_opts.size contains an invalid type' in exc.exconly()
  265. def test_named_volume_invalid_type_list(self):
  266. config_details = build_config_details({
  267. 'version': '2',
  268. 'services': {
  269. 'simple': {'image': 'busybox'}
  270. },
  271. 'volumes': []
  272. })
  273. with pytest.raises(ConfigurationError) as exc:
  274. config.load(config_details)
  275. assert "volume must be a mapping, not an array" in exc.exconly()
  276. def test_networks_invalid_type_list(self):
  277. config_details = build_config_details({
  278. 'version': '2',
  279. 'services': {
  280. 'simple': {'image': 'busybox'}
  281. },
  282. 'networks': []
  283. })
  284. with pytest.raises(ConfigurationError) as exc:
  285. config.load(config_details)
  286. assert "network must be a mapping, not an array" in exc.exconly()
  287. def test_load_service_with_name_version(self):
  288. with mock.patch('compose.config.config.log') as mock_logging:
  289. config_data = config.load(
  290. build_config_details({
  291. 'version': {
  292. 'image': 'busybox'
  293. }
  294. }, 'working_dir', 'filename.yml')
  295. )
  296. assert 'Unexpected type for "version" key in "filename.yml"' \
  297. in mock_logging.warn.call_args[0][0]
  298. service_dicts = config_data.services
  299. self.assertEqual(
  300. service_sort(service_dicts),
  301. service_sort([
  302. {
  303. 'name': 'version',
  304. 'image': 'busybox',
  305. }
  306. ])
  307. )
  308. def test_load_throws_error_when_not_dict(self):
  309. with self.assertRaises(ConfigurationError):
  310. config.load(
  311. build_config_details(
  312. {'web': 'busybox:latest'},
  313. 'working_dir',
  314. 'filename.yml'
  315. )
  316. )
  317. def test_load_throws_error_when_not_dict_v2(self):
  318. with self.assertRaises(ConfigurationError):
  319. config.load(
  320. build_config_details(
  321. {'version': '2', 'services': {'web': 'busybox:latest'}},
  322. 'working_dir',
  323. 'filename.yml'
  324. )
  325. )
  326. def test_load_throws_error_with_invalid_network_fields(self):
  327. with self.assertRaises(ConfigurationError):
  328. config.load(
  329. build_config_details({
  330. 'version': '2',
  331. 'services': {'web': 'busybox:latest'},
  332. 'networks': {
  333. 'invalid': {'foo', 'bar'}
  334. }
  335. }, 'working_dir', 'filename.yml')
  336. )
  337. def test_load_config_link_local_ips_network(self):
  338. base_file = config.ConfigFile(
  339. 'base.yaml',
  340. {
  341. 'version': str(V2_1),
  342. 'services': {
  343. 'web': {
  344. 'image': 'example/web',
  345. 'networks': {
  346. 'foobar': {
  347. 'aliases': ['foo', 'bar'],
  348. 'link_local_ips': ['169.254.8.8']
  349. }
  350. }
  351. }
  352. },
  353. 'networks': {'foobar': {}}
  354. }
  355. )
  356. details = config.ConfigDetails('.', [base_file])
  357. web_service = config.load(details).services[0]
  358. assert web_service['networks'] == {
  359. 'foobar': {
  360. 'aliases': ['foo', 'bar'],
  361. 'link_local_ips': ['169.254.8.8']
  362. }
  363. }
  364. def test_load_config_service_labels(self):
  365. base_file = config.ConfigFile(
  366. 'base.yaml',
  367. {
  368. 'version': '2.1',
  369. 'services': {
  370. 'web': {
  371. 'image': 'example/web',
  372. 'labels': ['label_key=label_val']
  373. },
  374. 'db': {
  375. 'image': 'example/db',
  376. 'labels': {
  377. 'label_key': 'label_val'
  378. }
  379. }
  380. },
  381. }
  382. )
  383. details = config.ConfigDetails('.', [base_file])
  384. service_dicts = config.load(details).services
  385. for service in service_dicts:
  386. assert service['labels'] == {
  387. 'label_key': 'label_val'
  388. }
  389. def test_load_config_custom_resource_names(self):
  390. base_file = config.ConfigFile(
  391. 'base.yaml', {
  392. 'version': '3.5',
  393. 'volumes': {
  394. 'abc': {
  395. 'name': 'xyz'
  396. }
  397. },
  398. 'networks': {
  399. 'abc': {
  400. 'name': 'xyz'
  401. }
  402. },
  403. 'secrets': {
  404. 'abc': {
  405. 'name': 'xyz'
  406. }
  407. },
  408. 'configs': {
  409. 'abc': {
  410. 'name': 'xyz'
  411. }
  412. }
  413. }
  414. )
  415. details = config.ConfigDetails('.', [base_file])
  416. loaded_config = config.load(details)
  417. assert loaded_config.networks['abc'] == {'name': 'xyz'}
  418. assert loaded_config.volumes['abc'] == {'name': 'xyz'}
  419. assert loaded_config.secrets['abc']['name'] == 'xyz'
  420. assert loaded_config.configs['abc']['name'] == 'xyz'
  421. def test_load_config_volume_and_network_labels(self):
  422. base_file = config.ConfigFile(
  423. 'base.yaml',
  424. {
  425. 'version': '2.1',
  426. 'services': {
  427. 'web': {
  428. 'image': 'example/web',
  429. },
  430. },
  431. 'networks': {
  432. 'with_label': {
  433. 'labels': {
  434. 'label_key': 'label_val'
  435. }
  436. }
  437. },
  438. 'volumes': {
  439. 'with_label': {
  440. 'labels': {
  441. 'label_key': 'label_val'
  442. }
  443. }
  444. }
  445. }
  446. )
  447. details = config.ConfigDetails('.', [base_file])
  448. loaded_config = config.load(details)
  449. assert loaded_config.networks == {
  450. 'with_label': {
  451. 'labels': {
  452. 'label_key': 'label_val'
  453. }
  454. }
  455. }
  456. assert loaded_config.volumes == {
  457. 'with_label': {
  458. 'labels': {
  459. 'label_key': 'label_val'
  460. }
  461. }
  462. }
  463. def test_load_config_invalid_service_names(self):
  464. for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
  465. with pytest.raises(ConfigurationError) as exc:
  466. config.load(build_config_details(
  467. {invalid_name: {'image': 'busybox'}}))
  468. assert 'Invalid service name \'%s\'' % invalid_name in exc.exconly()
  469. def test_load_config_invalid_service_names_v2(self):
  470. for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
  471. with pytest.raises(ConfigurationError) as exc:
  472. config.load(build_config_details(
  473. {
  474. 'version': '2',
  475. 'services': {invalid_name: {'image': 'busybox'}},
  476. }))
  477. assert 'Invalid service name \'%s\'' % invalid_name in exc.exconly()
  478. def test_load_with_invalid_field_name(self):
  479. with pytest.raises(ConfigurationError) as exc:
  480. config.load(build_config_details(
  481. {
  482. 'version': '2',
  483. 'services': {
  484. 'web': {'image': 'busybox', 'name': 'bogus'},
  485. }
  486. },
  487. 'working_dir',
  488. 'filename.yml',
  489. ))
  490. assert "Unsupported config option for services.web: 'name'" in exc.exconly()
  491. def test_load_with_invalid_field_name_v1(self):
  492. with pytest.raises(ConfigurationError) as exc:
  493. config.load(build_config_details(
  494. {
  495. 'web': {'image': 'busybox', 'name': 'bogus'},
  496. },
  497. 'working_dir',
  498. 'filename.yml',
  499. ))
  500. assert "Unsupported config option for web: 'name'" in exc.exconly()
  501. def test_load_invalid_service_definition(self):
  502. config_details = build_config_details(
  503. {'web': 'wrong'},
  504. 'working_dir',
  505. 'filename.yml')
  506. with pytest.raises(ConfigurationError) as exc:
  507. config.load(config_details)
  508. assert "service 'web' must be a mapping not a string." in exc.exconly()
  509. def test_load_with_empty_build_args(self):
  510. config_details = build_config_details(
  511. {
  512. 'version': '2',
  513. 'services': {
  514. 'web': {
  515. 'build': {
  516. 'context': '.',
  517. 'args': None,
  518. },
  519. },
  520. },
  521. }
  522. )
  523. with pytest.raises(ConfigurationError) as exc:
  524. config.load(config_details)
  525. assert (
  526. "services.web.build.args contains an invalid type, it should be an "
  527. "object, or an array" in exc.exconly()
  528. )
  529. def test_config_integer_service_name_raise_validation_error(self):
  530. with pytest.raises(ConfigurationError) as excinfo:
  531. config.load(
  532. build_config_details(
  533. {1: {'image': 'busybox'}},
  534. 'working_dir',
  535. 'filename.yml'
  536. )
  537. )
  538. assert (
  539. "In file 'filename.yml', the service name 1 must be a quoted string, i.e. '1'" in
  540. excinfo.exconly()
  541. )
  542. def test_config_integer_service_name_raise_validation_error_v2(self):
  543. with pytest.raises(ConfigurationError) as excinfo:
  544. config.load(
  545. build_config_details(
  546. {
  547. 'version': '2',
  548. 'services': {1: {'image': 'busybox'}}
  549. },
  550. 'working_dir',
  551. 'filename.yml'
  552. )
  553. )
  554. assert (
  555. "In file 'filename.yml', the service name 1 must be a quoted string, i.e. '1'." in
  556. excinfo.exconly()
  557. )
  558. def test_config_invalid_service_name_raise_validation_error(self):
  559. with pytest.raises(ConfigurationError) as excinfo:
  560. config.load(
  561. build_config_details({
  562. 'version': '2',
  563. 'services': {
  564. 'test_app': {'build': '.'},
  565. 'mong\\o': {'image': 'mongo'},
  566. }
  567. })
  568. )
  569. assert 'Invalid service name \'mong\\o\'' in excinfo.exconly()
  570. def test_config_duplicate_cache_from_values_validation_error(self):
  571. with pytest.raises(ConfigurationError) as exc:
  572. config.load(
  573. build_config_details({
  574. 'version': '2.3',
  575. 'services': {
  576. 'test': {'build': {'context': '.', 'cache_from': ['a', 'b', 'a']}}
  577. }
  578. })
  579. )
  580. assert 'build.cache_from contains non-unique items' in exc.exconly()
  581. def test_load_with_multiple_files_v1(self):
  582. base_file = config.ConfigFile(
  583. 'base.yaml',
  584. {
  585. 'web': {
  586. 'image': 'example/web',
  587. 'links': ['db'],
  588. },
  589. 'db': {
  590. 'image': 'example/db',
  591. },
  592. })
  593. override_file = config.ConfigFile(
  594. 'override.yaml',
  595. {
  596. 'web': {
  597. 'build': '/',
  598. 'volumes': ['/home/user/project:/code'],
  599. },
  600. })
  601. details = config.ConfigDetails('.', [base_file, override_file])
  602. service_dicts = config.load(details).services
  603. expected = [
  604. {
  605. 'name': 'web',
  606. 'build': {'context': os.path.abspath('/')},
  607. 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
  608. 'links': ['db'],
  609. },
  610. {
  611. 'name': 'db',
  612. 'image': 'example/db',
  613. },
  614. ]
  615. assert service_sort(service_dicts) == service_sort(expected)
  616. def test_load_with_multiple_files_and_empty_override(self):
  617. base_file = config.ConfigFile(
  618. 'base.yml',
  619. {'web': {'image': 'example/web'}})
  620. override_file = config.ConfigFile('override.yml', None)
  621. details = config.ConfigDetails('.', [base_file, override_file])
  622. with pytest.raises(ConfigurationError) as exc:
  623. config.load(details)
  624. error_msg = "Top level object in 'override.yml' needs to be an object"
  625. assert error_msg in exc.exconly()
  626. def test_load_with_multiple_files_and_empty_override_v2(self):
  627. base_file = config.ConfigFile(
  628. 'base.yml',
  629. {'version': '2', 'services': {'web': {'image': 'example/web'}}})
  630. override_file = config.ConfigFile('override.yml', None)
  631. details = config.ConfigDetails('.', [base_file, override_file])
  632. with pytest.raises(ConfigurationError) as exc:
  633. config.load(details)
  634. error_msg = "Top level object in 'override.yml' needs to be an object"
  635. assert error_msg in exc.exconly()
  636. def test_load_with_multiple_files_and_empty_base(self):
  637. base_file = config.ConfigFile('base.yml', None)
  638. override_file = config.ConfigFile(
  639. 'override.yml',
  640. {'web': {'image': 'example/web'}})
  641. details = config.ConfigDetails('.', [base_file, override_file])
  642. with pytest.raises(ConfigurationError) as exc:
  643. config.load(details)
  644. assert "Top level object in 'base.yml' needs to be an object" in exc.exconly()
  645. def test_load_with_multiple_files_and_empty_base_v2(self):
  646. base_file = config.ConfigFile('base.yml', None)
  647. override_file = config.ConfigFile(
  648. 'override.tml',
  649. {'version': '2', 'services': {'web': {'image': 'example/web'}}}
  650. )
  651. details = config.ConfigDetails('.', [base_file, override_file])
  652. with pytest.raises(ConfigurationError) as exc:
  653. config.load(details)
  654. assert "Top level object in 'base.yml' needs to be an object" in exc.exconly()
  655. def test_load_with_multiple_files_and_extends_in_override_file(self):
  656. base_file = config.ConfigFile(
  657. 'base.yaml',
  658. {
  659. 'web': {'image': 'example/web'},
  660. })
  661. override_file = config.ConfigFile(
  662. 'override.yaml',
  663. {
  664. 'web': {
  665. 'extends': {
  666. 'file': 'common.yml',
  667. 'service': 'base',
  668. },
  669. 'volumes': ['/home/user/project:/code'],
  670. },
  671. })
  672. details = config.ConfigDetails('.', [base_file, override_file])
  673. tmpdir = py.test.ensuretemp('config_test')
  674. self.addCleanup(tmpdir.remove)
  675. tmpdir.join('common.yml').write("""
  676. base:
  677. labels: ['label=one']
  678. """)
  679. with tmpdir.as_cwd():
  680. service_dicts = config.load(details).services
  681. expected = [
  682. {
  683. 'name': 'web',
  684. 'image': 'example/web',
  685. 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
  686. 'labels': {'label': 'one'},
  687. },
  688. ]
  689. self.assertEqual(service_sort(service_dicts), service_sort(expected))
  690. def test_load_mixed_extends_resolution(self):
  691. main_file = config.ConfigFile(
  692. 'main.yml', {
  693. 'version': '2.2',
  694. 'services': {
  695. 'prodweb': {
  696. 'extends': {
  697. 'service': 'web',
  698. 'file': 'base.yml'
  699. },
  700. 'environment': {'PROD': 'true'},
  701. },
  702. },
  703. }
  704. )
  705. tmpdir = pytest.ensuretemp('config_test')
  706. self.addCleanup(tmpdir.remove)
  707. tmpdir.join('base.yml').write("""
  708. version: '2.2'
  709. services:
  710. base:
  711. image: base
  712. web:
  713. extends: base
  714. """)
  715. details = config.ConfigDetails('.', [main_file])
  716. with tmpdir.as_cwd():
  717. service_dicts = config.load(details).services
  718. assert service_dicts[0] == {
  719. 'name': 'prodweb',
  720. 'image': 'base',
  721. 'environment': {'PROD': 'true'},
  722. }
  723. def test_load_with_multiple_files_and_invalid_override(self):
  724. base_file = config.ConfigFile(
  725. 'base.yaml',
  726. {'web': {'image': 'example/web'}})
  727. override_file = config.ConfigFile(
  728. 'override.yaml',
  729. {'bogus': 'thing'})
  730. details = config.ConfigDetails('.', [base_file, override_file])
  731. with pytest.raises(ConfigurationError) as exc:
  732. config.load(details)
  733. assert "service 'bogus' must be a mapping not a string." in exc.exconly()
  734. assert "In file 'override.yaml'" in exc.exconly()
  735. def test_load_sorts_in_dependency_order(self):
  736. config_details = build_config_details({
  737. 'web': {
  738. 'image': 'busybox:latest',
  739. 'links': ['db'],
  740. },
  741. 'db': {
  742. 'image': 'busybox:latest',
  743. 'volumes_from': ['volume:ro']
  744. },
  745. 'volume': {
  746. 'image': 'busybox:latest',
  747. 'volumes': ['/tmp'],
  748. }
  749. })
  750. services = config.load(config_details).services
  751. assert services[0]['name'] == 'volume'
  752. assert services[1]['name'] == 'db'
  753. assert services[2]['name'] == 'web'
  754. def test_load_with_extensions(self):
  755. config_details = build_config_details({
  756. 'version': '2.3',
  757. 'x-data': {
  758. 'lambda': 3,
  759. 'excess': [True, {}]
  760. }
  761. })
  762. config_data = config.load(config_details)
  763. assert config_data.services == []
  764. def test_config_build_configuration(self):
  765. service = config.load(
  766. build_config_details(
  767. {'web': {
  768. 'build': '.',
  769. 'dockerfile': 'Dockerfile-alt'
  770. }},
  771. 'tests/fixtures/extends',
  772. 'filename.yml'
  773. )
  774. ).services
  775. self.assertTrue('context' in service[0]['build'])
  776. self.assertEqual(service[0]['build']['dockerfile'], 'Dockerfile-alt')
  777. def test_config_build_configuration_v2(self):
  778. # service.dockerfile is invalid in v2
  779. with self.assertRaises(ConfigurationError):
  780. config.load(
  781. build_config_details(
  782. {
  783. 'version': '2',
  784. 'services': {
  785. 'web': {
  786. 'build': '.',
  787. 'dockerfile': 'Dockerfile-alt'
  788. }
  789. }
  790. },
  791. 'tests/fixtures/extends',
  792. 'filename.yml'
  793. )
  794. )
  795. service = config.load(
  796. build_config_details({
  797. 'version': '2',
  798. 'services': {
  799. 'web': {
  800. 'build': '.'
  801. }
  802. }
  803. }, 'tests/fixtures/extends', 'filename.yml')
  804. ).services[0]
  805. self.assertTrue('context' in service['build'])
  806. service = config.load(
  807. build_config_details(
  808. {
  809. 'version': '2',
  810. 'services': {
  811. 'web': {
  812. 'build': {
  813. 'context': '.',
  814. 'dockerfile': 'Dockerfile-alt'
  815. }
  816. }
  817. }
  818. },
  819. 'tests/fixtures/extends',
  820. 'filename.yml'
  821. )
  822. ).services
  823. self.assertTrue('context' in service[0]['build'])
  824. self.assertEqual(service[0]['build']['dockerfile'], 'Dockerfile-alt')
  825. def test_load_with_buildargs(self):
  826. service = config.load(
  827. build_config_details(
  828. {
  829. 'version': '2',
  830. 'services': {
  831. 'web': {
  832. 'build': {
  833. 'context': '.',
  834. 'dockerfile': 'Dockerfile-alt',
  835. 'args': {
  836. 'opt1': 42,
  837. 'opt2': 'foobar'
  838. }
  839. }
  840. }
  841. }
  842. },
  843. 'tests/fixtures/extends',
  844. 'filename.yml'
  845. )
  846. ).services[0]
  847. assert 'args' in service['build']
  848. assert 'opt1' in service['build']['args']
  849. assert isinstance(service['build']['args']['opt1'], str)
  850. assert service['build']['args']['opt1'] == '42'
  851. assert service['build']['args']['opt2'] == 'foobar'
  852. def test_load_build_labels_dict(self):
  853. service = config.load(
  854. build_config_details(
  855. {
  856. 'version': str(V3_3),
  857. 'services': {
  858. 'web': {
  859. 'build': {
  860. 'context': '.',
  861. 'dockerfile': 'Dockerfile-alt',
  862. 'labels': {
  863. 'label1': 42,
  864. 'label2': 'foobar'
  865. }
  866. }
  867. }
  868. }
  869. },
  870. 'tests/fixtures/extends',
  871. 'filename.yml'
  872. )
  873. ).services[0]
  874. assert 'labels' in service['build']
  875. assert 'label1' in service['build']['labels']
  876. assert service['build']['labels']['label1'] == 42
  877. assert service['build']['labels']['label2'] == 'foobar'
  878. def test_load_build_labels_list(self):
  879. base_file = config.ConfigFile(
  880. 'base.yml',
  881. {
  882. 'version': '2.3',
  883. 'services': {
  884. 'web': {
  885. 'build': {
  886. 'context': '.',
  887. 'labels': ['foo=bar', 'baz=true', 'foobar=1']
  888. },
  889. },
  890. },
  891. }
  892. )
  893. details = config.ConfigDetails('.', [base_file])
  894. service = config.load(details).services[0]
  895. assert service['build']['labels'] == {
  896. 'foo': 'bar', 'baz': 'true', 'foobar': '1'
  897. }
  898. def test_build_args_allow_empty_properties(self):
  899. service = config.load(
  900. build_config_details(
  901. {
  902. 'version': '2',
  903. 'services': {
  904. 'web': {
  905. 'build': {
  906. 'context': '.',
  907. 'dockerfile': 'Dockerfile-alt',
  908. 'args': {
  909. 'foo': None
  910. }
  911. }
  912. }
  913. }
  914. },
  915. 'tests/fixtures/extends',
  916. 'filename.yml'
  917. )
  918. ).services[0]
  919. assert 'args' in service['build']
  920. assert 'foo' in service['build']['args']
  921. assert service['build']['args']['foo'] == ''
  922. # If build argument is None then it will be converted to the empty
  923. # string. Make sure that int zero kept as it is, i.e. not converted to
  924. # the empty string
  925. def test_build_args_check_zero_preserved(self):
  926. service = config.load(
  927. build_config_details(
  928. {
  929. 'version': '2',
  930. 'services': {
  931. 'web': {
  932. 'build': {
  933. 'context': '.',
  934. 'dockerfile': 'Dockerfile-alt',
  935. 'args': {
  936. 'foo': 0
  937. }
  938. }
  939. }
  940. }
  941. },
  942. 'tests/fixtures/extends',
  943. 'filename.yml'
  944. )
  945. ).services[0]
  946. assert 'args' in service['build']
  947. assert 'foo' in service['build']['args']
  948. assert service['build']['args']['foo'] == '0'
  949. def test_load_with_multiple_files_mismatched_networks_format(self):
  950. base_file = config.ConfigFile(
  951. 'base.yaml',
  952. {
  953. 'version': '2',
  954. 'services': {
  955. 'web': {
  956. 'image': 'example/web',
  957. 'networks': {
  958. 'foobar': {'aliases': ['foo', 'bar']}
  959. }
  960. }
  961. },
  962. 'networks': {'foobar': {}, 'baz': {}}
  963. }
  964. )
  965. override_file = config.ConfigFile(
  966. 'override.yaml',
  967. {
  968. 'version': '2',
  969. 'services': {
  970. 'web': {
  971. 'networks': ['baz']
  972. }
  973. }
  974. }
  975. )
  976. details = config.ConfigDetails('.', [base_file, override_file])
  977. web_service = config.load(details).services[0]
  978. assert web_service['networks'] == {
  979. 'foobar': {'aliases': ['foo', 'bar']},
  980. 'baz': None
  981. }
  982. def test_load_with_multiple_files_v2(self):
  983. base_file = config.ConfigFile(
  984. 'base.yaml',
  985. {
  986. 'version': '2',
  987. 'services': {
  988. 'web': {
  989. 'image': 'example/web',
  990. 'depends_on': ['db'],
  991. },
  992. 'db': {
  993. 'image': 'example/db',
  994. }
  995. },
  996. })
  997. override_file = config.ConfigFile(
  998. 'override.yaml',
  999. {
  1000. 'version': '2',
  1001. 'services': {
  1002. 'web': {
  1003. 'build': '/',
  1004. 'volumes': ['/home/user/project:/code'],
  1005. 'depends_on': ['other'],
  1006. },
  1007. 'other': {
  1008. 'image': 'example/other',
  1009. }
  1010. }
  1011. })
  1012. details = config.ConfigDetails('.', [base_file, override_file])
  1013. service_dicts = config.load(details).services
  1014. expected = [
  1015. {
  1016. 'name': 'web',
  1017. 'build': {'context': os.path.abspath('/')},
  1018. 'image': 'example/web',
  1019. 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
  1020. 'depends_on': {
  1021. 'db': {'condition': 'service_started'},
  1022. 'other': {'condition': 'service_started'},
  1023. },
  1024. },
  1025. {
  1026. 'name': 'db',
  1027. 'image': 'example/db',
  1028. },
  1029. {
  1030. 'name': 'other',
  1031. 'image': 'example/other',
  1032. },
  1033. ]
  1034. assert service_sort(service_dicts) == service_sort(expected)
  1035. @mock.patch.dict(os.environ)
  1036. def test_load_with_multiple_files_v3_2(self):
  1037. os.environ['COMPOSE_CONVERT_WINDOWS_PATHS'] = 'true'
  1038. base_file = config.ConfigFile(
  1039. 'base.yaml',
  1040. {
  1041. 'version': '3.2',
  1042. 'services': {
  1043. 'web': {
  1044. 'image': 'example/web',
  1045. 'volumes': [
  1046. {'source': '/a', 'target': '/b', 'type': 'bind'},
  1047. {'source': 'vol', 'target': '/x', 'type': 'volume', 'read_only': True}
  1048. ],
  1049. 'stop_grace_period': '30s',
  1050. }
  1051. },
  1052. 'volumes': {'vol': {}}
  1053. }
  1054. )
  1055. override_file = config.ConfigFile(
  1056. 'override.yaml',
  1057. {
  1058. 'version': '3.2',
  1059. 'services': {
  1060. 'web': {
  1061. 'volumes': ['/c:/b', '/anonymous']
  1062. }
  1063. }
  1064. }
  1065. )
  1066. details = config.ConfigDetails('.', [base_file, override_file])
  1067. service_dicts = config.load(details).services
  1068. svc_volumes = map(lambda v: v.repr(), service_dicts[0]['volumes'])
  1069. for vol in svc_volumes:
  1070. assert vol in [
  1071. '/anonymous',
  1072. '/c:/b:rw',
  1073. {'source': 'vol', 'target': '/x', 'type': 'volume', 'read_only': True}
  1074. ]
  1075. assert service_dicts[0]['stop_grace_period'] == '30s'
  1076. @mock.patch.dict(os.environ)
  1077. def test_volume_mode_override(self):
  1078. os.environ['COMPOSE_CONVERT_WINDOWS_PATHS'] = 'true'
  1079. base_file = config.ConfigFile(
  1080. 'base.yaml',
  1081. {
  1082. 'version': '2.3',
  1083. 'services': {
  1084. 'web': {
  1085. 'image': 'example/web',
  1086. 'volumes': ['/c:/b:rw']
  1087. }
  1088. },
  1089. }
  1090. )
  1091. override_file = config.ConfigFile(
  1092. 'override.yaml',
  1093. {
  1094. 'version': '2.3',
  1095. 'services': {
  1096. 'web': {
  1097. 'volumes': ['/c:/b:ro']
  1098. }
  1099. }
  1100. }
  1101. )
  1102. details = config.ConfigDetails('.', [base_file, override_file])
  1103. service_dicts = config.load(details).services
  1104. svc_volumes = list(map(lambda v: v.repr(), service_dicts[0]['volumes']))
  1105. assert svc_volumes == ['/c:/b:ro']
  1106. def test_undeclared_volume_v2(self):
  1107. base_file = config.ConfigFile(
  1108. 'base.yaml',
  1109. {
  1110. 'version': '2',
  1111. 'services': {
  1112. 'web': {
  1113. 'image': 'busybox:latest',
  1114. 'volumes': ['data0028:/data:ro'],
  1115. },
  1116. },
  1117. }
  1118. )
  1119. details = config.ConfigDetails('.', [base_file])
  1120. with self.assertRaises(ConfigurationError):
  1121. config.load(details)
  1122. base_file = config.ConfigFile(
  1123. 'base.yaml',
  1124. {
  1125. 'version': '2',
  1126. 'services': {
  1127. 'web': {
  1128. 'image': 'busybox:latest',
  1129. 'volumes': ['./data0028:/data:ro'],
  1130. },
  1131. },
  1132. }
  1133. )
  1134. details = config.ConfigDetails('.', [base_file])
  1135. config_data = config.load(details)
  1136. volume = config_data.services[0].get('volumes')[0]
  1137. assert not volume.is_named_volume
  1138. def test_undeclared_volume_v1(self):
  1139. base_file = config.ConfigFile(
  1140. 'base.yaml',
  1141. {
  1142. 'web': {
  1143. 'image': 'busybox:latest',
  1144. 'volumes': ['data0028:/data:ro'],
  1145. },
  1146. }
  1147. )
  1148. details = config.ConfigDetails('.', [base_file])
  1149. config_data = config.load(details)
  1150. volume = config_data.services[0].get('volumes')[0]
  1151. assert volume.external == 'data0028'
  1152. assert volume.is_named_volume
  1153. def test_volumes_long_syntax(self):
  1154. base_file = config.ConfigFile(
  1155. 'base.yaml', {
  1156. 'version': '2.3',
  1157. 'services': {
  1158. 'web': {
  1159. 'image': 'busybox:latest',
  1160. 'volumes': [
  1161. {
  1162. 'target': '/anonymous', 'type': 'volume'
  1163. }, {
  1164. 'source': '/abc', 'target': '/xyz', 'type': 'bind'
  1165. }, {
  1166. 'source': '\\\\.\\pipe\\abcd', 'target': '/named_pipe', 'type': 'npipe'
  1167. }, {
  1168. 'type': 'tmpfs', 'target': '/tmpfs'
  1169. }
  1170. ]
  1171. },
  1172. },
  1173. },
  1174. )
  1175. details = config.ConfigDetails('.', [base_file])
  1176. config_data = config.load(details)
  1177. volumes = config_data.services[0].get('volumes')
  1178. anon_volume = [v for v in volumes if v.target == '/anonymous'][0]
  1179. tmpfs_mount = [v for v in volumes if v.type == 'tmpfs'][0]
  1180. host_mount = [v for v in volumes if v.type == 'bind'][0]
  1181. npipe_mount = [v for v in volumes if v.type == 'npipe'][0]
  1182. assert anon_volume.type == 'volume'
  1183. assert not anon_volume.is_named_volume
  1184. assert tmpfs_mount.target == '/tmpfs'
  1185. assert not tmpfs_mount.is_named_volume
  1186. assert host_mount.source == os.path.normpath('/abc')
  1187. assert host_mount.target == '/xyz'
  1188. assert not host_mount.is_named_volume
  1189. assert npipe_mount.source == '\\\\.\\pipe\\abcd'
  1190. assert npipe_mount.target == '/named_pipe'
  1191. assert not npipe_mount.is_named_volume
  1192. def test_load_bind_mount_relative_path(self):
  1193. expected_source = 'C:\\tmp\\web' if IS_WINDOWS_PLATFORM else '/tmp/web'
  1194. base_file = config.ConfigFile(
  1195. 'base.yaml', {
  1196. 'version': '3.4',
  1197. 'services': {
  1198. 'web': {
  1199. 'image': 'busybox:latest',
  1200. 'volumes': [
  1201. {'type': 'bind', 'source': './web', 'target': '/web'},
  1202. ],
  1203. },
  1204. },
  1205. },
  1206. )
  1207. details = config.ConfigDetails('/tmp', [base_file])
  1208. config_data = config.load(details)
  1209. mount = config_data.services[0].get('volumes')[0]
  1210. assert mount.target == '/web'
  1211. assert mount.type == 'bind'
  1212. assert mount.source == expected_source
  1213. def test_config_valid_service_names(self):
  1214. for valid_name in ['_', '-', '.__.', '_what-up.', 'what_.up----', 'whatup']:
  1215. services = config.load(
  1216. build_config_details(
  1217. {valid_name: {'image': 'busybox'}},
  1218. 'tests/fixtures/extends',
  1219. 'common.yml')).services
  1220. assert services[0]['name'] == valid_name
  1221. def test_config_hint(self):
  1222. with pytest.raises(ConfigurationError) as excinfo:
  1223. config.load(
  1224. build_config_details(
  1225. {
  1226. 'foo': {'image': 'busybox', 'privilige': 'something'},
  1227. },
  1228. 'tests/fixtures/extends',
  1229. 'filename.yml'
  1230. )
  1231. )
  1232. assert "(did you mean 'privileged'?)" in excinfo.exconly()
  1233. def test_load_errors_on_uppercase_with_no_image(self):
  1234. with pytest.raises(ConfigurationError) as exc:
  1235. config.load(build_config_details({
  1236. 'Foo': {'build': '.'},
  1237. }, 'tests/fixtures/build-ctx'))
  1238. assert "Service 'Foo' contains uppercase characters" in exc.exconly()
  1239. def test_invalid_config_v1(self):
  1240. with pytest.raises(ConfigurationError) as excinfo:
  1241. config.load(
  1242. build_config_details(
  1243. {
  1244. 'foo': {'image': 1},
  1245. },
  1246. 'tests/fixtures/extends',
  1247. 'filename.yml'
  1248. )
  1249. )
  1250. assert "foo.image contains an invalid type, it should be a string" \
  1251. in excinfo.exconly()
  1252. def test_invalid_config_v2(self):
  1253. with pytest.raises(ConfigurationError) as excinfo:
  1254. config.load(
  1255. build_config_details(
  1256. {
  1257. 'version': '2',
  1258. 'services': {
  1259. 'foo': {'image': 1},
  1260. },
  1261. },
  1262. 'tests/fixtures/extends',
  1263. 'filename.yml'
  1264. )
  1265. )
  1266. assert "services.foo.image contains an invalid type, it should be a string" \
  1267. in excinfo.exconly()
  1268. def test_invalid_config_build_and_image_specified_v1(self):
  1269. with pytest.raises(ConfigurationError) as excinfo:
  1270. config.load(
  1271. build_config_details(
  1272. {
  1273. 'foo': {'image': 'busybox', 'build': '.'},
  1274. },
  1275. 'tests/fixtures/extends',
  1276. 'filename.yml'
  1277. )
  1278. )
  1279. assert "foo has both an image and build path specified." in excinfo.exconly()
  1280. def test_invalid_config_type_should_be_an_array(self):
  1281. with pytest.raises(ConfigurationError) as excinfo:
  1282. config.load(
  1283. build_config_details(
  1284. {
  1285. 'foo': {'image': 'busybox', 'links': 'an_link'},
  1286. },
  1287. 'tests/fixtures/extends',
  1288. 'filename.yml'
  1289. )
  1290. )
  1291. assert "foo.links contains an invalid type, it should be an array" \
  1292. in excinfo.exconly()
  1293. def test_invalid_config_not_a_dictionary(self):
  1294. with pytest.raises(ConfigurationError) as excinfo:
  1295. config.load(
  1296. build_config_details(
  1297. ['foo', 'lol'],
  1298. 'tests/fixtures/extends',
  1299. 'filename.yml'
  1300. )
  1301. )
  1302. assert "Top level object in 'filename.yml' needs to be an object" \
  1303. in excinfo.exconly()
  1304. def test_invalid_config_not_unique_items(self):
  1305. with pytest.raises(ConfigurationError) as excinfo:
  1306. config.load(
  1307. build_config_details(
  1308. {
  1309. 'web': {'build': '.', 'devices': ['/dev/foo:/dev/foo', '/dev/foo:/dev/foo']}
  1310. },
  1311. 'tests/fixtures/extends',
  1312. 'filename.yml'
  1313. )
  1314. )
  1315. assert "has non-unique elements" in excinfo.exconly()
  1316. def test_invalid_list_of_strings_format(self):
  1317. with pytest.raises(ConfigurationError) as excinfo:
  1318. config.load(
  1319. build_config_details(
  1320. {
  1321. 'web': {'build': '.', 'command': [1]}
  1322. },
  1323. 'tests/fixtures/extends',
  1324. 'filename.yml'
  1325. )
  1326. )
  1327. assert "web.command contains 1, which is an invalid type, it should be a string" \
  1328. in excinfo.exconly()
  1329. def test_load_config_dockerfile_without_build_raises_error_v1(self):
  1330. with pytest.raises(ConfigurationError) as exc:
  1331. config.load(build_config_details({
  1332. 'web': {
  1333. 'image': 'busybox',
  1334. 'dockerfile': 'Dockerfile.alt'
  1335. }
  1336. }))
  1337. assert "web has both an image and alternate Dockerfile." in exc.exconly()
  1338. def test_config_extra_hosts_string_raises_validation_error(self):
  1339. with pytest.raises(ConfigurationError) as excinfo:
  1340. config.load(
  1341. build_config_details(
  1342. {'web': {
  1343. 'image': 'busybox',
  1344. 'extra_hosts': 'somehost:162.242.195.82'
  1345. }},
  1346. 'working_dir',
  1347. 'filename.yml'
  1348. )
  1349. )
  1350. assert "web.extra_hosts contains an invalid type" \
  1351. in excinfo.exconly()
  1352. def test_config_extra_hosts_list_of_dicts_validation_error(self):
  1353. with pytest.raises(ConfigurationError) as excinfo:
  1354. config.load(
  1355. build_config_details(
  1356. {'web': {
  1357. 'image': 'busybox',
  1358. 'extra_hosts': [
  1359. {'somehost': '162.242.195.82'},
  1360. {'otherhost': '50.31.209.229'}
  1361. ]
  1362. }},
  1363. 'working_dir',
  1364. 'filename.yml'
  1365. )
  1366. )
  1367. assert "web.extra_hosts contains {\"somehost\": \"162.242.195.82\"}, " \
  1368. "which is an invalid type, it should be a string" \
  1369. in excinfo.exconly()
  1370. def test_config_ulimits_invalid_keys_validation_error(self):
  1371. with pytest.raises(ConfigurationError) as exc:
  1372. config.load(build_config_details(
  1373. {
  1374. 'web': {
  1375. 'image': 'busybox',
  1376. 'ulimits': {
  1377. 'nofile': {
  1378. "not_soft_or_hard": 100,
  1379. "soft": 10000,
  1380. "hard": 20000,
  1381. }
  1382. }
  1383. }
  1384. },
  1385. 'working_dir',
  1386. 'filename.yml'))
  1387. assert "web.ulimits.nofile contains unsupported option: 'not_soft_or_hard'" \
  1388. in exc.exconly()
  1389. def test_config_ulimits_required_keys_validation_error(self):
  1390. with pytest.raises(ConfigurationError) as exc:
  1391. config.load(build_config_details(
  1392. {
  1393. 'web': {
  1394. 'image': 'busybox',
  1395. 'ulimits': {'nofile': {"soft": 10000}}
  1396. }
  1397. },
  1398. 'working_dir',
  1399. 'filename.yml'))
  1400. assert "web.ulimits.nofile" in exc.exconly()
  1401. assert "'hard' is a required property" in exc.exconly()
  1402. def test_config_ulimits_soft_greater_than_hard_error(self):
  1403. expected = "'soft' value can not be greater than 'hard' value"
  1404. with pytest.raises(ConfigurationError) as exc:
  1405. config.load(build_config_details(
  1406. {
  1407. 'web': {
  1408. 'image': 'busybox',
  1409. 'ulimits': {
  1410. 'nofile': {"soft": 10000, "hard": 1000}
  1411. }
  1412. }
  1413. },
  1414. 'working_dir',
  1415. 'filename.yml'))
  1416. assert expected in exc.exconly()
  1417. def test_valid_config_which_allows_two_type_definitions(self):
  1418. expose_values = [["8000"], [8000]]
  1419. for expose in expose_values:
  1420. service = config.load(
  1421. build_config_details(
  1422. {'web': {
  1423. 'image': 'busybox',
  1424. 'expose': expose
  1425. }},
  1426. 'working_dir',
  1427. 'filename.yml'
  1428. )
  1429. ).services
  1430. self.assertEqual(service[0]['expose'], expose)
  1431. def test_valid_config_oneof_string_or_list(self):
  1432. entrypoint_values = [["sh"], "sh"]
  1433. for entrypoint in entrypoint_values:
  1434. service = config.load(
  1435. build_config_details(
  1436. {'web': {
  1437. 'image': 'busybox',
  1438. 'entrypoint': entrypoint
  1439. }},
  1440. 'working_dir',
  1441. 'filename.yml'
  1442. )
  1443. ).services
  1444. self.assertEqual(service[0]['entrypoint'], entrypoint)
  1445. def test_logs_warning_for_boolean_in_environment(self):
  1446. config_details = build_config_details({
  1447. 'web': {
  1448. 'image': 'busybox',
  1449. 'environment': {'SHOW_STUFF': True}
  1450. }
  1451. })
  1452. with pytest.raises(ConfigurationError) as exc:
  1453. config.load(config_details)
  1454. assert "contains true, which is an invalid type" in exc.exconly()
  1455. def test_config_valid_environment_dict_key_contains_dashes(self):
  1456. services = config.load(
  1457. build_config_details(
  1458. {'web': {
  1459. 'image': 'busybox',
  1460. 'environment': {'SPRING_JPA_HIBERNATE_DDL-AUTO': 'none'}
  1461. }},
  1462. 'working_dir',
  1463. 'filename.yml'
  1464. )
  1465. ).services
  1466. self.assertEqual(services[0]['environment']['SPRING_JPA_HIBERNATE_DDL-AUTO'], 'none')
  1467. def test_load_yaml_with_yaml_error(self):
  1468. tmpdir = py.test.ensuretemp('invalid_yaml_test')
  1469. self.addCleanup(tmpdir.remove)
  1470. invalid_yaml_file = tmpdir.join('docker-compose.yml')
  1471. invalid_yaml_file.write("""
  1472. web:
  1473. this is bogus: ok: what
  1474. """)
  1475. with pytest.raises(ConfigurationError) as exc:
  1476. config.load_yaml(str(invalid_yaml_file))
  1477. assert 'line 3, column 32' in exc.exconly()
  1478. def test_validate_extra_hosts_invalid(self):
  1479. with pytest.raises(ConfigurationError) as exc:
  1480. config.load(build_config_details({
  1481. 'web': {
  1482. 'image': 'alpine',
  1483. 'extra_hosts': "www.example.com: 192.168.0.17",
  1484. }
  1485. }))
  1486. assert "web.extra_hosts contains an invalid type" in exc.exconly()
  1487. def test_validate_extra_hosts_invalid_list(self):
  1488. with pytest.raises(ConfigurationError) as exc:
  1489. config.load(build_config_details({
  1490. 'web': {
  1491. 'image': 'alpine',
  1492. 'extra_hosts': [
  1493. {'www.example.com': '192.168.0.17'},
  1494. {'api.example.com': '192.168.0.18'}
  1495. ],
  1496. }
  1497. }))
  1498. assert "which is an invalid type" in exc.exconly()
  1499. def test_normalize_dns_options(self):
  1500. actual = config.load(build_config_details({
  1501. 'web': {
  1502. 'image': 'alpine',
  1503. 'dns': '8.8.8.8',
  1504. 'dns_search': 'domain.local',
  1505. }
  1506. }))
  1507. assert actual.services == [
  1508. {
  1509. 'name': 'web',
  1510. 'image': 'alpine',
  1511. 'dns': ['8.8.8.8'],
  1512. 'dns_search': ['domain.local'],
  1513. }
  1514. ]
  1515. def test_tmpfs_option(self):
  1516. actual = config.load(build_config_details({
  1517. 'version': '2',
  1518. 'services': {
  1519. 'web': {
  1520. 'image': 'alpine',
  1521. 'tmpfs': '/run',
  1522. }
  1523. }
  1524. }))
  1525. assert actual.services == [
  1526. {
  1527. 'name': 'web',
  1528. 'image': 'alpine',
  1529. 'tmpfs': ['/run'],
  1530. }
  1531. ]
  1532. def test_oom_score_adj_option(self):
  1533. actual = config.load(build_config_details({
  1534. 'version': '2',
  1535. 'services': {
  1536. 'web': {
  1537. 'image': 'alpine',
  1538. 'oom_score_adj': 500
  1539. }
  1540. }
  1541. }))
  1542. assert actual.services == [
  1543. {
  1544. 'name': 'web',
  1545. 'image': 'alpine',
  1546. 'oom_score_adj': 500
  1547. }
  1548. ]
  1549. def test_swappiness_option(self):
  1550. actual = config.load(build_config_details({
  1551. 'version': '2',
  1552. 'services': {
  1553. 'web': {
  1554. 'image': 'alpine',
  1555. 'mem_swappiness': 10,
  1556. }
  1557. }
  1558. }))
  1559. assert actual.services == [
  1560. {
  1561. 'name': 'web',
  1562. 'image': 'alpine',
  1563. 'mem_swappiness': 10,
  1564. }
  1565. ]
  1566. def test_group_add_option(self):
  1567. actual = config.load(build_config_details({
  1568. 'version': '2',
  1569. 'services': {
  1570. 'web': {
  1571. 'image': 'alpine',
  1572. 'group_add': ["docker", 777]
  1573. }
  1574. }
  1575. }))
  1576. assert actual.services == [
  1577. {
  1578. 'name': 'web',
  1579. 'image': 'alpine',
  1580. 'group_add': ["docker", 777]
  1581. }
  1582. ]
  1583. def test_dns_opt_option(self):
  1584. actual = config.load(build_config_details({
  1585. 'version': '2',
  1586. 'services': {
  1587. 'web': {
  1588. 'image': 'alpine',
  1589. 'dns_opt': ["use-vc", "no-tld-query"]
  1590. }
  1591. }
  1592. }))
  1593. assert actual.services == [
  1594. {
  1595. 'name': 'web',
  1596. 'image': 'alpine',
  1597. 'dns_opt': ["use-vc", "no-tld-query"]
  1598. }
  1599. ]
  1600. def test_isolation_option(self):
  1601. actual = config.load(build_config_details({
  1602. 'version': str(V2_1),
  1603. 'services': {
  1604. 'web': {
  1605. 'image': 'win10',
  1606. 'isolation': 'hyperv'
  1607. }
  1608. }
  1609. }))
  1610. assert actual.services == [
  1611. {
  1612. 'name': 'web',
  1613. 'image': 'win10',
  1614. 'isolation': 'hyperv',
  1615. }
  1616. ]
  1617. def test_runtime_option(self):
  1618. actual = config.load(build_config_details({
  1619. 'version': str(V2_3),
  1620. 'services': {
  1621. 'web': {
  1622. 'image': 'nvidia/cuda',
  1623. 'runtime': 'nvidia'
  1624. }
  1625. }
  1626. }))
  1627. assert actual.services == [
  1628. {
  1629. 'name': 'web',
  1630. 'image': 'nvidia/cuda',
  1631. 'runtime': 'nvidia',
  1632. }
  1633. ]
  1634. def test_merge_service_dicts_from_files_with_extends_in_base(self):
  1635. base = {
  1636. 'volumes': ['.:/app'],
  1637. 'extends': {'service': 'app'}
  1638. }
  1639. override = {
  1640. 'image': 'alpine:edge',
  1641. }
  1642. actual = config.merge_service_dicts_from_files(
  1643. base,
  1644. override,
  1645. DEFAULT_VERSION)
  1646. assert actual == {
  1647. 'image': 'alpine:edge',
  1648. 'volumes': ['.:/app'],
  1649. 'extends': {'service': 'app'}
  1650. }
  1651. def test_merge_service_dicts_from_files_with_extends_in_override(self):
  1652. base = {
  1653. 'volumes': ['.:/app'],
  1654. 'extends': {'service': 'app'}
  1655. }
  1656. override = {
  1657. 'image': 'alpine:edge',
  1658. 'extends': {'service': 'foo'}
  1659. }
  1660. actual = config.merge_service_dicts_from_files(
  1661. base,
  1662. override,
  1663. DEFAULT_VERSION)
  1664. assert actual == {
  1665. 'image': 'alpine:edge',
  1666. 'volumes': ['.:/app'],
  1667. 'extends': {'service': 'foo'}
  1668. }
  1669. def test_merge_service_dicts_heterogeneous(self):
  1670. base = {
  1671. 'volumes': ['.:/app'],
  1672. 'ports': ['5432']
  1673. }
  1674. override = {
  1675. 'image': 'alpine:edge',
  1676. 'ports': [5432]
  1677. }
  1678. actual = config.merge_service_dicts_from_files(
  1679. base,
  1680. override,
  1681. DEFAULT_VERSION)
  1682. assert actual == {
  1683. 'image': 'alpine:edge',
  1684. 'volumes': ['.:/app'],
  1685. 'ports': types.ServicePort.parse('5432')
  1686. }
  1687. def test_merge_service_dicts_heterogeneous_2(self):
  1688. base = {
  1689. 'volumes': ['.:/app'],
  1690. 'ports': [5432]
  1691. }
  1692. override = {
  1693. 'image': 'alpine:edge',
  1694. 'ports': ['5432']
  1695. }
  1696. actual = config.merge_service_dicts_from_files(
  1697. base,
  1698. override,
  1699. DEFAULT_VERSION)
  1700. assert actual == {
  1701. 'image': 'alpine:edge',
  1702. 'volumes': ['.:/app'],
  1703. 'ports': types.ServicePort.parse('5432')
  1704. }
  1705. def test_merge_service_dicts_ports_sorting(self):
  1706. base = {
  1707. 'ports': [5432]
  1708. }
  1709. override = {
  1710. 'image': 'alpine:edge',
  1711. 'ports': ['5432/udp']
  1712. }
  1713. actual = config.merge_service_dicts_from_files(
  1714. base,
  1715. override,
  1716. DEFAULT_VERSION)
  1717. assert len(actual['ports']) == 2
  1718. assert types.ServicePort.parse('5432')[0] in actual['ports']
  1719. assert types.ServicePort.parse('5432/udp')[0] in actual['ports']
  1720. def test_merge_service_dicts_heterogeneous_volumes(self):
  1721. base = {
  1722. 'volumes': ['/a:/b', '/x:/z'],
  1723. }
  1724. override = {
  1725. 'image': 'alpine:edge',
  1726. 'volumes': [
  1727. {'source': '/e', 'target': '/b', 'type': 'bind'},
  1728. {'source': '/c', 'target': '/d', 'type': 'bind'}
  1729. ]
  1730. }
  1731. actual = config.merge_service_dicts_from_files(
  1732. base, override, V3_2
  1733. )
  1734. assert actual['volumes'] == [
  1735. {'source': '/e', 'target': '/b', 'type': 'bind'},
  1736. {'source': '/c', 'target': '/d', 'type': 'bind'},
  1737. '/x:/z'
  1738. ]
  1739. def test_merge_logging_v1(self):
  1740. base = {
  1741. 'image': 'alpine:edge',
  1742. 'log_driver': 'something',
  1743. 'log_opt': {'foo': 'three'},
  1744. }
  1745. override = {
  1746. 'image': 'alpine:edge',
  1747. 'command': 'true',
  1748. }
  1749. actual = config.merge_service_dicts(base, override, V1)
  1750. assert actual == {
  1751. 'image': 'alpine:edge',
  1752. 'log_driver': 'something',
  1753. 'log_opt': {'foo': 'three'},
  1754. 'command': 'true',
  1755. }
  1756. def test_merge_logging_v2(self):
  1757. base = {
  1758. 'image': 'alpine:edge',
  1759. 'logging': {
  1760. 'driver': 'json-file',
  1761. 'options': {
  1762. 'frequency': '2000',
  1763. 'timeout': '23'
  1764. }
  1765. }
  1766. }
  1767. override = {
  1768. 'logging': {
  1769. 'options': {
  1770. 'timeout': '360',
  1771. 'pretty-print': 'on'
  1772. }
  1773. }
  1774. }
  1775. actual = config.merge_service_dicts(base, override, V2_0)
  1776. assert actual == {
  1777. 'image': 'alpine:edge',
  1778. 'logging': {
  1779. 'driver': 'json-file',
  1780. 'options': {
  1781. 'frequency': '2000',
  1782. 'timeout': '360',
  1783. 'pretty-print': 'on'
  1784. }
  1785. }
  1786. }
  1787. def test_merge_logging_v2_override_driver(self):
  1788. base = {
  1789. 'image': 'alpine:edge',
  1790. 'logging': {
  1791. 'driver': 'json-file',
  1792. 'options': {
  1793. 'frequency': '2000',
  1794. 'timeout': '23'
  1795. }
  1796. }
  1797. }
  1798. override = {
  1799. 'logging': {
  1800. 'driver': 'syslog',
  1801. 'options': {
  1802. 'timeout': '360',
  1803. 'pretty-print': 'on'
  1804. }
  1805. }
  1806. }
  1807. actual = config.merge_service_dicts(base, override, V2_0)
  1808. assert actual == {
  1809. 'image': 'alpine:edge',
  1810. 'logging': {
  1811. 'driver': 'syslog',
  1812. 'options': {
  1813. 'timeout': '360',
  1814. 'pretty-print': 'on'
  1815. }
  1816. }
  1817. }
  1818. def test_merge_logging_v2_no_base_driver(self):
  1819. base = {
  1820. 'image': 'alpine:edge',
  1821. 'logging': {
  1822. 'options': {
  1823. 'frequency': '2000',
  1824. 'timeout': '23'
  1825. }
  1826. }
  1827. }
  1828. override = {
  1829. 'logging': {
  1830. 'driver': 'json-file',
  1831. 'options': {
  1832. 'timeout': '360',
  1833. 'pretty-print': 'on'
  1834. }
  1835. }
  1836. }
  1837. actual = config.merge_service_dicts(base, override, V2_0)
  1838. assert actual == {
  1839. 'image': 'alpine:edge',
  1840. 'logging': {
  1841. 'driver': 'json-file',
  1842. 'options': {
  1843. 'frequency': '2000',
  1844. 'timeout': '360',
  1845. 'pretty-print': 'on'
  1846. }
  1847. }
  1848. }
  1849. def test_merge_logging_v2_no_drivers(self):
  1850. base = {
  1851. 'image': 'alpine:edge',
  1852. 'logging': {
  1853. 'options': {
  1854. 'frequency': '2000',
  1855. 'timeout': '23'
  1856. }
  1857. }
  1858. }
  1859. override = {
  1860. 'logging': {
  1861. 'options': {
  1862. 'timeout': '360',
  1863. 'pretty-print': 'on'
  1864. }
  1865. }
  1866. }
  1867. actual = config.merge_service_dicts(base, override, V2_0)
  1868. assert actual == {
  1869. 'image': 'alpine:edge',
  1870. 'logging': {
  1871. 'options': {
  1872. 'frequency': '2000',
  1873. 'timeout': '360',
  1874. 'pretty-print': 'on'
  1875. }
  1876. }
  1877. }
  1878. def test_merge_logging_v2_no_override_options(self):
  1879. base = {
  1880. 'image': 'alpine:edge',
  1881. 'logging': {
  1882. 'driver': 'json-file',
  1883. 'options': {
  1884. 'frequency': '2000',
  1885. 'timeout': '23'
  1886. }
  1887. }
  1888. }
  1889. override = {
  1890. 'logging': {
  1891. 'driver': 'syslog'
  1892. }
  1893. }
  1894. actual = config.merge_service_dicts(base, override, V2_0)
  1895. assert actual == {
  1896. 'image': 'alpine:edge',
  1897. 'logging': {
  1898. 'driver': 'syslog',
  1899. }
  1900. }
  1901. def test_merge_logging_v2_no_base(self):
  1902. base = {
  1903. 'image': 'alpine:edge'
  1904. }
  1905. override = {
  1906. 'logging': {
  1907. 'driver': 'json-file',
  1908. 'options': {
  1909. 'frequency': '2000'
  1910. }
  1911. }
  1912. }
  1913. actual = config.merge_service_dicts(base, override, V2_0)
  1914. assert actual == {
  1915. 'image': 'alpine:edge',
  1916. 'logging': {
  1917. 'driver': 'json-file',
  1918. 'options': {
  1919. 'frequency': '2000'
  1920. }
  1921. }
  1922. }
  1923. def test_merge_logging_v2_no_override(self):
  1924. base = {
  1925. 'image': 'alpine:edge',
  1926. 'logging': {
  1927. 'driver': 'syslog',
  1928. 'options': {
  1929. 'frequency': '2000'
  1930. }
  1931. }
  1932. }
  1933. override = {}
  1934. actual = config.merge_service_dicts(base, override, V2_0)
  1935. assert actual == {
  1936. 'image': 'alpine:edge',
  1937. 'logging': {
  1938. 'driver': 'syslog',
  1939. 'options': {
  1940. 'frequency': '2000'
  1941. }
  1942. }
  1943. }
  1944. def test_merge_mixed_ports(self):
  1945. base = {
  1946. 'image': 'busybox:latest',
  1947. 'command': 'top',
  1948. 'ports': [
  1949. {
  1950. 'target': '1245',
  1951. 'published': '1245',
  1952. 'protocol': 'udp',
  1953. }
  1954. ]
  1955. }
  1956. override = {
  1957. 'ports': ['1245:1245/udp']
  1958. }
  1959. actual = config.merge_service_dicts(base, override, V3_1)
  1960. assert actual == {
  1961. 'image': 'busybox:latest',
  1962. 'command': 'top',
  1963. 'ports': [types.ServicePort('1245', '1245', 'udp', None, None)]
  1964. }
  1965. def test_merge_depends_on_no_override(self):
  1966. base = {
  1967. 'image': 'busybox',
  1968. 'depends_on': {
  1969. 'app1': {'condition': 'service_started'},
  1970. 'app2': {'condition': 'service_healthy'}
  1971. }
  1972. }
  1973. override = {}
  1974. actual = config.merge_service_dicts(base, override, V2_1)
  1975. assert actual == base
  1976. def test_merge_depends_on_mixed_syntax(self):
  1977. base = {
  1978. 'image': 'busybox',
  1979. 'depends_on': {
  1980. 'app1': {'condition': 'service_started'},
  1981. 'app2': {'condition': 'service_healthy'}
  1982. }
  1983. }
  1984. override = {
  1985. 'depends_on': ['app3']
  1986. }
  1987. actual = config.merge_service_dicts(base, override, V2_1)
  1988. assert actual == {
  1989. 'image': 'busybox',
  1990. 'depends_on': {
  1991. 'app1': {'condition': 'service_started'},
  1992. 'app2': {'condition': 'service_healthy'},
  1993. 'app3': {'condition': 'service_started'}
  1994. }
  1995. }
  1996. def test_empty_environment_key_allowed(self):
  1997. service_dict = config.load(
  1998. build_config_details(
  1999. {
  2000. 'web': {
  2001. 'build': '.',
  2002. 'environment': {
  2003. 'POSTGRES_PASSWORD': ''
  2004. },
  2005. },
  2006. },
  2007. '.',
  2008. None,
  2009. )
  2010. ).services[0]
  2011. self.assertEqual(service_dict['environment']['POSTGRES_PASSWORD'], '')
  2012. def test_merge_pid(self):
  2013. # Regression: https://github.com/docker/compose/issues/4184
  2014. base = {
  2015. 'image': 'busybox',
  2016. 'pid': 'host'
  2017. }
  2018. override = {
  2019. 'labels': {'com.docker.compose.test': 'yes'}
  2020. }
  2021. actual = config.merge_service_dicts(base, override, V2_0)
  2022. assert actual == {
  2023. 'image': 'busybox',
  2024. 'pid': 'host',
  2025. 'labels': {'com.docker.compose.test': 'yes'}
  2026. }
  2027. def test_merge_different_secrets(self):
  2028. base = {
  2029. 'image': 'busybox',
  2030. 'secrets': [
  2031. {'source': 'src.txt'}
  2032. ]
  2033. }
  2034. override = {'secrets': ['other-src.txt']}
  2035. actual = config.merge_service_dicts(base, override, V3_1)
  2036. assert secret_sort(actual['secrets']) == secret_sort([
  2037. {'source': 'src.txt'},
  2038. {'source': 'other-src.txt'}
  2039. ])
  2040. def test_merge_secrets_override(self):
  2041. base = {
  2042. 'image': 'busybox',
  2043. 'secrets': ['src.txt'],
  2044. }
  2045. override = {
  2046. 'secrets': [
  2047. {
  2048. 'source': 'src.txt',
  2049. 'target': 'data.txt',
  2050. 'mode': 0o400
  2051. }
  2052. ]
  2053. }
  2054. actual = config.merge_service_dicts(base, override, V3_1)
  2055. assert actual['secrets'] == override['secrets']
  2056. def test_merge_different_configs(self):
  2057. base = {
  2058. 'image': 'busybox',
  2059. 'configs': [
  2060. {'source': 'src.txt'}
  2061. ]
  2062. }
  2063. override = {'configs': ['other-src.txt']}
  2064. actual = config.merge_service_dicts(base, override, V3_3)
  2065. assert secret_sort(actual['configs']) == secret_sort([
  2066. {'source': 'src.txt'},
  2067. {'source': 'other-src.txt'}
  2068. ])
  2069. def test_merge_configs_override(self):
  2070. base = {
  2071. 'image': 'busybox',
  2072. 'configs': ['src.txt'],
  2073. }
  2074. override = {
  2075. 'configs': [
  2076. {
  2077. 'source': 'src.txt',
  2078. 'target': 'data.txt',
  2079. 'mode': 0o400
  2080. }
  2081. ]
  2082. }
  2083. actual = config.merge_service_dicts(base, override, V3_3)
  2084. assert actual['configs'] == override['configs']
  2085. def test_merge_deploy(self):
  2086. base = {
  2087. 'image': 'busybox',
  2088. }
  2089. override = {
  2090. 'deploy': {
  2091. 'mode': 'global',
  2092. 'restart_policy': {
  2093. 'condition': 'on-failure'
  2094. }
  2095. }
  2096. }
  2097. actual = config.merge_service_dicts(base, override, V3_0)
  2098. assert actual['deploy'] == override['deploy']
  2099. def test_merge_deploy_override(self):
  2100. base = {
  2101. 'image': 'busybox',
  2102. 'deploy': {
  2103. 'mode': 'global',
  2104. 'restart_policy': {
  2105. 'condition': 'on-failure'
  2106. },
  2107. 'placement': {
  2108. 'constraints': [
  2109. 'node.role == manager'
  2110. ]
  2111. }
  2112. }
  2113. }
  2114. override = {
  2115. 'deploy': {
  2116. 'mode': 'replicated',
  2117. 'restart_policy': {
  2118. 'condition': 'any'
  2119. }
  2120. }
  2121. }
  2122. actual = config.merge_service_dicts(base, override, V3_0)
  2123. assert actual['deploy'] == {
  2124. 'mode': 'replicated',
  2125. 'restart_policy': {
  2126. 'condition': 'any'
  2127. },
  2128. 'placement': {
  2129. 'constraints': [
  2130. 'node.role == manager'
  2131. ]
  2132. }
  2133. }
  2134. def test_merge_credential_spec(self):
  2135. base = {
  2136. 'image': 'bb',
  2137. 'credential_spec': {
  2138. 'file': '/hello-world',
  2139. }
  2140. }
  2141. override = {
  2142. 'credential_spec': {
  2143. 'registry': 'revolution.com',
  2144. }
  2145. }
  2146. actual = config.merge_service_dicts(base, override, V3_3)
  2147. assert actual['credential_spec'] == override['credential_spec']
  2148. def test_merge_scale(self):
  2149. base = {
  2150. 'image': 'bar',
  2151. 'scale': 2,
  2152. }
  2153. override = {
  2154. 'scale': 4,
  2155. }
  2156. actual = config.merge_service_dicts(base, override, V2_2)
  2157. assert actual == {'image': 'bar', 'scale': 4}
  2158. def test_merge_blkio_config(self):
  2159. base = {
  2160. 'image': 'bar',
  2161. 'blkio_config': {
  2162. 'weight': 300,
  2163. 'weight_device': [
  2164. {'path': '/dev/sda1', 'weight': 200}
  2165. ],
  2166. 'device_read_iops': [
  2167. {'path': '/dev/sda1', 'rate': 300}
  2168. ],
  2169. 'device_write_iops': [
  2170. {'path': '/dev/sda1', 'rate': 1000}
  2171. ]
  2172. }
  2173. }
  2174. override = {
  2175. 'blkio_config': {
  2176. 'weight': 450,
  2177. 'weight_device': [
  2178. {'path': '/dev/sda2', 'weight': 400}
  2179. ],
  2180. 'device_read_iops': [
  2181. {'path': '/dev/sda1', 'rate': 2000}
  2182. ],
  2183. 'device_read_bps': [
  2184. {'path': '/dev/sda1', 'rate': 1024}
  2185. ]
  2186. }
  2187. }
  2188. actual = config.merge_service_dicts(base, override, V2_2)
  2189. assert actual == {
  2190. 'image': 'bar',
  2191. 'blkio_config': {
  2192. 'weight': override['blkio_config']['weight'],
  2193. 'weight_device': (
  2194. base['blkio_config']['weight_device'] +
  2195. override['blkio_config']['weight_device']
  2196. ),
  2197. 'device_read_iops': override['blkio_config']['device_read_iops'],
  2198. 'device_read_bps': override['blkio_config']['device_read_bps'],
  2199. 'device_write_iops': base['blkio_config']['device_write_iops']
  2200. }
  2201. }
  2202. def test_merge_extra_hosts(self):
  2203. base = {
  2204. 'image': 'bar',
  2205. 'extra_hosts': {
  2206. 'foo': '1.2.3.4',
  2207. }
  2208. }
  2209. override = {
  2210. 'extra_hosts': ['bar:5.6.7.8', 'foo:127.0.0.1']
  2211. }
  2212. actual = config.merge_service_dicts(base, override, V2_0)
  2213. assert actual['extra_hosts'] == {
  2214. 'foo': '127.0.0.1',
  2215. 'bar': '5.6.7.8',
  2216. }
  2217. def test_merge_healthcheck_config(self):
  2218. base = {
  2219. 'image': 'bar',
  2220. 'healthcheck': {
  2221. 'start_period': 1000,
  2222. 'interval': 3000,
  2223. 'test': ['true']
  2224. }
  2225. }
  2226. override = {
  2227. 'healthcheck': {
  2228. 'interval': 5000,
  2229. 'timeout': 10000,
  2230. 'test': ['echo', 'OK'],
  2231. }
  2232. }
  2233. actual = config.merge_service_dicts(base, override, V2_3)
  2234. assert actual['healthcheck'] == {
  2235. 'start_period': base['healthcheck']['start_period'],
  2236. 'test': override['healthcheck']['test'],
  2237. 'interval': override['healthcheck']['interval'],
  2238. 'timeout': override['healthcheck']['timeout'],
  2239. }
  2240. def test_merge_healthcheck_override_disables(self):
  2241. base = {
  2242. 'image': 'bar',
  2243. 'healthcheck': {
  2244. 'start_period': 1000,
  2245. 'interval': 3000,
  2246. 'timeout': 2000,
  2247. 'retries': 3,
  2248. 'test': ['true']
  2249. }
  2250. }
  2251. override = {
  2252. 'healthcheck': {
  2253. 'disabled': True
  2254. }
  2255. }
  2256. actual = config.merge_service_dicts(base, override, V2_3)
  2257. assert actual['healthcheck'] == {'disabled': True}
  2258. def test_merge_healthcheck_override_enables(self):
  2259. base = {
  2260. 'image': 'bar',
  2261. 'healthcheck': {
  2262. 'disabled': True
  2263. }
  2264. }
  2265. override = {
  2266. 'healthcheck': {
  2267. 'disabled': False,
  2268. 'start_period': 1000,
  2269. 'interval': 3000,
  2270. 'timeout': 2000,
  2271. 'retries': 3,
  2272. 'test': ['true']
  2273. }
  2274. }
  2275. actual = config.merge_service_dicts(base, override, V2_3)
  2276. assert actual['healthcheck'] == override['healthcheck']
  2277. def test_external_volume_config(self):
  2278. config_details = build_config_details({
  2279. 'version': '2',
  2280. 'services': {
  2281. 'bogus': {'image': 'busybox'}
  2282. },
  2283. 'volumes': {
  2284. 'ext': {'external': True},
  2285. 'ext2': {'external': {'name': 'aliased'}}
  2286. }
  2287. })
  2288. config_result = config.load(config_details)
  2289. volumes = config_result.volumes
  2290. assert 'ext' in volumes
  2291. assert volumes['ext']['external'] is True
  2292. assert 'ext2' in volumes
  2293. assert volumes['ext2']['external']['name'] == 'aliased'
  2294. def test_external_volume_invalid_config(self):
  2295. config_details = build_config_details({
  2296. 'version': '2',
  2297. 'services': {
  2298. 'bogus': {'image': 'busybox'}
  2299. },
  2300. 'volumes': {
  2301. 'ext': {'external': True, 'driver': 'foo'}
  2302. }
  2303. })
  2304. with pytest.raises(ConfigurationError):
  2305. config.load(config_details)
  2306. def test_depends_on_orders_services(self):
  2307. config_details = build_config_details({
  2308. 'version': '2',
  2309. 'services': {
  2310. 'one': {'image': 'busybox', 'depends_on': ['three', 'two']},
  2311. 'two': {'image': 'busybox', 'depends_on': ['three']},
  2312. 'three': {'image': 'busybox'},
  2313. },
  2314. })
  2315. actual = config.load(config_details)
  2316. assert (
  2317. [service['name'] for service in actual.services] ==
  2318. ['three', 'two', 'one']
  2319. )
  2320. def test_depends_on_unknown_service_errors(self):
  2321. config_details = build_config_details({
  2322. 'version': '2',
  2323. 'services': {
  2324. 'one': {'image': 'busybox', 'depends_on': ['three']},
  2325. },
  2326. })
  2327. with pytest.raises(ConfigurationError) as exc:
  2328. config.load(config_details)
  2329. assert "Service 'one' depends on service 'three'" in exc.exconly()
  2330. def test_linked_service_is_undefined(self):
  2331. with self.assertRaises(ConfigurationError):
  2332. config.load(
  2333. build_config_details({
  2334. 'version': '2',
  2335. 'services': {
  2336. 'web': {'image': 'busybox', 'links': ['db:db']},
  2337. },
  2338. })
  2339. )
  2340. def test_load_dockerfile_without_context(self):
  2341. config_details = build_config_details({
  2342. 'version': '2',
  2343. 'services': {
  2344. 'one': {'build': {'dockerfile': 'Dockerfile.foo'}},
  2345. },
  2346. })
  2347. with pytest.raises(ConfigurationError) as exc:
  2348. config.load(config_details)
  2349. assert 'has neither an image nor a build context' in exc.exconly()
  2350. def test_load_secrets(self):
  2351. base_file = config.ConfigFile(
  2352. 'base.yaml',
  2353. {
  2354. 'version': '3.1',
  2355. 'services': {
  2356. 'web': {
  2357. 'image': 'example/web',
  2358. 'secrets': [
  2359. 'one',
  2360. {
  2361. 'source': 'source',
  2362. 'target': 'target',
  2363. 'uid': '100',
  2364. 'gid': '200',
  2365. 'mode': 0o777,
  2366. },
  2367. ],
  2368. },
  2369. },
  2370. 'secrets': {
  2371. 'one': {'file': 'secret.txt'},
  2372. },
  2373. })
  2374. details = config.ConfigDetails('.', [base_file])
  2375. service_dicts = config.load(details).services
  2376. expected = [
  2377. {
  2378. 'name': 'web',
  2379. 'image': 'example/web',
  2380. 'secrets': [
  2381. types.ServiceSecret('one', None, None, None, None, None),
  2382. types.ServiceSecret('source', 'target', '100', '200', 0o777, None),
  2383. ],
  2384. },
  2385. ]
  2386. assert service_sort(service_dicts) == service_sort(expected)
  2387. def test_load_secrets_multi_file(self):
  2388. base_file = config.ConfigFile(
  2389. 'base.yaml',
  2390. {
  2391. 'version': '3.1',
  2392. 'services': {
  2393. 'web': {
  2394. 'image': 'example/web',
  2395. 'secrets': ['one'],
  2396. },
  2397. },
  2398. 'secrets': {
  2399. 'one': {'file': 'secret.txt'},
  2400. },
  2401. })
  2402. override_file = config.ConfigFile(
  2403. 'base.yaml',
  2404. {
  2405. 'version': '3.1',
  2406. 'services': {
  2407. 'web': {
  2408. 'secrets': [
  2409. {
  2410. 'source': 'source',
  2411. 'target': 'target',
  2412. 'uid': '100',
  2413. 'gid': '200',
  2414. 'mode': 0o777,
  2415. },
  2416. ],
  2417. },
  2418. },
  2419. })
  2420. details = config.ConfigDetails('.', [base_file, override_file])
  2421. service_dicts = config.load(details).services
  2422. expected = [
  2423. {
  2424. 'name': 'web',
  2425. 'image': 'example/web',
  2426. 'secrets': [
  2427. types.ServiceSecret('one', None, None, None, None, None),
  2428. types.ServiceSecret('source', 'target', '100', '200', 0o777, None),
  2429. ],
  2430. },
  2431. ]
  2432. assert service_sort(service_dicts) == service_sort(expected)
  2433. def test_load_configs(self):
  2434. base_file = config.ConfigFile(
  2435. 'base.yaml',
  2436. {
  2437. 'version': '3.3',
  2438. 'services': {
  2439. 'web': {
  2440. 'image': 'example/web',
  2441. 'configs': [
  2442. 'one',
  2443. {
  2444. 'source': 'source',
  2445. 'target': 'target',
  2446. 'uid': '100',
  2447. 'gid': '200',
  2448. 'mode': 0o777,
  2449. },
  2450. ],
  2451. },
  2452. },
  2453. 'configs': {
  2454. 'one': {'file': 'secret.txt'},
  2455. },
  2456. })
  2457. details = config.ConfigDetails('.', [base_file])
  2458. service_dicts = config.load(details).services
  2459. expected = [
  2460. {
  2461. 'name': 'web',
  2462. 'image': 'example/web',
  2463. 'configs': [
  2464. types.ServiceConfig('one', None, None, None, None, None),
  2465. types.ServiceConfig('source', 'target', '100', '200', 0o777, None),
  2466. ],
  2467. },
  2468. ]
  2469. assert service_sort(service_dicts) == service_sort(expected)
  2470. def test_load_configs_multi_file(self):
  2471. base_file = config.ConfigFile(
  2472. 'base.yaml',
  2473. {
  2474. 'version': '3.3',
  2475. 'services': {
  2476. 'web': {
  2477. 'image': 'example/web',
  2478. 'configs': ['one'],
  2479. },
  2480. },
  2481. 'configs': {
  2482. 'one': {'file': 'secret.txt'},
  2483. },
  2484. })
  2485. override_file = config.ConfigFile(
  2486. 'base.yaml',
  2487. {
  2488. 'version': '3.3',
  2489. 'services': {
  2490. 'web': {
  2491. 'configs': [
  2492. {
  2493. 'source': 'source',
  2494. 'target': 'target',
  2495. 'uid': '100',
  2496. 'gid': '200',
  2497. 'mode': 0o777,
  2498. },
  2499. ],
  2500. },
  2501. },
  2502. })
  2503. details = config.ConfigDetails('.', [base_file, override_file])
  2504. service_dicts = config.load(details).services
  2505. expected = [
  2506. {
  2507. 'name': 'web',
  2508. 'image': 'example/web',
  2509. 'configs': [
  2510. types.ServiceConfig('one', None, None, None, None, None),
  2511. types.ServiceConfig('source', 'target', '100', '200', 0o777, None),
  2512. ],
  2513. },
  2514. ]
  2515. assert service_sort(service_dicts) == service_sort(expected)
  2516. def test_service_volume_invalid_config(self):
  2517. config_details = build_config_details(
  2518. {
  2519. 'version': '3.2',
  2520. 'services': {
  2521. 'web': {
  2522. 'build': {
  2523. 'context': '.',
  2524. 'args': None,
  2525. },
  2526. 'volumes': [
  2527. {
  2528. "type": "volume",
  2529. "source": "/data",
  2530. "garbage": {
  2531. "and": "error"
  2532. }
  2533. }
  2534. ]
  2535. },
  2536. },
  2537. }
  2538. )
  2539. with pytest.raises(ConfigurationError) as exc:
  2540. config.load(config_details)
  2541. assert "services.web.volumes contains unsupported option: 'garbage'" in exc.exconly()
  2542. class NetworkModeTest(unittest.TestCase):
  2543. def test_network_mode_standard(self):
  2544. config_data = config.load(build_config_details({
  2545. 'version': '2',
  2546. 'services': {
  2547. 'web': {
  2548. 'image': 'busybox',
  2549. 'command': "top",
  2550. 'network_mode': 'bridge',
  2551. },
  2552. },
  2553. }))
  2554. assert config_data.services[0]['network_mode'] == 'bridge'
  2555. def test_network_mode_standard_v1(self):
  2556. config_data = config.load(build_config_details({
  2557. 'web': {
  2558. 'image': 'busybox',
  2559. 'command': "top",
  2560. 'net': 'bridge',
  2561. },
  2562. }))
  2563. assert config_data.services[0]['network_mode'] == 'bridge'
  2564. assert 'net' not in config_data.services[0]
  2565. def test_network_mode_container(self):
  2566. config_data = config.load(build_config_details({
  2567. 'version': '2',
  2568. 'services': {
  2569. 'web': {
  2570. 'image': 'busybox',
  2571. 'command': "top",
  2572. 'network_mode': 'container:foo',
  2573. },
  2574. },
  2575. }))
  2576. assert config_data.services[0]['network_mode'] == 'container:foo'
  2577. def test_network_mode_container_v1(self):
  2578. config_data = config.load(build_config_details({
  2579. 'web': {
  2580. 'image': 'busybox',
  2581. 'command': "top",
  2582. 'net': 'container:foo',
  2583. },
  2584. }))
  2585. assert config_data.services[0]['network_mode'] == 'container:foo'
  2586. def test_network_mode_service(self):
  2587. config_data = config.load(build_config_details({
  2588. 'version': '2',
  2589. 'services': {
  2590. 'web': {
  2591. 'image': 'busybox',
  2592. 'command': "top",
  2593. 'network_mode': 'service:foo',
  2594. },
  2595. 'foo': {
  2596. 'image': 'busybox',
  2597. 'command': "top",
  2598. },
  2599. },
  2600. }))
  2601. assert config_data.services[1]['network_mode'] == 'service:foo'
  2602. def test_network_mode_service_v1(self):
  2603. config_data = config.load(build_config_details({
  2604. 'web': {
  2605. 'image': 'busybox',
  2606. 'command': "top",
  2607. 'net': 'container:foo',
  2608. },
  2609. 'foo': {
  2610. 'image': 'busybox',
  2611. 'command': "top",
  2612. },
  2613. }))
  2614. assert config_data.services[1]['network_mode'] == 'service:foo'
  2615. def test_network_mode_service_nonexistent(self):
  2616. with pytest.raises(ConfigurationError) as excinfo:
  2617. config.load(build_config_details({
  2618. 'version': '2',
  2619. 'services': {
  2620. 'web': {
  2621. 'image': 'busybox',
  2622. 'command': "top",
  2623. 'network_mode': 'service:foo',
  2624. },
  2625. },
  2626. }))
  2627. assert "service 'foo' which is undefined" in excinfo.exconly()
  2628. def test_network_mode_plus_networks_is_invalid(self):
  2629. with pytest.raises(ConfigurationError) as excinfo:
  2630. config.load(build_config_details({
  2631. 'version': '2',
  2632. 'services': {
  2633. 'web': {
  2634. 'image': 'busybox',
  2635. 'command': "top",
  2636. 'network_mode': 'bridge',
  2637. 'networks': ['front'],
  2638. },
  2639. },
  2640. 'networks': {
  2641. 'front': None,
  2642. }
  2643. }))
  2644. assert "'network_mode' and 'networks' cannot be combined" in excinfo.exconly()
  2645. class PortsTest(unittest.TestCase):
  2646. INVALID_PORTS_TYPES = [
  2647. {"1": "8000"},
  2648. False,
  2649. "8000",
  2650. 8000,
  2651. ]
  2652. NON_UNIQUE_SINGLE_PORTS = [
  2653. ["8000", "8000"],
  2654. ]
  2655. INVALID_PORT_MAPPINGS = [
  2656. ["8000-8004:8000-8002"],
  2657. ["4242:4242-4244"],
  2658. ]
  2659. VALID_SINGLE_PORTS = [
  2660. ["8000"],
  2661. ["8000/tcp"],
  2662. ["8000", "9000"],
  2663. [8000],
  2664. [8000, 9000],
  2665. ]
  2666. VALID_PORT_MAPPINGS = [
  2667. ["8000:8050"],
  2668. ["49153-49154:3002-3003"],
  2669. ]
  2670. def test_config_invalid_ports_type_validation(self):
  2671. for invalid_ports in self.INVALID_PORTS_TYPES:
  2672. with pytest.raises(ConfigurationError) as exc:
  2673. self.check_config({'ports': invalid_ports})
  2674. assert "contains an invalid type" in exc.value.msg
  2675. def test_config_non_unique_ports_validation(self):
  2676. for invalid_ports in self.NON_UNIQUE_SINGLE_PORTS:
  2677. with pytest.raises(ConfigurationError) as exc:
  2678. self.check_config({'ports': invalid_ports})
  2679. assert "non-unique" in exc.value.msg
  2680. def test_config_invalid_ports_format_validation(self):
  2681. for invalid_ports in self.INVALID_PORT_MAPPINGS:
  2682. with pytest.raises(ConfigurationError) as exc:
  2683. self.check_config({'ports': invalid_ports})
  2684. assert "Port ranges don't match in length" in exc.value.msg
  2685. def test_config_valid_ports_format_validation(self):
  2686. for valid_ports in self.VALID_SINGLE_PORTS + self.VALID_PORT_MAPPINGS:
  2687. self.check_config({'ports': valid_ports})
  2688. def test_config_invalid_expose_type_validation(self):
  2689. for invalid_expose in self.INVALID_PORTS_TYPES:
  2690. with pytest.raises(ConfigurationError) as exc:
  2691. self.check_config({'expose': invalid_expose})
  2692. assert "contains an invalid type" in exc.value.msg
  2693. def test_config_non_unique_expose_validation(self):
  2694. for invalid_expose in self.NON_UNIQUE_SINGLE_PORTS:
  2695. with pytest.raises(ConfigurationError) as exc:
  2696. self.check_config({'expose': invalid_expose})
  2697. assert "non-unique" in exc.value.msg
  2698. def test_config_invalid_expose_format_validation(self):
  2699. # Valid port mappings ARE NOT valid 'expose' entries
  2700. for invalid_expose in self.INVALID_PORT_MAPPINGS + self.VALID_PORT_MAPPINGS:
  2701. with pytest.raises(ConfigurationError) as exc:
  2702. self.check_config({'expose': invalid_expose})
  2703. assert "should be of the format" in exc.value.msg
  2704. def test_config_valid_expose_format_validation(self):
  2705. # Valid single ports ARE valid 'expose' entries
  2706. for valid_expose in self.VALID_SINGLE_PORTS:
  2707. self.check_config({'expose': valid_expose})
  2708. def check_config(self, cfg):
  2709. config.load(
  2710. build_config_details({
  2711. 'version': '2.3',
  2712. 'services': {
  2713. 'web': dict(image='busybox', **cfg)
  2714. },
  2715. }, 'working_dir', 'filename.yml')
  2716. )
  2717. class SubnetTest(unittest.TestCase):
  2718. INVALID_SUBNET_TYPES = [
  2719. None,
  2720. False,
  2721. 10,
  2722. ]
  2723. INVALID_SUBNET_MAPPINGS = [
  2724. "",
  2725. "192.168.0.1/sdfsdfs",
  2726. "192.168.0.1/",
  2727. "192.168.0.1/33",
  2728. "192.168.0.1/01",
  2729. "192.168.0.1",
  2730. "fe80:0000:0000:0000:0204:61ff:fe9d:f156/sdfsdfs",
  2731. "fe80:0000:0000:0000:0204:61ff:fe9d:f156/",
  2732. "fe80:0000:0000:0000:0204:61ff:fe9d:f156/129",
  2733. "fe80:0000:0000:0000:0204:61ff:fe9d:f156/01",
  2734. "fe80:0000:0000:0000:0204:61ff:fe9d:f156",
  2735. "ge80:0000:0000:0000:0204:61ff:fe9d:f156/128",
  2736. "192.168.0.1/31/31",
  2737. ]
  2738. VALID_SUBNET_MAPPINGS = [
  2739. "192.168.0.1/0",
  2740. "192.168.0.1/32",
  2741. "fe80:0000:0000:0000:0204:61ff:fe9d:f156/0",
  2742. "fe80:0000:0000:0000:0204:61ff:fe9d:f156/128",
  2743. "1:2:3:4:5:6:7:8/0",
  2744. "1::/0",
  2745. "1:2:3:4:5:6:7::/0",
  2746. "1::8/0",
  2747. "1:2:3:4:5:6::8/0",
  2748. "::/0",
  2749. "::8/0",
  2750. "::2:3:4:5:6:7:8/0",
  2751. "fe80::7:8%eth0/0",
  2752. "fe80::7:8%1/0",
  2753. "::255.255.255.255/0",
  2754. "::ffff:255.255.255.255/0",
  2755. "::ffff:0:255.255.255.255/0",
  2756. "2001:db8:3:4::192.0.2.33/0",
  2757. "64:ff9b::192.0.2.33/0",
  2758. ]
  2759. def test_config_invalid_subnet_type_validation(self):
  2760. for invalid_subnet in self.INVALID_SUBNET_TYPES:
  2761. with pytest.raises(ConfigurationError) as exc:
  2762. self.check_config(invalid_subnet)
  2763. assert "contains an invalid type" in exc.value.msg
  2764. def test_config_invalid_subnet_format_validation(self):
  2765. for invalid_subnet in self.INVALID_SUBNET_MAPPINGS:
  2766. with pytest.raises(ConfigurationError) as exc:
  2767. self.check_config(invalid_subnet)
  2768. assert "should use the CIDR format" in exc.value.msg
  2769. def test_config_valid_subnet_format_validation(self):
  2770. for valid_subnet in self.VALID_SUBNET_MAPPINGS:
  2771. self.check_config(valid_subnet)
  2772. def check_config(self, subnet):
  2773. config.load(
  2774. build_config_details({
  2775. 'version': '3.5',
  2776. 'services': {
  2777. 'web': {
  2778. 'image': 'busybox'
  2779. }
  2780. },
  2781. 'networks': {
  2782. 'default': {
  2783. 'ipam': {
  2784. 'config': [
  2785. {
  2786. 'subnet': subnet
  2787. }
  2788. ],
  2789. 'driver': 'default'
  2790. }
  2791. }
  2792. }
  2793. })
  2794. )
  2795. class InterpolationTest(unittest.TestCase):
  2796. @mock.patch.dict(os.environ)
  2797. def test_config_file_with_environment_file(self):
  2798. project_dir = 'tests/fixtures/default-env-file'
  2799. service_dicts = config.load(
  2800. config.find(
  2801. project_dir, None, Environment.from_env_file(project_dir)
  2802. )
  2803. ).services
  2804. self.assertEqual(service_dicts[0], {
  2805. 'name': 'web',
  2806. 'image': 'alpine:latest',
  2807. 'ports': [
  2808. types.ServicePort.parse('5643')[0],
  2809. types.ServicePort.parse('9999')[0]
  2810. ],
  2811. 'command': 'true'
  2812. })
  2813. @mock.patch.dict(os.environ)
  2814. def test_config_file_with_environment_variable(self):
  2815. project_dir = 'tests/fixtures/environment-interpolation'
  2816. os.environ.update(
  2817. IMAGE="busybox",
  2818. HOST_PORT="80",
  2819. LABEL_VALUE="myvalue",
  2820. )
  2821. service_dicts = config.load(
  2822. config.find(
  2823. project_dir, None, Environment.from_env_file(project_dir)
  2824. )
  2825. ).services
  2826. self.assertEqual(service_dicts, [
  2827. {
  2828. 'name': 'web',
  2829. 'image': 'busybox',
  2830. 'ports': types.ServicePort.parse('80:8000'),
  2831. 'labels': {'mylabel': 'myvalue'},
  2832. 'hostname': 'host-',
  2833. 'command': '${ESCAPED}',
  2834. }
  2835. ])
  2836. @mock.patch.dict(os.environ)
  2837. def test_config_file_with_environment_variable_with_defaults(self):
  2838. project_dir = 'tests/fixtures/environment-interpolation-with-defaults'
  2839. os.environ.update(
  2840. IMAGE="busybox",
  2841. )
  2842. service_dicts = config.load(
  2843. config.find(
  2844. project_dir, None, Environment.from_env_file(project_dir)
  2845. )
  2846. ).services
  2847. self.assertEqual(service_dicts, [
  2848. {
  2849. 'name': 'web',
  2850. 'image': 'busybox',
  2851. 'ports': types.ServicePort.parse('80:8000'),
  2852. 'hostname': 'host-',
  2853. }
  2854. ])
  2855. @mock.patch.dict(os.environ)
  2856. def test_unset_variable_produces_warning(self):
  2857. os.environ.pop('FOO', None)
  2858. os.environ.pop('BAR', None)
  2859. config_details = build_config_details(
  2860. {
  2861. 'web': {
  2862. 'image': '${FOO}',
  2863. 'command': '${BAR}',
  2864. 'container_name': '${BAR}',
  2865. },
  2866. },
  2867. '.',
  2868. None,
  2869. )
  2870. with mock.patch('compose.config.environment.log') as log:
  2871. config.load(config_details)
  2872. self.assertEqual(2, log.warn.call_count)
  2873. warnings = sorted(args[0][0] for args in log.warn.call_args_list)
  2874. self.assertIn('BAR', warnings[0])
  2875. self.assertIn('FOO', warnings[1])
  2876. @mock.patch.dict(os.environ)
  2877. def test_invalid_interpolation(self):
  2878. with self.assertRaises(config.ConfigurationError) as cm:
  2879. config.load(
  2880. build_config_details(
  2881. {'web': {'image': '${'}},
  2882. 'working_dir',
  2883. 'filename.yml'
  2884. )
  2885. )
  2886. self.assertIn('Invalid', cm.exception.msg)
  2887. self.assertIn('for "image" option', cm.exception.msg)
  2888. self.assertIn('in service "web"', cm.exception.msg)
  2889. self.assertIn('"${"', cm.exception.msg)
  2890. @mock.patch.dict(os.environ)
  2891. def test_interpolation_secrets_section(self):
  2892. os.environ['FOO'] = 'baz.bar'
  2893. config_dict = config.load(build_config_details({
  2894. 'version': '3.1',
  2895. 'secrets': {
  2896. 'secretdata': {
  2897. 'external': {'name': '$FOO'}
  2898. }
  2899. }
  2900. }))
  2901. assert config_dict.secrets == {
  2902. 'secretdata': {
  2903. 'external': {'name': 'baz.bar'},
  2904. 'name': 'baz.bar'
  2905. }
  2906. }
  2907. @mock.patch.dict(os.environ)
  2908. def test_interpolation_configs_section(self):
  2909. os.environ['FOO'] = 'baz.bar'
  2910. config_dict = config.load(build_config_details({
  2911. 'version': '3.3',
  2912. 'configs': {
  2913. 'configdata': {
  2914. 'external': {'name': '$FOO'}
  2915. }
  2916. }
  2917. }))
  2918. assert config_dict.configs == {
  2919. 'configdata': {
  2920. 'external': {'name': 'baz.bar'},
  2921. 'name': 'baz.bar'
  2922. }
  2923. }
  2924. class VolumeConfigTest(unittest.TestCase):
  2925. def test_no_binding(self):
  2926. d = make_service_dict('foo', {'build': '.', 'volumes': ['/data']}, working_dir='.')
  2927. self.assertEqual(d['volumes'], ['/data'])
  2928. @mock.patch.dict(os.environ)
  2929. def test_volume_binding_with_environment_variable(self):
  2930. os.environ['VOLUME_PATH'] = '/host/path'
  2931. d = config.load(
  2932. build_config_details(
  2933. {'foo': {'build': '.', 'volumes': ['${VOLUME_PATH}:/container/path']}},
  2934. '.',
  2935. None,
  2936. )
  2937. ).services[0]
  2938. self.assertEqual(d['volumes'], [VolumeSpec.parse('/host/path:/container/path')])
  2939. @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
  2940. @mock.patch.dict(os.environ)
  2941. def test_volume_binding_with_home(self):
  2942. os.environ['HOME'] = '/home/user'
  2943. d = make_service_dict('foo', {'build': '.', 'volumes': ['~:/container/path']}, working_dir='.')
  2944. self.assertEqual(d['volumes'], ['/home/user:/container/path'])
  2945. def test_name_does_not_expand(self):
  2946. d = make_service_dict('foo', {'build': '.', 'volumes': ['mydatavolume:/data']}, working_dir='.')
  2947. self.assertEqual(d['volumes'], ['mydatavolume:/data'])
  2948. def test_absolute_posix_path_does_not_expand(self):
  2949. d = make_service_dict('foo', {'build': '.', 'volumes': ['/var/lib/data:/data']}, working_dir='.')
  2950. self.assertEqual(d['volumes'], ['/var/lib/data:/data'])
  2951. def test_absolute_windows_path_does_not_expand(self):
  2952. d = make_service_dict('foo', {'build': '.', 'volumes': ['c:\\data:/data']}, working_dir='.')
  2953. self.assertEqual(d['volumes'], ['c:\\data:/data'])
  2954. @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
  2955. def test_relative_path_does_expand_posix(self):
  2956. d = make_service_dict(
  2957. 'foo',
  2958. {'build': '.', 'volumes': ['./data:/data']},
  2959. working_dir='/home/me/myproject')
  2960. self.assertEqual(d['volumes'], ['/home/me/myproject/data:/data'])
  2961. d = make_service_dict(
  2962. 'foo',
  2963. {'build': '.', 'volumes': ['.:/data']},
  2964. working_dir='/home/me/myproject')
  2965. self.assertEqual(d['volumes'], ['/home/me/myproject:/data'])
  2966. d = make_service_dict(
  2967. 'foo',
  2968. {'build': '.', 'volumes': ['../otherproject:/data']},
  2969. working_dir='/home/me/myproject')
  2970. self.assertEqual(d['volumes'], ['/home/me/otherproject:/data'])
  2971. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='windows paths')
  2972. def test_relative_path_does_expand_windows(self):
  2973. d = make_service_dict(
  2974. 'foo',
  2975. {'build': '.', 'volumes': ['./data:/data']},
  2976. working_dir='c:\\Users\\me\\myproject')
  2977. self.assertEqual(d['volumes'], ['c:\\Users\\me\\myproject\\data:/data'])
  2978. d = make_service_dict(
  2979. 'foo',
  2980. {'build': '.', 'volumes': ['.:/data']},
  2981. working_dir='c:\\Users\\me\\myproject')
  2982. self.assertEqual(d['volumes'], ['c:\\Users\\me\\myproject:/data'])
  2983. d = make_service_dict(
  2984. 'foo',
  2985. {'build': '.', 'volumes': ['../otherproject:/data']},
  2986. working_dir='c:\\Users\\me\\myproject')
  2987. self.assertEqual(d['volumes'], ['c:\\Users\\me\\otherproject:/data'])
  2988. @mock.patch.dict(os.environ)
  2989. def test_home_directory_with_driver_does_not_expand(self):
  2990. os.environ['NAME'] = 'surprise!'
  2991. d = make_service_dict('foo', {
  2992. 'build': '.',
  2993. 'volumes': ['~:/data'],
  2994. 'volume_driver': 'foodriver',
  2995. }, working_dir='.')
  2996. self.assertEqual(d['volumes'], ['~:/data'])
  2997. def test_volume_path_with_non_ascii_directory(self):
  2998. volume = u'/Füü/data:/data'
  2999. container_path = config.resolve_volume_path(".", volume)
  3000. self.assertEqual(container_path, volume)
  3001. class MergePathMappingTest(object):
  3002. config_name = ""
  3003. def test_empty(self):
  3004. service_dict = config.merge_service_dicts({}, {}, DEFAULT_VERSION)
  3005. assert self.config_name not in service_dict
  3006. def test_no_override(self):
  3007. service_dict = config.merge_service_dicts(
  3008. {self.config_name: ['/foo:/code', '/data']},
  3009. {},
  3010. DEFAULT_VERSION)
  3011. assert set(service_dict[self.config_name]) == set(['/foo:/code', '/data'])
  3012. def test_no_base(self):
  3013. service_dict = config.merge_service_dicts(
  3014. {},
  3015. {self.config_name: ['/bar:/code']},
  3016. DEFAULT_VERSION)
  3017. assert set(service_dict[self.config_name]) == set(['/bar:/code'])
  3018. def test_override_explicit_path(self):
  3019. service_dict = config.merge_service_dicts(
  3020. {self.config_name: ['/foo:/code', '/data']},
  3021. {self.config_name: ['/bar:/code']},
  3022. DEFAULT_VERSION)
  3023. assert set(service_dict[self.config_name]) == set(['/bar:/code', '/data'])
  3024. def test_add_explicit_path(self):
  3025. service_dict = config.merge_service_dicts(
  3026. {self.config_name: ['/foo:/code', '/data']},
  3027. {self.config_name: ['/bar:/code', '/quux:/data']},
  3028. DEFAULT_VERSION)
  3029. assert set(service_dict[self.config_name]) == set(['/bar:/code', '/quux:/data'])
  3030. def test_remove_explicit_path(self):
  3031. service_dict = config.merge_service_dicts(
  3032. {self.config_name: ['/foo:/code', '/quux:/data']},
  3033. {self.config_name: ['/bar:/code', '/data']},
  3034. DEFAULT_VERSION)
  3035. assert set(service_dict[self.config_name]) == set(['/bar:/code', '/data'])
  3036. class MergeVolumesTest(unittest.TestCase, MergePathMappingTest):
  3037. config_name = 'volumes'
  3038. class MergeDevicesTest(unittest.TestCase, MergePathMappingTest):
  3039. config_name = 'devices'
  3040. class BuildOrImageMergeTest(unittest.TestCase):
  3041. def test_merge_build_or_image_no_override(self):
  3042. self.assertEqual(
  3043. config.merge_service_dicts({'build': '.'}, {}, V1),
  3044. {'build': '.'},
  3045. )
  3046. self.assertEqual(
  3047. config.merge_service_dicts({'image': 'redis'}, {}, V1),
  3048. {'image': 'redis'},
  3049. )
  3050. def test_merge_build_or_image_override_with_same(self):
  3051. self.assertEqual(
  3052. config.merge_service_dicts({'build': '.'}, {'build': './web'}, V1),
  3053. {'build': './web'},
  3054. )
  3055. self.assertEqual(
  3056. config.merge_service_dicts({'image': 'redis'}, {'image': 'postgres'}, V1),
  3057. {'image': 'postgres'},
  3058. )
  3059. def test_merge_build_or_image_override_with_other(self):
  3060. self.assertEqual(
  3061. config.merge_service_dicts({'build': '.'}, {'image': 'redis'}, V1),
  3062. {'image': 'redis'},
  3063. )
  3064. self.assertEqual(
  3065. config.merge_service_dicts({'image': 'redis'}, {'build': '.'}, V1),
  3066. {'build': '.'}
  3067. )
  3068. class MergeListsTest(object):
  3069. config_name = ""
  3070. base_config = []
  3071. override_config = []
  3072. def merged_config(self):
  3073. return set(self.base_config) | set(self.override_config)
  3074. def test_empty(self):
  3075. assert self.config_name not in config.merge_service_dicts({}, {}, DEFAULT_VERSION)
  3076. def test_no_override(self):
  3077. service_dict = config.merge_service_dicts(
  3078. {self.config_name: self.base_config},
  3079. {},
  3080. DEFAULT_VERSION)
  3081. assert set(service_dict[self.config_name]) == set(self.base_config)
  3082. def test_no_base(self):
  3083. service_dict = config.merge_service_dicts(
  3084. {},
  3085. {self.config_name: self.base_config},
  3086. DEFAULT_VERSION)
  3087. assert set(service_dict[self.config_name]) == set(self.base_config)
  3088. def test_add_item(self):
  3089. service_dict = config.merge_service_dicts(
  3090. {self.config_name: self.base_config},
  3091. {self.config_name: self.override_config},
  3092. DEFAULT_VERSION)
  3093. assert set(service_dict[self.config_name]) == set(self.merged_config())
  3094. class MergePortsTest(unittest.TestCase, MergeListsTest):
  3095. config_name = 'ports'
  3096. base_config = ['10:8000', '9000']
  3097. override_config = ['20:8000']
  3098. def merged_config(self):
  3099. return self.convert(self.base_config) | self.convert(self.override_config)
  3100. def convert(self, port_config):
  3101. return set(config.merge_service_dicts(
  3102. {self.config_name: port_config},
  3103. {self.config_name: []},
  3104. DEFAULT_VERSION
  3105. )[self.config_name])
  3106. def test_duplicate_port_mappings(self):
  3107. service_dict = config.merge_service_dicts(
  3108. {self.config_name: self.base_config},
  3109. {self.config_name: self.base_config},
  3110. DEFAULT_VERSION
  3111. )
  3112. assert set(service_dict[self.config_name]) == self.convert(self.base_config)
  3113. def test_no_override(self):
  3114. service_dict = config.merge_service_dicts(
  3115. {self.config_name: self.base_config},
  3116. {},
  3117. DEFAULT_VERSION)
  3118. assert set(service_dict[self.config_name]) == self.convert(self.base_config)
  3119. def test_no_base(self):
  3120. service_dict = config.merge_service_dicts(
  3121. {},
  3122. {self.config_name: self.base_config},
  3123. DEFAULT_VERSION)
  3124. assert set(service_dict[self.config_name]) == self.convert(self.base_config)
  3125. class MergeNetworksTest(unittest.TestCase, MergeListsTest):
  3126. config_name = 'networks'
  3127. base_config = ['frontend', 'backend']
  3128. override_config = ['monitoring']
  3129. class MergeStringsOrListsTest(unittest.TestCase):
  3130. def test_no_override(self):
  3131. service_dict = config.merge_service_dicts(
  3132. {'dns': '8.8.8.8'},
  3133. {},
  3134. DEFAULT_VERSION)
  3135. assert set(service_dict['dns']) == set(['8.8.8.8'])
  3136. def test_no_base(self):
  3137. service_dict = config.merge_service_dicts(
  3138. {},
  3139. {'dns': '8.8.8.8'},
  3140. DEFAULT_VERSION)
  3141. assert set(service_dict['dns']) == set(['8.8.8.8'])
  3142. def test_add_string(self):
  3143. service_dict = config.merge_service_dicts(
  3144. {'dns': ['8.8.8.8']},
  3145. {'dns': '9.9.9.9'},
  3146. DEFAULT_VERSION)
  3147. assert set(service_dict['dns']) == set(['8.8.8.8', '9.9.9.9'])
  3148. def test_add_list(self):
  3149. service_dict = config.merge_service_dicts(
  3150. {'dns': '8.8.8.8'},
  3151. {'dns': ['9.9.9.9']},
  3152. DEFAULT_VERSION)
  3153. assert set(service_dict['dns']) == set(['8.8.8.8', '9.9.9.9'])
  3154. class MergeLabelsTest(unittest.TestCase):
  3155. def test_empty(self):
  3156. assert 'labels' not in config.merge_service_dicts({}, {}, DEFAULT_VERSION)
  3157. def test_no_override(self):
  3158. service_dict = config.merge_service_dicts(
  3159. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
  3160. make_service_dict('foo', {'build': '.'}, 'tests/'),
  3161. DEFAULT_VERSION)
  3162. assert service_dict['labels'] == {'foo': '1', 'bar': ''}
  3163. def test_no_base(self):
  3164. service_dict = config.merge_service_dicts(
  3165. make_service_dict('foo', {'build': '.'}, 'tests/'),
  3166. make_service_dict('foo', {'build': '.', 'labels': ['foo=2']}, 'tests/'),
  3167. DEFAULT_VERSION)
  3168. assert service_dict['labels'] == {'foo': '2'}
  3169. def test_override_explicit_value(self):
  3170. service_dict = config.merge_service_dicts(
  3171. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
  3172. make_service_dict('foo', {'build': '.', 'labels': ['foo=2']}, 'tests/'),
  3173. DEFAULT_VERSION)
  3174. assert service_dict['labels'] == {'foo': '2', 'bar': ''}
  3175. def test_add_explicit_value(self):
  3176. service_dict = config.merge_service_dicts(
  3177. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
  3178. make_service_dict('foo', {'build': '.', 'labels': ['bar=2']}, 'tests/'),
  3179. DEFAULT_VERSION)
  3180. assert service_dict['labels'] == {'foo': '1', 'bar': '2'}
  3181. def test_remove_explicit_value(self):
  3182. service_dict = config.merge_service_dicts(
  3183. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar=2']}, 'tests/'),
  3184. make_service_dict('foo', {'build': '.', 'labels': ['bar']}, 'tests/'),
  3185. DEFAULT_VERSION)
  3186. assert service_dict['labels'] == {'foo': '1', 'bar': ''}
  3187. class MergeBuildTest(unittest.TestCase):
  3188. def test_full(self):
  3189. base = {
  3190. 'context': '.',
  3191. 'dockerfile': 'Dockerfile',
  3192. 'args': {
  3193. 'x': '1',
  3194. 'y': '2',
  3195. },
  3196. 'cache_from': ['ubuntu'],
  3197. 'labels': ['com.docker.compose.test=true']
  3198. }
  3199. override = {
  3200. 'context': './prod',
  3201. 'dockerfile': 'Dockerfile.prod',
  3202. 'args': ['x=12'],
  3203. 'cache_from': ['debian'],
  3204. 'labels': {
  3205. 'com.docker.compose.test': 'false',
  3206. 'com.docker.compose.prod': 'true',
  3207. }
  3208. }
  3209. result = config.merge_build(None, {'build': base}, {'build': override})
  3210. assert result['context'] == override['context']
  3211. assert result['dockerfile'] == override['dockerfile']
  3212. assert result['args'] == {'x': '12', 'y': '2'}
  3213. assert set(result['cache_from']) == set(['ubuntu', 'debian'])
  3214. assert result['labels'] == override['labels']
  3215. def test_empty_override(self):
  3216. base = {
  3217. 'context': '.',
  3218. 'dockerfile': 'Dockerfile',
  3219. 'args': {
  3220. 'x': '1',
  3221. 'y': '2',
  3222. },
  3223. 'cache_from': ['ubuntu'],
  3224. 'labels': {
  3225. 'com.docker.compose.test': 'true'
  3226. }
  3227. }
  3228. override = {}
  3229. result = config.merge_build(None, {'build': base}, {'build': override})
  3230. assert result == base
  3231. def test_empty_base(self):
  3232. base = {}
  3233. override = {
  3234. 'context': './prod',
  3235. 'dockerfile': 'Dockerfile.prod',
  3236. 'args': {'x': '12'},
  3237. 'cache_from': ['debian'],
  3238. 'labels': {
  3239. 'com.docker.compose.test': 'false',
  3240. 'com.docker.compose.prod': 'true',
  3241. }
  3242. }
  3243. result = config.merge_build(None, {'build': base}, {'build': override})
  3244. assert result == override
  3245. class MemoryOptionsTest(unittest.TestCase):
  3246. def test_validation_fails_with_just_memswap_limit(self):
  3247. """
  3248. When you set a 'memswap_limit' it is invalid config unless you also set
  3249. a mem_limit
  3250. """
  3251. with pytest.raises(ConfigurationError) as excinfo:
  3252. config.load(
  3253. build_config_details(
  3254. {
  3255. 'foo': {'image': 'busybox', 'memswap_limit': 2000000},
  3256. },
  3257. 'tests/fixtures/extends',
  3258. 'filename.yml'
  3259. )
  3260. )
  3261. assert "foo.memswap_limit is invalid: when defining " \
  3262. "'memswap_limit' you must set 'mem_limit' as well" \
  3263. in excinfo.exconly()
  3264. def test_validation_with_correct_memswap_values(self):
  3265. service_dict = config.load(
  3266. build_config_details(
  3267. {'foo': {'image': 'busybox', 'mem_limit': 1000000, 'memswap_limit': 2000000}},
  3268. 'tests/fixtures/extends',
  3269. 'common.yml'
  3270. )
  3271. ).services
  3272. self.assertEqual(service_dict[0]['memswap_limit'], 2000000)
  3273. def test_memswap_can_be_a_string(self):
  3274. service_dict = config.load(
  3275. build_config_details(
  3276. {'foo': {'image': 'busybox', 'mem_limit': "1G", 'memswap_limit': "512M"}},
  3277. 'tests/fixtures/extends',
  3278. 'common.yml'
  3279. )
  3280. ).services
  3281. self.assertEqual(service_dict[0]['memswap_limit'], "512M")
  3282. class EnvTest(unittest.TestCase):
  3283. def test_parse_environment_as_list(self):
  3284. environment = [
  3285. 'NORMAL=F1',
  3286. 'CONTAINS_EQUALS=F=2',
  3287. 'TRAILING_EQUALS=',
  3288. ]
  3289. self.assertEqual(
  3290. config.parse_environment(environment),
  3291. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  3292. )
  3293. def test_parse_environment_as_dict(self):
  3294. environment = {
  3295. 'NORMAL': 'F1',
  3296. 'CONTAINS_EQUALS': 'F=2',
  3297. 'TRAILING_EQUALS': None,
  3298. }
  3299. self.assertEqual(config.parse_environment(environment), environment)
  3300. def test_parse_environment_invalid(self):
  3301. with self.assertRaises(ConfigurationError):
  3302. config.parse_environment('a=b')
  3303. def test_parse_environment_empty(self):
  3304. self.assertEqual(config.parse_environment(None), {})
  3305. @mock.patch.dict(os.environ)
  3306. def test_resolve_environment(self):
  3307. os.environ['FILE_DEF'] = 'E1'
  3308. os.environ['FILE_DEF_EMPTY'] = 'E2'
  3309. os.environ['ENV_DEF'] = 'E3'
  3310. service_dict = {
  3311. 'build': '.',
  3312. 'environment': {
  3313. 'FILE_DEF': 'F1',
  3314. 'FILE_DEF_EMPTY': '',
  3315. 'ENV_DEF': None,
  3316. 'NO_DEF': None
  3317. },
  3318. }
  3319. self.assertEqual(
  3320. resolve_environment(
  3321. service_dict, Environment.from_env_file(None)
  3322. ),
  3323. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None},
  3324. )
  3325. def test_resolve_environment_from_env_file(self):
  3326. self.assertEqual(
  3327. resolve_environment({'env_file': ['tests/fixtures/env/one.env']}),
  3328. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  3329. )
  3330. def test_environment_overrides_env_file(self):
  3331. self.assertEqual(
  3332. resolve_environment({
  3333. 'environment': {'FOO': 'baz'},
  3334. 'env_file': ['tests/fixtures/env/one.env'],
  3335. }),
  3336. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz'},
  3337. )
  3338. def test_resolve_environment_with_multiple_env_files(self):
  3339. service_dict = {
  3340. 'env_file': [
  3341. 'tests/fixtures/env/one.env',
  3342. 'tests/fixtures/env/two.env'
  3343. ]
  3344. }
  3345. self.assertEqual(
  3346. resolve_environment(service_dict),
  3347. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  3348. )
  3349. def test_resolve_environment_nonexistent_file(self):
  3350. with pytest.raises(ConfigurationError) as exc:
  3351. config.load(build_config_details(
  3352. {'foo': {'image': 'example', 'env_file': 'nonexistent.env'}},
  3353. working_dir='tests/fixtures/env'))
  3354. assert 'Couldn\'t find env file' in exc.exconly()
  3355. assert 'nonexistent.env' in exc.exconly()
  3356. @mock.patch.dict(os.environ)
  3357. def test_resolve_environment_from_env_file_with_empty_values(self):
  3358. os.environ['FILE_DEF'] = 'E1'
  3359. os.environ['FILE_DEF_EMPTY'] = 'E2'
  3360. os.environ['ENV_DEF'] = 'E3'
  3361. self.assertEqual(
  3362. resolve_environment(
  3363. {'env_file': ['tests/fixtures/env/resolve.env']},
  3364. Environment.from_env_file(None)
  3365. ),
  3366. {
  3367. 'FILE_DEF': u'bär',
  3368. 'FILE_DEF_EMPTY': '',
  3369. 'ENV_DEF': 'E3',
  3370. 'NO_DEF': None
  3371. },
  3372. )
  3373. @mock.patch.dict(os.environ)
  3374. def test_resolve_build_args(self):
  3375. os.environ['env_arg'] = 'value2'
  3376. build = {
  3377. 'context': '.',
  3378. 'args': {
  3379. 'arg1': 'value1',
  3380. 'empty_arg': '',
  3381. 'env_arg': None,
  3382. 'no_env': None
  3383. }
  3384. }
  3385. self.assertEqual(
  3386. resolve_build_args(build['args'], Environment.from_env_file(build['context'])),
  3387. {'arg1': 'value1', 'empty_arg': '', 'env_arg': 'value2', 'no_env': None},
  3388. )
  3389. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  3390. @mock.patch.dict(os.environ)
  3391. def test_resolve_path(self):
  3392. os.environ['HOSTENV'] = '/tmp'
  3393. os.environ['CONTAINERENV'] = '/host/tmp'
  3394. service_dict = config.load(
  3395. build_config_details(
  3396. {'foo': {'build': '.', 'volumes': ['$HOSTENV:$CONTAINERENV']}},
  3397. "tests/fixtures/env",
  3398. )
  3399. ).services[0]
  3400. self.assertEqual(
  3401. set(service_dict['volumes']),
  3402. set([VolumeSpec.parse('/tmp:/host/tmp')]))
  3403. service_dict = config.load(
  3404. build_config_details(
  3405. {'foo': {'build': '.', 'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}},
  3406. "tests/fixtures/env",
  3407. )
  3408. ).services[0]
  3409. self.assertEqual(
  3410. set(service_dict['volumes']),
  3411. set([VolumeSpec.parse('/opt/tmp:/opt/host/tmp')]))
  3412. def load_from_filename(filename, override_dir=None):
  3413. return config.load(
  3414. config.find('.', [filename], Environment.from_env_file('.'), override_dir=override_dir)
  3415. ).services
  3416. class ExtendsTest(unittest.TestCase):
  3417. def test_extends(self):
  3418. service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
  3419. self.assertEqual(service_sort(service_dicts), service_sort([
  3420. {
  3421. 'name': 'mydb',
  3422. 'image': 'busybox',
  3423. 'command': 'top',
  3424. },
  3425. {
  3426. 'name': 'myweb',
  3427. 'image': 'busybox',
  3428. 'command': 'top',
  3429. 'network_mode': 'bridge',
  3430. 'links': ['mydb:db'],
  3431. 'environment': {
  3432. "FOO": "1",
  3433. "BAR": "2",
  3434. "BAZ": "2",
  3435. },
  3436. }
  3437. ]))
  3438. def test_merging_env_labels_ulimits(self):
  3439. service_dicts = load_from_filename('tests/fixtures/extends/common-env-labels-ulimits.yml')
  3440. self.assertEqual(service_sort(service_dicts), service_sort([
  3441. {
  3442. 'name': 'web',
  3443. 'image': 'busybox',
  3444. 'command': '/bin/true',
  3445. 'network_mode': 'host',
  3446. 'environment': {
  3447. "FOO": "2",
  3448. "BAR": "1",
  3449. "BAZ": "3",
  3450. },
  3451. 'labels': {'label': 'one'},
  3452. 'ulimits': {'nproc': 65535, 'memlock': {'soft': 1024, 'hard': 2048}}
  3453. }
  3454. ]))
  3455. def test_nested(self):
  3456. service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
  3457. self.assertEqual(service_dicts, [
  3458. {
  3459. 'name': 'myweb',
  3460. 'image': 'busybox',
  3461. 'command': '/bin/true',
  3462. 'network_mode': 'host',
  3463. 'environment': {
  3464. "FOO": "2",
  3465. "BAR": "2",
  3466. },
  3467. },
  3468. ])
  3469. def test_self_referencing_file(self):
  3470. """
  3471. We specify a 'file' key that is the filename we're already in.
  3472. """
  3473. service_dicts = load_from_filename('tests/fixtures/extends/specify-file-as-self.yml')
  3474. self.assertEqual(service_sort(service_dicts), service_sort([
  3475. {
  3476. 'environment':
  3477. {
  3478. 'YEP': '1', 'BAR': '1', 'BAZ': '3'
  3479. },
  3480. 'image': 'busybox',
  3481. 'name': 'myweb'
  3482. },
  3483. {
  3484. 'environment':
  3485. {'YEP': '1'},
  3486. 'image': 'busybox',
  3487. 'name': 'otherweb'
  3488. },
  3489. {
  3490. 'environment':
  3491. {'YEP': '1', 'BAZ': '3'},
  3492. 'image': 'busybox',
  3493. 'name': 'web'
  3494. }
  3495. ]))
  3496. def test_circular(self):
  3497. with pytest.raises(config.CircularReference) as exc:
  3498. load_from_filename('tests/fixtures/extends/circle-1.yml')
  3499. path = [
  3500. (os.path.basename(filename), service_name)
  3501. for (filename, service_name) in exc.value.trail
  3502. ]
  3503. expected = [
  3504. ('circle-1.yml', 'web'),
  3505. ('circle-2.yml', 'other'),
  3506. ('circle-1.yml', 'web'),
  3507. ]
  3508. self.assertEqual(path, expected)
  3509. def test_extends_validation_empty_dictionary(self):
  3510. with pytest.raises(ConfigurationError) as excinfo:
  3511. config.load(
  3512. build_config_details(
  3513. {
  3514. 'web': {'image': 'busybox', 'extends': {}},
  3515. },
  3516. 'tests/fixtures/extends',
  3517. 'filename.yml'
  3518. )
  3519. )
  3520. assert 'service' in excinfo.exconly()
  3521. def test_extends_validation_missing_service_key(self):
  3522. with pytest.raises(ConfigurationError) as excinfo:
  3523. config.load(
  3524. build_config_details(
  3525. {
  3526. 'web': {'image': 'busybox', 'extends': {'file': 'common.yml'}},
  3527. },
  3528. 'tests/fixtures/extends',
  3529. 'filename.yml'
  3530. )
  3531. )
  3532. assert "'service' is a required property" in excinfo.exconly()
  3533. def test_extends_validation_invalid_key(self):
  3534. with pytest.raises(ConfigurationError) as excinfo:
  3535. config.load(
  3536. build_config_details(
  3537. {
  3538. 'web': {
  3539. 'image': 'busybox',
  3540. 'extends': {
  3541. 'file': 'common.yml',
  3542. 'service': 'web',
  3543. 'rogue_key': 'is not allowed'
  3544. }
  3545. },
  3546. },
  3547. 'tests/fixtures/extends',
  3548. 'filename.yml'
  3549. )
  3550. )
  3551. assert "web.extends contains unsupported option: 'rogue_key'" \
  3552. in excinfo.exconly()
  3553. def test_extends_validation_sub_property_key(self):
  3554. with pytest.raises(ConfigurationError) as excinfo:
  3555. config.load(
  3556. build_config_details(
  3557. {
  3558. 'web': {
  3559. 'image': 'busybox',
  3560. 'extends': {
  3561. 'file': 1,
  3562. 'service': 'web',
  3563. }
  3564. },
  3565. },
  3566. 'tests/fixtures/extends',
  3567. 'filename.yml'
  3568. )
  3569. )
  3570. assert "web.extends.file contains 1, which is an invalid type, it should be a string" \
  3571. in excinfo.exconly()
  3572. def test_extends_validation_no_file_key_no_filename_set(self):
  3573. dictionary = {'extends': {'service': 'web'}}
  3574. with pytest.raises(ConfigurationError) as excinfo:
  3575. make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  3576. assert 'file' in excinfo.exconly()
  3577. def test_extends_validation_valid_config(self):
  3578. service = config.load(
  3579. build_config_details(
  3580. {
  3581. 'web': {'image': 'busybox', 'extends': {'service': 'web', 'file': 'common.yml'}},
  3582. },
  3583. 'tests/fixtures/extends',
  3584. 'common.yml'
  3585. )
  3586. ).services
  3587. self.assertEqual(len(service), 1)
  3588. self.assertIsInstance(service[0], dict)
  3589. self.assertEqual(service[0]['command'], "/bin/true")
  3590. def test_extended_service_with_invalid_config(self):
  3591. with pytest.raises(ConfigurationError) as exc:
  3592. load_from_filename('tests/fixtures/extends/service-with-invalid-schema.yml')
  3593. assert (
  3594. "myweb has neither an image nor a build context specified" in
  3595. exc.exconly()
  3596. )
  3597. def test_extended_service_with_valid_config(self):
  3598. service = load_from_filename('tests/fixtures/extends/service-with-valid-composite-extends.yml')
  3599. self.assertEqual(service[0]['command'], "top")
  3600. def test_extends_file_defaults_to_self(self):
  3601. """
  3602. Test not specifying a file in our extends options that the
  3603. config is valid and correctly extends from itself.
  3604. """
  3605. service_dicts = load_from_filename('tests/fixtures/extends/no-file-specified.yml')
  3606. self.assertEqual(service_sort(service_dicts), service_sort([
  3607. {
  3608. 'name': 'myweb',
  3609. 'image': 'busybox',
  3610. 'environment': {
  3611. "BAR": "1",
  3612. "BAZ": "3",
  3613. }
  3614. },
  3615. {
  3616. 'name': 'web',
  3617. 'image': 'busybox',
  3618. 'environment': {
  3619. "BAZ": "3",
  3620. }
  3621. }
  3622. ]))
  3623. def test_invalid_links_in_extended_service(self):
  3624. with pytest.raises(ConfigurationError) as excinfo:
  3625. load_from_filename('tests/fixtures/extends/invalid-links.yml')
  3626. assert "services with 'links' cannot be extended" in excinfo.exconly()
  3627. def test_invalid_volumes_from_in_extended_service(self):
  3628. with pytest.raises(ConfigurationError) as excinfo:
  3629. load_from_filename('tests/fixtures/extends/invalid-volumes.yml')
  3630. assert "services with 'volumes_from' cannot be extended" in excinfo.exconly()
  3631. def test_invalid_net_in_extended_service(self):
  3632. with pytest.raises(ConfigurationError) as excinfo:
  3633. load_from_filename('tests/fixtures/extends/invalid-net-v2.yml')
  3634. assert 'network_mode: service' in excinfo.exconly()
  3635. assert 'cannot be extended' in excinfo.exconly()
  3636. with pytest.raises(ConfigurationError) as excinfo:
  3637. load_from_filename('tests/fixtures/extends/invalid-net.yml')
  3638. assert 'net: container' in excinfo.exconly()
  3639. assert 'cannot be extended' in excinfo.exconly()
  3640. @mock.patch.dict(os.environ)
  3641. def test_load_config_runs_interpolation_in_extended_service(self):
  3642. os.environ.update(HOSTNAME_VALUE="penguin")
  3643. expected_interpolated_value = "host-penguin"
  3644. service_dicts = load_from_filename(
  3645. 'tests/fixtures/extends/valid-interpolation.yml')
  3646. for service in service_dicts:
  3647. assert service['hostname'] == expected_interpolated_value
  3648. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  3649. def test_volume_path(self):
  3650. dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
  3651. paths = [
  3652. VolumeSpec(
  3653. os.path.abspath('tests/fixtures/volume-path/common/foo'),
  3654. '/foo',
  3655. 'rw'),
  3656. VolumeSpec(
  3657. os.path.abspath('tests/fixtures/volume-path/bar'),
  3658. '/bar',
  3659. 'rw')
  3660. ]
  3661. self.assertEqual(set(dicts[0]['volumes']), set(paths))
  3662. def test_parent_build_path_dne(self):
  3663. child = load_from_filename('tests/fixtures/extends/nonexistent-path-child.yml')
  3664. self.assertEqual(child, [
  3665. {
  3666. 'name': 'dnechild',
  3667. 'image': 'busybox',
  3668. 'command': '/bin/true',
  3669. 'environment': {
  3670. "FOO": "1",
  3671. "BAR": "2",
  3672. },
  3673. },
  3674. ])
  3675. def test_load_throws_error_when_base_service_does_not_exist(self):
  3676. with pytest.raises(ConfigurationError) as excinfo:
  3677. load_from_filename('tests/fixtures/extends/nonexistent-service.yml')
  3678. assert "Cannot extend service 'foo'" in excinfo.exconly()
  3679. assert "Service not found" in excinfo.exconly()
  3680. def test_partial_service_config_in_extends_is_still_valid(self):
  3681. dicts = load_from_filename('tests/fixtures/extends/valid-common-config.yml')
  3682. self.assertEqual(dicts[0]['environment'], {'FOO': '1'})
  3683. def test_extended_service_with_verbose_and_shorthand_way(self):
  3684. services = load_from_filename('tests/fixtures/extends/verbose-and-shorthand.yml')
  3685. self.assertEqual(service_sort(services), service_sort([
  3686. {
  3687. 'name': 'base',
  3688. 'image': 'busybox',
  3689. 'environment': {'BAR': '1'},
  3690. },
  3691. {
  3692. 'name': 'verbose',
  3693. 'image': 'busybox',
  3694. 'environment': {'BAR': '1', 'FOO': '1'},
  3695. },
  3696. {
  3697. 'name': 'shorthand',
  3698. 'image': 'busybox',
  3699. 'environment': {'BAR': '1', 'FOO': '2'},
  3700. },
  3701. ]))
  3702. @mock.patch.dict(os.environ)
  3703. def test_extends_with_environment_and_env_files(self):
  3704. tmpdir = py.test.ensuretemp('test_extends_with_environment')
  3705. self.addCleanup(tmpdir.remove)
  3706. commondir = tmpdir.mkdir('common')
  3707. commondir.join('base.yml').write("""
  3708. app:
  3709. image: 'example/app'
  3710. env_file:
  3711. - 'envs'
  3712. environment:
  3713. - SECRET
  3714. - TEST_ONE=common
  3715. - TEST_TWO=common
  3716. """)
  3717. tmpdir.join('docker-compose.yml').write("""
  3718. ext:
  3719. extends:
  3720. file: common/base.yml
  3721. service: app
  3722. env_file:
  3723. - 'envs'
  3724. environment:
  3725. - THING
  3726. - TEST_ONE=top
  3727. """)
  3728. commondir.join('envs').write("""
  3729. COMMON_ENV_FILE
  3730. TEST_ONE=common-env-file
  3731. TEST_TWO=common-env-file
  3732. TEST_THREE=common-env-file
  3733. TEST_FOUR=common-env-file
  3734. """)
  3735. tmpdir.join('envs').write("""
  3736. TOP_ENV_FILE
  3737. TEST_ONE=top-env-file
  3738. TEST_TWO=top-env-file
  3739. TEST_THREE=top-env-file
  3740. """)
  3741. expected = [
  3742. {
  3743. 'name': 'ext',
  3744. 'image': 'example/app',
  3745. 'environment': {
  3746. 'SECRET': 'secret',
  3747. 'TOP_ENV_FILE': 'secret',
  3748. 'COMMON_ENV_FILE': 'secret',
  3749. 'THING': 'thing',
  3750. 'TEST_ONE': 'top',
  3751. 'TEST_TWO': 'common',
  3752. 'TEST_THREE': 'top-env-file',
  3753. 'TEST_FOUR': 'common-env-file',
  3754. },
  3755. },
  3756. ]
  3757. os.environ['SECRET'] = 'secret'
  3758. os.environ['THING'] = 'thing'
  3759. os.environ['COMMON_ENV_FILE'] = 'secret'
  3760. os.environ['TOP_ENV_FILE'] = 'secret'
  3761. config = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  3762. assert config == expected
  3763. def test_extends_with_mixed_versions_is_error(self):
  3764. tmpdir = py.test.ensuretemp('test_extends_with_mixed_version')
  3765. self.addCleanup(tmpdir.remove)
  3766. tmpdir.join('docker-compose.yml').write("""
  3767. version: "2"
  3768. services:
  3769. web:
  3770. extends:
  3771. file: base.yml
  3772. service: base
  3773. image: busybox
  3774. """)
  3775. tmpdir.join('base.yml').write("""
  3776. base:
  3777. volumes: ['/foo']
  3778. ports: ['3000:3000']
  3779. """)
  3780. with pytest.raises(ConfigurationError) as exc:
  3781. load_from_filename(str(tmpdir.join('docker-compose.yml')))
  3782. assert 'Version mismatch' in exc.exconly()
  3783. def test_extends_with_defined_version_passes(self):
  3784. tmpdir = py.test.ensuretemp('test_extends_with_defined_version')
  3785. self.addCleanup(tmpdir.remove)
  3786. tmpdir.join('docker-compose.yml').write("""
  3787. version: "2"
  3788. services:
  3789. web:
  3790. extends:
  3791. file: base.yml
  3792. service: base
  3793. image: busybox
  3794. """)
  3795. tmpdir.join('base.yml').write("""
  3796. version: "2"
  3797. services:
  3798. base:
  3799. volumes: ['/foo']
  3800. ports: ['3000:3000']
  3801. command: top
  3802. """)
  3803. service = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  3804. self.assertEqual(service[0]['command'], "top")
  3805. def test_extends_with_depends_on(self):
  3806. tmpdir = py.test.ensuretemp('test_extends_with_depends_on')
  3807. self.addCleanup(tmpdir.remove)
  3808. tmpdir.join('docker-compose.yml').write("""
  3809. version: "2"
  3810. services:
  3811. base:
  3812. image: example
  3813. web:
  3814. extends: base
  3815. image: busybox
  3816. depends_on: ['other']
  3817. other:
  3818. image: example
  3819. """)
  3820. services = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  3821. assert service_sort(services)[2]['depends_on'] == {
  3822. 'other': {'condition': 'service_started'}
  3823. }
  3824. def test_extends_with_healthcheck(self):
  3825. service_dicts = load_from_filename('tests/fixtures/extends/healthcheck-2.yml')
  3826. assert service_sort(service_dicts) == [{
  3827. 'name': 'demo',
  3828. 'image': 'foobar:latest',
  3829. 'healthcheck': {
  3830. 'test': ['CMD', '/health.sh'],
  3831. 'interval': 10000000000,
  3832. 'timeout': 5000000000,
  3833. 'retries': 36,
  3834. }
  3835. }]
  3836. def test_extends_with_ports(self):
  3837. tmpdir = py.test.ensuretemp('test_extends_with_ports')
  3838. self.addCleanup(tmpdir.remove)
  3839. tmpdir.join('docker-compose.yml').write("""
  3840. version: '2'
  3841. services:
  3842. a:
  3843. image: nginx
  3844. ports:
  3845. - 80
  3846. b:
  3847. extends:
  3848. service: a
  3849. """)
  3850. services = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  3851. assert len(services) == 2
  3852. for svc in services:
  3853. assert svc['ports'] == [types.ServicePort('80', None, None, None, None)]
  3854. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  3855. class ExpandPathTest(unittest.TestCase):
  3856. working_dir = '/home/user/somedir'
  3857. def test_expand_path_normal(self):
  3858. result = config.expand_path(self.working_dir, 'myfile')
  3859. self.assertEqual(result, self.working_dir + '/' + 'myfile')
  3860. def test_expand_path_absolute(self):
  3861. abs_path = '/home/user/otherdir/somefile'
  3862. result = config.expand_path(self.working_dir, abs_path)
  3863. self.assertEqual(result, abs_path)
  3864. def test_expand_path_with_tilde(self):
  3865. test_path = '~/otherdir/somefile'
  3866. with mock.patch.dict(os.environ):
  3867. os.environ['HOME'] = user_path = '/home/user/'
  3868. result = config.expand_path(self.working_dir, test_path)
  3869. self.assertEqual(result, user_path + 'otherdir/somefile')
  3870. class VolumePathTest(unittest.TestCase):
  3871. def test_split_path_mapping_with_windows_path(self):
  3872. host_path = "c:\\Users\\msamblanet\\Documents\\anvil\\connect\\config"
  3873. windows_volume_path = host_path + ":/opt/connect/config:ro"
  3874. expected_mapping = ("/opt/connect/config", (host_path, 'ro'))
  3875. mapping = config.split_path_mapping(windows_volume_path)
  3876. assert mapping == expected_mapping
  3877. def test_split_path_mapping_with_windows_path_in_container(self):
  3878. host_path = 'c:\\Users\\remilia\\data'
  3879. container_path = 'c:\\scarletdevil\\data'
  3880. expected_mapping = (container_path, (host_path, None))
  3881. mapping = config.split_path_mapping('{0}:{1}'.format(host_path, container_path))
  3882. assert mapping == expected_mapping
  3883. def test_split_path_mapping_with_root_mount(self):
  3884. host_path = '/'
  3885. container_path = '/var/hostroot'
  3886. expected_mapping = (container_path, (host_path, None))
  3887. mapping = config.split_path_mapping('{0}:{1}'.format(host_path, container_path))
  3888. assert mapping == expected_mapping
  3889. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  3890. class BuildPathTest(unittest.TestCase):
  3891. def setUp(self):
  3892. self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
  3893. def test_nonexistent_path(self):
  3894. with self.assertRaises(ConfigurationError):
  3895. config.load(
  3896. build_config_details(
  3897. {
  3898. 'foo': {'build': 'nonexistent.path'},
  3899. },
  3900. 'working_dir',
  3901. 'filename.yml'
  3902. )
  3903. )
  3904. def test_relative_path(self):
  3905. relative_build_path = '../build-ctx/'
  3906. service_dict = make_service_dict(
  3907. 'relpath',
  3908. {'build': relative_build_path},
  3909. working_dir='tests/fixtures/build-path'
  3910. )
  3911. self.assertEqual(service_dict['build'], self.abs_context_path)
  3912. def test_absolute_path(self):
  3913. service_dict = make_service_dict(
  3914. 'abspath',
  3915. {'build': self.abs_context_path},
  3916. working_dir='tests/fixtures/build-path'
  3917. )
  3918. self.assertEqual(service_dict['build'], self.abs_context_path)
  3919. def test_from_file(self):
  3920. service_dict = load_from_filename('tests/fixtures/build-path/docker-compose.yml')
  3921. self.assertEqual(service_dict, [{'name': 'foo', 'build': {'context': self.abs_context_path}}])
  3922. def test_from_file_override_dir(self):
  3923. override_dir = os.path.join(os.getcwd(), 'tests/fixtures/')
  3924. service_dict = load_from_filename(
  3925. 'tests/fixtures/build-path-override-dir/docker-compose.yml', override_dir=override_dir)
  3926. self.assertEquals(service_dict, [{'name': 'foo', 'build': {'context': self.abs_context_path}}])
  3927. def test_valid_url_in_build_path(self):
  3928. valid_urls = [
  3929. 'git://github.com/docker/docker',
  3930. '[email protected]:docker/docker.git',
  3931. '[email protected]:atlassianlabs/atlassian-docker.git',
  3932. 'https://github.com/docker/docker.git',
  3933. 'http://github.com/docker/docker.git',
  3934. 'github.com/docker/docker.git',
  3935. ]
  3936. for valid_url in valid_urls:
  3937. service_dict = config.load(build_config_details({
  3938. 'validurl': {'build': valid_url},
  3939. }, '.', None)).services
  3940. assert service_dict[0]['build'] == {'context': valid_url}
  3941. def test_invalid_url_in_build_path(self):
  3942. invalid_urls = [
  3943. 'example.com/bogus',
  3944. 'ftp://example.com/',
  3945. '/path/does/not/exist',
  3946. ]
  3947. for invalid_url in invalid_urls:
  3948. with pytest.raises(ConfigurationError) as exc:
  3949. config.load(build_config_details({
  3950. 'invalidurl': {'build': invalid_url},
  3951. }, '.', None))
  3952. assert 'build path' in exc.exconly()
  3953. class HealthcheckTest(unittest.TestCase):
  3954. def test_healthcheck(self):
  3955. config_dict = config.load(
  3956. build_config_details({
  3957. 'version': '2.3',
  3958. 'services': {
  3959. 'test': {
  3960. 'image': 'busybox',
  3961. 'healthcheck': {
  3962. 'test': ['CMD', 'true'],
  3963. 'interval': '1s',
  3964. 'timeout': '1m',
  3965. 'retries': 3,
  3966. 'start_period': '10s',
  3967. }
  3968. }
  3969. }
  3970. })
  3971. )
  3972. serialized_config = yaml.load(serialize_config(config_dict))
  3973. serialized_service = serialized_config['services']['test']
  3974. assert serialized_service['healthcheck'] == {
  3975. 'test': ['CMD', 'true'],
  3976. 'interval': '1s',
  3977. 'timeout': '1m',
  3978. 'retries': 3,
  3979. 'start_period': '10s'
  3980. }
  3981. def test_disable(self):
  3982. config_dict = config.load(
  3983. build_config_details({
  3984. 'version': '2.3',
  3985. 'services': {
  3986. 'test': {
  3987. 'image': 'busybox',
  3988. 'healthcheck': {
  3989. 'disable': True,
  3990. }
  3991. }
  3992. }
  3993. })
  3994. )
  3995. serialized_config = yaml.load(serialize_config(config_dict))
  3996. serialized_service = serialized_config['services']['test']
  3997. assert serialized_service['healthcheck'] == {
  3998. 'test': ['NONE'],
  3999. }
  4000. def test_disable_with_other_config_is_invalid(self):
  4001. with pytest.raises(ConfigurationError) as excinfo:
  4002. config.load(
  4003. build_config_details({
  4004. 'version': '2.3',
  4005. 'services': {
  4006. 'invalid-healthcheck': {
  4007. 'image': 'busybox',
  4008. 'healthcheck': {
  4009. 'disable': True,
  4010. 'interval': '1s',
  4011. }
  4012. }
  4013. }
  4014. })
  4015. )
  4016. assert 'invalid-healthcheck' in excinfo.exconly()
  4017. assert '"disable: true" cannot be combined with other options' in excinfo.exconly()
  4018. def test_healthcheck_with_invalid_test(self):
  4019. with pytest.raises(ConfigurationError) as excinfo:
  4020. config.load(
  4021. build_config_details({
  4022. 'version': '2.3',
  4023. 'services': {
  4024. 'invalid-healthcheck': {
  4025. 'image': 'busybox',
  4026. 'healthcheck': {
  4027. 'test': ['true'],
  4028. 'interval': '1s',
  4029. 'timeout': '1m',
  4030. 'retries': 3,
  4031. 'start_period': '10s',
  4032. }
  4033. }
  4034. }
  4035. })
  4036. )
  4037. assert 'invalid-healthcheck' in excinfo.exconly()
  4038. assert 'the first item must be either NONE, CMD or CMD-SHELL' in excinfo.exconly()
  4039. class GetDefaultConfigFilesTestCase(unittest.TestCase):
  4040. files = [
  4041. 'docker-compose.yml',
  4042. 'docker-compose.yaml',
  4043. ]
  4044. def test_get_config_path_default_file_in_basedir(self):
  4045. for index, filename in enumerate(self.files):
  4046. self.assertEqual(
  4047. filename,
  4048. get_config_filename_for_files(self.files[index:]))
  4049. with self.assertRaises(config.ComposeFileNotFound):
  4050. get_config_filename_for_files([])
  4051. def test_get_config_path_default_file_in_parent_dir(self):
  4052. """Test with files placed in the subdir"""
  4053. def get_config_in_subdir(files):
  4054. return get_config_filename_for_files(files, subdir=True)
  4055. for index, filename in enumerate(self.files):
  4056. self.assertEqual(filename, get_config_in_subdir(self.files[index:]))
  4057. with self.assertRaises(config.ComposeFileNotFound):
  4058. get_config_in_subdir([])
  4059. def get_config_filename_for_files(filenames, subdir=None):
  4060. def make_files(dirname, filenames):
  4061. for fname in filenames:
  4062. with open(os.path.join(dirname, fname), 'w') as f:
  4063. f.write('')
  4064. project_dir = tempfile.mkdtemp()
  4065. try:
  4066. make_files(project_dir, filenames)
  4067. if subdir:
  4068. base_dir = tempfile.mkdtemp(dir=project_dir)
  4069. else:
  4070. base_dir = project_dir
  4071. filename, = config.get_default_config_files(base_dir)
  4072. return os.path.basename(filename)
  4073. finally:
  4074. shutil.rmtree(project_dir)
  4075. class SerializeTest(unittest.TestCase):
  4076. def test_denormalize_depends_on_v3(self):
  4077. service_dict = {
  4078. 'image': 'busybox',
  4079. 'command': 'true',
  4080. 'depends_on': {
  4081. 'service2': {'condition': 'service_started'},
  4082. 'service3': {'condition': 'service_started'},
  4083. }
  4084. }
  4085. assert denormalize_service_dict(service_dict, V3_0) == {
  4086. 'image': 'busybox',
  4087. 'command': 'true',
  4088. 'depends_on': ['service2', 'service3']
  4089. }
  4090. def test_denormalize_depends_on_v2_1(self):
  4091. service_dict = {
  4092. 'image': 'busybox',
  4093. 'command': 'true',
  4094. 'depends_on': {
  4095. 'service2': {'condition': 'service_started'},
  4096. 'service3': {'condition': 'service_started'},
  4097. }
  4098. }
  4099. assert denormalize_service_dict(service_dict, V2_1) == service_dict
  4100. def test_serialize_time(self):
  4101. data = {
  4102. 9: '9ns',
  4103. 9000: '9us',
  4104. 9000000: '9ms',
  4105. 90000000: '90ms',
  4106. 900000000: '900ms',
  4107. 999999999: '999999999ns',
  4108. 1000000000: '1s',
  4109. 60000000000: '1m',
  4110. 60000000001: '60000000001ns',
  4111. 9000000000000: '150m',
  4112. 90000000000000: '25h',
  4113. }
  4114. for k, v in data.items():
  4115. assert serialize_ns_time_value(k) == v
  4116. def test_denormalize_healthcheck(self):
  4117. service_dict = {
  4118. 'image': 'test',
  4119. 'healthcheck': {
  4120. 'test': 'exit 1',
  4121. 'interval': '1m40s',
  4122. 'timeout': '30s',
  4123. 'retries': 5,
  4124. 'start_period': '2s90ms'
  4125. }
  4126. }
  4127. processed_service = config.process_service(config.ServiceConfig(
  4128. '.', 'test', 'test', service_dict
  4129. ))
  4130. denormalized_service = denormalize_service_dict(processed_service, V2_3)
  4131. assert denormalized_service['healthcheck']['interval'] == '100s'
  4132. assert denormalized_service['healthcheck']['timeout'] == '30s'
  4133. assert denormalized_service['healthcheck']['start_period'] == '2090ms'
  4134. def test_denormalize_image_has_digest(self):
  4135. service_dict = {
  4136. 'image': 'busybox'
  4137. }
  4138. image_digest = 'busybox@sha256:abcde'
  4139. assert denormalize_service_dict(service_dict, V3_0, image_digest) == {
  4140. 'image': 'busybox@sha256:abcde'
  4141. }
  4142. def test_denormalize_image_no_digest(self):
  4143. service_dict = {
  4144. 'image': 'busybox'
  4145. }
  4146. assert denormalize_service_dict(service_dict, V3_0) == {
  4147. 'image': 'busybox'
  4148. }
  4149. def test_serialize_secrets(self):
  4150. service_dict = {
  4151. 'image': 'example/web',
  4152. 'secrets': [
  4153. {'source': 'one'},
  4154. {
  4155. 'source': 'source',
  4156. 'target': 'target',
  4157. 'uid': '100',
  4158. 'gid': '200',
  4159. 'mode': 0o777,
  4160. }
  4161. ]
  4162. }
  4163. secrets_dict = {
  4164. 'one': {'file': '/one.txt'},
  4165. 'source': {'file': '/source.pem'},
  4166. 'two': {'external': True},
  4167. }
  4168. config_dict = config.load(build_config_details({
  4169. 'version': '3.1',
  4170. 'services': {'web': service_dict},
  4171. 'secrets': secrets_dict
  4172. }))
  4173. serialized_config = yaml.load(serialize_config(config_dict))
  4174. serialized_service = serialized_config['services']['web']
  4175. assert secret_sort(serialized_service['secrets']) == secret_sort(service_dict['secrets'])
  4176. assert 'secrets' in serialized_config
  4177. assert serialized_config['secrets']['two'] == secrets_dict['two']
  4178. def test_serialize_ports(self):
  4179. config_dict = config.Config(version=V2_0, services=[
  4180. {
  4181. 'ports': [types.ServicePort('80', '8080', None, None, None)],
  4182. 'image': 'alpine',
  4183. 'name': 'web'
  4184. }
  4185. ], volumes={}, networks={}, secrets={}, configs={})
  4186. serialized_config = yaml.load(serialize_config(config_dict))
  4187. assert '8080:80/tcp' in serialized_config['services']['web']['ports']
  4188. def test_serialize_configs(self):
  4189. service_dict = {
  4190. 'image': 'example/web',
  4191. 'configs': [
  4192. {'source': 'one'},
  4193. {
  4194. 'source': 'source',
  4195. 'target': 'target',
  4196. 'uid': '100',
  4197. 'gid': '200',
  4198. 'mode': 0o777,
  4199. }
  4200. ]
  4201. }
  4202. configs_dict = {
  4203. 'one': {'file': '/one.txt'},
  4204. 'source': {'file': '/source.pem'},
  4205. 'two': {'external': True},
  4206. }
  4207. config_dict = config.load(build_config_details({
  4208. 'version': '3.3',
  4209. 'services': {'web': service_dict},
  4210. 'configs': configs_dict
  4211. }))
  4212. serialized_config = yaml.load(serialize_config(config_dict))
  4213. serialized_service = serialized_config['services']['web']
  4214. assert secret_sort(serialized_service['configs']) == secret_sort(service_dict['configs'])
  4215. assert 'configs' in serialized_config
  4216. assert serialized_config['configs']['two'] == configs_dict['two']
  4217. def test_serialize_bool_string(self):
  4218. cfg = {
  4219. 'version': '2.2',
  4220. 'services': {
  4221. 'web': {
  4222. 'image': 'example/web',
  4223. 'command': 'true',
  4224. 'environment': {'FOO': 'Y', 'BAR': 'on'}
  4225. }
  4226. }
  4227. }
  4228. config_dict = config.load(build_config_details(cfg))
  4229. serialized_config = serialize_config(config_dict)
  4230. assert 'command: "true"\n' in serialized_config
  4231. assert 'FOO: "Y"\n' in serialized_config
  4232. assert 'BAR: "on"\n' in serialized_config
  4233. def test_serialize_escape_dollar_sign(self):
  4234. cfg = {
  4235. 'version': '2.2',
  4236. 'services': {
  4237. 'web': {
  4238. 'image': 'busybox',
  4239. 'command': 'echo $$FOO',
  4240. 'environment': {
  4241. 'CURRENCY': '$$'
  4242. },
  4243. 'entrypoint': ['$$SHELL', '-c'],
  4244. }
  4245. }
  4246. }
  4247. config_dict = config.load(build_config_details(cfg))
  4248. serialized_config = yaml.load(serialize_config(config_dict))
  4249. serialized_service = serialized_config['services']['web']
  4250. assert serialized_service['environment']['CURRENCY'] == '$$'
  4251. assert serialized_service['command'] == 'echo $$FOO'
  4252. assert serialized_service['entrypoint'][0] == '$$SHELL'