service_test.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. import docker
  4. import pytest
  5. from .. import mock
  6. from .. import unittest
  7. from compose.const import IS_WINDOWS_PLATFORM
  8. from compose.const import LABEL_CONFIG_HASH
  9. from compose.const import LABEL_ONE_OFF
  10. from compose.const import LABEL_PROJECT
  11. from compose.const import LABEL_SERVICE
  12. from compose.container import Container
  13. from compose.service import build_volume_binding
  14. from compose.service import ConfigError
  15. from compose.service import ContainerNet
  16. from compose.service import get_container_data_volumes
  17. from compose.service import merge_volume_bindings
  18. from compose.service import NeedsBuildError
  19. from compose.service import Net
  20. from compose.service import NoSuchImageError
  21. from compose.service import parse_repository_tag
  22. from compose.service import parse_volume_spec
  23. from compose.service import Service
  24. from compose.service import ServiceNet
  25. from compose.service import VolumeFromSpec
  26. class ServiceTest(unittest.TestCase):
  27. def setUp(self):
  28. self.mock_client = mock.create_autospec(docker.Client)
  29. def test_project_validation(self):
  30. self.assertRaises(ConfigError, lambda: Service(name='foo', project='>', image='foo'))
  31. Service(name='foo', project='bar.bar__', image='foo')
  32. def test_containers(self):
  33. service = Service('db', self.mock_client, 'myproject', image='foo')
  34. self.mock_client.containers.return_value = []
  35. self.assertEqual(list(service.containers()), [])
  36. def test_containers_with_containers(self):
  37. self.mock_client.containers.return_value = [
  38. dict(Name=str(i), Image='foo', Id=i) for i in range(3)
  39. ]
  40. service = Service('db', self.mock_client, 'myproject', image='foo')
  41. self.assertEqual([c.id for c in service.containers()], list(range(3)))
  42. expected_labels = [
  43. '{0}=myproject'.format(LABEL_PROJECT),
  44. '{0}=db'.format(LABEL_SERVICE),
  45. '{0}=False'.format(LABEL_ONE_OFF),
  46. ]
  47. self.mock_client.containers.assert_called_once_with(
  48. all=False,
  49. filters={'label': expected_labels})
  50. def test_container_without_name(self):
  51. self.mock_client.containers.return_value = [
  52. {'Image': 'foo', 'Id': '1', 'Name': '1'},
  53. {'Image': 'foo', 'Id': '2', 'Name': None},
  54. {'Image': 'foo', 'Id': '3'},
  55. ]
  56. service = Service('db', self.mock_client, 'myproject', image='foo')
  57. self.assertEqual([c.id for c in service.containers()], ['1'])
  58. self.assertEqual(service._next_container_number(), 2)
  59. self.assertEqual(service.get_container(1).id, '1')
  60. def test_get_volumes_from_container(self):
  61. container_id = 'aabbccddee'
  62. service = Service(
  63. 'test',
  64. image='foo',
  65. volumes_from=[VolumeFromSpec(mock.Mock(id=container_id, spec=Container), 'rw')])
  66. self.assertEqual(service._get_volumes_from(), [container_id + ':rw'])
  67. def test_get_volumes_from_container_read_only(self):
  68. container_id = 'aabbccddee'
  69. service = Service(
  70. 'test',
  71. image='foo',
  72. volumes_from=[VolumeFromSpec(mock.Mock(id=container_id, spec=Container), 'ro')])
  73. self.assertEqual(service._get_volumes_from(), [container_id + ':ro'])
  74. def test_get_volumes_from_service_container_exists(self):
  75. container_ids = ['aabbccddee', '12345']
  76. from_service = mock.create_autospec(Service)
  77. from_service.containers.return_value = [
  78. mock.Mock(id=container_id, spec=Container)
  79. for container_id in container_ids
  80. ]
  81. service = Service('test', volumes_from=[VolumeFromSpec(from_service, 'rw')], image='foo')
  82. self.assertEqual(service._get_volumes_from(), [cid + ":rw" for cid in container_ids])
  83. def test_get_volumes_from_service_container_exists_with_flags(self):
  84. for mode in ['ro', 'rw', 'z', 'rw,z', 'z,rw']:
  85. container_ids = ['aabbccddee:' + mode, '12345:' + mode]
  86. from_service = mock.create_autospec(Service)
  87. from_service.containers.return_value = [
  88. mock.Mock(id=container_id.split(':')[0], spec=Container)
  89. for container_id in container_ids
  90. ]
  91. service = Service('test', volumes_from=[VolumeFromSpec(from_service, mode)], image='foo')
  92. self.assertEqual(service._get_volumes_from(), container_ids)
  93. def test_get_volumes_from_service_no_container(self):
  94. container_id = 'abababab'
  95. from_service = mock.create_autospec(Service)
  96. from_service.containers.return_value = []
  97. from_service.create_container.return_value = mock.Mock(
  98. id=container_id,
  99. spec=Container)
  100. service = Service('test', image='foo', volumes_from=[VolumeFromSpec(from_service, 'rw')])
  101. self.assertEqual(service._get_volumes_from(), [container_id + ':rw'])
  102. from_service.create_container.assert_called_once_with()
  103. def test_split_domainname_none(self):
  104. service = Service('foo', image='foo', hostname='name', client=self.mock_client)
  105. opts = service._get_container_create_options({'image': 'foo'}, 1)
  106. self.assertEqual(opts['hostname'], 'name', 'hostname')
  107. self.assertFalse('domainname' in opts, 'domainname')
  108. def test_memory_swap_limit(self):
  109. self.mock_client.create_host_config.return_value = {}
  110. service = Service(name='foo', image='foo', hostname='name', client=self.mock_client, mem_limit=1000000000, memswap_limit=2000000000)
  111. service._get_container_create_options({'some': 'overrides'}, 1)
  112. self.assertTrue(self.mock_client.create_host_config.called)
  113. self.assertEqual(
  114. self.mock_client.create_host_config.call_args[1]['mem_limit'],
  115. 1000000000
  116. )
  117. self.assertEqual(
  118. self.mock_client.create_host_config.call_args[1]['memswap_limit'],
  119. 2000000000
  120. )
  121. def test_log_opt(self):
  122. self.mock_client.create_host_config.return_value = {}
  123. log_opt = {'syslog-address': 'tcp://192.168.0.42:123'}
  124. service = Service(name='foo', image='foo', hostname='name', client=self.mock_client, log_driver='syslog', log_opt=log_opt)
  125. service._get_container_create_options({'some': 'overrides'}, 1)
  126. self.assertTrue(self.mock_client.create_host_config.called)
  127. self.assertEqual(
  128. self.mock_client.create_host_config.call_args[1]['log_config'],
  129. {'Type': 'syslog', 'Config': {'syslog-address': 'tcp://192.168.0.42:123'}}
  130. )
  131. def test_split_domainname_fqdn(self):
  132. service = Service(
  133. 'foo',
  134. hostname='name.domain.tld',
  135. image='foo',
  136. client=self.mock_client)
  137. opts = service._get_container_create_options({'image': 'foo'}, 1)
  138. self.assertEqual(opts['hostname'], 'name', 'hostname')
  139. self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
  140. def test_split_domainname_both(self):
  141. service = Service(
  142. 'foo',
  143. hostname='name',
  144. image='foo',
  145. domainname='domain.tld',
  146. client=self.mock_client)
  147. opts = service._get_container_create_options({'image': 'foo'}, 1)
  148. self.assertEqual(opts['hostname'], 'name', 'hostname')
  149. self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
  150. def test_split_domainname_weird(self):
  151. service = Service(
  152. 'foo',
  153. hostname='name.sub',
  154. domainname='domain.tld',
  155. image='foo',
  156. client=self.mock_client)
  157. opts = service._get_container_create_options({'image': 'foo'}, 1)
  158. self.assertEqual(opts['hostname'], 'name.sub', 'hostname')
  159. self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
  160. def test_get_container_create_options_with_name_option(self):
  161. service = Service(
  162. 'foo',
  163. image='foo',
  164. client=self.mock_client,
  165. container_name='foo1')
  166. name = 'the_new_name'
  167. opts = service._get_container_create_options(
  168. {'name': name},
  169. 1,
  170. one_off=True)
  171. self.assertEqual(opts['name'], name)
  172. def test_get_container_create_options_does_not_mutate_options(self):
  173. labels = {'thing': 'real'}
  174. environment = {'also': 'real'}
  175. service = Service(
  176. 'foo',
  177. image='foo',
  178. labels=dict(labels),
  179. client=self.mock_client,
  180. environment=dict(environment),
  181. )
  182. self.mock_client.inspect_image.return_value = {'Id': 'abcd'}
  183. prev_container = mock.Mock(
  184. id='ababab',
  185. image_config={'ContainerConfig': {}})
  186. opts = service._get_container_create_options(
  187. {},
  188. 1,
  189. previous_container=prev_container)
  190. self.assertEqual(service.options['labels'], labels)
  191. self.assertEqual(service.options['environment'], environment)
  192. self.assertEqual(
  193. opts['labels'][LABEL_CONFIG_HASH],
  194. '3c85881a8903b9d73a06c41860c8be08acce1494ab4cf8408375966dccd714de')
  195. self.assertEqual(
  196. opts['environment'],
  197. {
  198. 'affinity:container': '=ababab',
  199. 'also': 'real',
  200. }
  201. )
  202. def test_get_container_not_found(self):
  203. self.mock_client.containers.return_value = []
  204. service = Service('foo', client=self.mock_client, image='foo')
  205. self.assertRaises(ValueError, service.get_container)
  206. @mock.patch('compose.service.Container', autospec=True)
  207. def test_get_container(self, mock_container_class):
  208. container_dict = dict(Name='default_foo_2')
  209. self.mock_client.containers.return_value = [container_dict]
  210. service = Service('foo', image='foo', client=self.mock_client)
  211. container = service.get_container(number=2)
  212. self.assertEqual(container, mock_container_class.from_ps.return_value)
  213. mock_container_class.from_ps.assert_called_once_with(
  214. self.mock_client, container_dict)
  215. @mock.patch('compose.service.log', autospec=True)
  216. def test_pull_image(self, mock_log):
  217. service = Service('foo', client=self.mock_client, image='someimage:sometag')
  218. service.pull()
  219. self.mock_client.pull.assert_called_once_with(
  220. 'someimage',
  221. tag='sometag',
  222. stream=True)
  223. mock_log.info.assert_called_once_with('Pulling foo (someimage:sometag)...')
  224. def test_pull_image_no_tag(self):
  225. service = Service('foo', client=self.mock_client, image='ababab')
  226. service.pull()
  227. self.mock_client.pull.assert_called_once_with(
  228. 'ababab',
  229. tag='latest',
  230. stream=True)
  231. @mock.patch('compose.service.log', autospec=True)
  232. def test_pull_image_digest(self, mock_log):
  233. service = Service('foo', client=self.mock_client, image='someimage@sha256:1234')
  234. service.pull()
  235. self.mock_client.pull.assert_called_once_with(
  236. 'someimage',
  237. tag='sha256:1234',
  238. stream=True)
  239. mock_log.info.assert_called_once_with('Pulling foo (someimage@sha256:1234)...')
  240. @mock.patch('compose.service.Container', autospec=True)
  241. def test_recreate_container(self, _):
  242. mock_container = mock.create_autospec(Container)
  243. service = Service('foo', client=self.mock_client, image='someimage')
  244. service.image = lambda: {'Id': 'abc123'}
  245. new_container = service.recreate_container(mock_container)
  246. mock_container.stop.assert_called_once_with(timeout=10)
  247. self.mock_client.rename.assert_called_once_with(
  248. mock_container.id,
  249. '%s_%s' % (mock_container.short_id, mock_container.name))
  250. new_container.start.assert_called_once_with()
  251. mock_container.remove.assert_called_once_with()
  252. @mock.patch('compose.service.Container', autospec=True)
  253. def test_recreate_container_with_timeout(self, _):
  254. mock_container = mock.create_autospec(Container)
  255. self.mock_client.inspect_image.return_value = {'Id': 'abc123'}
  256. service = Service('foo', client=self.mock_client, image='someimage')
  257. service.recreate_container(mock_container, timeout=1)
  258. mock_container.stop.assert_called_once_with(timeout=1)
  259. def test_parse_repository_tag(self):
  260. self.assertEqual(parse_repository_tag("root"), ("root", "", ":"))
  261. self.assertEqual(parse_repository_tag("root:tag"), ("root", "tag", ":"))
  262. self.assertEqual(parse_repository_tag("user/repo"), ("user/repo", "", ":"))
  263. self.assertEqual(parse_repository_tag("user/repo:tag"), ("user/repo", "tag", ":"))
  264. self.assertEqual(parse_repository_tag("url:5000/repo"), ("url:5000/repo", "", ":"))
  265. self.assertEqual(parse_repository_tag("url:5000/repo:tag"), ("url:5000/repo", "tag", ":"))
  266. self.assertEqual(parse_repository_tag("root@sha256:digest"), ("root", "sha256:digest", "@"))
  267. self.assertEqual(parse_repository_tag("user/repo@sha256:digest"), ("user/repo", "sha256:digest", "@"))
  268. self.assertEqual(parse_repository_tag("url:5000/repo@sha256:digest"), ("url:5000/repo", "sha256:digest", "@"))
  269. @mock.patch('compose.service.Container', autospec=True)
  270. def test_create_container_latest_is_used_when_no_tag_specified(self, mock_container):
  271. service = Service('foo', client=self.mock_client, image='someimage')
  272. images = []
  273. def pull(repo, tag=None, **kwargs):
  274. self.assertEqual('someimage', repo)
  275. self.assertEqual('latest', tag)
  276. images.append({'Id': 'abc123'})
  277. return []
  278. service.image = lambda *args, **kwargs: mock_get_image(images)
  279. self.mock_client.pull = pull
  280. service.create_container()
  281. self.assertEqual(1, len(images))
  282. def test_create_container_with_build(self):
  283. service = Service('foo', client=self.mock_client, build='.')
  284. images = []
  285. service.image = lambda *args, **kwargs: mock_get_image(images)
  286. service.build = lambda: images.append({'Id': 'abc123'})
  287. service.create_container(do_build=True)
  288. self.assertEqual(1, len(images))
  289. def test_create_container_no_build(self):
  290. service = Service('foo', client=self.mock_client, build='.')
  291. service.image = lambda: {'Id': 'abc123'}
  292. service.create_container(do_build=False)
  293. self.assertFalse(self.mock_client.build.called)
  294. def test_create_container_no_build_but_needs_build(self):
  295. service = Service('foo', client=self.mock_client, build='.')
  296. service.image = lambda *args, **kwargs: mock_get_image([])
  297. with self.assertRaises(NeedsBuildError):
  298. service.create_container(do_build=False)
  299. def test_build_does_not_pull(self):
  300. self.mock_client.build.return_value = [
  301. b'{"stream": "Successfully built 12345"}',
  302. ]
  303. service = Service('foo', client=self.mock_client, build='.')
  304. service.build()
  305. self.assertEqual(self.mock_client.build.call_count, 1)
  306. self.assertFalse(self.mock_client.build.call_args[1]['pull'])
  307. def test_config_dict(self):
  308. self.mock_client.inspect_image.return_value = {'Id': 'abcd'}
  309. service = Service(
  310. 'foo',
  311. image='example.com/foo',
  312. client=self.mock_client,
  313. net=ServiceNet(Service('other')),
  314. links=[(Service('one'), 'one')],
  315. volumes_from=[Service('two')])
  316. config_dict = service.config_dict()
  317. expected = {
  318. 'image_id': 'abcd',
  319. 'options': {'image': 'example.com/foo'},
  320. 'links': [('one', 'one')],
  321. 'net': 'other',
  322. 'volumes_from': ['two'],
  323. }
  324. self.assertEqual(config_dict, expected)
  325. def test_config_dict_with_net_from_container(self):
  326. self.mock_client.inspect_image.return_value = {'Id': 'abcd'}
  327. container = Container(
  328. self.mock_client,
  329. {'Id': 'aaabbb', 'Name': '/foo_1'})
  330. service = Service(
  331. 'foo',
  332. image='example.com/foo',
  333. client=self.mock_client,
  334. net=container)
  335. config_dict = service.config_dict()
  336. expected = {
  337. 'image_id': 'abcd',
  338. 'options': {'image': 'example.com/foo'},
  339. 'links': [],
  340. 'net': 'aaabbb',
  341. 'volumes_from': [],
  342. }
  343. self.assertEqual(config_dict, expected)
  344. class NetTestCase(unittest.TestCase):
  345. def test_net(self):
  346. net = Net('host')
  347. self.assertEqual(net.id, 'host')
  348. self.assertEqual(net.mode, 'host')
  349. self.assertEqual(net.service_name, None)
  350. def test_net_container(self):
  351. container_id = 'abcd'
  352. net = ContainerNet(Container(None, {'Id': container_id}))
  353. self.assertEqual(net.id, container_id)
  354. self.assertEqual(net.mode, 'container:' + container_id)
  355. self.assertEqual(net.service_name, None)
  356. def test_net_service(self):
  357. container_id = 'bbbb'
  358. service_name = 'web'
  359. mock_client = mock.create_autospec(docker.Client)
  360. mock_client.containers.return_value = [
  361. {'Id': container_id, 'Name': container_id, 'Image': 'abcd'},
  362. ]
  363. service = Service(name=service_name, client=mock_client)
  364. net = ServiceNet(service)
  365. self.assertEqual(net.id, service_name)
  366. self.assertEqual(net.mode, 'container:' + container_id)
  367. self.assertEqual(net.service_name, service_name)
  368. def test_net_service_no_containers(self):
  369. service_name = 'web'
  370. mock_client = mock.create_autospec(docker.Client)
  371. mock_client.containers.return_value = []
  372. service = Service(name=service_name, client=mock_client)
  373. net = ServiceNet(service)
  374. self.assertEqual(net.id, service_name)
  375. self.assertEqual(net.mode, None)
  376. self.assertEqual(net.service_name, service_name)
  377. def mock_get_image(images):
  378. if images:
  379. return images[0]
  380. else:
  381. raise NoSuchImageError()
  382. class ServiceVolumesTest(unittest.TestCase):
  383. def setUp(self):
  384. self.mock_client = mock.create_autospec(docker.Client)
  385. def test_parse_volume_spec_only_one_path(self):
  386. spec = parse_volume_spec('/the/volume')
  387. self.assertEqual(spec, (None, '/the/volume', 'rw'))
  388. def test_parse_volume_spec_internal_and_external(self):
  389. spec = parse_volume_spec('external:interval')
  390. self.assertEqual(spec, ('external', 'interval', 'rw'))
  391. def test_parse_volume_spec_with_mode(self):
  392. spec = parse_volume_spec('external:interval:ro')
  393. self.assertEqual(spec, ('external', 'interval', 'ro'))
  394. spec = parse_volume_spec('external:interval:z')
  395. self.assertEqual(spec, ('external', 'interval', 'z'))
  396. def test_parse_volume_spec_too_many_parts(self):
  397. with self.assertRaises(ConfigError):
  398. parse_volume_spec('one:two:three:four')
  399. @pytest.mark.xfail((not IS_WINDOWS_PLATFORM), reason='does not have a drive')
  400. def test_parse_volume_windows_absolute_path(self):
  401. windows_absolute_path = "c:\\Users\\me\\Documents\\shiny\\config:\\opt\\shiny\\config:ro"
  402. spec = parse_volume_spec(windows_absolute_path)
  403. self.assertEqual(
  404. spec,
  405. (
  406. "/c/Users/me/Documents/shiny/config",
  407. "/opt/shiny/config",
  408. "ro"
  409. )
  410. )
  411. def test_build_volume_binding(self):
  412. binding = build_volume_binding(parse_volume_spec('/outside:/inside'))
  413. self.assertEqual(binding, ('/inside', '/outside:/inside:rw'))
  414. def test_get_container_data_volumes(self):
  415. options = [
  416. '/host/volume:/host/volume:ro',
  417. '/new/volume',
  418. '/existing/volume',
  419. ]
  420. self.mock_client.inspect_image.return_value = {
  421. 'ContainerConfig': {
  422. 'Volumes': {
  423. '/mnt/image/data': {},
  424. }
  425. }
  426. }
  427. container = Container(self.mock_client, {
  428. 'Image': 'ababab',
  429. 'Volumes': {
  430. '/host/volume': '/host/volume',
  431. '/existing/volume': '/var/lib/docker/aaaaaaaa',
  432. '/removed/volume': '/var/lib/docker/bbbbbbbb',
  433. '/mnt/image/data': '/var/lib/docker/cccccccc',
  434. },
  435. }, has_been_inspected=True)
  436. expected = {
  437. '/existing/volume': '/var/lib/docker/aaaaaaaa:/existing/volume:rw',
  438. '/mnt/image/data': '/var/lib/docker/cccccccc:/mnt/image/data:rw',
  439. }
  440. binds = get_container_data_volumes(container, options)
  441. self.assertEqual(binds, expected)
  442. def test_merge_volume_bindings(self):
  443. options = [
  444. '/host/volume:/host/volume:ro',
  445. '/host/rw/volume:/host/rw/volume',
  446. '/new/volume',
  447. '/existing/volume',
  448. ]
  449. self.mock_client.inspect_image.return_value = {
  450. 'ContainerConfig': {'Volumes': {}}
  451. }
  452. intermediate_container = Container(self.mock_client, {
  453. 'Image': 'ababab',
  454. 'Volumes': {'/existing/volume': '/var/lib/docker/aaaaaaaa'},
  455. }, has_been_inspected=True)
  456. expected = [
  457. '/host/volume:/host/volume:ro',
  458. '/host/rw/volume:/host/rw/volume:rw',
  459. '/var/lib/docker/aaaaaaaa:/existing/volume:rw',
  460. ]
  461. binds = merge_volume_bindings(options, intermediate_container)
  462. self.assertEqual(set(binds), set(expected))
  463. def test_mount_same_host_path_to_two_volumes(self):
  464. service = Service(
  465. 'web',
  466. image='busybox',
  467. volumes=[
  468. '/host/path:/data1',
  469. '/host/path:/data2',
  470. ],
  471. client=self.mock_client,
  472. )
  473. self.mock_client.inspect_image.return_value = {
  474. 'Id': 'ababab',
  475. 'ContainerConfig': {
  476. 'Volumes': {}
  477. }
  478. }
  479. service._get_container_create_options(
  480. override_options={},
  481. number=1,
  482. )
  483. self.assertEqual(
  484. set(self.mock_client.create_host_config.call_args[1]['binds']),
  485. set([
  486. '/host/path:/data1:rw',
  487. '/host/path:/data2:rw',
  488. ]),
  489. )
  490. def test_different_host_path_in_container_json(self):
  491. service = Service(
  492. 'web',
  493. image='busybox',
  494. volumes=['/host/path:/data'],
  495. client=self.mock_client,
  496. )
  497. self.mock_client.inspect_image.return_value = {
  498. 'Id': 'ababab',
  499. 'ContainerConfig': {
  500. 'Volumes': {
  501. '/data': {},
  502. }
  503. }
  504. }
  505. self.mock_client.inspect_container.return_value = {
  506. 'Id': '123123123',
  507. 'Image': 'ababab',
  508. 'Volumes': {
  509. '/data': '/mnt/sda1/host/path',
  510. },
  511. }
  512. service._get_container_create_options(
  513. override_options={},
  514. number=1,
  515. previous_container=Container(self.mock_client, {'Id': '123123123'}),
  516. )
  517. self.assertEqual(
  518. self.mock_client.create_host_config.call_args[1]['binds'],
  519. ['/mnt/sda1/host/path:/data:rw'],
  520. )
  521. def test_create_with_special_volume_mode(self):
  522. self.mock_client.inspect_image.return_value = {'Id': 'imageid'}
  523. create_calls = []
  524. def create_container(*args, **kwargs):
  525. create_calls.append((args, kwargs))
  526. return {'Id': 'containerid'}
  527. self.mock_client.create_container = create_container
  528. volumes = ['/tmp:/foo:z']
  529. Service(
  530. 'web',
  531. client=self.mock_client,
  532. image='busybox',
  533. volumes=volumes,
  534. ).create_container()
  535. self.assertEqual(len(create_calls), 1)
  536. self.assertEqual(self.mock_client.create_host_config.call_args[1]['binds'], volumes)