service_test.py 24 KB

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