config_test.py 91 KB

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