config_test.py 155 KB

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