service_test.py 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import os
  4. import re
  5. import shutil
  6. import tempfile
  7. from distutils.spawn import find_executable
  8. from os import path
  9. import pytest
  10. from docker.errors import APIError
  11. from six import StringIO
  12. from six import text_type
  13. from .. import mock
  14. from .testcases import docker_client
  15. from .testcases import DockerClientTestCase
  16. from .testcases import get_links
  17. from .testcases import pull_busybox
  18. from .testcases import SWARM_SKIP_CONTAINERS_ALL
  19. from .testcases import SWARM_SKIP_CPU_SHARES
  20. from compose import __version__
  21. from compose.config.types import MountSpec
  22. from compose.config.types import VolumeFromSpec
  23. from compose.config.types import VolumeSpec
  24. from compose.const import IS_WINDOWS_PLATFORM
  25. from compose.const import LABEL_CONFIG_HASH
  26. from compose.const import LABEL_CONTAINER_NUMBER
  27. from compose.const import LABEL_ONE_OFF
  28. from compose.const import LABEL_PROJECT
  29. from compose.const import LABEL_SERVICE
  30. from compose.const import LABEL_VERSION
  31. from compose.container import Container
  32. from compose.errors import OperationFailedError
  33. from compose.project import OneOffFilter
  34. from compose.service import ConvergencePlan
  35. from compose.service import ConvergenceStrategy
  36. from compose.service import NetworkMode
  37. from compose.service import PidMode
  38. from compose.service import Service
  39. from compose.utils import parse_nanoseconds_int
  40. from tests.helpers import create_custom_host_file
  41. from tests.integration.testcases import is_cluster
  42. from tests.integration.testcases import no_cluster
  43. from tests.integration.testcases import v2_1_only
  44. from tests.integration.testcases import v2_2_only
  45. from tests.integration.testcases import v2_3_only
  46. from tests.integration.testcases import v2_only
  47. from tests.integration.testcases import v3_only
  48. def create_and_start_container(service, **override_options):
  49. container = service.create_container(**override_options)
  50. return service.start_container(container)
  51. class ServiceTest(DockerClientTestCase):
  52. def test_containers(self):
  53. foo = self.create_service('foo')
  54. bar = self.create_service('bar')
  55. create_and_start_container(foo)
  56. assert len(foo.containers()) == 1
  57. assert foo.containers()[0].name == 'composetest_foo_1'
  58. assert len(bar.containers()) == 0
  59. create_and_start_container(bar)
  60. create_and_start_container(bar)
  61. assert len(foo.containers()) == 1
  62. assert len(bar.containers()) == 2
  63. names = [c.name for c in bar.containers()]
  64. assert 'composetest_bar_1' in names
  65. assert 'composetest_bar_2' in names
  66. def test_containers_one_off(self):
  67. db = self.create_service('db')
  68. container = db.create_container(one_off=True)
  69. assert db.containers(stopped=True) == []
  70. assert db.containers(one_off=OneOffFilter.only, stopped=True) == [container]
  71. def test_project_is_added_to_container_name(self):
  72. service = self.create_service('web')
  73. create_and_start_container(service)
  74. assert service.containers()[0].name == 'composetest_web_1'
  75. def test_create_container_with_one_off(self):
  76. db = self.create_service('db')
  77. container = db.create_container(one_off=True)
  78. assert container.name == 'composetest_db_run_1'
  79. def test_create_container_with_one_off_when_existing_container_is_running(self):
  80. db = self.create_service('db')
  81. db.start()
  82. container = db.create_container(one_off=True)
  83. assert container.name == 'composetest_db_run_1'
  84. def test_create_container_with_unspecified_volume(self):
  85. service = self.create_service('db', volumes=[VolumeSpec.parse('/var/db')])
  86. container = service.create_container()
  87. service.start_container(container)
  88. assert container.get_mount('/var/db')
  89. def test_create_container_with_volume_driver(self):
  90. service = self.create_service('db', volume_driver='foodriver')
  91. container = service.create_container()
  92. service.start_container(container)
  93. assert 'foodriver' == container.get('HostConfig.VolumeDriver')
  94. @pytest.mark.skipif(SWARM_SKIP_CPU_SHARES, reason='Swarm --cpu-shares bug')
  95. def test_create_container_with_cpu_shares(self):
  96. service = self.create_service('db', cpu_shares=73)
  97. container = service.create_container()
  98. service.start_container(container)
  99. assert container.get('HostConfig.CpuShares') == 73
  100. def test_create_container_with_cpu_quota(self):
  101. service = self.create_service('db', cpu_quota=40000)
  102. container = service.create_container()
  103. container.start()
  104. assert container.get('HostConfig.CpuQuota') == 40000
  105. @v2_2_only()
  106. def test_create_container_with_cpu_count(self):
  107. self.require_api_version('1.25')
  108. service = self.create_service('db', cpu_count=2)
  109. container = service.create_container()
  110. service.start_container(container)
  111. assert container.get('HostConfig.CpuCount') == 2
  112. @v2_2_only()
  113. @pytest.mark.skipif(not IS_WINDOWS_PLATFORM, reason='cpu_percent is not supported for Linux')
  114. def test_create_container_with_cpu_percent(self):
  115. self.require_api_version('1.25')
  116. service = self.create_service('db', cpu_percent=12)
  117. container = service.create_container()
  118. service.start_container(container)
  119. assert container.get('HostConfig.CpuPercent') == 12
  120. @v2_2_only()
  121. def test_create_container_with_cpus(self):
  122. self.require_api_version('1.25')
  123. service = self.create_service('db', cpus=1)
  124. container = service.create_container()
  125. service.start_container(container)
  126. assert container.get('HostConfig.NanoCpus') == 1000000000
  127. def test_create_container_with_shm_size(self):
  128. self.require_api_version('1.22')
  129. service = self.create_service('db', shm_size=67108864)
  130. container = service.create_container()
  131. service.start_container(container)
  132. assert container.get('HostConfig.ShmSize') == 67108864
  133. def test_create_container_with_init_bool(self):
  134. self.require_api_version('1.25')
  135. service = self.create_service('db', init=True)
  136. container = service.create_container()
  137. service.start_container(container)
  138. assert container.get('HostConfig.Init') is True
  139. @pytest.mark.xfail(True, reason='Option has been removed in Engine 17.06.0')
  140. def test_create_container_with_init_path(self):
  141. self.require_api_version('1.25')
  142. docker_init_path = find_executable('docker-init')
  143. service = self.create_service('db', init=docker_init_path)
  144. container = service.create_container()
  145. service.start_container(container)
  146. assert container.get('HostConfig.InitPath') == docker_init_path
  147. @pytest.mark.xfail(True, reason='Some kernels/configs do not support pids_limit')
  148. def test_create_container_with_pids_limit(self):
  149. self.require_api_version('1.23')
  150. service = self.create_service('db', pids_limit=10)
  151. container = service.create_container()
  152. service.start_container(container)
  153. assert container.get('HostConfig.PidsLimit') == 10
  154. def test_create_container_with_extra_hosts_list(self):
  155. extra_hosts = ['somehost:162.242.195.82', 'otherhost:50.31.209.229']
  156. service = self.create_service('db', extra_hosts=extra_hosts)
  157. container = service.create_container()
  158. service.start_container(container)
  159. assert set(container.get('HostConfig.ExtraHosts')) == set(extra_hosts)
  160. def test_create_container_with_extra_hosts_dicts(self):
  161. extra_hosts = {'somehost': '162.242.195.82', 'otherhost': '50.31.209.229'}
  162. extra_hosts_list = ['somehost:162.242.195.82', 'otherhost:50.31.209.229']
  163. service = self.create_service('db', extra_hosts=extra_hosts)
  164. container = service.create_container()
  165. service.start_container(container)
  166. assert set(container.get('HostConfig.ExtraHosts')) == set(extra_hosts_list)
  167. def test_create_container_with_cpu_set(self):
  168. service = self.create_service('db', cpuset='0')
  169. container = service.create_container()
  170. service.start_container(container)
  171. assert container.get('HostConfig.CpusetCpus') == '0'
  172. def test_create_container_with_read_only_root_fs(self):
  173. read_only = True
  174. service = self.create_service('db', read_only=read_only)
  175. container = service.create_container()
  176. service.start_container(container)
  177. assert container.get('HostConfig.ReadonlyRootfs') == read_only
  178. def test_create_container_with_blkio_config(self):
  179. blkio_config = {
  180. 'weight': 300,
  181. 'weight_device': [{'path': '/dev/sda', 'weight': 200}],
  182. 'device_read_bps': [{'path': '/dev/sda', 'rate': 1024 * 1024 * 100}],
  183. 'device_read_iops': [{'path': '/dev/sda', 'rate': 1000}],
  184. 'device_write_bps': [{'path': '/dev/sda', 'rate': 1024 * 1024}],
  185. 'device_write_iops': [{'path': '/dev/sda', 'rate': 800}]
  186. }
  187. service = self.create_service('web', blkio_config=blkio_config)
  188. container = service.create_container()
  189. assert container.get('HostConfig.BlkioWeight') == 300
  190. assert container.get('HostConfig.BlkioWeightDevice') == [{
  191. 'Path': '/dev/sda', 'Weight': 200
  192. }]
  193. assert container.get('HostConfig.BlkioDeviceReadBps') == [{
  194. 'Path': '/dev/sda', 'Rate': 1024 * 1024 * 100
  195. }]
  196. assert container.get('HostConfig.BlkioDeviceWriteBps') == [{
  197. 'Path': '/dev/sda', 'Rate': 1024 * 1024
  198. }]
  199. assert container.get('HostConfig.BlkioDeviceReadIOps') == [{
  200. 'Path': '/dev/sda', 'Rate': 1000
  201. }]
  202. assert container.get('HostConfig.BlkioDeviceWriteIOps') == [{
  203. 'Path': '/dev/sda', 'Rate': 800
  204. }]
  205. def test_create_container_with_security_opt(self):
  206. security_opt = ['label:disable']
  207. service = self.create_service('db', security_opt=security_opt)
  208. container = service.create_container()
  209. service.start_container(container)
  210. assert set(container.get('HostConfig.SecurityOpt')) == set(security_opt)
  211. @pytest.mark.xfail(True, reason='Not supported on most drivers')
  212. def test_create_container_with_storage_opt(self):
  213. storage_opt = {'size': '1G'}
  214. service = self.create_service('db', storage_opt=storage_opt)
  215. container = service.create_container()
  216. service.start_container(container)
  217. assert container.get('HostConfig.StorageOpt') == storage_opt
  218. def test_create_container_with_oom_kill_disable(self):
  219. self.require_api_version('1.20')
  220. service = self.create_service('db', oom_kill_disable=True)
  221. container = service.create_container()
  222. assert container.get('HostConfig.OomKillDisable') is True
  223. def test_create_container_with_mac_address(self):
  224. service = self.create_service('db', mac_address='02:42:ac:11:65:43')
  225. container = service.create_container()
  226. service.start_container(container)
  227. assert container.inspect()['Config']['MacAddress'] == '02:42:ac:11:65:43'
  228. def test_create_container_with_specified_volume(self):
  229. host_path = '/tmp/host-path'
  230. container_path = '/container-path'
  231. service = self.create_service(
  232. 'db',
  233. volumes=[VolumeSpec(host_path, container_path, 'rw')])
  234. container = service.create_container()
  235. service.start_container(container)
  236. assert container.get_mount(container_path)
  237. # Match the last component ("host-path"), because boot2docker symlinks /tmp
  238. actual_host_path = container.get_mount(container_path)['Source']
  239. assert path.basename(actual_host_path) == path.basename(host_path), (
  240. "Last component differs: %s, %s" % (actual_host_path, host_path)
  241. )
  242. @v2_3_only()
  243. def test_create_container_with_host_mount(self):
  244. host_path = '/tmp/host-path'
  245. container_path = '/container-path'
  246. create_custom_host_file(self.client, path.join(host_path, 'a.txt'), 'test')
  247. service = self.create_service(
  248. 'db',
  249. volumes=[
  250. MountSpec(type='bind', source=host_path, target=container_path, read_only=True)
  251. ]
  252. )
  253. container = service.create_container()
  254. service.start_container(container)
  255. mount = container.get_mount(container_path)
  256. assert mount
  257. assert path.basename(mount['Source']) == path.basename(host_path)
  258. assert mount['RW'] is False
  259. @v2_3_only()
  260. def test_create_container_with_tmpfs_mount(self):
  261. container_path = '/container-tmpfs'
  262. service = self.create_service(
  263. 'db',
  264. volumes=[MountSpec(type='tmpfs', target=container_path)]
  265. )
  266. container = service.create_container()
  267. service.start_container(container)
  268. mount = container.get_mount(container_path)
  269. assert mount
  270. assert mount['Type'] == 'tmpfs'
  271. @v2_3_only()
  272. def test_create_container_with_volume_mount(self):
  273. container_path = '/container-volume'
  274. volume_name = 'composetest_abcde'
  275. self.client.create_volume(volume_name)
  276. service = self.create_service(
  277. 'db',
  278. volumes=[MountSpec(type='volume', source=volume_name, target=container_path)]
  279. )
  280. container = service.create_container()
  281. service.start_container(container)
  282. mount = container.get_mount(container_path)
  283. assert mount
  284. assert mount['Name'] == volume_name
  285. @v3_only()
  286. def test_create_container_with_legacy_mount(self):
  287. # Ensure mounts are converted to volumes if API version < 1.30
  288. # Needed to support long syntax in the 3.2 format
  289. client = docker_client({}, version='1.25')
  290. container_path = '/container-volume'
  291. volume_name = 'composetest_abcde'
  292. self.client.create_volume(volume_name)
  293. service = Service('db', client=client, volumes=[
  294. MountSpec(type='volume', source=volume_name, target=container_path)
  295. ], image='busybox:latest', command=['top'], project='composetest')
  296. container = service.create_container()
  297. service.start_container(container)
  298. mount = container.get_mount(container_path)
  299. assert mount
  300. assert mount['Name'] == volume_name
  301. def test_create_container_with_healthcheck_config(self):
  302. one_second = parse_nanoseconds_int('1s')
  303. healthcheck = {
  304. 'test': ['true'],
  305. 'interval': 2 * one_second,
  306. 'timeout': 5 * one_second,
  307. 'retries': 5,
  308. 'start_period': 2 * one_second
  309. }
  310. service = self.create_service('db', healthcheck=healthcheck)
  311. container = service.create_container()
  312. remote_healthcheck = container.get('Config.Healthcheck')
  313. assert remote_healthcheck['Test'] == healthcheck['test']
  314. assert remote_healthcheck['Interval'] == healthcheck['interval']
  315. assert remote_healthcheck['Timeout'] == healthcheck['timeout']
  316. assert remote_healthcheck['Retries'] == healthcheck['retries']
  317. assert remote_healthcheck['StartPeriod'] == healthcheck['start_period']
  318. def test_recreate_preserves_volume_with_trailing_slash(self):
  319. """When the Compose file specifies a trailing slash in the container path, make
  320. sure we copy the volume over when recreating.
  321. """
  322. service = self.create_service('data', volumes=[VolumeSpec.parse('/data/')])
  323. old_container = create_and_start_container(service)
  324. volume_path = old_container.get_mount('/data')['Source']
  325. new_container = service.recreate_container(old_container)
  326. assert new_container.get_mount('/data')['Source'] == volume_path
  327. def test_duplicate_volume_trailing_slash(self):
  328. """
  329. When an image specifies a volume, and the Compose file specifies a host path
  330. but adds a trailing slash, make sure that we don't create duplicate binds.
  331. """
  332. host_path = '/tmp/data'
  333. container_path = '/data'
  334. volumes = [VolumeSpec.parse('{}:{}/'.format(host_path, container_path))]
  335. tmp_container = self.client.create_container(
  336. 'busybox', 'true',
  337. volumes={container_path: {}},
  338. labels={'com.docker.compose.test_image': 'true'},
  339. host_config={}
  340. )
  341. image = self.client.commit(tmp_container)['Id']
  342. service = self.create_service('db', image=image, volumes=volumes)
  343. old_container = create_and_start_container(service)
  344. assert old_container.get('Config.Volumes') == {container_path: {}}
  345. service = self.create_service('db', image=image, volumes=volumes)
  346. new_container = service.recreate_container(old_container)
  347. assert new_container.get('Config.Volumes') == {container_path: {}}
  348. assert service.containers(stopped=False) == [new_container]
  349. def test_create_container_with_volumes_from(self):
  350. volume_service = self.create_service('data')
  351. volume_container_1 = volume_service.create_container()
  352. volume_container_2 = Container.create(
  353. self.client,
  354. image='busybox:latest',
  355. command=["top"],
  356. labels={LABEL_PROJECT: 'composetest'},
  357. host_config={},
  358. environment=['affinity:container=={}'.format(volume_container_1.id)],
  359. )
  360. host_service = self.create_service(
  361. 'host',
  362. volumes_from=[
  363. VolumeFromSpec(volume_service, 'rw', 'service'),
  364. VolumeFromSpec(volume_container_2, 'rw', 'container')
  365. ],
  366. environment=['affinity:container=={}'.format(volume_container_1.id)],
  367. )
  368. host_container = host_service.create_container()
  369. host_service.start_container(host_container)
  370. assert volume_container_1.id + ':rw' in host_container.get('HostConfig.VolumesFrom')
  371. assert volume_container_2.id + ':rw' in host_container.get('HostConfig.VolumesFrom')
  372. def test_execute_convergence_plan_recreate(self):
  373. service = self.create_service(
  374. 'db',
  375. environment={'FOO': '1'},
  376. volumes=[VolumeSpec.parse('/etc')],
  377. entrypoint=['top'],
  378. command=['-d', '1']
  379. )
  380. old_container = service.create_container()
  381. assert old_container.get('Config.Entrypoint') == ['top']
  382. assert old_container.get('Config.Cmd') == ['-d', '1']
  383. assert 'FOO=1' in old_container.get('Config.Env')
  384. assert old_container.name == 'composetest_db_1'
  385. service.start_container(old_container)
  386. old_container.inspect() # reload volume data
  387. volume_path = old_container.get_mount('/etc')['Source']
  388. num_containers_before = len(self.client.containers(all=True))
  389. service.options['environment']['FOO'] = '2'
  390. new_container, = service.execute_convergence_plan(
  391. ConvergencePlan('recreate', [old_container]))
  392. assert new_container.get('Config.Entrypoint') == ['top']
  393. assert new_container.get('Config.Cmd') == ['-d', '1']
  394. assert 'FOO=2' in new_container.get('Config.Env')
  395. assert new_container.name == 'composetest_db_1'
  396. assert new_container.get_mount('/etc')['Source'] == volume_path
  397. if not is_cluster(self.client):
  398. assert (
  399. 'affinity:container==%s' % old_container.id in
  400. new_container.get('Config.Env')
  401. )
  402. else:
  403. # In Swarm, the env marker is consumed and the container should be deployed
  404. # on the same node.
  405. assert old_container.get('Node.Name') == new_container.get('Node.Name')
  406. assert len(self.client.containers(all=True)) == num_containers_before
  407. assert old_container.id != new_container.id
  408. with pytest.raises(APIError):
  409. self.client.inspect_container(old_container.id)
  410. def test_execute_convergence_plan_recreate_twice(self):
  411. service = self.create_service(
  412. 'db',
  413. volumes=[VolumeSpec.parse('/etc')],
  414. entrypoint=['top'],
  415. command=['-d', '1'])
  416. orig_container = service.create_container()
  417. service.start_container(orig_container)
  418. orig_container.inspect() # reload volume data
  419. volume_path = orig_container.get_mount('/etc')['Source']
  420. # Do this twice to reproduce the bug
  421. for _ in range(2):
  422. new_container, = service.execute_convergence_plan(
  423. ConvergencePlan('recreate', [orig_container]))
  424. assert new_container.get_mount('/etc')['Source'] == volume_path
  425. if not is_cluster(self.client):
  426. assert ('affinity:container==%s' % orig_container.id in
  427. new_container.get('Config.Env'))
  428. else:
  429. # In Swarm, the env marker is consumed and the container should be deployed
  430. # on the same node.
  431. assert orig_container.get('Node.Name') == new_container.get('Node.Name')
  432. orig_container = new_container
  433. @v2_3_only()
  434. def test_execute_convergence_plan_recreate_twice_with_mount(self):
  435. service = self.create_service(
  436. 'db',
  437. volumes=[MountSpec(target='/etc', type='volume')],
  438. entrypoint=['top'],
  439. command=['-d', '1']
  440. )
  441. orig_container = service.create_container()
  442. service.start_container(orig_container)
  443. orig_container.inspect() # reload volume data
  444. volume_path = orig_container.get_mount('/etc')['Source']
  445. # Do this twice to reproduce the bug
  446. for _ in range(2):
  447. new_container, = service.execute_convergence_plan(
  448. ConvergencePlan('recreate', [orig_container])
  449. )
  450. assert new_container.get_mount('/etc')['Source'] == volume_path
  451. if not is_cluster(self.client):
  452. assert ('affinity:container==%s' % orig_container.id in
  453. new_container.get('Config.Env'))
  454. else:
  455. # In Swarm, the env marker is consumed and the container should be deployed
  456. # on the same node.
  457. assert orig_container.get('Node.Name') == new_container.get('Node.Name')
  458. orig_container = new_container
  459. def test_execute_convergence_plan_when_containers_are_stopped(self):
  460. service = self.create_service(
  461. 'db',
  462. environment={'FOO': '1'},
  463. volumes=[VolumeSpec.parse('/var/db')],
  464. entrypoint=['top'],
  465. command=['-d', '1']
  466. )
  467. service.create_container()
  468. containers = service.containers(stopped=True)
  469. assert len(containers) == 1
  470. container, = containers
  471. assert not container.is_running
  472. service.execute_convergence_plan(ConvergencePlan('start', [container]))
  473. containers = service.containers()
  474. assert len(containers) == 1
  475. container.inspect()
  476. assert container == containers[0]
  477. assert container.is_running
  478. def test_execute_convergence_plan_with_image_declared_volume(self):
  479. service = Service(
  480. project='composetest',
  481. name='db',
  482. client=self.client,
  483. build={'context': 'tests/fixtures/dockerfile-with-volume'},
  484. )
  485. old_container = create_and_start_container(service)
  486. assert [mount['Destination'] for mount in old_container.get('Mounts')] == ['/data']
  487. volume_path = old_container.get_mount('/data')['Source']
  488. new_container, = service.execute_convergence_plan(
  489. ConvergencePlan('recreate', [old_container]))
  490. assert [mount['Destination'] for mount in new_container.get('Mounts')] == ['/data']
  491. assert new_container.get_mount('/data')['Source'] == volume_path
  492. def test_execute_convergence_plan_when_image_volume_masks_config(self):
  493. service = self.create_service(
  494. 'db',
  495. build={'context': 'tests/fixtures/dockerfile-with-volume'},
  496. )
  497. old_container = create_and_start_container(service)
  498. assert [mount['Destination'] for mount in old_container.get('Mounts')] == ['/data']
  499. volume_path = old_container.get_mount('/data')['Source']
  500. service.options['volumes'] = [VolumeSpec.parse('/tmp:/data')]
  501. with mock.patch('compose.service.log') as mock_log:
  502. new_container, = service.execute_convergence_plan(
  503. ConvergencePlan('recreate', [old_container]))
  504. mock_log.warn.assert_called_once_with(mock.ANY)
  505. _, args, kwargs = mock_log.warn.mock_calls[0]
  506. assert "Service \"db\" is using volume \"/data\" from the previous container" in args[0]
  507. assert [mount['Destination'] for mount in new_container.get('Mounts')] == ['/data']
  508. assert new_container.get_mount('/data')['Source'] == volume_path
  509. def test_execute_convergence_plan_when_host_volume_is_removed(self):
  510. host_path = '/tmp/host-path'
  511. service = self.create_service(
  512. 'db',
  513. build={'context': 'tests/fixtures/dockerfile-with-volume'},
  514. volumes=[VolumeSpec(host_path, '/data', 'rw')])
  515. old_container = create_and_start_container(service)
  516. assert (
  517. [mount['Destination'] for mount in old_container.get('Mounts')] ==
  518. ['/data']
  519. )
  520. service.options['volumes'] = []
  521. with mock.patch('compose.service.log', autospec=True) as mock_log:
  522. new_container, = service.execute_convergence_plan(
  523. ConvergencePlan('recreate', [old_container]))
  524. assert not mock_log.warn.called
  525. assert (
  526. [mount['Destination'] for mount in new_container.get('Mounts')] ==
  527. ['/data']
  528. )
  529. assert new_container.get_mount('/data')['Source'] != host_path
  530. def test_execute_convergence_plan_without_start(self):
  531. service = self.create_service(
  532. 'db',
  533. build={'context': 'tests/fixtures/dockerfile-with-volume'}
  534. )
  535. containers = service.execute_convergence_plan(ConvergencePlan('create', []), start=False)
  536. service_containers = service.containers(stopped=True)
  537. assert len(service_containers) == 1
  538. assert not service_containers[0].is_running
  539. containers = service.execute_convergence_plan(
  540. ConvergencePlan('recreate', containers),
  541. start=False)
  542. service_containers = service.containers(stopped=True)
  543. assert len(service_containers) == 1
  544. assert not service_containers[0].is_running
  545. service.execute_convergence_plan(ConvergencePlan('start', containers), start=False)
  546. service_containers = service.containers(stopped=True)
  547. assert len(service_containers) == 1
  548. assert not service_containers[0].is_running
  549. def test_start_container_passes_through_options(self):
  550. db = self.create_service('db')
  551. create_and_start_container(db, environment={'FOO': 'BAR'})
  552. assert db.containers()[0].environment['FOO'] == 'BAR'
  553. def test_start_container_inherits_options_from_constructor(self):
  554. db = self.create_service('db', environment={'FOO': 'BAR'})
  555. create_and_start_container(db)
  556. assert db.containers()[0].environment['FOO'] == 'BAR'
  557. @no_cluster('No legacy links support in Swarm')
  558. def test_start_container_creates_links(self):
  559. db = self.create_service('db')
  560. web = self.create_service('web', links=[(db, None)])
  561. create_and_start_container(db)
  562. create_and_start_container(db)
  563. create_and_start_container(web)
  564. assert set(get_links(web.containers()[0])) == set([
  565. 'composetest_db_1', 'db_1',
  566. 'composetest_db_2', 'db_2',
  567. 'db'
  568. ])
  569. @no_cluster('No legacy links support in Swarm')
  570. def test_start_container_creates_links_with_names(self):
  571. db = self.create_service('db')
  572. web = self.create_service('web', links=[(db, 'custom_link_name')])
  573. create_and_start_container(db)
  574. create_and_start_container(db)
  575. create_and_start_container(web)
  576. assert set(get_links(web.containers()[0])) == set([
  577. 'composetest_db_1', 'db_1',
  578. 'composetest_db_2', 'db_2',
  579. 'custom_link_name'
  580. ])
  581. @no_cluster('No legacy links support in Swarm')
  582. def test_start_container_with_external_links(self):
  583. db = self.create_service('db')
  584. web = self.create_service('web', external_links=['composetest_db_1',
  585. 'composetest_db_2',
  586. 'composetest_db_3:db_3'])
  587. for _ in range(3):
  588. create_and_start_container(db)
  589. create_and_start_container(web)
  590. assert set(get_links(web.containers()[0])) == set([
  591. 'composetest_db_1',
  592. 'composetest_db_2',
  593. 'db_3'
  594. ])
  595. @no_cluster('No legacy links support in Swarm')
  596. def test_start_normal_container_does_not_create_links_to_its_own_service(self):
  597. db = self.create_service('db')
  598. create_and_start_container(db)
  599. create_and_start_container(db)
  600. c = create_and_start_container(db)
  601. assert set(get_links(c)) == set([])
  602. @no_cluster('No legacy links support in Swarm')
  603. def test_start_one_off_container_creates_links_to_its_own_service(self):
  604. db = self.create_service('db')
  605. create_and_start_container(db)
  606. create_and_start_container(db)
  607. c = create_and_start_container(db, one_off=OneOffFilter.only)
  608. assert set(get_links(c)) == set([
  609. 'composetest_db_1', 'db_1',
  610. 'composetest_db_2', 'db_2',
  611. 'db'
  612. ])
  613. def test_start_container_builds_images(self):
  614. service = Service(
  615. name='test',
  616. client=self.client,
  617. build={'context': 'tests/fixtures/simple-dockerfile'},
  618. project='composetest',
  619. )
  620. container = create_and_start_container(service)
  621. container.wait()
  622. assert b'success' in container.logs()
  623. assert len(self.client.images(name='composetest_test')) >= 1
  624. def test_start_container_uses_tagged_image_if_it_exists(self):
  625. self.check_build('tests/fixtures/simple-dockerfile', tag='composetest_test')
  626. service = Service(
  627. name='test',
  628. client=self.client,
  629. build={'context': 'this/does/not/exist/and/will/throw/error'},
  630. project='composetest',
  631. )
  632. container = create_and_start_container(service)
  633. container.wait()
  634. assert b'success' in container.logs()
  635. def test_start_container_creates_ports(self):
  636. service = self.create_service('web', ports=[8000])
  637. container = create_and_start_container(service).inspect()
  638. assert list(container['NetworkSettings']['Ports'].keys()) == ['8000/tcp']
  639. assert container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'] != '8000'
  640. def test_build(self):
  641. base_dir = tempfile.mkdtemp()
  642. self.addCleanup(shutil.rmtree, base_dir)
  643. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  644. f.write("FROM busybox\n")
  645. service = self.create_service('web', build={'context': base_dir})
  646. service.build()
  647. self.addCleanup(self.client.remove_image, service.image_name)
  648. assert self.client.inspect_image('composetest_web')
  649. def test_build_non_ascii_filename(self):
  650. base_dir = tempfile.mkdtemp()
  651. self.addCleanup(shutil.rmtree, base_dir)
  652. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  653. f.write("FROM busybox\n")
  654. with open(os.path.join(base_dir.encode('utf8'), b'foo\xE2bar'), 'w') as f:
  655. f.write("hello world\n")
  656. service = self.create_service('web', build={'context': text_type(base_dir)})
  657. service.build()
  658. self.addCleanup(self.client.remove_image, service.image_name)
  659. assert self.client.inspect_image('composetest_web')
  660. def test_build_with_image_name(self):
  661. base_dir = tempfile.mkdtemp()
  662. self.addCleanup(shutil.rmtree, base_dir)
  663. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  664. f.write("FROM busybox\n")
  665. image_name = 'examples/composetest:latest'
  666. self.addCleanup(self.client.remove_image, image_name)
  667. self.create_service('web', build={'context': base_dir}, image=image_name).build()
  668. assert self.client.inspect_image(image_name)
  669. def test_build_with_git_url(self):
  670. build_url = "https://github.com/dnephin/docker-build-from-url.git"
  671. service = self.create_service('buildwithurl', build={'context': build_url})
  672. self.addCleanup(self.client.remove_image, service.image_name)
  673. service.build()
  674. assert service.image()
  675. def test_build_with_build_args(self):
  676. base_dir = tempfile.mkdtemp()
  677. self.addCleanup(shutil.rmtree, base_dir)
  678. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  679. f.write("FROM busybox\n")
  680. f.write("ARG build_version\n")
  681. f.write("RUN echo ${build_version}\n")
  682. service = self.create_service('buildwithargs',
  683. build={'context': text_type(base_dir),
  684. 'args': {"build_version": "1"}})
  685. service.build()
  686. self.addCleanup(self.client.remove_image, service.image_name)
  687. assert service.image()
  688. assert "build_version=1" in service.image()['ContainerConfig']['Cmd']
  689. def test_build_with_build_args_override(self):
  690. base_dir = tempfile.mkdtemp()
  691. self.addCleanup(shutil.rmtree, base_dir)
  692. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  693. f.write("FROM busybox\n")
  694. f.write("ARG build_version\n")
  695. f.write("RUN echo ${build_version}\n")
  696. service = self.create_service('buildwithargs',
  697. build={'context': text_type(base_dir),
  698. 'args': {"build_version": "1"}})
  699. service.build(build_args_override={'build_version': '2'})
  700. self.addCleanup(self.client.remove_image, service.image_name)
  701. assert service.image()
  702. assert "build_version=2" in service.image()['ContainerConfig']['Cmd']
  703. def test_build_with_build_labels(self):
  704. base_dir = tempfile.mkdtemp()
  705. self.addCleanup(shutil.rmtree, base_dir)
  706. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  707. f.write('FROM busybox\n')
  708. service = self.create_service('buildlabels', build={
  709. 'context': text_type(base_dir),
  710. 'labels': {'com.docker.compose.test': 'true'}
  711. })
  712. service.build()
  713. self.addCleanup(self.client.remove_image, service.image_name)
  714. assert service.image()
  715. assert service.image()['Config']['Labels']['com.docker.compose.test'] == 'true'
  716. @no_cluster('Container networks not on Swarm')
  717. def test_build_with_network(self):
  718. base_dir = tempfile.mkdtemp()
  719. self.addCleanup(shutil.rmtree, base_dir)
  720. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  721. f.write('FROM busybox\n')
  722. f.write('RUN ping -c1 google.local\n')
  723. net_container = self.client.create_container(
  724. 'busybox', 'top', host_config=self.client.create_host_config(
  725. extra_hosts={'google.local': '127.0.0.1'}
  726. ), name='composetest_build_network'
  727. )
  728. self.addCleanup(self.client.remove_container, net_container, force=True)
  729. self.client.start(net_container)
  730. service = self.create_service('buildwithnet', build={
  731. 'context': text_type(base_dir),
  732. 'network': 'container:{}'.format(net_container['Id'])
  733. })
  734. service.build()
  735. self.addCleanup(self.client.remove_image, service.image_name)
  736. assert service.image()
  737. @v2_3_only()
  738. @no_cluster('Not supported on UCP 2.2.0-beta1') # FIXME: remove once support is added
  739. def test_build_with_target(self):
  740. self.require_api_version('1.30')
  741. base_dir = tempfile.mkdtemp()
  742. self.addCleanup(shutil.rmtree, base_dir)
  743. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  744. f.write('FROM busybox as one\n')
  745. f.write('LABEL com.docker.compose.test=true\n')
  746. f.write('LABEL com.docker.compose.test.target=one\n')
  747. f.write('FROM busybox as two\n')
  748. f.write('LABEL com.docker.compose.test.target=two\n')
  749. service = self.create_service('buildtarget', build={
  750. 'context': text_type(base_dir),
  751. 'target': 'one'
  752. })
  753. service.build()
  754. assert service.image()
  755. assert service.image()['Config']['Labels']['com.docker.compose.test.target'] == 'one'
  756. @v2_3_only()
  757. def test_build_with_extra_hosts(self):
  758. self.require_api_version('1.27')
  759. base_dir = tempfile.mkdtemp()
  760. self.addCleanup(shutil.rmtree, base_dir)
  761. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  762. f.write('\n'.join([
  763. 'FROM busybox',
  764. 'RUN ping -c1 foobar',
  765. 'RUN ping -c1 baz',
  766. ]))
  767. service = self.create_service('build_extra_hosts', build={
  768. 'context': text_type(base_dir),
  769. 'extra_hosts': {
  770. 'foobar': '127.0.0.1',
  771. 'baz': '127.0.0.1'
  772. }
  773. })
  774. service.build()
  775. assert service.image()
  776. def test_start_container_stays_unprivileged(self):
  777. service = self.create_service('web')
  778. container = create_and_start_container(service).inspect()
  779. assert container['HostConfig']['Privileged'] is False
  780. def test_start_container_becomes_privileged(self):
  781. service = self.create_service('web', privileged=True)
  782. container = create_and_start_container(service).inspect()
  783. assert container['HostConfig']['Privileged'] is True
  784. def test_expose_does_not_publish_ports(self):
  785. service = self.create_service('web', expose=["8000"])
  786. container = create_and_start_container(service).inspect()
  787. assert container['NetworkSettings']['Ports'] == {'8000/tcp': None}
  788. def test_start_container_creates_port_with_explicit_protocol(self):
  789. service = self.create_service('web', ports=['8000/udp'])
  790. container = create_and_start_container(service).inspect()
  791. assert list(container['NetworkSettings']['Ports'].keys()) == ['8000/udp']
  792. def test_start_container_creates_fixed_external_ports(self):
  793. service = self.create_service('web', ports=['8000:8000'])
  794. container = create_and_start_container(service).inspect()
  795. assert '8000/tcp' in container['NetworkSettings']['Ports']
  796. assert container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'] == '8000'
  797. def test_start_container_creates_fixed_external_ports_when_it_is_different_to_internal_port(self):
  798. service = self.create_service('web', ports=['8001:8000'])
  799. container = create_and_start_container(service).inspect()
  800. assert '8000/tcp' in container['NetworkSettings']['Ports']
  801. assert container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'] == '8001'
  802. def test_port_with_explicit_interface(self):
  803. service = self.create_service('web', ports=[
  804. '127.0.0.1:8001:8000',
  805. '0.0.0.0:9001:9000/udp',
  806. ])
  807. container = create_and_start_container(service).inspect()
  808. assert container['NetworkSettings']['Ports']['8000/tcp'] == [{
  809. 'HostIp': '127.0.0.1',
  810. 'HostPort': '8001',
  811. }]
  812. assert container['NetworkSettings']['Ports']['9000/udp'][0]['HostPort'] == '9001'
  813. if not is_cluster(self.client):
  814. assert container['NetworkSettings']['Ports']['9000/udp'][0]['HostIp'] == '0.0.0.0'
  815. # self.assertEqual(container['NetworkSettings']['Ports'], {
  816. # '8000/tcp': [
  817. # {
  818. # 'HostIp': '127.0.0.1',
  819. # 'HostPort': '8001',
  820. # },
  821. # ],
  822. # '9000/udp': [
  823. # {
  824. # 'HostIp': '0.0.0.0',
  825. # 'HostPort': '9001',
  826. # },
  827. # ],
  828. # })
  829. def test_create_with_image_id(self):
  830. # Get image id for the current busybox:latest
  831. pull_busybox(self.client)
  832. image_id = self.client.inspect_image('busybox:latest')['Id'][:12]
  833. service = self.create_service('foo', image=image_id)
  834. service.create_container()
  835. def test_scale(self):
  836. service = self.create_service('web')
  837. service.scale(1)
  838. assert len(service.containers()) == 1
  839. # Ensure containers don't have stdout or stdin connected
  840. container = service.containers()[0]
  841. config = container.inspect()['Config']
  842. assert not config['AttachStderr']
  843. assert not config['AttachStdout']
  844. assert not config['AttachStdin']
  845. service.scale(3)
  846. assert len(service.containers()) == 3
  847. service.scale(1)
  848. assert len(service.containers()) == 1
  849. service.scale(0)
  850. assert len(service.containers()) == 0
  851. @pytest.mark.skipif(
  852. SWARM_SKIP_CONTAINERS_ALL,
  853. reason='Swarm /containers/json bug'
  854. )
  855. def test_scale_with_stopped_containers(self):
  856. """
  857. Given there are some stopped containers and scale is called with a
  858. desired number that is the same as the number of stopped containers,
  859. test that those containers are restarted and not removed/recreated.
  860. """
  861. service = self.create_service('web')
  862. next_number = service._next_container_number()
  863. valid_numbers = [next_number, next_number + 1]
  864. service.create_container(number=next_number)
  865. service.create_container(number=next_number + 1)
  866. with mock.patch('sys.stderr', new_callable=StringIO) as mock_stderr:
  867. service.scale(2)
  868. for container in service.containers():
  869. assert container.is_running
  870. assert container.number in valid_numbers
  871. captured_output = mock_stderr.getvalue()
  872. assert 'Creating' not in captured_output
  873. assert 'Starting' in captured_output
  874. def test_scale_with_stopped_containers_and_needing_creation(self):
  875. """
  876. Given there are some stopped containers and scale is called with a
  877. desired number that is greater than the number of stopped containers,
  878. test that those containers are restarted and required number are created.
  879. """
  880. service = self.create_service('web')
  881. next_number = service._next_container_number()
  882. service.create_container(number=next_number, quiet=True)
  883. for container in service.containers():
  884. assert not container.is_running
  885. with mock.patch('sys.stderr', new_callable=StringIO) as mock_stderr:
  886. service.scale(2)
  887. assert len(service.containers()) == 2
  888. for container in service.containers():
  889. assert container.is_running
  890. captured_output = mock_stderr.getvalue()
  891. assert 'Creating' in captured_output
  892. assert 'Starting' in captured_output
  893. def test_scale_with_api_error(self):
  894. """Test that when scaling if the API returns an error, that error is handled
  895. and the remaining threads continue.
  896. """
  897. service = self.create_service('web')
  898. next_number = service._next_container_number()
  899. service.create_container(number=next_number, quiet=True)
  900. with mock.patch(
  901. 'compose.container.Container.create',
  902. side_effect=APIError(
  903. message="testing",
  904. response={},
  905. explanation="Boom")):
  906. with mock.patch('sys.stderr', new_callable=StringIO) as mock_stderr:
  907. with pytest.raises(OperationFailedError):
  908. service.scale(3)
  909. assert len(service.containers()) == 1
  910. assert service.containers()[0].is_running
  911. assert (
  912. "ERROR: for composetest_web_2 Cannot create container for service"
  913. " web: Boom" in mock_stderr.getvalue()
  914. )
  915. def test_scale_with_unexpected_exception(self):
  916. """Test that when scaling if the API returns an error, that is not of type
  917. APIError, that error is re-raised.
  918. """
  919. service = self.create_service('web')
  920. next_number = service._next_container_number()
  921. service.create_container(number=next_number, quiet=True)
  922. with mock.patch(
  923. 'compose.container.Container.create',
  924. side_effect=ValueError("BOOM")
  925. ):
  926. with pytest.raises(ValueError):
  927. service.scale(3)
  928. assert len(service.containers()) == 1
  929. assert service.containers()[0].is_running
  930. @mock.patch('compose.service.log')
  931. def test_scale_with_desired_number_already_achieved(self, mock_log):
  932. """
  933. Test that calling scale with a desired number that is equal to the
  934. number of containers already running results in no change.
  935. """
  936. service = self.create_service('web')
  937. next_number = service._next_container_number()
  938. container = service.create_container(number=next_number, quiet=True)
  939. container.start()
  940. container.inspect()
  941. assert container.is_running
  942. assert len(service.containers()) == 1
  943. service.scale(1)
  944. assert len(service.containers()) == 1
  945. container.inspect()
  946. assert container.is_running
  947. captured_output = mock_log.info.call_args[0]
  948. assert 'Desired container number already achieved' in captured_output
  949. @mock.patch('compose.service.log')
  950. def test_scale_with_custom_container_name_outputs_warning(self, mock_log):
  951. """Test that calling scale on a service that has a custom container name
  952. results in warning output.
  953. """
  954. service = self.create_service('app', container_name='custom-container')
  955. assert service.custom_container_name == 'custom-container'
  956. with pytest.raises(OperationFailedError):
  957. service.scale(3)
  958. captured_output = mock_log.warn.call_args[0][0]
  959. assert len(service.containers()) == 1
  960. assert "Remove the custom name to scale the service." in captured_output
  961. def test_scale_sets_ports(self):
  962. service = self.create_service('web', ports=['8000'])
  963. service.scale(2)
  964. containers = service.containers()
  965. assert len(containers) == 2
  966. for container in containers:
  967. assert list(container.get('HostConfig.PortBindings')) == ['8000/tcp']
  968. def test_scale_with_immediate_exit(self):
  969. service = self.create_service('web', image='busybox', command='true')
  970. service.scale(2)
  971. assert len(service.containers(stopped=True)) == 2
  972. def test_network_mode_none(self):
  973. service = self.create_service('web', network_mode=NetworkMode('none'))
  974. container = create_and_start_container(service)
  975. assert container.get('HostConfig.NetworkMode') == 'none'
  976. def test_network_mode_bridged(self):
  977. service = self.create_service('web', network_mode=NetworkMode('bridge'))
  978. container = create_and_start_container(service)
  979. assert container.get('HostConfig.NetworkMode') == 'bridge'
  980. def test_network_mode_host(self):
  981. service = self.create_service('web', network_mode=NetworkMode('host'))
  982. container = create_and_start_container(service)
  983. assert container.get('HostConfig.NetworkMode') == 'host'
  984. def test_pid_mode_none_defined(self):
  985. service = self.create_service('web', pid_mode=None)
  986. container = create_and_start_container(service)
  987. assert container.get('HostConfig.PidMode') == ''
  988. def test_pid_mode_host(self):
  989. service = self.create_service('web', pid_mode=PidMode('host'))
  990. container = create_and_start_container(service)
  991. assert container.get('HostConfig.PidMode') == 'host'
  992. @v2_1_only()
  993. def test_userns_mode_none_defined(self):
  994. service = self.create_service('web', userns_mode=None)
  995. container = create_and_start_container(service)
  996. assert container.get('HostConfig.UsernsMode') == ''
  997. @v2_1_only()
  998. def test_userns_mode_host(self):
  999. service = self.create_service('web', userns_mode='host')
  1000. container = create_and_start_container(service)
  1001. assert container.get('HostConfig.UsernsMode') == 'host'
  1002. def test_dns_no_value(self):
  1003. service = self.create_service('web')
  1004. container = create_and_start_container(service)
  1005. assert container.get('HostConfig.Dns') is None
  1006. def test_dns_list(self):
  1007. service = self.create_service('web', dns=['8.8.8.8', '9.9.9.9'])
  1008. container = create_and_start_container(service)
  1009. assert container.get('HostConfig.Dns') == ['8.8.8.8', '9.9.9.9']
  1010. def test_mem_swappiness(self):
  1011. service = self.create_service('web', mem_swappiness=11)
  1012. container = create_and_start_container(service)
  1013. assert container.get('HostConfig.MemorySwappiness') == 11
  1014. def test_mem_reservation(self):
  1015. service = self.create_service('web', mem_reservation='20m')
  1016. container = create_and_start_container(service)
  1017. assert container.get('HostConfig.MemoryReservation') == 20 * 1024 * 1024
  1018. def test_restart_always_value(self):
  1019. service = self.create_service('web', restart={'Name': 'always'})
  1020. container = create_and_start_container(service)
  1021. assert container.get('HostConfig.RestartPolicy.Name') == 'always'
  1022. def test_oom_score_adj_value(self):
  1023. service = self.create_service('web', oom_score_adj=500)
  1024. container = create_and_start_container(service)
  1025. assert container.get('HostConfig.OomScoreAdj') == 500
  1026. def test_group_add_value(self):
  1027. service = self.create_service('web', group_add=["root", "1"])
  1028. container = create_and_start_container(service)
  1029. host_container_groupadd = container.get('HostConfig.GroupAdd')
  1030. assert "root" in host_container_groupadd
  1031. assert "1" in host_container_groupadd
  1032. def test_dns_opt_value(self):
  1033. service = self.create_service('web', dns_opt=["use-vc", "no-tld-query"])
  1034. container = create_and_start_container(service)
  1035. dns_opt = container.get('HostConfig.DnsOptions')
  1036. assert 'use-vc' in dns_opt
  1037. assert 'no-tld-query' in dns_opt
  1038. def test_restart_on_failure_value(self):
  1039. service = self.create_service('web', restart={
  1040. 'Name': 'on-failure',
  1041. 'MaximumRetryCount': 5
  1042. })
  1043. container = create_and_start_container(service)
  1044. assert container.get('HostConfig.RestartPolicy.Name') == 'on-failure'
  1045. assert container.get('HostConfig.RestartPolicy.MaximumRetryCount') == 5
  1046. def test_cap_add_list(self):
  1047. service = self.create_service('web', cap_add=['SYS_ADMIN', 'NET_ADMIN'])
  1048. container = create_and_start_container(service)
  1049. assert container.get('HostConfig.CapAdd') == ['SYS_ADMIN', 'NET_ADMIN']
  1050. def test_cap_drop_list(self):
  1051. service = self.create_service('web', cap_drop=['SYS_ADMIN', 'NET_ADMIN'])
  1052. container = create_and_start_container(service)
  1053. assert container.get('HostConfig.CapDrop') == ['SYS_ADMIN', 'NET_ADMIN']
  1054. def test_dns_search(self):
  1055. service = self.create_service('web', dns_search=['dc1.example.com', 'dc2.example.com'])
  1056. container = create_and_start_container(service)
  1057. assert container.get('HostConfig.DnsSearch') == ['dc1.example.com', 'dc2.example.com']
  1058. @v2_only()
  1059. def test_tmpfs(self):
  1060. service = self.create_service('web', tmpfs=['/run'])
  1061. container = create_and_start_container(service)
  1062. assert container.get('HostConfig.Tmpfs') == {'/run': ''}
  1063. def test_working_dir_param(self):
  1064. service = self.create_service('container', working_dir='/working/dir/sample')
  1065. container = service.create_container()
  1066. assert container.get('Config.WorkingDir') == '/working/dir/sample'
  1067. def test_split_env(self):
  1068. service = self.create_service(
  1069. 'web',
  1070. environment=['NORMAL=F1', 'CONTAINS_EQUALS=F=2', 'TRAILING_EQUALS='])
  1071. env = create_and_start_container(service).environment
  1072. for k, v in {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''}.items():
  1073. assert env[k] == v
  1074. def test_env_from_file_combined_with_env(self):
  1075. service = self.create_service(
  1076. 'web',
  1077. environment=['ONE=1', 'TWO=2', 'THREE=3'],
  1078. env_file=['tests/fixtures/env/one.env', 'tests/fixtures/env/two.env'])
  1079. env = create_and_start_container(service).environment
  1080. for k, v in {
  1081. 'ONE': '1',
  1082. 'TWO': '2',
  1083. 'THREE': '3',
  1084. 'FOO': 'baz',
  1085. 'DOO': 'dah'
  1086. }.items():
  1087. assert env[k] == v
  1088. @v3_only()
  1089. def test_build_with_cachefrom(self):
  1090. base_dir = tempfile.mkdtemp()
  1091. self.addCleanup(shutil.rmtree, base_dir)
  1092. with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
  1093. f.write("FROM busybox\n")
  1094. service = self.create_service('cache_from',
  1095. build={'context': base_dir,
  1096. 'cache_from': ['build1']})
  1097. service.build()
  1098. self.addCleanup(self.client.remove_image, service.image_name)
  1099. assert service.image()
  1100. @mock.patch.dict(os.environ)
  1101. def test_resolve_env(self):
  1102. os.environ['FILE_DEF'] = 'E1'
  1103. os.environ['FILE_DEF_EMPTY'] = 'E2'
  1104. os.environ['ENV_DEF'] = 'E3'
  1105. service = self.create_service(
  1106. 'web',
  1107. environment={
  1108. 'FILE_DEF': 'F1',
  1109. 'FILE_DEF_EMPTY': '',
  1110. 'ENV_DEF': None,
  1111. 'NO_DEF': None
  1112. }
  1113. )
  1114. env = create_and_start_container(service).environment
  1115. for k, v in {
  1116. 'FILE_DEF': 'F1',
  1117. 'FILE_DEF_EMPTY': '',
  1118. 'ENV_DEF': 'E3',
  1119. 'NO_DEF': None
  1120. }.items():
  1121. assert env[k] == v
  1122. def test_with_high_enough_api_version_we_get_default_network_mode(self):
  1123. # TODO: remove this test once minimum docker version is 1.8.x
  1124. with mock.patch.object(self.client, '_version', '1.20'):
  1125. service = self.create_service('web')
  1126. service_config = service._get_container_host_config({})
  1127. assert service_config['NetworkMode'] == 'default'
  1128. def test_labels(self):
  1129. labels_dict = {
  1130. 'com.example.description': "Accounting webapp",
  1131. 'com.example.department': "Finance",
  1132. 'com.example.label-with-empty-value': "",
  1133. }
  1134. compose_labels = {
  1135. LABEL_CONTAINER_NUMBER: '1',
  1136. LABEL_ONE_OFF: 'False',
  1137. LABEL_PROJECT: 'composetest',
  1138. LABEL_SERVICE: 'web',
  1139. LABEL_VERSION: __version__,
  1140. }
  1141. expected = dict(labels_dict, **compose_labels)
  1142. service = self.create_service('web', labels=labels_dict)
  1143. labels = create_and_start_container(service).labels.items()
  1144. for pair in expected.items():
  1145. assert pair in labels
  1146. def test_empty_labels(self):
  1147. labels_dict = {'foo': '', 'bar': ''}
  1148. service = self.create_service('web', labels=labels_dict)
  1149. labels = create_and_start_container(service).labels.items()
  1150. for name in labels_dict:
  1151. assert (name, '') in labels
  1152. def test_stop_signal(self):
  1153. stop_signal = 'SIGINT'
  1154. service = self.create_service('web', stop_signal=stop_signal)
  1155. container = create_and_start_container(service)
  1156. assert container.stop_signal == stop_signal
  1157. def test_custom_container_name(self):
  1158. service = self.create_service('web', container_name='my-web-container')
  1159. assert service.custom_container_name == 'my-web-container'
  1160. container = create_and_start_container(service)
  1161. assert container.name == 'my-web-container'
  1162. one_off_container = service.create_container(one_off=True)
  1163. assert one_off_container.name != 'my-web-container'
  1164. @pytest.mark.skipif(True, reason="Broken on 1.11.0 - 17.03.0")
  1165. def test_log_drive_invalid(self):
  1166. service = self.create_service('web', logging={'driver': 'xxx'})
  1167. expected_error_msg = "logger: no log driver named 'xxx' is registered"
  1168. with pytest.raises(APIError) as excinfo:
  1169. create_and_start_container(service)
  1170. assert re.search(expected_error_msg, excinfo.value)
  1171. def test_log_drive_empty_default_jsonfile(self):
  1172. service = self.create_service('web')
  1173. log_config = create_and_start_container(service).log_config
  1174. assert 'json-file' == log_config['Type']
  1175. assert not log_config['Config']
  1176. def test_log_drive_none(self):
  1177. service = self.create_service('web', logging={'driver': 'none'})
  1178. log_config = create_and_start_container(service).log_config
  1179. assert 'none' == log_config['Type']
  1180. assert not log_config['Config']
  1181. def test_devices(self):
  1182. service = self.create_service('web', devices=["/dev/random:/dev/mapped-random"])
  1183. device_config = create_and_start_container(service).get('HostConfig.Devices')
  1184. device_dict = {
  1185. 'PathOnHost': '/dev/random',
  1186. 'CgroupPermissions': 'rwm',
  1187. 'PathInContainer': '/dev/mapped-random'
  1188. }
  1189. assert 1 == len(device_config)
  1190. assert device_dict == device_config[0]
  1191. def test_duplicate_containers(self):
  1192. service = self.create_service('web')
  1193. options = service._get_container_create_options({}, 1)
  1194. original = Container.create(service.client, **options)
  1195. assert set(service.containers(stopped=True)) == set([original])
  1196. assert set(service.duplicate_containers()) == set()
  1197. options['name'] = 'temporary_container_name'
  1198. duplicate = Container.create(service.client, **options)
  1199. assert set(service.containers(stopped=True)) == set([original, duplicate])
  1200. assert set(service.duplicate_containers()) == set([duplicate])
  1201. def converge(service, strategy=ConvergenceStrategy.changed):
  1202. """Create a converge plan from a strategy and execute the plan."""
  1203. plan = service.convergence_plan(strategy)
  1204. return service.execute_convergence_plan(plan, timeout=1)
  1205. class ConfigHashTest(DockerClientTestCase):
  1206. def test_no_config_hash_when_one_off(self):
  1207. web = self.create_service('web')
  1208. container = web.create_container(one_off=True)
  1209. assert LABEL_CONFIG_HASH not in container.labels
  1210. def test_no_config_hash_when_overriding_options(self):
  1211. web = self.create_service('web')
  1212. container = web.create_container(environment={'FOO': '1'})
  1213. assert LABEL_CONFIG_HASH not in container.labels
  1214. def test_config_hash_with_custom_labels(self):
  1215. web = self.create_service('web', labels={'foo': '1'})
  1216. container = converge(web)[0]
  1217. assert LABEL_CONFIG_HASH in container.labels
  1218. assert 'foo' in container.labels
  1219. def test_config_hash_sticks_around(self):
  1220. web = self.create_service('web', command=["top"])
  1221. container = converge(web)[0]
  1222. assert LABEL_CONFIG_HASH in container.labels
  1223. web = self.create_service('web', command=["top", "-d", "1"])
  1224. container = converge(web)[0]
  1225. assert LABEL_CONFIG_HASH in container.labels