config_test.py 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407
  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. from ...helpers import build_config_details
  12. from compose.config import config
  13. from compose.config.config import resolve_build_args
  14. from compose.config.config import resolve_environment
  15. from compose.config.config import V1
  16. from compose.config.config import V2_0
  17. from compose.config.config import V2_1
  18. from compose.config.config import V3_0
  19. from compose.config.config import V3_1
  20. from compose.config.environment import Environment
  21. from compose.config.errors import ConfigurationError
  22. from compose.config.errors import VERSION_EXPLANATION
  23. from compose.config.serialize import denormalize_service_dict
  24. from compose.config.serialize import serialize_ns_time_value
  25. from compose.config.types import VolumeSpec
  26. from compose.const import IS_WINDOWS_PLATFORM
  27. from compose.utils import nanoseconds_from_time_seconds
  28. from tests import mock
  29. from tests import unittest
  30. DEFAULT_VERSION = V2_0
  31. def make_service_dict(name, service_dict, working_dir, filename=None):
  32. """Test helper function to construct a ServiceExtendsResolver
  33. """
  34. resolver = config.ServiceExtendsResolver(
  35. config.ServiceConfig(
  36. working_dir=working_dir,
  37. filename=filename,
  38. name=name,
  39. config=service_dict),
  40. config.ConfigFile(filename=filename, config={}),
  41. environment=Environment.from_env_file(working_dir)
  42. )
  43. return config.process_service(resolver.run())
  44. def service_sort(services):
  45. return sorted(services, key=itemgetter('name'))
  46. class ConfigTest(unittest.TestCase):
  47. def test_load(self):
  48. service_dicts = config.load(
  49. build_config_details(
  50. {
  51. 'foo': {'image': 'busybox'},
  52. 'bar': {'image': 'busybox', 'environment': ['FOO=1']},
  53. },
  54. 'tests/fixtures/extends',
  55. 'common.yml'
  56. )
  57. ).services
  58. self.assertEqual(
  59. service_sort(service_dicts),
  60. service_sort([
  61. {
  62. 'name': 'bar',
  63. 'image': 'busybox',
  64. 'environment': {'FOO': '1'},
  65. },
  66. {
  67. 'name': 'foo',
  68. 'image': 'busybox',
  69. }
  70. ])
  71. )
  72. def test_load_v2(self):
  73. config_data = config.load(
  74. build_config_details({
  75. 'version': '2',
  76. 'services': {
  77. 'foo': {'image': 'busybox'},
  78. 'bar': {'image': 'busybox', 'environment': ['FOO=1']},
  79. },
  80. 'volumes': {
  81. 'hello': {
  82. 'driver': 'default',
  83. 'driver_opts': {'beep': 'boop'}
  84. }
  85. },
  86. 'networks': {
  87. 'default': {
  88. 'driver': 'bridge',
  89. 'driver_opts': {'beep': 'boop'}
  90. },
  91. 'with_ipam': {
  92. 'ipam': {
  93. 'driver': 'default',
  94. 'config': [
  95. {'subnet': '172.28.0.0/16'}
  96. ]
  97. }
  98. },
  99. 'internal': {
  100. 'driver': 'bridge',
  101. 'internal': True
  102. }
  103. }
  104. }, 'working_dir', 'filename.yml')
  105. )
  106. service_dicts = config_data.services
  107. volume_dict = config_data.volumes
  108. networks_dict = config_data.networks
  109. self.assertEqual(
  110. service_sort(service_dicts),
  111. service_sort([
  112. {
  113. 'name': 'bar',
  114. 'image': 'busybox',
  115. 'environment': {'FOO': '1'},
  116. },
  117. {
  118. 'name': 'foo',
  119. 'image': 'busybox',
  120. }
  121. ])
  122. )
  123. self.assertEqual(volume_dict, {
  124. 'hello': {
  125. 'driver': 'default',
  126. 'driver_opts': {'beep': 'boop'}
  127. }
  128. })
  129. self.assertEqual(networks_dict, {
  130. 'default': {
  131. 'driver': 'bridge',
  132. 'driver_opts': {'beep': 'boop'}
  133. },
  134. 'with_ipam': {
  135. 'ipam': {
  136. 'driver': 'default',
  137. 'config': [
  138. {'subnet': '172.28.0.0/16'}
  139. ]
  140. }
  141. },
  142. 'internal': {
  143. 'driver': 'bridge',
  144. 'internal': True
  145. }
  146. })
  147. def test_valid_versions(self):
  148. for version in ['2', '2.0']:
  149. cfg = config.load(build_config_details({'version': version}))
  150. assert cfg.version == V2_0
  151. cfg = config.load(build_config_details({'version': '2.1'}))
  152. assert cfg.version == V2_1
  153. for version in ['3', '3.0']:
  154. cfg = config.load(build_config_details({'version': version}))
  155. assert cfg.version == V3_0
  156. cfg = config.load(build_config_details({'version': '3.1'}))
  157. assert cfg.version == V3_1
  158. def test_v1_file_version(self):
  159. cfg = config.load(build_config_details({'web': {'image': 'busybox'}}))
  160. assert cfg.version == V1
  161. assert list(s['name'] for s in cfg.services) == ['web']
  162. cfg = config.load(build_config_details({'version': {'image': 'busybox'}}))
  163. assert cfg.version == V1
  164. assert list(s['name'] for s in cfg.services) == ['version']
  165. def test_wrong_version_type(self):
  166. for version in [None, 1, 2, 2.0]:
  167. with pytest.raises(ConfigurationError) as excinfo:
  168. config.load(
  169. build_config_details(
  170. {'version': version},
  171. filename='filename.yml',
  172. )
  173. )
  174. assert 'Version in "filename.yml" is invalid - it should be a string.' \
  175. in excinfo.exconly()
  176. def test_unsupported_version(self):
  177. with pytest.raises(ConfigurationError) as excinfo:
  178. config.load(
  179. build_config_details(
  180. {'version': '2.18'},
  181. filename='filename.yml',
  182. )
  183. )
  184. assert 'Version in "filename.yml" is unsupported' in excinfo.exconly()
  185. assert VERSION_EXPLANATION in excinfo.exconly()
  186. def test_version_1_is_invalid(self):
  187. with pytest.raises(ConfigurationError) as excinfo:
  188. config.load(
  189. build_config_details(
  190. {
  191. 'version': '1',
  192. 'web': {'image': 'busybox'},
  193. },
  194. filename='filename.yml',
  195. )
  196. )
  197. assert 'Version in "filename.yml" is invalid' in excinfo.exconly()
  198. assert VERSION_EXPLANATION in excinfo.exconly()
  199. def test_v1_file_with_version_is_invalid(self):
  200. with pytest.raises(ConfigurationError) as excinfo:
  201. config.load(
  202. build_config_details(
  203. {
  204. 'version': '2',
  205. 'web': {'image': 'busybox'},
  206. },
  207. filename='filename.yml',
  208. )
  209. )
  210. assert 'Additional properties are not allowed' in excinfo.exconly()
  211. assert VERSION_EXPLANATION in excinfo.exconly()
  212. def test_named_volume_config_empty(self):
  213. config_details = build_config_details({
  214. 'version': '2',
  215. 'services': {
  216. 'simple': {'image': 'busybox'}
  217. },
  218. 'volumes': {
  219. 'simple': None,
  220. 'other': {},
  221. }
  222. })
  223. config_result = config.load(config_details)
  224. volumes = config_result.volumes
  225. assert 'simple' in volumes
  226. assert volumes['simple'] == {}
  227. assert volumes['other'] == {}
  228. def test_named_volume_numeric_driver_opt(self):
  229. config_details = build_config_details({
  230. 'version': '2',
  231. 'services': {
  232. 'simple': {'image': 'busybox'}
  233. },
  234. 'volumes': {
  235. 'simple': {'driver_opts': {'size': 42}},
  236. }
  237. })
  238. cfg = config.load(config_details)
  239. assert cfg.volumes['simple']['driver_opts']['size'] == '42'
  240. def test_volume_invalid_driver_opt(self):
  241. config_details = build_config_details({
  242. 'version': '2',
  243. 'services': {
  244. 'simple': {'image': 'busybox'}
  245. },
  246. 'volumes': {
  247. 'simple': {'driver_opts': {'size': True}},
  248. }
  249. })
  250. with pytest.raises(ConfigurationError) as exc:
  251. config.load(config_details)
  252. assert 'driver_opts.size contains an invalid type' in exc.exconly()
  253. def test_named_volume_invalid_type_list(self):
  254. config_details = build_config_details({
  255. 'version': '2',
  256. 'services': {
  257. 'simple': {'image': 'busybox'}
  258. },
  259. 'volumes': []
  260. })
  261. with pytest.raises(ConfigurationError) as exc:
  262. config.load(config_details)
  263. assert "volume must be a mapping, not an array" in exc.exconly()
  264. def test_networks_invalid_type_list(self):
  265. config_details = build_config_details({
  266. 'version': '2',
  267. 'services': {
  268. 'simple': {'image': 'busybox'}
  269. },
  270. 'networks': []
  271. })
  272. with pytest.raises(ConfigurationError) as exc:
  273. config.load(config_details)
  274. assert "network must be a mapping, not an array" in exc.exconly()
  275. def test_load_service_with_name_version(self):
  276. with mock.patch('compose.config.config.log') as mock_logging:
  277. config_data = config.load(
  278. build_config_details({
  279. 'version': {
  280. 'image': 'busybox'
  281. }
  282. }, 'working_dir', 'filename.yml')
  283. )
  284. assert 'Unexpected type for "version" key in "filename.yml"' \
  285. in mock_logging.warn.call_args[0][0]
  286. service_dicts = config_data.services
  287. self.assertEqual(
  288. service_sort(service_dicts),
  289. service_sort([
  290. {
  291. 'name': 'version',
  292. 'image': 'busybox',
  293. }
  294. ])
  295. )
  296. def test_load_throws_error_when_not_dict(self):
  297. with self.assertRaises(ConfigurationError):
  298. config.load(
  299. build_config_details(
  300. {'web': 'busybox:latest'},
  301. 'working_dir',
  302. 'filename.yml'
  303. )
  304. )
  305. def test_load_throws_error_when_not_dict_v2(self):
  306. with self.assertRaises(ConfigurationError):
  307. config.load(
  308. build_config_details(
  309. {'version': '2', 'services': {'web': 'busybox:latest'}},
  310. 'working_dir',
  311. 'filename.yml'
  312. )
  313. )
  314. def test_load_throws_error_with_invalid_network_fields(self):
  315. with self.assertRaises(ConfigurationError):
  316. config.load(
  317. build_config_details({
  318. 'version': '2',
  319. 'services': {'web': 'busybox:latest'},
  320. 'networks': {
  321. 'invalid': {'foo', 'bar'}
  322. }
  323. }, 'working_dir', 'filename.yml')
  324. )
  325. def test_load_config_link_local_ips_network(self):
  326. base_file = config.ConfigFile(
  327. 'base.yaml',
  328. {
  329. 'version': V2_1,
  330. 'services': {
  331. 'web': {
  332. 'image': 'example/web',
  333. 'networks': {
  334. 'foobar': {
  335. 'aliases': ['foo', 'bar'],
  336. 'link_local_ips': ['169.254.8.8']
  337. }
  338. }
  339. }
  340. },
  341. 'networks': {'foobar': {}}
  342. }
  343. )
  344. details = config.ConfigDetails('.', [base_file])
  345. web_service = config.load(details).services[0]
  346. assert web_service['networks'] == {
  347. 'foobar': {
  348. 'aliases': ['foo', 'bar'],
  349. 'link_local_ips': ['169.254.8.8']
  350. }
  351. }
  352. def test_load_config_volume_and_network_labels(self):
  353. base_file = config.ConfigFile(
  354. 'base.yaml',
  355. {
  356. 'version': '2.1',
  357. 'services': {
  358. 'web': {
  359. 'image': 'example/web',
  360. },
  361. },
  362. 'networks': {
  363. 'with_label': {
  364. 'labels': {
  365. 'label_key': 'label_val'
  366. }
  367. }
  368. },
  369. 'volumes': {
  370. 'with_label': {
  371. 'labels': {
  372. 'label_key': 'label_val'
  373. }
  374. }
  375. }
  376. }
  377. )
  378. details = config.ConfigDetails('.', [base_file])
  379. network_dict = config.load(details).networks
  380. volume_dict = config.load(details).volumes
  381. self.assertEqual(
  382. network_dict,
  383. {
  384. 'with_label': {
  385. 'labels': {
  386. 'label_key': 'label_val'
  387. }
  388. }
  389. }
  390. )
  391. self.assertEqual(
  392. volume_dict,
  393. {
  394. 'with_label': {
  395. 'labels': {
  396. 'label_key': 'label_val'
  397. }
  398. }
  399. }
  400. )
  401. def test_load_config_invalid_service_names(self):
  402. for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
  403. with pytest.raises(ConfigurationError) as exc:
  404. config.load(build_config_details(
  405. {invalid_name: {'image': 'busybox'}}))
  406. assert 'Invalid service name \'%s\'' % invalid_name in exc.exconly()
  407. def test_load_config_invalid_service_names_v2(self):
  408. for invalid_name in ['?not?allowed', ' ', '', '!', '/', '\xe2']:
  409. with pytest.raises(ConfigurationError) as exc:
  410. config.load(build_config_details(
  411. {
  412. 'version': '2',
  413. 'services': {invalid_name: {'image': 'busybox'}},
  414. }))
  415. assert 'Invalid service name \'%s\'' % invalid_name in exc.exconly()
  416. def test_load_with_invalid_field_name(self):
  417. with pytest.raises(ConfigurationError) as exc:
  418. config.load(build_config_details(
  419. {
  420. 'version': '2',
  421. 'services': {
  422. 'web': {'image': 'busybox', 'name': 'bogus'},
  423. }
  424. },
  425. 'working_dir',
  426. 'filename.yml',
  427. ))
  428. assert "Unsupported config option for services.web: 'name'" in exc.exconly()
  429. def test_load_with_invalid_field_name_v1(self):
  430. with pytest.raises(ConfigurationError) as exc:
  431. config.load(build_config_details(
  432. {
  433. 'web': {'image': 'busybox', 'name': 'bogus'},
  434. },
  435. 'working_dir',
  436. 'filename.yml',
  437. ))
  438. assert "Unsupported config option for web: 'name'" in exc.exconly()
  439. def test_load_invalid_service_definition(self):
  440. config_details = build_config_details(
  441. {'web': 'wrong'},
  442. 'working_dir',
  443. 'filename.yml')
  444. with pytest.raises(ConfigurationError) as exc:
  445. config.load(config_details)
  446. assert "service 'web' must be a mapping not a string." in exc.exconly()
  447. def test_load_with_empty_build_args(self):
  448. config_details = build_config_details(
  449. {
  450. 'version': '2',
  451. 'services': {
  452. 'web': {
  453. 'build': {
  454. 'context': '.',
  455. 'args': None,
  456. },
  457. },
  458. },
  459. }
  460. )
  461. with pytest.raises(ConfigurationError) as exc:
  462. config.load(config_details)
  463. assert (
  464. "services.web.build.args contains an invalid type, it should be an "
  465. "object, or an array" in exc.exconly()
  466. )
  467. def test_config_integer_service_name_raise_validation_error(self):
  468. with pytest.raises(ConfigurationError) as excinfo:
  469. config.load(
  470. build_config_details(
  471. {1: {'image': 'busybox'}},
  472. 'working_dir',
  473. 'filename.yml'
  474. )
  475. )
  476. assert (
  477. "In file 'filename.yml', the service name 1 must be a quoted string, i.e. '1'" in
  478. excinfo.exconly()
  479. )
  480. def test_config_integer_service_name_raise_validation_error_v2(self):
  481. with pytest.raises(ConfigurationError) as excinfo:
  482. config.load(
  483. build_config_details(
  484. {
  485. 'version': '2',
  486. 'services': {1: {'image': 'busybox'}}
  487. },
  488. 'working_dir',
  489. 'filename.yml'
  490. )
  491. )
  492. assert (
  493. "In file 'filename.yml', the service name 1 must be a quoted string, i.e. '1'." in
  494. excinfo.exconly()
  495. )
  496. def test_load_with_multiple_files_v1(self):
  497. base_file = config.ConfigFile(
  498. 'base.yaml',
  499. {
  500. 'web': {
  501. 'image': 'example/web',
  502. 'links': ['db'],
  503. },
  504. 'db': {
  505. 'image': 'example/db',
  506. },
  507. })
  508. override_file = config.ConfigFile(
  509. 'override.yaml',
  510. {
  511. 'web': {
  512. 'build': '/',
  513. 'volumes': ['/home/user/project:/code'],
  514. },
  515. })
  516. details = config.ConfigDetails('.', [base_file, override_file])
  517. service_dicts = config.load(details).services
  518. expected = [
  519. {
  520. 'name': 'web',
  521. 'build': {'context': os.path.abspath('/')},
  522. 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
  523. 'links': ['db'],
  524. },
  525. {
  526. 'name': 'db',
  527. 'image': 'example/db',
  528. },
  529. ]
  530. assert service_sort(service_dicts) == service_sort(expected)
  531. def test_load_with_multiple_files_and_empty_override(self):
  532. base_file = config.ConfigFile(
  533. 'base.yml',
  534. {'web': {'image': 'example/web'}})
  535. override_file = config.ConfigFile('override.yml', None)
  536. details = config.ConfigDetails('.', [base_file, override_file])
  537. with pytest.raises(ConfigurationError) as exc:
  538. config.load(details)
  539. error_msg = "Top level object in 'override.yml' needs to be an object"
  540. assert error_msg in exc.exconly()
  541. def test_load_with_multiple_files_and_empty_override_v2(self):
  542. base_file = config.ConfigFile(
  543. 'base.yml',
  544. {'version': '2', 'services': {'web': {'image': 'example/web'}}})
  545. override_file = config.ConfigFile('override.yml', None)
  546. details = config.ConfigDetails('.', [base_file, override_file])
  547. with pytest.raises(ConfigurationError) as exc:
  548. config.load(details)
  549. error_msg = "Top level object in 'override.yml' needs to be an object"
  550. assert error_msg in exc.exconly()
  551. def test_load_with_multiple_files_and_empty_base(self):
  552. base_file = config.ConfigFile('base.yml', None)
  553. override_file = config.ConfigFile(
  554. 'override.yml',
  555. {'web': {'image': 'example/web'}})
  556. details = config.ConfigDetails('.', [base_file, override_file])
  557. with pytest.raises(ConfigurationError) as exc:
  558. config.load(details)
  559. assert "Top level object in 'base.yml' needs to be an object" in exc.exconly()
  560. def test_load_with_multiple_files_and_empty_base_v2(self):
  561. base_file = config.ConfigFile('base.yml', None)
  562. override_file = config.ConfigFile(
  563. 'override.tml',
  564. {'version': '2', 'services': {'web': {'image': 'example/web'}}}
  565. )
  566. details = config.ConfigDetails('.', [base_file, override_file])
  567. with pytest.raises(ConfigurationError) as exc:
  568. config.load(details)
  569. assert "Top level object in 'base.yml' needs to be an object" in exc.exconly()
  570. def test_load_with_multiple_files_and_extends_in_override_file(self):
  571. base_file = config.ConfigFile(
  572. 'base.yaml',
  573. {
  574. 'web': {'image': 'example/web'},
  575. })
  576. override_file = config.ConfigFile(
  577. 'override.yaml',
  578. {
  579. 'web': {
  580. 'extends': {
  581. 'file': 'common.yml',
  582. 'service': 'base',
  583. },
  584. 'volumes': ['/home/user/project:/code'],
  585. },
  586. })
  587. details = config.ConfigDetails('.', [base_file, override_file])
  588. tmpdir = py.test.ensuretemp('config_test')
  589. self.addCleanup(tmpdir.remove)
  590. tmpdir.join('common.yml').write("""
  591. base:
  592. labels: ['label=one']
  593. """)
  594. with tmpdir.as_cwd():
  595. service_dicts = config.load(details).services
  596. expected = [
  597. {
  598. 'name': 'web',
  599. 'image': 'example/web',
  600. 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
  601. 'labels': {'label': 'one'},
  602. },
  603. ]
  604. self.assertEqual(service_sort(service_dicts), service_sort(expected))
  605. def test_load_with_multiple_files_and_invalid_override(self):
  606. base_file = config.ConfigFile(
  607. 'base.yaml',
  608. {'web': {'image': 'example/web'}})
  609. override_file = config.ConfigFile(
  610. 'override.yaml',
  611. {'bogus': 'thing'})
  612. details = config.ConfigDetails('.', [base_file, override_file])
  613. with pytest.raises(ConfigurationError) as exc:
  614. config.load(details)
  615. assert "service 'bogus' must be a mapping not a string." in exc.exconly()
  616. assert "In file 'override.yaml'" in exc.exconly()
  617. def test_load_sorts_in_dependency_order(self):
  618. config_details = build_config_details({
  619. 'web': {
  620. 'image': 'busybox:latest',
  621. 'links': ['db'],
  622. },
  623. 'db': {
  624. 'image': 'busybox:latest',
  625. 'volumes_from': ['volume:ro']
  626. },
  627. 'volume': {
  628. 'image': 'busybox:latest',
  629. 'volumes': ['/tmp'],
  630. }
  631. })
  632. services = config.load(config_details).services
  633. assert services[0]['name'] == 'volume'
  634. assert services[1]['name'] == 'db'
  635. assert services[2]['name'] == 'web'
  636. def test_config_build_configuration(self):
  637. service = config.load(
  638. build_config_details(
  639. {'web': {
  640. 'build': '.',
  641. 'dockerfile': 'Dockerfile-alt'
  642. }},
  643. 'tests/fixtures/extends',
  644. 'filename.yml'
  645. )
  646. ).services
  647. self.assertTrue('context' in service[0]['build'])
  648. self.assertEqual(service[0]['build']['dockerfile'], 'Dockerfile-alt')
  649. def test_config_build_configuration_v2(self):
  650. # service.dockerfile is invalid in v2
  651. with self.assertRaises(ConfigurationError):
  652. config.load(
  653. build_config_details(
  654. {
  655. 'version': '2',
  656. 'services': {
  657. 'web': {
  658. 'build': '.',
  659. 'dockerfile': 'Dockerfile-alt'
  660. }
  661. }
  662. },
  663. 'tests/fixtures/extends',
  664. 'filename.yml'
  665. )
  666. )
  667. service = config.load(
  668. build_config_details({
  669. 'version': '2',
  670. 'services': {
  671. 'web': {
  672. 'build': '.'
  673. }
  674. }
  675. }, 'tests/fixtures/extends', 'filename.yml')
  676. ).services[0]
  677. self.assertTrue('context' in service['build'])
  678. service = config.load(
  679. build_config_details(
  680. {
  681. 'version': '2',
  682. 'services': {
  683. 'web': {
  684. 'build': {
  685. 'context': '.',
  686. 'dockerfile': 'Dockerfile-alt'
  687. }
  688. }
  689. }
  690. },
  691. 'tests/fixtures/extends',
  692. 'filename.yml'
  693. )
  694. ).services
  695. self.assertTrue('context' in service[0]['build'])
  696. self.assertEqual(service[0]['build']['dockerfile'], 'Dockerfile-alt')
  697. def test_load_with_buildargs(self):
  698. service = config.load(
  699. build_config_details(
  700. {
  701. 'version': '2',
  702. 'services': {
  703. 'web': {
  704. 'build': {
  705. 'context': '.',
  706. 'dockerfile': 'Dockerfile-alt',
  707. 'args': {
  708. 'opt1': 42,
  709. 'opt2': 'foobar'
  710. }
  711. }
  712. }
  713. }
  714. },
  715. 'tests/fixtures/extends',
  716. 'filename.yml'
  717. )
  718. ).services[0]
  719. assert 'args' in service['build']
  720. assert 'opt1' in service['build']['args']
  721. assert isinstance(service['build']['args']['opt1'], str)
  722. assert service['build']['args']['opt1'] == '42'
  723. assert service['build']['args']['opt2'] == 'foobar'
  724. def test_build_args_allow_empty_properties(self):
  725. service = config.load(
  726. build_config_details(
  727. {
  728. 'version': '2',
  729. 'services': {
  730. 'web': {
  731. 'build': {
  732. 'context': '.',
  733. 'dockerfile': 'Dockerfile-alt',
  734. 'args': {
  735. 'foo': None
  736. }
  737. }
  738. }
  739. }
  740. },
  741. 'tests/fixtures/extends',
  742. 'filename.yml'
  743. )
  744. ).services[0]
  745. assert 'args' in service['build']
  746. assert 'foo' in service['build']['args']
  747. assert service['build']['args']['foo'] == ''
  748. # If build argument is None then it will be converted to the empty
  749. # string. Make sure that int zero kept as it is, i.e. not converted to
  750. # the empty string
  751. def test_build_args_check_zero_preserved(self):
  752. service = config.load(
  753. build_config_details(
  754. {
  755. 'version': '2',
  756. 'services': {
  757. 'web': {
  758. 'build': {
  759. 'context': '.',
  760. 'dockerfile': 'Dockerfile-alt',
  761. 'args': {
  762. 'foo': 0
  763. }
  764. }
  765. }
  766. }
  767. },
  768. 'tests/fixtures/extends',
  769. 'filename.yml'
  770. )
  771. ).services[0]
  772. assert 'args' in service['build']
  773. assert 'foo' in service['build']['args']
  774. assert service['build']['args']['foo'] == '0'
  775. def test_load_with_multiple_files_mismatched_networks_format(self):
  776. base_file = config.ConfigFile(
  777. 'base.yaml',
  778. {
  779. 'version': '2',
  780. 'services': {
  781. 'web': {
  782. 'image': 'example/web',
  783. 'networks': {
  784. 'foobar': {'aliases': ['foo', 'bar']}
  785. }
  786. }
  787. },
  788. 'networks': {'foobar': {}, 'baz': {}}
  789. }
  790. )
  791. override_file = config.ConfigFile(
  792. 'override.yaml',
  793. {
  794. 'version': '2',
  795. 'services': {
  796. 'web': {
  797. 'networks': ['baz']
  798. }
  799. }
  800. }
  801. )
  802. details = config.ConfigDetails('.', [base_file, override_file])
  803. web_service = config.load(details).services[0]
  804. assert web_service['networks'] == {
  805. 'foobar': {'aliases': ['foo', 'bar']},
  806. 'baz': None
  807. }
  808. def test_load_with_multiple_files_v2(self):
  809. base_file = config.ConfigFile(
  810. 'base.yaml',
  811. {
  812. 'version': '2',
  813. 'services': {
  814. 'web': {
  815. 'image': 'example/web',
  816. 'depends_on': ['db'],
  817. },
  818. 'db': {
  819. 'image': 'example/db',
  820. }
  821. },
  822. })
  823. override_file = config.ConfigFile(
  824. 'override.yaml',
  825. {
  826. 'version': '2',
  827. 'services': {
  828. 'web': {
  829. 'build': '/',
  830. 'volumes': ['/home/user/project:/code'],
  831. 'depends_on': ['other'],
  832. },
  833. 'other': {
  834. 'image': 'example/other',
  835. }
  836. }
  837. })
  838. details = config.ConfigDetails('.', [base_file, override_file])
  839. service_dicts = config.load(details).services
  840. expected = [
  841. {
  842. 'name': 'web',
  843. 'build': {'context': os.path.abspath('/')},
  844. 'image': 'example/web',
  845. 'volumes': [VolumeSpec.parse('/home/user/project:/code')],
  846. 'depends_on': {
  847. 'db': {'condition': 'service_started'},
  848. 'other': {'condition': 'service_started'},
  849. },
  850. },
  851. {
  852. 'name': 'db',
  853. 'image': 'example/db',
  854. },
  855. {
  856. 'name': 'other',
  857. 'image': 'example/other',
  858. },
  859. ]
  860. assert service_sort(service_dicts) == service_sort(expected)
  861. def test_undeclared_volume_v2(self):
  862. base_file = config.ConfigFile(
  863. 'base.yaml',
  864. {
  865. 'version': '2',
  866. 'services': {
  867. 'web': {
  868. 'image': 'busybox:latest',
  869. 'volumes': ['data0028:/data:ro'],
  870. },
  871. },
  872. }
  873. )
  874. details = config.ConfigDetails('.', [base_file])
  875. with self.assertRaises(ConfigurationError):
  876. config.load(details)
  877. base_file = config.ConfigFile(
  878. 'base.yaml',
  879. {
  880. 'version': '2',
  881. 'services': {
  882. 'web': {
  883. 'image': 'busybox:latest',
  884. 'volumes': ['./data0028:/data:ro'],
  885. },
  886. },
  887. }
  888. )
  889. details = config.ConfigDetails('.', [base_file])
  890. config_data = config.load(details)
  891. volume = config_data.services[0].get('volumes')[0]
  892. assert not volume.is_named_volume
  893. def test_undeclared_volume_v1(self):
  894. base_file = config.ConfigFile(
  895. 'base.yaml',
  896. {
  897. 'web': {
  898. 'image': 'busybox:latest',
  899. 'volumes': ['data0028:/data:ro'],
  900. },
  901. }
  902. )
  903. details = config.ConfigDetails('.', [base_file])
  904. config_data = config.load(details)
  905. volume = config_data.services[0].get('volumes')[0]
  906. assert volume.external == 'data0028'
  907. assert volume.is_named_volume
  908. def test_config_valid_service_names(self):
  909. for valid_name in ['_', '-', '.__.', '_what-up.', 'what_.up----', 'whatup']:
  910. services = config.load(
  911. build_config_details(
  912. {valid_name: {'image': 'busybox'}},
  913. 'tests/fixtures/extends',
  914. 'common.yml')).services
  915. assert services[0]['name'] == valid_name
  916. def test_config_hint(self):
  917. with pytest.raises(ConfigurationError) as excinfo:
  918. config.load(
  919. build_config_details(
  920. {
  921. 'foo': {'image': 'busybox', 'privilige': 'something'},
  922. },
  923. 'tests/fixtures/extends',
  924. 'filename.yml'
  925. )
  926. )
  927. assert "(did you mean 'privileged'?)" in excinfo.exconly()
  928. def test_load_errors_on_uppercase_with_no_image(self):
  929. with pytest.raises(ConfigurationError) as exc:
  930. config.load(build_config_details({
  931. 'Foo': {'build': '.'},
  932. }, 'tests/fixtures/build-ctx'))
  933. assert "Service 'Foo' contains uppercase characters" in exc.exconly()
  934. def test_invalid_config_v1(self):
  935. with pytest.raises(ConfigurationError) as excinfo:
  936. config.load(
  937. build_config_details(
  938. {
  939. 'foo': {'image': 1},
  940. },
  941. 'tests/fixtures/extends',
  942. 'filename.yml'
  943. )
  944. )
  945. assert "foo.image contains an invalid type, it should be a string" \
  946. in excinfo.exconly()
  947. def test_invalid_config_v2(self):
  948. with pytest.raises(ConfigurationError) as excinfo:
  949. config.load(
  950. build_config_details(
  951. {
  952. 'version': '2',
  953. 'services': {
  954. 'foo': {'image': 1},
  955. },
  956. },
  957. 'tests/fixtures/extends',
  958. 'filename.yml'
  959. )
  960. )
  961. assert "services.foo.image contains an invalid type, it should be a string" \
  962. in excinfo.exconly()
  963. def test_invalid_config_build_and_image_specified_v1(self):
  964. with pytest.raises(ConfigurationError) as excinfo:
  965. config.load(
  966. build_config_details(
  967. {
  968. 'foo': {'image': 'busybox', 'build': '.'},
  969. },
  970. 'tests/fixtures/extends',
  971. 'filename.yml'
  972. )
  973. )
  974. assert "foo has both an image and build path specified." in excinfo.exconly()
  975. def test_invalid_config_type_should_be_an_array(self):
  976. with pytest.raises(ConfigurationError) as excinfo:
  977. config.load(
  978. build_config_details(
  979. {
  980. 'foo': {'image': 'busybox', 'links': 'an_link'},
  981. },
  982. 'tests/fixtures/extends',
  983. 'filename.yml'
  984. )
  985. )
  986. assert "foo.links contains an invalid type, it should be an array" \
  987. in excinfo.exconly()
  988. def test_invalid_config_not_a_dictionary(self):
  989. with pytest.raises(ConfigurationError) as excinfo:
  990. config.load(
  991. build_config_details(
  992. ['foo', 'lol'],
  993. 'tests/fixtures/extends',
  994. 'filename.yml'
  995. )
  996. )
  997. assert "Top level object in 'filename.yml' needs to be an object" \
  998. in excinfo.exconly()
  999. def test_invalid_config_not_unique_items(self):
  1000. with pytest.raises(ConfigurationError) as excinfo:
  1001. config.load(
  1002. build_config_details(
  1003. {
  1004. 'web': {'build': '.', 'devices': ['/dev/foo:/dev/foo', '/dev/foo:/dev/foo']}
  1005. },
  1006. 'tests/fixtures/extends',
  1007. 'filename.yml'
  1008. )
  1009. )
  1010. assert "has non-unique elements" in excinfo.exconly()
  1011. def test_invalid_list_of_strings_format(self):
  1012. with pytest.raises(ConfigurationError) as excinfo:
  1013. config.load(
  1014. build_config_details(
  1015. {
  1016. 'web': {'build': '.', 'command': [1]}
  1017. },
  1018. 'tests/fixtures/extends',
  1019. 'filename.yml'
  1020. )
  1021. )
  1022. assert "web.command contains 1, which is an invalid type, it should be a string" \
  1023. in excinfo.exconly()
  1024. def test_load_config_dockerfile_without_build_raises_error_v1(self):
  1025. with pytest.raises(ConfigurationError) as exc:
  1026. config.load(build_config_details({
  1027. 'web': {
  1028. 'image': 'busybox',
  1029. 'dockerfile': 'Dockerfile.alt'
  1030. }
  1031. }))
  1032. assert "web has both an image and alternate Dockerfile." in exc.exconly()
  1033. def test_config_extra_hosts_string_raises_validation_error(self):
  1034. with pytest.raises(ConfigurationError) as excinfo:
  1035. config.load(
  1036. build_config_details(
  1037. {'web': {
  1038. 'image': 'busybox',
  1039. 'extra_hosts': 'somehost:162.242.195.82'
  1040. }},
  1041. 'working_dir',
  1042. 'filename.yml'
  1043. )
  1044. )
  1045. assert "web.extra_hosts contains an invalid type" \
  1046. in excinfo.exconly()
  1047. def test_config_extra_hosts_list_of_dicts_validation_error(self):
  1048. with pytest.raises(ConfigurationError) as excinfo:
  1049. config.load(
  1050. build_config_details(
  1051. {'web': {
  1052. 'image': 'busybox',
  1053. 'extra_hosts': [
  1054. {'somehost': '162.242.195.82'},
  1055. {'otherhost': '50.31.209.229'}
  1056. ]
  1057. }},
  1058. 'working_dir',
  1059. 'filename.yml'
  1060. )
  1061. )
  1062. assert "web.extra_hosts contains {\"somehost\": \"162.242.195.82\"}, " \
  1063. "which is an invalid type, it should be a string" \
  1064. in excinfo.exconly()
  1065. def test_config_ulimits_invalid_keys_validation_error(self):
  1066. with pytest.raises(ConfigurationError) as exc:
  1067. config.load(build_config_details(
  1068. {
  1069. 'web': {
  1070. 'image': 'busybox',
  1071. 'ulimits': {
  1072. 'nofile': {
  1073. "not_soft_or_hard": 100,
  1074. "soft": 10000,
  1075. "hard": 20000,
  1076. }
  1077. }
  1078. }
  1079. },
  1080. 'working_dir',
  1081. 'filename.yml'))
  1082. assert "web.ulimits.nofile contains unsupported option: 'not_soft_or_hard'" \
  1083. in exc.exconly()
  1084. def test_config_ulimits_required_keys_validation_error(self):
  1085. with pytest.raises(ConfigurationError) as exc:
  1086. config.load(build_config_details(
  1087. {
  1088. 'web': {
  1089. 'image': 'busybox',
  1090. 'ulimits': {'nofile': {"soft": 10000}}
  1091. }
  1092. },
  1093. 'working_dir',
  1094. 'filename.yml'))
  1095. assert "web.ulimits.nofile" in exc.exconly()
  1096. assert "'hard' is a required property" in exc.exconly()
  1097. def test_config_ulimits_soft_greater_than_hard_error(self):
  1098. expected = "'soft' value can not be greater than 'hard' value"
  1099. with pytest.raises(ConfigurationError) as exc:
  1100. config.load(build_config_details(
  1101. {
  1102. 'web': {
  1103. 'image': 'busybox',
  1104. 'ulimits': {
  1105. 'nofile': {"soft": 10000, "hard": 1000}
  1106. }
  1107. }
  1108. },
  1109. 'working_dir',
  1110. 'filename.yml'))
  1111. assert expected in exc.exconly()
  1112. def test_valid_config_which_allows_two_type_definitions(self):
  1113. expose_values = [["8000"], [8000]]
  1114. for expose in expose_values:
  1115. service = config.load(
  1116. build_config_details(
  1117. {'web': {
  1118. 'image': 'busybox',
  1119. 'expose': expose
  1120. }},
  1121. 'working_dir',
  1122. 'filename.yml'
  1123. )
  1124. ).services
  1125. self.assertEqual(service[0]['expose'], expose)
  1126. def test_valid_config_oneof_string_or_list(self):
  1127. entrypoint_values = [["sh"], "sh"]
  1128. for entrypoint in entrypoint_values:
  1129. service = config.load(
  1130. build_config_details(
  1131. {'web': {
  1132. 'image': 'busybox',
  1133. 'entrypoint': entrypoint
  1134. }},
  1135. 'working_dir',
  1136. 'filename.yml'
  1137. )
  1138. ).services
  1139. self.assertEqual(service[0]['entrypoint'], entrypoint)
  1140. def test_logs_warning_for_boolean_in_environment(self):
  1141. config_details = build_config_details({
  1142. 'web': {
  1143. 'image': 'busybox',
  1144. 'environment': {'SHOW_STUFF': True}
  1145. }
  1146. })
  1147. with pytest.raises(ConfigurationError) as exc:
  1148. config.load(config_details)
  1149. assert "contains true, which is an invalid type" in exc.exconly()
  1150. def test_config_valid_environment_dict_key_contains_dashes(self):
  1151. services = config.load(
  1152. build_config_details(
  1153. {'web': {
  1154. 'image': 'busybox',
  1155. 'environment': {'SPRING_JPA_HIBERNATE_DDL-AUTO': 'none'}
  1156. }},
  1157. 'working_dir',
  1158. 'filename.yml'
  1159. )
  1160. ).services
  1161. self.assertEqual(services[0]['environment']['SPRING_JPA_HIBERNATE_DDL-AUTO'], 'none')
  1162. def test_load_yaml_with_yaml_error(self):
  1163. tmpdir = py.test.ensuretemp('invalid_yaml_test')
  1164. self.addCleanup(tmpdir.remove)
  1165. invalid_yaml_file = tmpdir.join('docker-compose.yml')
  1166. invalid_yaml_file.write("""
  1167. web:
  1168. this is bogus: ok: what
  1169. """)
  1170. with pytest.raises(ConfigurationError) as exc:
  1171. config.load_yaml(str(invalid_yaml_file))
  1172. assert 'line 3, column 32' in exc.exconly()
  1173. def test_validate_extra_hosts_invalid(self):
  1174. with pytest.raises(ConfigurationError) as exc:
  1175. config.load(build_config_details({
  1176. 'web': {
  1177. 'image': 'alpine',
  1178. 'extra_hosts': "www.example.com: 192.168.0.17",
  1179. }
  1180. }))
  1181. assert "web.extra_hosts contains an invalid type" in exc.exconly()
  1182. def test_validate_extra_hosts_invalid_list(self):
  1183. with pytest.raises(ConfigurationError) as exc:
  1184. config.load(build_config_details({
  1185. 'web': {
  1186. 'image': 'alpine',
  1187. 'extra_hosts': [
  1188. {'www.example.com': '192.168.0.17'},
  1189. {'api.example.com': '192.168.0.18'}
  1190. ],
  1191. }
  1192. }))
  1193. assert "which is an invalid type" in exc.exconly()
  1194. def test_normalize_dns_options(self):
  1195. actual = config.load(build_config_details({
  1196. 'web': {
  1197. 'image': 'alpine',
  1198. 'dns': '8.8.8.8',
  1199. 'dns_search': 'domain.local',
  1200. }
  1201. }))
  1202. assert actual.services == [
  1203. {
  1204. 'name': 'web',
  1205. 'image': 'alpine',
  1206. 'dns': ['8.8.8.8'],
  1207. 'dns_search': ['domain.local'],
  1208. }
  1209. ]
  1210. def test_tmpfs_option(self):
  1211. actual = config.load(build_config_details({
  1212. 'version': '2',
  1213. 'services': {
  1214. 'web': {
  1215. 'image': 'alpine',
  1216. 'tmpfs': '/run',
  1217. }
  1218. }
  1219. }))
  1220. assert actual.services == [
  1221. {
  1222. 'name': 'web',
  1223. 'image': 'alpine',
  1224. 'tmpfs': ['/run'],
  1225. }
  1226. ]
  1227. def test_oom_score_adj_option(self):
  1228. actual = config.load(build_config_details({
  1229. 'version': '2',
  1230. 'services': {
  1231. 'web': {
  1232. 'image': 'alpine',
  1233. 'oom_score_adj': 500
  1234. }
  1235. }
  1236. }))
  1237. assert actual.services == [
  1238. {
  1239. 'name': 'web',
  1240. 'image': 'alpine',
  1241. 'oom_score_adj': 500
  1242. }
  1243. ]
  1244. def test_swappiness_option(self):
  1245. actual = config.load(build_config_details({
  1246. 'version': '2',
  1247. 'services': {
  1248. 'web': {
  1249. 'image': 'alpine',
  1250. 'mem_swappiness': 10,
  1251. }
  1252. }
  1253. }))
  1254. assert actual.services == [
  1255. {
  1256. 'name': 'web',
  1257. 'image': 'alpine',
  1258. 'mem_swappiness': 10,
  1259. }
  1260. ]
  1261. def test_group_add_option(self):
  1262. actual = config.load(build_config_details({
  1263. 'version': '2',
  1264. 'services': {
  1265. 'web': {
  1266. 'image': 'alpine',
  1267. 'group_add': ["docker", 777]
  1268. }
  1269. }
  1270. }))
  1271. assert actual.services == [
  1272. {
  1273. 'name': 'web',
  1274. 'image': 'alpine',
  1275. 'group_add': ["docker", 777]
  1276. }
  1277. ]
  1278. def test_isolation_option(self):
  1279. actual = config.load(build_config_details({
  1280. 'version': V2_1,
  1281. 'services': {
  1282. 'web': {
  1283. 'image': 'win10',
  1284. 'isolation': 'hyperv'
  1285. }
  1286. }
  1287. }))
  1288. assert actual.services == [
  1289. {
  1290. 'name': 'web',
  1291. 'image': 'win10',
  1292. 'isolation': 'hyperv',
  1293. }
  1294. ]
  1295. def test_merge_service_dicts_from_files_with_extends_in_base(self):
  1296. base = {
  1297. 'volumes': ['.:/app'],
  1298. 'extends': {'service': 'app'}
  1299. }
  1300. override = {
  1301. 'image': 'alpine:edge',
  1302. }
  1303. actual = config.merge_service_dicts_from_files(
  1304. base,
  1305. override,
  1306. DEFAULT_VERSION)
  1307. assert actual == {
  1308. 'image': 'alpine:edge',
  1309. 'volumes': ['.:/app'],
  1310. 'extends': {'service': 'app'}
  1311. }
  1312. def test_merge_service_dicts_from_files_with_extends_in_override(self):
  1313. base = {
  1314. 'volumes': ['.:/app'],
  1315. 'extends': {'service': 'app'}
  1316. }
  1317. override = {
  1318. 'image': 'alpine:edge',
  1319. 'extends': {'service': 'foo'}
  1320. }
  1321. actual = config.merge_service_dicts_from_files(
  1322. base,
  1323. override,
  1324. DEFAULT_VERSION)
  1325. assert actual == {
  1326. 'image': 'alpine:edge',
  1327. 'volumes': ['.:/app'],
  1328. 'extends': {'service': 'foo'}
  1329. }
  1330. def test_merge_build_args(self):
  1331. base = {
  1332. 'build': {
  1333. 'context': '.',
  1334. 'args': {
  1335. 'ONE': '1',
  1336. 'TWO': '2',
  1337. },
  1338. }
  1339. }
  1340. override = {
  1341. 'build': {
  1342. 'args': {
  1343. 'TWO': 'dos',
  1344. 'THREE': '3',
  1345. },
  1346. }
  1347. }
  1348. actual = config.merge_service_dicts(
  1349. base,
  1350. override,
  1351. DEFAULT_VERSION)
  1352. assert actual == {
  1353. 'build': {
  1354. 'context': '.',
  1355. 'args': {
  1356. 'ONE': '1',
  1357. 'TWO': 'dos',
  1358. 'THREE': '3',
  1359. },
  1360. }
  1361. }
  1362. def test_merge_logging_v1(self):
  1363. base = {
  1364. 'image': 'alpine:edge',
  1365. 'log_driver': 'something',
  1366. 'log_opt': {'foo': 'three'},
  1367. }
  1368. override = {
  1369. 'image': 'alpine:edge',
  1370. 'command': 'true',
  1371. }
  1372. actual = config.merge_service_dicts(base, override, V1)
  1373. assert actual == {
  1374. 'image': 'alpine:edge',
  1375. 'log_driver': 'something',
  1376. 'log_opt': {'foo': 'three'},
  1377. 'command': 'true',
  1378. }
  1379. def test_merge_logging_v2(self):
  1380. base = {
  1381. 'image': 'alpine:edge',
  1382. 'logging': {
  1383. 'driver': 'json-file',
  1384. 'options': {
  1385. 'frequency': '2000',
  1386. 'timeout': '23'
  1387. }
  1388. }
  1389. }
  1390. override = {
  1391. 'logging': {
  1392. 'options': {
  1393. 'timeout': '360',
  1394. 'pretty-print': 'on'
  1395. }
  1396. }
  1397. }
  1398. actual = config.merge_service_dicts(base, override, V2_0)
  1399. assert actual == {
  1400. 'image': 'alpine:edge',
  1401. 'logging': {
  1402. 'driver': 'json-file',
  1403. 'options': {
  1404. 'frequency': '2000',
  1405. 'timeout': '360',
  1406. 'pretty-print': 'on'
  1407. }
  1408. }
  1409. }
  1410. def test_merge_logging_v2_override_driver(self):
  1411. base = {
  1412. 'image': 'alpine:edge',
  1413. 'logging': {
  1414. 'driver': 'json-file',
  1415. 'options': {
  1416. 'frequency': '2000',
  1417. 'timeout': '23'
  1418. }
  1419. }
  1420. }
  1421. override = {
  1422. 'logging': {
  1423. 'driver': 'syslog',
  1424. 'options': {
  1425. 'timeout': '360',
  1426. 'pretty-print': 'on'
  1427. }
  1428. }
  1429. }
  1430. actual = config.merge_service_dicts(base, override, V2_0)
  1431. assert actual == {
  1432. 'image': 'alpine:edge',
  1433. 'logging': {
  1434. 'driver': 'syslog',
  1435. 'options': {
  1436. 'timeout': '360',
  1437. 'pretty-print': 'on'
  1438. }
  1439. }
  1440. }
  1441. def test_merge_logging_v2_no_base_driver(self):
  1442. base = {
  1443. 'image': 'alpine:edge',
  1444. 'logging': {
  1445. 'options': {
  1446. 'frequency': '2000',
  1447. 'timeout': '23'
  1448. }
  1449. }
  1450. }
  1451. override = {
  1452. 'logging': {
  1453. 'driver': 'json-file',
  1454. 'options': {
  1455. 'timeout': '360',
  1456. 'pretty-print': 'on'
  1457. }
  1458. }
  1459. }
  1460. actual = config.merge_service_dicts(base, override, V2_0)
  1461. assert actual == {
  1462. 'image': 'alpine:edge',
  1463. 'logging': {
  1464. 'driver': 'json-file',
  1465. 'options': {
  1466. 'frequency': '2000',
  1467. 'timeout': '360',
  1468. 'pretty-print': 'on'
  1469. }
  1470. }
  1471. }
  1472. def test_merge_logging_v2_no_drivers(self):
  1473. base = {
  1474. 'image': 'alpine:edge',
  1475. 'logging': {
  1476. 'options': {
  1477. 'frequency': '2000',
  1478. 'timeout': '23'
  1479. }
  1480. }
  1481. }
  1482. override = {
  1483. 'logging': {
  1484. 'options': {
  1485. 'timeout': '360',
  1486. 'pretty-print': 'on'
  1487. }
  1488. }
  1489. }
  1490. actual = config.merge_service_dicts(base, override, V2_0)
  1491. assert actual == {
  1492. 'image': 'alpine:edge',
  1493. 'logging': {
  1494. 'options': {
  1495. 'frequency': '2000',
  1496. 'timeout': '360',
  1497. 'pretty-print': 'on'
  1498. }
  1499. }
  1500. }
  1501. def test_merge_logging_v2_no_override_options(self):
  1502. base = {
  1503. 'image': 'alpine:edge',
  1504. 'logging': {
  1505. 'driver': 'json-file',
  1506. 'options': {
  1507. 'frequency': '2000',
  1508. 'timeout': '23'
  1509. }
  1510. }
  1511. }
  1512. override = {
  1513. 'logging': {
  1514. 'driver': 'syslog'
  1515. }
  1516. }
  1517. actual = config.merge_service_dicts(base, override, V2_0)
  1518. assert actual == {
  1519. 'image': 'alpine:edge',
  1520. 'logging': {
  1521. 'driver': 'syslog',
  1522. 'options': None
  1523. }
  1524. }
  1525. def test_merge_logging_v2_no_base(self):
  1526. base = {
  1527. 'image': 'alpine:edge'
  1528. }
  1529. override = {
  1530. 'logging': {
  1531. 'driver': 'json-file',
  1532. 'options': {
  1533. 'frequency': '2000'
  1534. }
  1535. }
  1536. }
  1537. actual = config.merge_service_dicts(base, override, V2_0)
  1538. assert actual == {
  1539. 'image': 'alpine:edge',
  1540. 'logging': {
  1541. 'driver': 'json-file',
  1542. 'options': {
  1543. 'frequency': '2000'
  1544. }
  1545. }
  1546. }
  1547. def test_merge_logging_v2_no_override(self):
  1548. base = {
  1549. 'image': 'alpine:edge',
  1550. 'logging': {
  1551. 'driver': 'syslog',
  1552. 'options': {
  1553. 'frequency': '2000'
  1554. }
  1555. }
  1556. }
  1557. override = {}
  1558. actual = config.merge_service_dicts(base, override, V2_0)
  1559. assert actual == {
  1560. 'image': 'alpine:edge',
  1561. 'logging': {
  1562. 'driver': 'syslog',
  1563. 'options': {
  1564. 'frequency': '2000'
  1565. }
  1566. }
  1567. }
  1568. def test_merge_depends_on_no_override(self):
  1569. base = {
  1570. 'image': 'busybox',
  1571. 'depends_on': {
  1572. 'app1': {'condition': 'service_started'},
  1573. 'app2': {'condition': 'service_healthy'}
  1574. }
  1575. }
  1576. override = {}
  1577. actual = config.merge_service_dicts(base, override, V2_1)
  1578. assert actual == base
  1579. def test_merge_depends_on_mixed_syntax(self):
  1580. base = {
  1581. 'image': 'busybox',
  1582. 'depends_on': {
  1583. 'app1': {'condition': 'service_started'},
  1584. 'app2': {'condition': 'service_healthy'}
  1585. }
  1586. }
  1587. override = {
  1588. 'depends_on': ['app3']
  1589. }
  1590. actual = config.merge_service_dicts(base, override, V2_1)
  1591. assert actual == {
  1592. 'image': 'busybox',
  1593. 'depends_on': {
  1594. 'app1': {'condition': 'service_started'},
  1595. 'app2': {'condition': 'service_healthy'},
  1596. 'app3': {'condition': 'service_started'}
  1597. }
  1598. }
  1599. def test_merge_pid(self):
  1600. # Regression: https://github.com/docker/compose/issues/4184
  1601. base = {
  1602. 'image': 'busybox',
  1603. 'pid': 'host'
  1604. }
  1605. override = {
  1606. 'labels': {'com.docker.compose.test': 'yes'}
  1607. }
  1608. actual = config.merge_service_dicts(base, override, V2_0)
  1609. assert actual == {
  1610. 'image': 'busybox',
  1611. 'pid': 'host',
  1612. 'labels': {'com.docker.compose.test': 'yes'}
  1613. }
  1614. def test_external_volume_config(self):
  1615. config_details = build_config_details({
  1616. 'version': '2',
  1617. 'services': {
  1618. 'bogus': {'image': 'busybox'}
  1619. },
  1620. 'volumes': {
  1621. 'ext': {'external': True},
  1622. 'ext2': {'external': {'name': 'aliased'}}
  1623. }
  1624. })
  1625. config_result = config.load(config_details)
  1626. volumes = config_result.volumes
  1627. assert 'ext' in volumes
  1628. assert volumes['ext']['external'] is True
  1629. assert 'ext2' in volumes
  1630. assert volumes['ext2']['external']['name'] == 'aliased'
  1631. def test_external_volume_invalid_config(self):
  1632. config_details = build_config_details({
  1633. 'version': '2',
  1634. 'services': {
  1635. 'bogus': {'image': 'busybox'}
  1636. },
  1637. 'volumes': {
  1638. 'ext': {'external': True, 'driver': 'foo'}
  1639. }
  1640. })
  1641. with pytest.raises(ConfigurationError):
  1642. config.load(config_details)
  1643. def test_depends_on_orders_services(self):
  1644. config_details = build_config_details({
  1645. 'version': '2',
  1646. 'services': {
  1647. 'one': {'image': 'busybox', 'depends_on': ['three', 'two']},
  1648. 'two': {'image': 'busybox', 'depends_on': ['three']},
  1649. 'three': {'image': 'busybox'},
  1650. },
  1651. })
  1652. actual = config.load(config_details)
  1653. assert (
  1654. [service['name'] for service in actual.services] ==
  1655. ['three', 'two', 'one']
  1656. )
  1657. def test_depends_on_unknown_service_errors(self):
  1658. config_details = build_config_details({
  1659. 'version': '2',
  1660. 'services': {
  1661. 'one': {'image': 'busybox', 'depends_on': ['three']},
  1662. },
  1663. })
  1664. with pytest.raises(ConfigurationError) as exc:
  1665. config.load(config_details)
  1666. assert "Service 'one' depends on service 'three'" in exc.exconly()
  1667. def test_linked_service_is_undefined(self):
  1668. with self.assertRaises(ConfigurationError):
  1669. config.load(
  1670. build_config_details({
  1671. 'version': '2',
  1672. 'services': {
  1673. 'web': {'image': 'busybox', 'links': ['db:db']},
  1674. },
  1675. })
  1676. )
  1677. def test_load_dockerfile_without_context(self):
  1678. config_details = build_config_details({
  1679. 'version': '2',
  1680. 'services': {
  1681. 'one': {'build': {'dockerfile': 'Dockerfile.foo'}},
  1682. },
  1683. })
  1684. with pytest.raises(ConfigurationError) as exc:
  1685. config.load(config_details)
  1686. assert 'has neither an image nor a build context' in exc.exconly()
  1687. class NetworkModeTest(unittest.TestCase):
  1688. def test_network_mode_standard(self):
  1689. config_data = config.load(build_config_details({
  1690. 'version': '2',
  1691. 'services': {
  1692. 'web': {
  1693. 'image': 'busybox',
  1694. 'command': "top",
  1695. 'network_mode': 'bridge',
  1696. },
  1697. },
  1698. }))
  1699. assert config_data.services[0]['network_mode'] == 'bridge'
  1700. def test_network_mode_standard_v1(self):
  1701. config_data = config.load(build_config_details({
  1702. 'web': {
  1703. 'image': 'busybox',
  1704. 'command': "top",
  1705. 'net': 'bridge',
  1706. },
  1707. }))
  1708. assert config_data.services[0]['network_mode'] == 'bridge'
  1709. assert 'net' not in config_data.services[0]
  1710. def test_network_mode_container(self):
  1711. config_data = config.load(build_config_details({
  1712. 'version': '2',
  1713. 'services': {
  1714. 'web': {
  1715. 'image': 'busybox',
  1716. 'command': "top",
  1717. 'network_mode': 'container:foo',
  1718. },
  1719. },
  1720. }))
  1721. assert config_data.services[0]['network_mode'] == 'container:foo'
  1722. def test_network_mode_container_v1(self):
  1723. config_data = config.load(build_config_details({
  1724. 'web': {
  1725. 'image': 'busybox',
  1726. 'command': "top",
  1727. 'net': 'container:foo',
  1728. },
  1729. }))
  1730. assert config_data.services[0]['network_mode'] == 'container:foo'
  1731. def test_network_mode_service(self):
  1732. config_data = config.load(build_config_details({
  1733. 'version': '2',
  1734. 'services': {
  1735. 'web': {
  1736. 'image': 'busybox',
  1737. 'command': "top",
  1738. 'network_mode': 'service:foo',
  1739. },
  1740. 'foo': {
  1741. 'image': 'busybox',
  1742. 'command': "top",
  1743. },
  1744. },
  1745. }))
  1746. assert config_data.services[1]['network_mode'] == 'service:foo'
  1747. def test_network_mode_service_v1(self):
  1748. config_data = config.load(build_config_details({
  1749. 'web': {
  1750. 'image': 'busybox',
  1751. 'command': "top",
  1752. 'net': 'container:foo',
  1753. },
  1754. 'foo': {
  1755. 'image': 'busybox',
  1756. 'command': "top",
  1757. },
  1758. }))
  1759. assert config_data.services[1]['network_mode'] == 'service:foo'
  1760. def test_network_mode_service_nonexistent(self):
  1761. with pytest.raises(ConfigurationError) as excinfo:
  1762. config.load(build_config_details({
  1763. 'version': '2',
  1764. 'services': {
  1765. 'web': {
  1766. 'image': 'busybox',
  1767. 'command': "top",
  1768. 'network_mode': 'service:foo',
  1769. },
  1770. },
  1771. }))
  1772. assert "service 'foo' which is undefined" in excinfo.exconly()
  1773. def test_network_mode_plus_networks_is_invalid(self):
  1774. with pytest.raises(ConfigurationError) as excinfo:
  1775. config.load(build_config_details({
  1776. 'version': '2',
  1777. 'services': {
  1778. 'web': {
  1779. 'image': 'busybox',
  1780. 'command': "top",
  1781. 'network_mode': 'bridge',
  1782. 'networks': ['front'],
  1783. },
  1784. },
  1785. 'networks': {
  1786. 'front': None,
  1787. }
  1788. }))
  1789. assert "'network_mode' and 'networks' cannot be combined" in excinfo.exconly()
  1790. class PortsTest(unittest.TestCase):
  1791. INVALID_PORTS_TYPES = [
  1792. {"1": "8000"},
  1793. False,
  1794. "8000",
  1795. 8000,
  1796. ]
  1797. NON_UNIQUE_SINGLE_PORTS = [
  1798. ["8000", "8000"],
  1799. ]
  1800. INVALID_PORT_MAPPINGS = [
  1801. ["8000-8001:8000"],
  1802. ]
  1803. VALID_SINGLE_PORTS = [
  1804. ["8000"],
  1805. ["8000/tcp"],
  1806. ["8000", "9000"],
  1807. [8000],
  1808. [8000, 9000],
  1809. ]
  1810. VALID_PORT_MAPPINGS = [
  1811. ["8000:8050"],
  1812. ["49153-49154:3002-3003"],
  1813. ]
  1814. def test_config_invalid_ports_type_validation(self):
  1815. for invalid_ports in self.INVALID_PORTS_TYPES:
  1816. with pytest.raises(ConfigurationError) as exc:
  1817. self.check_config({'ports': invalid_ports})
  1818. assert "contains an invalid type" in exc.value.msg
  1819. def test_config_non_unique_ports_validation(self):
  1820. for invalid_ports in self.NON_UNIQUE_SINGLE_PORTS:
  1821. with pytest.raises(ConfigurationError) as exc:
  1822. self.check_config({'ports': invalid_ports})
  1823. assert "non-unique" in exc.value.msg
  1824. def test_config_invalid_ports_format_validation(self):
  1825. for invalid_ports in self.INVALID_PORT_MAPPINGS:
  1826. with pytest.raises(ConfigurationError) as exc:
  1827. self.check_config({'ports': invalid_ports})
  1828. assert "Port ranges don't match in length" in exc.value.msg
  1829. def test_config_valid_ports_format_validation(self):
  1830. for valid_ports in self.VALID_SINGLE_PORTS + self.VALID_PORT_MAPPINGS:
  1831. self.check_config({'ports': valid_ports})
  1832. def test_config_invalid_expose_type_validation(self):
  1833. for invalid_expose in self.INVALID_PORTS_TYPES:
  1834. with pytest.raises(ConfigurationError) as exc:
  1835. self.check_config({'expose': invalid_expose})
  1836. assert "contains an invalid type" in exc.value.msg
  1837. def test_config_non_unique_expose_validation(self):
  1838. for invalid_expose in self.NON_UNIQUE_SINGLE_PORTS:
  1839. with pytest.raises(ConfigurationError) as exc:
  1840. self.check_config({'expose': invalid_expose})
  1841. assert "non-unique" in exc.value.msg
  1842. def test_config_invalid_expose_format_validation(self):
  1843. # Valid port mappings ARE NOT valid 'expose' entries
  1844. for invalid_expose in self.INVALID_PORT_MAPPINGS + self.VALID_PORT_MAPPINGS:
  1845. with pytest.raises(ConfigurationError) as exc:
  1846. self.check_config({'expose': invalid_expose})
  1847. assert "should be of the format" in exc.value.msg
  1848. def test_config_valid_expose_format_validation(self):
  1849. # Valid single ports ARE valid 'expose' entries
  1850. for valid_expose in self.VALID_SINGLE_PORTS:
  1851. self.check_config({'expose': valid_expose})
  1852. def check_config(self, cfg):
  1853. config.load(
  1854. build_config_details(
  1855. {'web': dict(image='busybox', **cfg)},
  1856. 'working_dir',
  1857. 'filename.yml'
  1858. )
  1859. )
  1860. class InterpolationTest(unittest.TestCase):
  1861. @mock.patch.dict(os.environ)
  1862. def test_config_file_with_environment_file(self):
  1863. project_dir = 'tests/fixtures/default-env-file'
  1864. service_dicts = config.load(
  1865. config.find(
  1866. project_dir, None, Environment.from_env_file(project_dir)
  1867. )
  1868. ).services
  1869. self.assertEqual(service_dicts[0], {
  1870. 'name': 'web',
  1871. 'image': 'alpine:latest',
  1872. 'ports': ['5643', '9999'],
  1873. 'command': 'true'
  1874. })
  1875. @mock.patch.dict(os.environ)
  1876. def test_config_file_with_environment_variable(self):
  1877. project_dir = 'tests/fixtures/environment-interpolation'
  1878. os.environ.update(
  1879. IMAGE="busybox",
  1880. HOST_PORT="80",
  1881. LABEL_VALUE="myvalue",
  1882. )
  1883. service_dicts = config.load(
  1884. config.find(
  1885. project_dir, None, Environment.from_env_file(project_dir)
  1886. )
  1887. ).services
  1888. self.assertEqual(service_dicts, [
  1889. {
  1890. 'name': 'web',
  1891. 'image': 'busybox',
  1892. 'ports': ['80:8000'],
  1893. 'labels': {'mylabel': 'myvalue'},
  1894. 'hostname': 'host-',
  1895. 'command': '${ESCAPED}',
  1896. }
  1897. ])
  1898. @mock.patch.dict(os.environ)
  1899. def test_unset_variable_produces_warning(self):
  1900. os.environ.pop('FOO', None)
  1901. os.environ.pop('BAR', None)
  1902. config_details = build_config_details(
  1903. {
  1904. 'web': {
  1905. 'image': '${FOO}',
  1906. 'command': '${BAR}',
  1907. 'container_name': '${BAR}',
  1908. },
  1909. },
  1910. '.',
  1911. None,
  1912. )
  1913. with mock.patch('compose.config.environment.log') as log:
  1914. config.load(config_details)
  1915. self.assertEqual(2, log.warn.call_count)
  1916. warnings = sorted(args[0][0] for args in log.warn.call_args_list)
  1917. self.assertIn('BAR', warnings[0])
  1918. self.assertIn('FOO', warnings[1])
  1919. @mock.patch.dict(os.environ)
  1920. def test_invalid_interpolation(self):
  1921. with self.assertRaises(config.ConfigurationError) as cm:
  1922. config.load(
  1923. build_config_details(
  1924. {'web': {'image': '${'}},
  1925. 'working_dir',
  1926. 'filename.yml'
  1927. )
  1928. )
  1929. self.assertIn('Invalid', cm.exception.msg)
  1930. self.assertIn('for "image" option', cm.exception.msg)
  1931. self.assertIn('in service "web"', cm.exception.msg)
  1932. self.assertIn('"${"', cm.exception.msg)
  1933. def test_empty_environment_key_allowed(self):
  1934. service_dict = config.load(
  1935. build_config_details(
  1936. {
  1937. 'web': {
  1938. 'build': '.',
  1939. 'environment': {
  1940. 'POSTGRES_PASSWORD': ''
  1941. },
  1942. },
  1943. },
  1944. '.',
  1945. None,
  1946. )
  1947. ).services[0]
  1948. self.assertEquals(service_dict['environment']['POSTGRES_PASSWORD'], '')
  1949. class VolumeConfigTest(unittest.TestCase):
  1950. def test_no_binding(self):
  1951. d = make_service_dict('foo', {'build': '.', 'volumes': ['/data']}, working_dir='.')
  1952. self.assertEqual(d['volumes'], ['/data'])
  1953. @mock.patch.dict(os.environ)
  1954. def test_volume_binding_with_environment_variable(self):
  1955. os.environ['VOLUME_PATH'] = '/host/path'
  1956. d = config.load(
  1957. build_config_details(
  1958. {'foo': {'build': '.', 'volumes': ['${VOLUME_PATH}:/container/path']}},
  1959. '.',
  1960. None,
  1961. )
  1962. ).services[0]
  1963. self.assertEqual(d['volumes'], [VolumeSpec.parse('/host/path:/container/path')])
  1964. @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
  1965. @mock.patch.dict(os.environ)
  1966. def test_volume_binding_with_home(self):
  1967. os.environ['HOME'] = '/home/user'
  1968. d = make_service_dict('foo', {'build': '.', 'volumes': ['~:/container/path']}, working_dir='.')
  1969. self.assertEqual(d['volumes'], ['/home/user:/container/path'])
  1970. def test_name_does_not_expand(self):
  1971. d = make_service_dict('foo', {'build': '.', 'volumes': ['mydatavolume:/data']}, working_dir='.')
  1972. self.assertEqual(d['volumes'], ['mydatavolume:/data'])
  1973. def test_absolute_posix_path_does_not_expand(self):
  1974. d = make_service_dict('foo', {'build': '.', 'volumes': ['/var/lib/data:/data']}, working_dir='.')
  1975. self.assertEqual(d['volumes'], ['/var/lib/data:/data'])
  1976. def test_absolute_windows_path_does_not_expand(self):
  1977. d = make_service_dict('foo', {'build': '.', 'volumes': ['c:\\data:/data']}, working_dir='.')
  1978. self.assertEqual(d['volumes'], ['c:\\data:/data'])
  1979. @pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='posix paths')
  1980. def test_relative_path_does_expand_posix(self):
  1981. d = make_service_dict(
  1982. 'foo',
  1983. {'build': '.', 'volumes': ['./data:/data']},
  1984. working_dir='/home/me/myproject')
  1985. self.assertEqual(d['volumes'], ['/home/me/myproject/data:/data'])
  1986. d = make_service_dict(
  1987. 'foo',
  1988. {'build': '.', 'volumes': ['.:/data']},
  1989. working_dir='/home/me/myproject')
  1990. self.assertEqual(d['volumes'], ['/home/me/myproject:/data'])
  1991. d = make_service_dict(
  1992. 'foo',
  1993. {'build': '.', 'volumes': ['../otherproject:/data']},
  1994. working_dir='/home/me/myproject')
  1995. self.assertEqual(d['volumes'], ['/home/me/otherproject:/data'])
  1996. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='windows paths')
  1997. def test_relative_path_does_expand_windows(self):
  1998. d = make_service_dict(
  1999. 'foo',
  2000. {'build': '.', 'volumes': ['./data:/data']},
  2001. working_dir='c:\\Users\\me\\myproject')
  2002. self.assertEqual(d['volumes'], ['c:\\Users\\me\\myproject\\data:/data'])
  2003. d = make_service_dict(
  2004. 'foo',
  2005. {'build': '.', 'volumes': ['.:/data']},
  2006. working_dir='c:\\Users\\me\\myproject')
  2007. self.assertEqual(d['volumes'], ['c:\\Users\\me\\myproject:/data'])
  2008. d = make_service_dict(
  2009. 'foo',
  2010. {'build': '.', 'volumes': ['../otherproject:/data']},
  2011. working_dir='c:\\Users\\me\\myproject')
  2012. self.assertEqual(d['volumes'], ['c:\\Users\\me\\otherproject:/data'])
  2013. @mock.patch.dict(os.environ)
  2014. def test_home_directory_with_driver_does_not_expand(self):
  2015. os.environ['NAME'] = 'surprise!'
  2016. d = make_service_dict('foo', {
  2017. 'build': '.',
  2018. 'volumes': ['~:/data'],
  2019. 'volume_driver': 'foodriver',
  2020. }, working_dir='.')
  2021. self.assertEqual(d['volumes'], ['~:/data'])
  2022. def test_volume_path_with_non_ascii_directory(self):
  2023. volume = u'/Füü/data:/data'
  2024. container_path = config.resolve_volume_path(".", volume)
  2025. self.assertEqual(container_path, volume)
  2026. class MergePathMappingTest(object):
  2027. config_name = ""
  2028. def test_empty(self):
  2029. service_dict = config.merge_service_dicts({}, {}, DEFAULT_VERSION)
  2030. assert self.config_name not in service_dict
  2031. def test_no_override(self):
  2032. service_dict = config.merge_service_dicts(
  2033. {self.config_name: ['/foo:/code', '/data']},
  2034. {},
  2035. DEFAULT_VERSION)
  2036. assert set(service_dict[self.config_name]) == set(['/foo:/code', '/data'])
  2037. def test_no_base(self):
  2038. service_dict = config.merge_service_dicts(
  2039. {},
  2040. {self.config_name: ['/bar:/code']},
  2041. DEFAULT_VERSION)
  2042. assert set(service_dict[self.config_name]) == set(['/bar:/code'])
  2043. def test_override_explicit_path(self):
  2044. service_dict = config.merge_service_dicts(
  2045. {self.config_name: ['/foo:/code', '/data']},
  2046. {self.config_name: ['/bar:/code']},
  2047. DEFAULT_VERSION)
  2048. assert set(service_dict[self.config_name]) == set(['/bar:/code', '/data'])
  2049. def test_add_explicit_path(self):
  2050. service_dict = config.merge_service_dicts(
  2051. {self.config_name: ['/foo:/code', '/data']},
  2052. {self.config_name: ['/bar:/code', '/quux:/data']},
  2053. DEFAULT_VERSION)
  2054. assert set(service_dict[self.config_name]) == set(['/bar:/code', '/quux:/data'])
  2055. def test_remove_explicit_path(self):
  2056. service_dict = config.merge_service_dicts(
  2057. {self.config_name: ['/foo:/code', '/quux:/data']},
  2058. {self.config_name: ['/bar:/code', '/data']},
  2059. DEFAULT_VERSION)
  2060. assert set(service_dict[self.config_name]) == set(['/bar:/code', '/data'])
  2061. class MergeVolumesTest(unittest.TestCase, MergePathMappingTest):
  2062. config_name = 'volumes'
  2063. class MergeDevicesTest(unittest.TestCase, MergePathMappingTest):
  2064. config_name = 'devices'
  2065. class BuildOrImageMergeTest(unittest.TestCase):
  2066. def test_merge_build_or_image_no_override(self):
  2067. self.assertEqual(
  2068. config.merge_service_dicts({'build': '.'}, {}, V1),
  2069. {'build': '.'},
  2070. )
  2071. self.assertEqual(
  2072. config.merge_service_dicts({'image': 'redis'}, {}, V1),
  2073. {'image': 'redis'},
  2074. )
  2075. def test_merge_build_or_image_override_with_same(self):
  2076. self.assertEqual(
  2077. config.merge_service_dicts({'build': '.'}, {'build': './web'}, V1),
  2078. {'build': './web'},
  2079. )
  2080. self.assertEqual(
  2081. config.merge_service_dicts({'image': 'redis'}, {'image': 'postgres'}, V1),
  2082. {'image': 'postgres'},
  2083. )
  2084. def test_merge_build_or_image_override_with_other(self):
  2085. self.assertEqual(
  2086. config.merge_service_dicts({'build': '.'}, {'image': 'redis'}, V1),
  2087. {'image': 'redis'},
  2088. )
  2089. self.assertEqual(
  2090. config.merge_service_dicts({'image': 'redis'}, {'build': '.'}, V1),
  2091. {'build': '.'}
  2092. )
  2093. class MergeListsTest(object):
  2094. config_name = ""
  2095. base_config = []
  2096. override_config = []
  2097. def merged_config(self):
  2098. return set(self.base_config) | set(self.override_config)
  2099. def test_empty(self):
  2100. assert self.config_name not in config.merge_service_dicts({}, {}, DEFAULT_VERSION)
  2101. def test_no_override(self):
  2102. service_dict = config.merge_service_dicts(
  2103. {self.config_name: self.base_config},
  2104. {},
  2105. DEFAULT_VERSION)
  2106. assert set(service_dict[self.config_name]) == set(self.base_config)
  2107. def test_no_base(self):
  2108. service_dict = config.merge_service_dicts(
  2109. {},
  2110. {self.config_name: self.base_config},
  2111. DEFAULT_VERSION)
  2112. assert set(service_dict[self.config_name]) == set(self.base_config)
  2113. def test_add_item(self):
  2114. service_dict = config.merge_service_dicts(
  2115. {self.config_name: self.base_config},
  2116. {self.config_name: self.override_config},
  2117. DEFAULT_VERSION)
  2118. assert set(service_dict[self.config_name]) == set(self.merged_config())
  2119. class MergePortsTest(unittest.TestCase, MergeListsTest):
  2120. config_name = 'ports'
  2121. base_config = ['10:8000', '9000']
  2122. override_config = ['20:8000']
  2123. def test_duplicate_port_mappings(self):
  2124. service_dict = config.merge_service_dicts(
  2125. {self.config_name: self.base_config},
  2126. {self.config_name: self.base_config},
  2127. DEFAULT_VERSION
  2128. )
  2129. assert set(service_dict[self.config_name]) == set(self.base_config)
  2130. class MergeNetworksTest(unittest.TestCase, MergeListsTest):
  2131. config_name = 'networks'
  2132. base_config = ['frontend', 'backend']
  2133. override_config = ['monitoring']
  2134. class MergeStringsOrListsTest(unittest.TestCase):
  2135. def test_no_override(self):
  2136. service_dict = config.merge_service_dicts(
  2137. {'dns': '8.8.8.8'},
  2138. {},
  2139. DEFAULT_VERSION)
  2140. assert set(service_dict['dns']) == set(['8.8.8.8'])
  2141. def test_no_base(self):
  2142. service_dict = config.merge_service_dicts(
  2143. {},
  2144. {'dns': '8.8.8.8'},
  2145. DEFAULT_VERSION)
  2146. assert set(service_dict['dns']) == set(['8.8.8.8'])
  2147. def test_add_string(self):
  2148. service_dict = config.merge_service_dicts(
  2149. {'dns': ['8.8.8.8']},
  2150. {'dns': '9.9.9.9'},
  2151. DEFAULT_VERSION)
  2152. assert set(service_dict['dns']) == set(['8.8.8.8', '9.9.9.9'])
  2153. def test_add_list(self):
  2154. service_dict = config.merge_service_dicts(
  2155. {'dns': '8.8.8.8'},
  2156. {'dns': ['9.9.9.9']},
  2157. DEFAULT_VERSION)
  2158. assert set(service_dict['dns']) == set(['8.8.8.8', '9.9.9.9'])
  2159. class MergeLabelsTest(unittest.TestCase):
  2160. def test_empty(self):
  2161. assert 'labels' not in config.merge_service_dicts({}, {}, DEFAULT_VERSION)
  2162. def test_no_override(self):
  2163. service_dict = config.merge_service_dicts(
  2164. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
  2165. make_service_dict('foo', {'build': '.'}, 'tests/'),
  2166. DEFAULT_VERSION)
  2167. assert service_dict['labels'] == {'foo': '1', 'bar': ''}
  2168. def test_no_base(self):
  2169. service_dict = config.merge_service_dicts(
  2170. make_service_dict('foo', {'build': '.'}, 'tests/'),
  2171. make_service_dict('foo', {'build': '.', 'labels': ['foo=2']}, 'tests/'),
  2172. DEFAULT_VERSION)
  2173. assert service_dict['labels'] == {'foo': '2'}
  2174. def test_override_explicit_value(self):
  2175. service_dict = config.merge_service_dicts(
  2176. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
  2177. make_service_dict('foo', {'build': '.', 'labels': ['foo=2']}, 'tests/'),
  2178. DEFAULT_VERSION)
  2179. assert service_dict['labels'] == {'foo': '2', 'bar': ''}
  2180. def test_add_explicit_value(self):
  2181. service_dict = config.merge_service_dicts(
  2182. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar']}, 'tests/'),
  2183. make_service_dict('foo', {'build': '.', 'labels': ['bar=2']}, 'tests/'),
  2184. DEFAULT_VERSION)
  2185. assert service_dict['labels'] == {'foo': '1', 'bar': '2'}
  2186. def test_remove_explicit_value(self):
  2187. service_dict = config.merge_service_dicts(
  2188. make_service_dict('foo', {'build': '.', 'labels': ['foo=1', 'bar=2']}, 'tests/'),
  2189. make_service_dict('foo', {'build': '.', 'labels': ['bar']}, 'tests/'),
  2190. DEFAULT_VERSION)
  2191. assert service_dict['labels'] == {'foo': '1', 'bar': ''}
  2192. class MemoryOptionsTest(unittest.TestCase):
  2193. def test_validation_fails_with_just_memswap_limit(self):
  2194. """
  2195. When you set a 'memswap_limit' it is invalid config unless you also set
  2196. a mem_limit
  2197. """
  2198. with pytest.raises(ConfigurationError) as excinfo:
  2199. config.load(
  2200. build_config_details(
  2201. {
  2202. 'foo': {'image': 'busybox', 'memswap_limit': 2000000},
  2203. },
  2204. 'tests/fixtures/extends',
  2205. 'filename.yml'
  2206. )
  2207. )
  2208. assert "foo.memswap_limit is invalid: when defining " \
  2209. "'memswap_limit' you must set 'mem_limit' as well" \
  2210. in excinfo.exconly()
  2211. def test_validation_with_correct_memswap_values(self):
  2212. service_dict = config.load(
  2213. build_config_details(
  2214. {'foo': {'image': 'busybox', 'mem_limit': 1000000, 'memswap_limit': 2000000}},
  2215. 'tests/fixtures/extends',
  2216. 'common.yml'
  2217. )
  2218. ).services
  2219. self.assertEqual(service_dict[0]['memswap_limit'], 2000000)
  2220. def test_memswap_can_be_a_string(self):
  2221. service_dict = config.load(
  2222. build_config_details(
  2223. {'foo': {'image': 'busybox', 'mem_limit': "1G", 'memswap_limit': "512M"}},
  2224. 'tests/fixtures/extends',
  2225. 'common.yml'
  2226. )
  2227. ).services
  2228. self.assertEqual(service_dict[0]['memswap_limit'], "512M")
  2229. class EnvTest(unittest.TestCase):
  2230. def test_parse_environment_as_list(self):
  2231. environment = [
  2232. 'NORMAL=F1',
  2233. 'CONTAINS_EQUALS=F=2',
  2234. 'TRAILING_EQUALS=',
  2235. ]
  2236. self.assertEqual(
  2237. config.parse_environment(environment),
  2238. {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''},
  2239. )
  2240. def test_parse_environment_as_dict(self):
  2241. environment = {
  2242. 'NORMAL': 'F1',
  2243. 'CONTAINS_EQUALS': 'F=2',
  2244. 'TRAILING_EQUALS': None,
  2245. }
  2246. self.assertEqual(config.parse_environment(environment), environment)
  2247. def test_parse_environment_invalid(self):
  2248. with self.assertRaises(ConfigurationError):
  2249. config.parse_environment('a=b')
  2250. def test_parse_environment_empty(self):
  2251. self.assertEqual(config.parse_environment(None), {})
  2252. @mock.patch.dict(os.environ)
  2253. def test_resolve_environment(self):
  2254. os.environ['FILE_DEF'] = 'E1'
  2255. os.environ['FILE_DEF_EMPTY'] = 'E2'
  2256. os.environ['ENV_DEF'] = 'E3'
  2257. service_dict = {
  2258. 'build': '.',
  2259. 'environment': {
  2260. 'FILE_DEF': 'F1',
  2261. 'FILE_DEF_EMPTY': '',
  2262. 'ENV_DEF': None,
  2263. 'NO_DEF': None
  2264. },
  2265. }
  2266. self.assertEqual(
  2267. resolve_environment(
  2268. service_dict, Environment.from_env_file(None)
  2269. ),
  2270. {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': None},
  2271. )
  2272. def test_resolve_environment_from_env_file(self):
  2273. self.assertEqual(
  2274. resolve_environment({'env_file': ['tests/fixtures/env/one.env']}),
  2275. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'bar'},
  2276. )
  2277. def test_environment_overrides_env_file(self):
  2278. self.assertEqual(
  2279. resolve_environment({
  2280. 'environment': {'FOO': 'baz'},
  2281. 'env_file': ['tests/fixtures/env/one.env'],
  2282. }),
  2283. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz'},
  2284. )
  2285. def test_resolve_environment_with_multiple_env_files(self):
  2286. service_dict = {
  2287. 'env_file': [
  2288. 'tests/fixtures/env/one.env',
  2289. 'tests/fixtures/env/two.env'
  2290. ]
  2291. }
  2292. self.assertEqual(
  2293. resolve_environment(service_dict),
  2294. {'ONE': '2', 'TWO': '1', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'},
  2295. )
  2296. def test_resolve_environment_nonexistent_file(self):
  2297. with pytest.raises(ConfigurationError) as exc:
  2298. config.load(build_config_details(
  2299. {'foo': {'image': 'example', 'env_file': 'nonexistent.env'}},
  2300. working_dir='tests/fixtures/env'))
  2301. assert 'Couldn\'t find env file' in exc.exconly()
  2302. assert 'nonexistent.env' in exc.exconly()
  2303. @mock.patch.dict(os.environ)
  2304. def test_resolve_environment_from_env_file_with_empty_values(self):
  2305. os.environ['FILE_DEF'] = 'E1'
  2306. os.environ['FILE_DEF_EMPTY'] = 'E2'
  2307. os.environ['ENV_DEF'] = 'E3'
  2308. self.assertEqual(
  2309. resolve_environment(
  2310. {'env_file': ['tests/fixtures/env/resolve.env']},
  2311. Environment.from_env_file(None)
  2312. ),
  2313. {
  2314. 'FILE_DEF': u'bär',
  2315. 'FILE_DEF_EMPTY': '',
  2316. 'ENV_DEF': 'E3',
  2317. 'NO_DEF': None
  2318. },
  2319. )
  2320. @mock.patch.dict(os.environ)
  2321. def test_resolve_build_args(self):
  2322. os.environ['env_arg'] = 'value2'
  2323. build = {
  2324. 'context': '.',
  2325. 'args': {
  2326. 'arg1': 'value1',
  2327. 'empty_arg': '',
  2328. 'env_arg': None,
  2329. 'no_env': None
  2330. }
  2331. }
  2332. self.assertEqual(
  2333. resolve_build_args(build, Environment.from_env_file(build['context'])),
  2334. {'arg1': 'value1', 'empty_arg': '', 'env_arg': 'value2', 'no_env': None},
  2335. )
  2336. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  2337. @mock.patch.dict(os.environ)
  2338. def test_resolve_path(self):
  2339. os.environ['HOSTENV'] = '/tmp'
  2340. os.environ['CONTAINERENV'] = '/host/tmp'
  2341. service_dict = config.load(
  2342. build_config_details(
  2343. {'foo': {'build': '.', 'volumes': ['$HOSTENV:$CONTAINERENV']}},
  2344. "tests/fixtures/env",
  2345. )
  2346. ).services[0]
  2347. self.assertEqual(
  2348. set(service_dict['volumes']),
  2349. set([VolumeSpec.parse('/tmp:/host/tmp')]))
  2350. service_dict = config.load(
  2351. build_config_details(
  2352. {'foo': {'build': '.', 'volumes': ['/opt${HOSTENV}:/opt${CONTAINERENV}']}},
  2353. "tests/fixtures/env",
  2354. )
  2355. ).services[0]
  2356. self.assertEqual(
  2357. set(service_dict['volumes']),
  2358. set([VolumeSpec.parse('/opt/tmp:/opt/host/tmp')]))
  2359. def load_from_filename(filename):
  2360. return config.load(
  2361. config.find('.', [filename], Environment.from_env_file('.'))
  2362. ).services
  2363. class ExtendsTest(unittest.TestCase):
  2364. def test_extends(self):
  2365. service_dicts = load_from_filename('tests/fixtures/extends/docker-compose.yml')
  2366. self.assertEqual(service_sort(service_dicts), service_sort([
  2367. {
  2368. 'name': 'mydb',
  2369. 'image': 'busybox',
  2370. 'command': 'top',
  2371. },
  2372. {
  2373. 'name': 'myweb',
  2374. 'image': 'busybox',
  2375. 'command': 'top',
  2376. 'network_mode': 'bridge',
  2377. 'links': ['mydb:db'],
  2378. 'environment': {
  2379. "FOO": "1",
  2380. "BAR": "2",
  2381. "BAZ": "2",
  2382. },
  2383. }
  2384. ]))
  2385. def test_merging_env_labels_ulimits(self):
  2386. service_dicts = load_from_filename('tests/fixtures/extends/common-env-labels-ulimits.yml')
  2387. self.assertEqual(service_sort(service_dicts), service_sort([
  2388. {
  2389. 'name': 'web',
  2390. 'image': 'busybox',
  2391. 'command': '/bin/true',
  2392. 'network_mode': 'host',
  2393. 'environment': {
  2394. "FOO": "2",
  2395. "BAR": "1",
  2396. "BAZ": "3",
  2397. },
  2398. 'labels': {'label': 'one'},
  2399. 'ulimits': {'nproc': 65535, 'memlock': {'soft': 1024, 'hard': 2048}}
  2400. }
  2401. ]))
  2402. def test_nested(self):
  2403. service_dicts = load_from_filename('tests/fixtures/extends/nested.yml')
  2404. self.assertEqual(service_dicts, [
  2405. {
  2406. 'name': 'myweb',
  2407. 'image': 'busybox',
  2408. 'command': '/bin/true',
  2409. 'network_mode': 'host',
  2410. 'environment': {
  2411. "FOO": "2",
  2412. "BAR": "2",
  2413. },
  2414. },
  2415. ])
  2416. def test_self_referencing_file(self):
  2417. """
  2418. We specify a 'file' key that is the filename we're already in.
  2419. """
  2420. service_dicts = load_from_filename('tests/fixtures/extends/specify-file-as-self.yml')
  2421. self.assertEqual(service_sort(service_dicts), service_sort([
  2422. {
  2423. 'environment':
  2424. {
  2425. 'YEP': '1', 'BAR': '1', 'BAZ': '3'
  2426. },
  2427. 'image': 'busybox',
  2428. 'name': 'myweb'
  2429. },
  2430. {
  2431. 'environment':
  2432. {'YEP': '1'},
  2433. 'image': 'busybox',
  2434. 'name': 'otherweb'
  2435. },
  2436. {
  2437. 'environment':
  2438. {'YEP': '1', 'BAZ': '3'},
  2439. 'image': 'busybox',
  2440. 'name': 'web'
  2441. }
  2442. ]))
  2443. def test_circular(self):
  2444. with pytest.raises(config.CircularReference) as exc:
  2445. load_from_filename('tests/fixtures/extends/circle-1.yml')
  2446. path = [
  2447. (os.path.basename(filename), service_name)
  2448. for (filename, service_name) in exc.value.trail
  2449. ]
  2450. expected = [
  2451. ('circle-1.yml', 'web'),
  2452. ('circle-2.yml', 'other'),
  2453. ('circle-1.yml', 'web'),
  2454. ]
  2455. self.assertEqual(path, expected)
  2456. def test_extends_validation_empty_dictionary(self):
  2457. with pytest.raises(ConfigurationError) as excinfo:
  2458. config.load(
  2459. build_config_details(
  2460. {
  2461. 'web': {'image': 'busybox', 'extends': {}},
  2462. },
  2463. 'tests/fixtures/extends',
  2464. 'filename.yml'
  2465. )
  2466. )
  2467. assert 'service' in excinfo.exconly()
  2468. def test_extends_validation_missing_service_key(self):
  2469. with pytest.raises(ConfigurationError) as excinfo:
  2470. config.load(
  2471. build_config_details(
  2472. {
  2473. 'web': {'image': 'busybox', 'extends': {'file': 'common.yml'}},
  2474. },
  2475. 'tests/fixtures/extends',
  2476. 'filename.yml'
  2477. )
  2478. )
  2479. assert "'service' is a required property" in excinfo.exconly()
  2480. def test_extends_validation_invalid_key(self):
  2481. with pytest.raises(ConfigurationError) as excinfo:
  2482. config.load(
  2483. build_config_details(
  2484. {
  2485. 'web': {
  2486. 'image': 'busybox',
  2487. 'extends': {
  2488. 'file': 'common.yml',
  2489. 'service': 'web',
  2490. 'rogue_key': 'is not allowed'
  2491. }
  2492. },
  2493. },
  2494. 'tests/fixtures/extends',
  2495. 'filename.yml'
  2496. )
  2497. )
  2498. assert "web.extends contains unsupported option: 'rogue_key'" \
  2499. in excinfo.exconly()
  2500. def test_extends_validation_sub_property_key(self):
  2501. with pytest.raises(ConfigurationError) as excinfo:
  2502. config.load(
  2503. build_config_details(
  2504. {
  2505. 'web': {
  2506. 'image': 'busybox',
  2507. 'extends': {
  2508. 'file': 1,
  2509. 'service': 'web',
  2510. }
  2511. },
  2512. },
  2513. 'tests/fixtures/extends',
  2514. 'filename.yml'
  2515. )
  2516. )
  2517. assert "web.extends.file contains 1, which is an invalid type, it should be a string" \
  2518. in excinfo.exconly()
  2519. def test_extends_validation_no_file_key_no_filename_set(self):
  2520. dictionary = {'extends': {'service': 'web'}}
  2521. with pytest.raises(ConfigurationError) as excinfo:
  2522. make_service_dict('myweb', dictionary, working_dir='tests/fixtures/extends')
  2523. assert 'file' in excinfo.exconly()
  2524. def test_extends_validation_valid_config(self):
  2525. service = config.load(
  2526. build_config_details(
  2527. {
  2528. 'web': {'image': 'busybox', 'extends': {'service': 'web', 'file': 'common.yml'}},
  2529. },
  2530. 'tests/fixtures/extends',
  2531. 'common.yml'
  2532. )
  2533. ).services
  2534. self.assertEquals(len(service), 1)
  2535. self.assertIsInstance(service[0], dict)
  2536. self.assertEquals(service[0]['command'], "/bin/true")
  2537. def test_extended_service_with_invalid_config(self):
  2538. with pytest.raises(ConfigurationError) as exc:
  2539. load_from_filename('tests/fixtures/extends/service-with-invalid-schema.yml')
  2540. assert (
  2541. "myweb has neither an image nor a build context specified" in
  2542. exc.exconly()
  2543. )
  2544. def test_extended_service_with_valid_config(self):
  2545. service = load_from_filename('tests/fixtures/extends/service-with-valid-composite-extends.yml')
  2546. self.assertEquals(service[0]['command'], "top")
  2547. def test_extends_file_defaults_to_self(self):
  2548. """
  2549. Test not specifying a file in our extends options that the
  2550. config is valid and correctly extends from itself.
  2551. """
  2552. service_dicts = load_from_filename('tests/fixtures/extends/no-file-specified.yml')
  2553. self.assertEqual(service_sort(service_dicts), service_sort([
  2554. {
  2555. 'name': 'myweb',
  2556. 'image': 'busybox',
  2557. 'environment': {
  2558. "BAR": "1",
  2559. "BAZ": "3",
  2560. }
  2561. },
  2562. {
  2563. 'name': 'web',
  2564. 'image': 'busybox',
  2565. 'environment': {
  2566. "BAZ": "3",
  2567. }
  2568. }
  2569. ]))
  2570. def test_invalid_links_in_extended_service(self):
  2571. with pytest.raises(ConfigurationError) as excinfo:
  2572. load_from_filename('tests/fixtures/extends/invalid-links.yml')
  2573. assert "services with 'links' cannot be extended" in excinfo.exconly()
  2574. def test_invalid_volumes_from_in_extended_service(self):
  2575. with pytest.raises(ConfigurationError) as excinfo:
  2576. load_from_filename('tests/fixtures/extends/invalid-volumes.yml')
  2577. assert "services with 'volumes_from' cannot be extended" in excinfo.exconly()
  2578. def test_invalid_net_in_extended_service(self):
  2579. with pytest.raises(ConfigurationError) as excinfo:
  2580. load_from_filename('tests/fixtures/extends/invalid-net-v2.yml')
  2581. assert 'network_mode: service' in excinfo.exconly()
  2582. assert 'cannot be extended' in excinfo.exconly()
  2583. with pytest.raises(ConfigurationError) as excinfo:
  2584. load_from_filename('tests/fixtures/extends/invalid-net.yml')
  2585. assert 'net: container' in excinfo.exconly()
  2586. assert 'cannot be extended' in excinfo.exconly()
  2587. @mock.patch.dict(os.environ)
  2588. def test_load_config_runs_interpolation_in_extended_service(self):
  2589. os.environ.update(HOSTNAME_VALUE="penguin")
  2590. expected_interpolated_value = "host-penguin"
  2591. service_dicts = load_from_filename(
  2592. 'tests/fixtures/extends/valid-interpolation.yml')
  2593. for service in service_dicts:
  2594. assert service['hostname'] == expected_interpolated_value
  2595. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  2596. def test_volume_path(self):
  2597. dicts = load_from_filename('tests/fixtures/volume-path/docker-compose.yml')
  2598. paths = [
  2599. VolumeSpec(
  2600. os.path.abspath('tests/fixtures/volume-path/common/foo'),
  2601. '/foo',
  2602. 'rw'),
  2603. VolumeSpec(
  2604. os.path.abspath('tests/fixtures/volume-path/bar'),
  2605. '/bar',
  2606. 'rw')
  2607. ]
  2608. self.assertEqual(set(dicts[0]['volumes']), set(paths))
  2609. def test_parent_build_path_dne(self):
  2610. child = load_from_filename('tests/fixtures/extends/nonexistent-path-child.yml')
  2611. self.assertEqual(child, [
  2612. {
  2613. 'name': 'dnechild',
  2614. 'image': 'busybox',
  2615. 'command': '/bin/true',
  2616. 'environment': {
  2617. "FOO": "1",
  2618. "BAR": "2",
  2619. },
  2620. },
  2621. ])
  2622. def test_load_throws_error_when_base_service_does_not_exist(self):
  2623. with pytest.raises(ConfigurationError) as excinfo:
  2624. load_from_filename('tests/fixtures/extends/nonexistent-service.yml')
  2625. assert "Cannot extend service 'foo'" in excinfo.exconly()
  2626. assert "Service not found" in excinfo.exconly()
  2627. def test_partial_service_config_in_extends_is_still_valid(self):
  2628. dicts = load_from_filename('tests/fixtures/extends/valid-common-config.yml')
  2629. self.assertEqual(dicts[0]['environment'], {'FOO': '1'})
  2630. def test_extended_service_with_verbose_and_shorthand_way(self):
  2631. services = load_from_filename('tests/fixtures/extends/verbose-and-shorthand.yml')
  2632. self.assertEqual(service_sort(services), service_sort([
  2633. {
  2634. 'name': 'base',
  2635. 'image': 'busybox',
  2636. 'environment': {'BAR': '1'},
  2637. },
  2638. {
  2639. 'name': 'verbose',
  2640. 'image': 'busybox',
  2641. 'environment': {'BAR': '1', 'FOO': '1'},
  2642. },
  2643. {
  2644. 'name': 'shorthand',
  2645. 'image': 'busybox',
  2646. 'environment': {'BAR': '1', 'FOO': '2'},
  2647. },
  2648. ]))
  2649. @mock.patch.dict(os.environ)
  2650. def test_extends_with_environment_and_env_files(self):
  2651. tmpdir = py.test.ensuretemp('test_extends_with_environment')
  2652. self.addCleanup(tmpdir.remove)
  2653. commondir = tmpdir.mkdir('common')
  2654. commondir.join('base.yml').write("""
  2655. app:
  2656. image: 'example/app'
  2657. env_file:
  2658. - 'envs'
  2659. environment:
  2660. - SECRET
  2661. - TEST_ONE=common
  2662. - TEST_TWO=common
  2663. """)
  2664. tmpdir.join('docker-compose.yml').write("""
  2665. ext:
  2666. extends:
  2667. file: common/base.yml
  2668. service: app
  2669. env_file:
  2670. - 'envs'
  2671. environment:
  2672. - THING
  2673. - TEST_ONE=top
  2674. """)
  2675. commondir.join('envs').write("""
  2676. COMMON_ENV_FILE
  2677. TEST_ONE=common-env-file
  2678. TEST_TWO=common-env-file
  2679. TEST_THREE=common-env-file
  2680. TEST_FOUR=common-env-file
  2681. """)
  2682. tmpdir.join('envs').write("""
  2683. TOP_ENV_FILE
  2684. TEST_ONE=top-env-file
  2685. TEST_TWO=top-env-file
  2686. TEST_THREE=top-env-file
  2687. """)
  2688. expected = [
  2689. {
  2690. 'name': 'ext',
  2691. 'image': 'example/app',
  2692. 'environment': {
  2693. 'SECRET': 'secret',
  2694. 'TOP_ENV_FILE': 'secret',
  2695. 'COMMON_ENV_FILE': 'secret',
  2696. 'THING': 'thing',
  2697. 'TEST_ONE': 'top',
  2698. 'TEST_TWO': 'common',
  2699. 'TEST_THREE': 'top-env-file',
  2700. 'TEST_FOUR': 'common-env-file',
  2701. },
  2702. },
  2703. ]
  2704. os.environ['SECRET'] = 'secret'
  2705. os.environ['THING'] = 'thing'
  2706. os.environ['COMMON_ENV_FILE'] = 'secret'
  2707. os.environ['TOP_ENV_FILE'] = 'secret'
  2708. config = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  2709. assert config == expected
  2710. def test_extends_with_mixed_versions_is_error(self):
  2711. tmpdir = py.test.ensuretemp('test_extends_with_mixed_version')
  2712. self.addCleanup(tmpdir.remove)
  2713. tmpdir.join('docker-compose.yml').write("""
  2714. version: "2"
  2715. services:
  2716. web:
  2717. extends:
  2718. file: base.yml
  2719. service: base
  2720. image: busybox
  2721. """)
  2722. tmpdir.join('base.yml').write("""
  2723. base:
  2724. volumes: ['/foo']
  2725. ports: ['3000:3000']
  2726. """)
  2727. with pytest.raises(ConfigurationError) as exc:
  2728. load_from_filename(str(tmpdir.join('docker-compose.yml')))
  2729. assert 'Version mismatch' in exc.exconly()
  2730. def test_extends_with_defined_version_passes(self):
  2731. tmpdir = py.test.ensuretemp('test_extends_with_defined_version')
  2732. self.addCleanup(tmpdir.remove)
  2733. tmpdir.join('docker-compose.yml').write("""
  2734. version: "2"
  2735. services:
  2736. web:
  2737. extends:
  2738. file: base.yml
  2739. service: base
  2740. image: busybox
  2741. """)
  2742. tmpdir.join('base.yml').write("""
  2743. version: "2"
  2744. services:
  2745. base:
  2746. volumes: ['/foo']
  2747. ports: ['3000:3000']
  2748. command: top
  2749. """)
  2750. service = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  2751. self.assertEquals(service[0]['command'], "top")
  2752. def test_extends_with_depends_on(self):
  2753. tmpdir = py.test.ensuretemp('test_extends_with_defined_version')
  2754. self.addCleanup(tmpdir.remove)
  2755. tmpdir.join('docker-compose.yml').write("""
  2756. version: "2"
  2757. services:
  2758. base:
  2759. image: example
  2760. web:
  2761. extends: base
  2762. image: busybox
  2763. depends_on: ['other']
  2764. other:
  2765. image: example
  2766. """)
  2767. services = load_from_filename(str(tmpdir.join('docker-compose.yml')))
  2768. assert service_sort(services)[2]['depends_on'] == {
  2769. 'other': {'condition': 'service_started'}
  2770. }
  2771. def test_extends_with_healthcheck(self):
  2772. service_dicts = load_from_filename('tests/fixtures/extends/healthcheck-2.yml')
  2773. assert service_sort(service_dicts) == [{
  2774. 'name': 'demo',
  2775. 'image': 'foobar:latest',
  2776. 'healthcheck': {
  2777. 'test': ['CMD', '/health.sh'],
  2778. 'interval': 10000000000,
  2779. 'timeout': 5000000000,
  2780. 'retries': 36,
  2781. }
  2782. }]
  2783. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  2784. class ExpandPathTest(unittest.TestCase):
  2785. working_dir = '/home/user/somedir'
  2786. def test_expand_path_normal(self):
  2787. result = config.expand_path(self.working_dir, 'myfile')
  2788. self.assertEqual(result, self.working_dir + '/' + 'myfile')
  2789. def test_expand_path_absolute(self):
  2790. abs_path = '/home/user/otherdir/somefile'
  2791. result = config.expand_path(self.working_dir, abs_path)
  2792. self.assertEqual(result, abs_path)
  2793. def test_expand_path_with_tilde(self):
  2794. test_path = '~/otherdir/somefile'
  2795. with mock.patch.dict(os.environ):
  2796. os.environ['HOME'] = user_path = '/home/user/'
  2797. result = config.expand_path(self.working_dir, test_path)
  2798. self.assertEqual(result, user_path + 'otherdir/somefile')
  2799. class VolumePathTest(unittest.TestCase):
  2800. def test_split_path_mapping_with_windows_path(self):
  2801. host_path = "c:\\Users\\msamblanet\\Documents\\anvil\\connect\\config"
  2802. windows_volume_path = host_path + ":/opt/connect/config:ro"
  2803. expected_mapping = ("/opt/connect/config:ro", host_path)
  2804. mapping = config.split_path_mapping(windows_volume_path)
  2805. assert mapping == expected_mapping
  2806. def test_split_path_mapping_with_windows_path_in_container(self):
  2807. host_path = 'c:\\Users\\remilia\\data'
  2808. container_path = 'c:\\scarletdevil\\data'
  2809. expected_mapping = (container_path, host_path)
  2810. mapping = config.split_path_mapping('{0}:{1}'.format(host_path, container_path))
  2811. assert mapping == expected_mapping
  2812. def test_split_path_mapping_with_root_mount(self):
  2813. host_path = '/'
  2814. container_path = '/var/hostroot'
  2815. expected_mapping = (container_path, host_path)
  2816. mapping = config.split_path_mapping('{0}:{1}'.format(host_path, container_path))
  2817. assert mapping == expected_mapping
  2818. @pytest.mark.xfail(IS_WINDOWS_PLATFORM, reason='paths use slash')
  2819. class BuildPathTest(unittest.TestCase):
  2820. def setUp(self):
  2821. self.abs_context_path = os.path.join(os.getcwd(), 'tests/fixtures/build-ctx')
  2822. def test_nonexistent_path(self):
  2823. with self.assertRaises(ConfigurationError):
  2824. config.load(
  2825. build_config_details(
  2826. {
  2827. 'foo': {'build': 'nonexistent.path'},
  2828. },
  2829. 'working_dir',
  2830. 'filename.yml'
  2831. )
  2832. )
  2833. def test_relative_path(self):
  2834. relative_build_path = '../build-ctx/'
  2835. service_dict = make_service_dict(
  2836. 'relpath',
  2837. {'build': relative_build_path},
  2838. working_dir='tests/fixtures/build-path'
  2839. )
  2840. self.assertEquals(service_dict['build'], self.abs_context_path)
  2841. def test_absolute_path(self):
  2842. service_dict = make_service_dict(
  2843. 'abspath',
  2844. {'build': self.abs_context_path},
  2845. working_dir='tests/fixtures/build-path'
  2846. )
  2847. self.assertEquals(service_dict['build'], self.abs_context_path)
  2848. def test_from_file(self):
  2849. service_dict = load_from_filename('tests/fixtures/build-path/docker-compose.yml')
  2850. self.assertEquals(service_dict, [{'name': 'foo', 'build': {'context': self.abs_context_path}}])
  2851. def test_valid_url_in_build_path(self):
  2852. valid_urls = [
  2853. 'git://github.com/docker/docker',
  2854. '[email protected]:docker/docker.git',
  2855. '[email protected]:atlassianlabs/atlassian-docker.git',
  2856. 'https://github.com/docker/docker.git',
  2857. 'http://github.com/docker/docker.git',
  2858. 'github.com/docker/docker.git',
  2859. ]
  2860. for valid_url in valid_urls:
  2861. service_dict = config.load(build_config_details({
  2862. 'validurl': {'build': valid_url},
  2863. }, '.', None)).services
  2864. assert service_dict[0]['build'] == {'context': valid_url}
  2865. def test_invalid_url_in_build_path(self):
  2866. invalid_urls = [
  2867. 'example.com/bogus',
  2868. 'ftp://example.com/',
  2869. '/path/does/not/exist',
  2870. ]
  2871. for invalid_url in invalid_urls:
  2872. with pytest.raises(ConfigurationError) as exc:
  2873. config.load(build_config_details({
  2874. 'invalidurl': {'build': invalid_url},
  2875. }, '.', None))
  2876. assert 'build path' in exc.exconly()
  2877. class HealthcheckTest(unittest.TestCase):
  2878. def test_healthcheck(self):
  2879. service_dict = make_service_dict(
  2880. 'test',
  2881. {'healthcheck': {
  2882. 'test': ['CMD', 'true'],
  2883. 'interval': '1s',
  2884. 'timeout': '1m',
  2885. 'retries': 3,
  2886. }},
  2887. '.',
  2888. )
  2889. assert service_dict['healthcheck'] == {
  2890. 'test': ['CMD', 'true'],
  2891. 'interval': nanoseconds_from_time_seconds(1),
  2892. 'timeout': nanoseconds_from_time_seconds(60),
  2893. 'retries': 3,
  2894. }
  2895. def test_disable(self):
  2896. service_dict = make_service_dict(
  2897. 'test',
  2898. {'healthcheck': {
  2899. 'disable': True,
  2900. }},
  2901. '.',
  2902. )
  2903. assert service_dict['healthcheck'] == {
  2904. 'test': ['NONE'],
  2905. }
  2906. def test_disable_with_other_config_is_invalid(self):
  2907. with pytest.raises(ConfigurationError) as excinfo:
  2908. make_service_dict(
  2909. 'invalid-healthcheck',
  2910. {'healthcheck': {
  2911. 'disable': True,
  2912. 'interval': '1s',
  2913. }},
  2914. '.',
  2915. )
  2916. assert 'invalid-healthcheck' in excinfo.exconly()
  2917. assert 'disable' in excinfo.exconly()
  2918. class GetDefaultConfigFilesTestCase(unittest.TestCase):
  2919. files = [
  2920. 'docker-compose.yml',
  2921. 'docker-compose.yaml',
  2922. ]
  2923. def test_get_config_path_default_file_in_basedir(self):
  2924. for index, filename in enumerate(self.files):
  2925. self.assertEqual(
  2926. filename,
  2927. get_config_filename_for_files(self.files[index:]))
  2928. with self.assertRaises(config.ComposeFileNotFound):
  2929. get_config_filename_for_files([])
  2930. def test_get_config_path_default_file_in_parent_dir(self):
  2931. """Test with files placed in the subdir"""
  2932. def get_config_in_subdir(files):
  2933. return get_config_filename_for_files(files, subdir=True)
  2934. for index, filename in enumerate(self.files):
  2935. self.assertEqual(filename, get_config_in_subdir(self.files[index:]))
  2936. with self.assertRaises(config.ComposeFileNotFound):
  2937. get_config_in_subdir([])
  2938. def get_config_filename_for_files(filenames, subdir=None):
  2939. def make_files(dirname, filenames):
  2940. for fname in filenames:
  2941. with open(os.path.join(dirname, fname), 'w') as f:
  2942. f.write('')
  2943. project_dir = tempfile.mkdtemp()
  2944. try:
  2945. make_files(project_dir, filenames)
  2946. if subdir:
  2947. base_dir = tempfile.mkdtemp(dir=project_dir)
  2948. else:
  2949. base_dir = project_dir
  2950. filename, = config.get_default_config_files(base_dir)
  2951. return os.path.basename(filename)
  2952. finally:
  2953. shutil.rmtree(project_dir)
  2954. class SerializeTest(unittest.TestCase):
  2955. def test_denormalize_depends_on_v3(self):
  2956. service_dict = {
  2957. 'image': 'busybox',
  2958. 'command': 'true',
  2959. 'depends_on': {
  2960. 'service2': {'condition': 'service_started'},
  2961. 'service3': {'condition': 'service_started'},
  2962. }
  2963. }
  2964. assert denormalize_service_dict(service_dict, V3_0) == {
  2965. 'image': 'busybox',
  2966. 'command': 'true',
  2967. 'depends_on': ['service2', 'service3']
  2968. }
  2969. def test_denormalize_depends_on_v2_1(self):
  2970. service_dict = {
  2971. 'image': 'busybox',
  2972. 'command': 'true',
  2973. 'depends_on': {
  2974. 'service2': {'condition': 'service_started'},
  2975. 'service3': {'condition': 'service_started'},
  2976. }
  2977. }
  2978. assert denormalize_service_dict(service_dict, V2_1) == service_dict
  2979. def test_serialize_time(self):
  2980. data = {
  2981. 9: '9ns',
  2982. 9000: '9us',
  2983. 9000000: '9ms',
  2984. 90000000: '90ms',
  2985. 900000000: '900ms',
  2986. 999999999: '999999999ns',
  2987. 1000000000: '1s',
  2988. 60000000000: '1m',
  2989. 60000000001: '60000000001ns',
  2990. 9000000000000: '150m',
  2991. 90000000000000: '25h',
  2992. }
  2993. for k, v in data.items():
  2994. assert serialize_ns_time_value(k) == v
  2995. def test_denormalize_healthcheck(self):
  2996. service_dict = {
  2997. 'image': 'test',
  2998. 'healthcheck': {
  2999. 'test': 'exit 1',
  3000. 'interval': '1m40s',
  3001. 'timeout': '30s',
  3002. 'retries': 5
  3003. }
  3004. }
  3005. processed_service = config.process_service(config.ServiceConfig(
  3006. '.', 'test', 'test', service_dict
  3007. ))
  3008. denormalized_service = denormalize_service_dict(processed_service, V2_1)
  3009. assert denormalized_service['healthcheck']['interval'] == '100s'
  3010. assert denormalized_service['healthcheck']['timeout'] == '30s'