2014-01-06 03:26:32 +01:00
|
|
|
from __future__ import absolute_import
|
2015-08-24 21:25:25 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-08-02 22:31:08 +02:00
|
|
|
import os
|
2015-08-24 21:25:25 +02:00
|
|
|
import shutil
|
|
|
|
import tempfile
|
2014-12-13 02:53:02 +01:00
|
|
|
from os import path
|
2014-08-02 22:31:08 +02:00
|
|
|
|
2015-06-14 23:11:29 +02:00
|
|
|
from docker.errors import APIError
|
2015-08-24 21:25:25 +02:00
|
|
|
from six import StringIO
|
|
|
|
from six import text_type
|
2015-04-30 12:57:46 +02:00
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
from .. import mock
|
2015-08-24 21:25:25 +02:00
|
|
|
from .testcases import DockerClientTestCase
|
2015-09-22 18:24:56 +02:00
|
|
|
from .testcases import pull_busybox
|
2015-04-26 23:09:20 +02:00
|
|
|
from compose import __version__
|
2015-08-24 21:25:25 +02:00
|
|
|
from compose.const import LABEL_CONTAINER_NUMBER
|
|
|
|
from compose.const import LABEL_ONE_OFF
|
|
|
|
from compose.const import LABEL_PROJECT
|
|
|
|
from compose.const import LABEL_SERVICE
|
|
|
|
from compose.const import LABEL_VERSION
|
2015-01-12 15:59:05 +01:00
|
|
|
from compose.container import Container
|
2015-08-24 21:25:25 +02:00
|
|
|
from compose.service import build_extra_hosts
|
|
|
|
from compose.service import ConfigError
|
|
|
|
from compose.service import ConvergencePlan
|
2015-09-03 19:13:22 +02:00
|
|
|
from compose.service import Net
|
2015-08-24 21:25:25 +02:00
|
|
|
from compose.service import Service
|
2015-07-18 11:38:46 +02:00
|
|
|
from compose.service import VolumeFromSpec
|
2014-08-02 22:31:08 +02:00
|
|
|
|
2013-12-09 16:00:01 +01:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
def create_and_start_container(service, **override_options):
|
|
|
|
container = service.create_container(**override_options)
|
2015-02-16 15:30:31 +01:00
|
|
|
return service.start_container(container)
|
2014-10-26 18:22:16 +01:00
|
|
|
|
|
|
|
|
2013-12-18 18:01:50 +01:00
|
|
|
class ServiceTest(DockerClientTestCase):
|
2013-12-09 16:00:41 +01:00
|
|
|
def test_containers(self):
|
|
|
|
foo = self.create_service('foo')
|
|
|
|
bar = self.create_service('bar')
|
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(foo)
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(foo.containers()), 1)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(foo.containers()[0].name, 'composetest_foo_1')
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(bar.containers()), 0)
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(bar)
|
|
|
|
create_and_start_container(bar)
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(foo.containers()), 1)
|
|
|
|
self.assertEqual(len(bar.containers()), 2)
|
2013-12-09 16:00:41 +01:00
|
|
|
|
2013-12-18 19:37:48 +01:00
|
|
|
names = [c.name for c in bar.containers()]
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertIn('composetest_bar_1', names)
|
|
|
|
self.assertIn('composetest_bar_2', names)
|
2013-12-19 16:16:17 +01:00
|
|
|
|
2013-12-20 11:46:55 +01:00
|
|
|
def test_containers_one_off(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
container = db.create_container(one_off=True)
|
|
|
|
self.assertEqual(db.containers(stopped=True), [])
|
|
|
|
self.assertEqual(db.containers(one_off=True, stopped=True), [container])
|
|
|
|
|
2013-12-19 16:16:17 +01:00
|
|
|
def test_project_is_added_to_container_name(self):
|
2014-01-02 16:27:51 +01:00
|
|
|
service = self.create_service('web')
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(service)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(service.containers()[0].name, 'composetest_web_1')
|
2013-12-09 19:42:33 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
def test_start_stop(self):
|
2013-12-19 15:47:43 +01:00
|
|
|
service = self.create_service('scalingtest')
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 0)
|
2013-12-09 12:41:05 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
service.create_container()
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 12:41:05 +01:00
|
|
|
|
2013-12-10 21:47:08 +01:00
|
|
|
service.start()
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 12:41:05 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
service.stop(timeout=1)
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(service.containers()), 0)
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2013-12-20 17:22:54 +01:00
|
|
|
service.stop(timeout=1)
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(len(service.containers()), 0)
|
2013-12-20 17:22:54 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2013-12-20 17:53:07 +01:00
|
|
|
def test_kill_remove(self):
|
|
|
|
service = self.create_service('scalingtest')
|
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(service)
|
2013-12-20 17:53:07 +01:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
service.remove_stopped()
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
service.kill()
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
service.remove_stopped()
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 0)
|
|
|
|
|
2013-12-20 11:46:55 +01:00
|
|
|
def test_create_container_with_one_off(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
container = db.create_container(one_off=True)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(container.name, 'composetest_db_run_1')
|
2013-12-20 11:46:55 +01:00
|
|
|
|
2013-12-20 13:55:45 +01:00
|
|
|
def test_create_container_with_one_off_when_existing_container_is_running(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
db.start()
|
|
|
|
container = db.create_container(one_off=True)
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(container.name, 'composetest_db_run_1')
|
2013-12-20 13:55:45 +01:00
|
|
|
|
2014-01-15 13:43:40 +01:00
|
|
|
def test_create_container_with_unspecified_volume(self):
|
|
|
|
service = self.create_service('db', volumes=['/var/db'])
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
2015-05-10 01:38:53 +02:00
|
|
|
self.assertIn('/var/db', container.get('Volumes'))
|
2014-01-15 13:43:40 +01:00
|
|
|
|
2015-06-03 13:21:29 +02:00
|
|
|
def test_create_container_with_volume_driver(self):
|
|
|
|
service = self.create_service('db', volume_driver='foodriver')
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
|
|
|
self.assertEqual('foodriver', container.get('Config.VolumeDriver'))
|
|
|
|
|
2015-01-11 19:58:08 +01:00
|
|
|
def test_create_container_with_cpu_shares(self):
|
|
|
|
service = self.create_service('db', cpu_shares=73)
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
2015-07-28 15:51:27 +02:00
|
|
|
self.assertEqual(container.get('HostConfig.CpuShares'), 73)
|
2015-01-11 19:58:08 +01:00
|
|
|
|
2015-01-21 20:33:51 +01:00
|
|
|
def test_build_extra_hosts(self):
|
|
|
|
# string
|
2015-03-24 11:25:09 +01:00
|
|
|
self.assertRaises(ConfigError, lambda: build_extra_hosts("www.example.com: 192.168.0.17"))
|
2015-01-21 20:33:51 +01:00
|
|
|
|
|
|
|
# list of strings
|
|
|
|
self.assertEqual(build_extra_hosts(
|
2015-03-24 11:25:09 +01:00
|
|
|
["www.example.com:192.168.0.17"]),
|
|
|
|
{'www.example.com': '192.168.0.17'})
|
2015-01-21 20:33:51 +01:00
|
|
|
self.assertEqual(build_extra_hosts(
|
2015-03-24 11:25:09 +01:00
|
|
|
["www.example.com: 192.168.0.17"]),
|
|
|
|
{'www.example.com': '192.168.0.17'})
|
|
|
|
self.assertEqual(build_extra_hosts(
|
|
|
|
["www.example.com: 192.168.0.17",
|
|
|
|
"static.example.com:192.168.0.19",
|
|
|
|
"api.example.com: 192.168.0.18"]),
|
|
|
|
{'www.example.com': '192.168.0.17',
|
|
|
|
'static.example.com': '192.168.0.19',
|
|
|
|
'api.example.com': '192.168.0.18'})
|
|
|
|
|
2015-01-21 20:33:51 +01:00
|
|
|
# list of dictionaries
|
2015-03-24 11:25:09 +01:00
|
|
|
self.assertRaises(ConfigError, lambda: build_extra_hosts(
|
|
|
|
[{'www.example.com': '192.168.0.17'},
|
|
|
|
{'api.example.com': '192.168.0.18'}]))
|
|
|
|
|
|
|
|
# dictionaries
|
2015-01-21 20:33:51 +01:00
|
|
|
self.assertEqual(build_extra_hosts(
|
2015-03-24 11:25:09 +01:00
|
|
|
{'www.example.com': '192.168.0.17',
|
|
|
|
'api.example.com': '192.168.0.18'}),
|
|
|
|
{'www.example.com': '192.168.0.17',
|
|
|
|
'api.example.com': '192.168.0.18'})
|
2015-01-21 20:33:51 +01:00
|
|
|
|
2015-01-15 21:58:17 +01:00
|
|
|
def test_create_container_with_extra_hosts_list(self):
|
2015-03-24 11:25:09 +01:00
|
|
|
extra_hosts = ['somehost:162.242.195.82', 'otherhost:50.31.209.229']
|
2015-01-15 21:58:17 +01:00
|
|
|
service = self.create_service('db', extra_hosts=extra_hosts)
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
2015-03-24 11:25:09 +01:00
|
|
|
self.assertEqual(set(container.get('HostConfig.ExtraHosts')), set(extra_hosts))
|
2015-01-15 21:58:17 +01:00
|
|
|
|
2015-03-24 11:25:09 +01:00
|
|
|
def test_create_container_with_extra_hosts_dicts(self):
|
|
|
|
extra_hosts = {'somehost': '162.242.195.82', 'otherhost': '50.31.209.229'}
|
|
|
|
extra_hosts_list = ['somehost:162.242.195.82', 'otherhost:50.31.209.229']
|
2015-01-15 21:58:17 +01:00
|
|
|
service = self.create_service('db', extra_hosts=extra_hosts)
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
2015-03-24 11:25:09 +01:00
|
|
|
self.assertEqual(set(container.get('HostConfig.ExtraHosts')), set(extra_hosts_list))
|
2015-01-15 21:58:17 +01:00
|
|
|
|
2015-04-23 03:33:46 +02:00
|
|
|
def test_create_container_with_cpu_set(self):
|
|
|
|
service = self.create_service('db', cpuset='0')
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
2015-07-28 15:51:27 +02:00
|
|
|
self.assertEqual(container.get('HostConfig.CpusetCpus'), '0')
|
2015-04-23 03:33:46 +02:00
|
|
|
|
2015-03-29 17:28:57 +02:00
|
|
|
def test_create_container_with_read_only_root_fs(self):
|
|
|
|
read_only = True
|
|
|
|
service = self.create_service('db', read_only=read_only)
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
|
|
|
self.assertEqual(container.get('HostConfig.ReadonlyRootfs'), read_only, container.get('HostConfig'))
|
|
|
|
|
2015-04-17 03:34:42 +02:00
|
|
|
def test_create_container_with_security_opt(self):
|
|
|
|
security_opt = ['label:disable']
|
|
|
|
service = self.create_service('db', security_opt=security_opt)
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
|
|
|
self.assertEqual(set(container.get('HostConfig.SecurityOpt')), set(security_opt))
|
|
|
|
|
2015-03-20 20:14:30 +01:00
|
|
|
def test_create_container_with_mac_address(self):
|
|
|
|
service = self.create_service('db', mac_address='02:42:ac:11:65:43')
|
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
|
|
|
self.assertEqual(container.inspect()['Config']['MacAddress'], '02:42:ac:11:65:43')
|
|
|
|
|
2014-04-25 13:28:00 +02:00
|
|
|
def test_create_container_with_specified_volume(self):
|
2014-12-13 02:53:02 +01:00
|
|
|
host_path = '/tmp/host-path'
|
|
|
|
container_path = '/container-path'
|
|
|
|
|
|
|
|
service = self.create_service('db', volumes=['%s:%s' % (host_path, container_path)])
|
2014-04-25 13:28:00 +02:00
|
|
|
container = service.create_container()
|
|
|
|
service.start_container(container)
|
2014-12-13 02:53:02 +01:00
|
|
|
|
|
|
|
volumes = container.inspect()['Volumes']
|
|
|
|
self.assertIn(container_path, volumes)
|
|
|
|
|
|
|
|
# Match the last component ("host-path"), because boot2docker symlinks /tmp
|
|
|
|
actual_host_path = volumes[container_path]
|
|
|
|
self.assertTrue(path.basename(actual_host_path) == path.basename(host_path),
|
2015-03-26 04:13:01 +01:00
|
|
|
msg=("Last component differs: %s, %s" % (actual_host_path, host_path)))
|
2014-04-25 13:28:00 +02:00
|
|
|
|
2015-07-30 11:31:54 +02:00
|
|
|
def test_recreate_preserves_volume_with_trailing_slash(self):
|
|
|
|
"""
|
|
|
|
When the Compose file specifies a trailing slash in the container path, make
|
|
|
|
sure we copy the volume over when recreating.
|
|
|
|
"""
|
|
|
|
service = self.create_service('data', volumes=['/data/'])
|
|
|
|
old_container = create_and_start_container(service)
|
|
|
|
volume_path = old_container.get('Volumes')['/data']
|
|
|
|
|
|
|
|
new_container = service.recreate_container(old_container)
|
|
|
|
self.assertEqual(new_container.get('Volumes')['/data'], volume_path)
|
|
|
|
|
2015-07-29 19:04:19 +02:00
|
|
|
def test_duplicate_volume_trailing_slash(self):
|
|
|
|
"""
|
|
|
|
When an image specifies a volume, and the Compose file specifies a host path
|
|
|
|
but adds a trailing slash, make sure that we don't create duplicate binds.
|
|
|
|
"""
|
|
|
|
host_path = '/tmp/data'
|
|
|
|
container_path = '/data'
|
|
|
|
volumes = ['{}:{}/'.format(host_path, container_path)]
|
|
|
|
|
|
|
|
tmp_container = self.client.create_container(
|
|
|
|
'busybox', 'true',
|
|
|
|
volumes={container_path: {}},
|
|
|
|
labels={'com.docker.compose.test_image': 'true'},
|
|
|
|
)
|
|
|
|
image = self.client.commit(tmp_container)['Id']
|
|
|
|
|
|
|
|
service = self.create_service('db', image=image, volumes=volumes)
|
|
|
|
old_container = create_and_start_container(service)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
old_container.get('Config.Volumes'),
|
|
|
|
{container_path: {}},
|
|
|
|
)
|
|
|
|
|
|
|
|
service = self.create_service('db', image=image, volumes=volumes)
|
|
|
|
new_container = service.recreate_container(old_container)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
new_container.get('Config.Volumes'),
|
|
|
|
{container_path: {}},
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(service.containers(stopped=False), [new_container])
|
|
|
|
|
2014-05-29 09:40:11 +02:00
|
|
|
def test_create_container_with_volumes_from(self):
|
|
|
|
volume_service = self.create_service('data')
|
|
|
|
volume_container_1 = volume_service.create_container()
|
2015-05-30 22:17:21 +02:00
|
|
|
volume_container_2 = Container.create(
|
|
|
|
self.client,
|
|
|
|
image='busybox:latest',
|
|
|
|
command=["top"],
|
|
|
|
labels={LABEL_PROJECT: 'composetest'},
|
|
|
|
)
|
2015-09-25 18:18:46 +02:00
|
|
|
host_service = self.create_service(
|
|
|
|
'host',
|
|
|
|
volumes_from=[
|
|
|
|
VolumeFromSpec(volume_service, 'rw'),
|
|
|
|
VolumeFromSpec(volume_container_2, 'rw')
|
|
|
|
]
|
|
|
|
)
|
2014-05-29 09:40:11 +02:00
|
|
|
host_container = host_service.create_container()
|
|
|
|
host_service.start_container(host_container)
|
2015-07-18 11:38:46 +02:00
|
|
|
self.assertIn(volume_container_1.id + ':rw',
|
2014-08-31 19:07:49 +02:00
|
|
|
host_container.get('HostConfig.VolumesFrom'))
|
2015-07-18 11:38:46 +02:00
|
|
|
self.assertIn(volume_container_2.id + ':rw',
|
2014-08-31 19:07:49 +02:00
|
|
|
host_container.get('HostConfig.VolumesFrom'))
|
2014-05-29 09:40:11 +02:00
|
|
|
|
2015-06-14 23:11:29 +02:00
|
|
|
def test_execute_convergence_plan_recreate(self):
|
2014-02-21 19:12:51 +01:00
|
|
|
service = self.create_service(
|
|
|
|
'db',
|
|
|
|
environment={'FOO': '1'},
|
2014-08-31 19:07:49 +02:00
|
|
|
volumes=['/etc'],
|
2015-05-21 17:19:15 +02:00
|
|
|
entrypoint=['top'],
|
|
|
|
command=['-d', '1']
|
2014-02-21 19:12:51 +01:00
|
|
|
)
|
2014-01-15 14:06:25 +01:00
|
|
|
old_container = service.create_container()
|
2015-05-21 17:19:15 +02:00
|
|
|
self.assertEqual(old_container.get('Config.Entrypoint'), ['top'])
|
|
|
|
self.assertEqual(old_container.get('Config.Cmd'), ['-d', '1'])
|
2015-05-10 01:38:53 +02:00
|
|
|
self.assertIn('FOO=1', old_container.get('Config.Env'))
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(old_container.name, 'composetest_db_1')
|
2014-01-15 14:06:25 +01:00
|
|
|
service.start_container(old_container)
|
2015-05-10 01:38:53 +02:00
|
|
|
old_container.inspect() # reload volume data
|
|
|
|
volume_path = old_container.get('Volumes')['/etc']
|
2014-01-15 13:22:55 +01:00
|
|
|
|
2014-01-15 18:06:16 +01:00
|
|
|
num_containers_before = len(self.client.containers(all=True))
|
|
|
|
|
2014-01-15 13:22:55 +01:00
|
|
|
service.options['environment']['FOO'] = '2'
|
2015-06-14 23:11:29 +02:00
|
|
|
new_container, = service.execute_convergence_plan(
|
|
|
|
ConvergencePlan('recreate', [old_container]))
|
2015-01-20 21:01:50 +01:00
|
|
|
|
2015-05-21 17:19:15 +02:00
|
|
|
self.assertEqual(new_container.get('Config.Entrypoint'), ['top'])
|
|
|
|
self.assertEqual(new_container.get('Config.Cmd'), ['-d', '1'])
|
2015-05-10 01:38:53 +02:00
|
|
|
self.assertIn('FOO=2', new_container.get('Config.Env'))
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(new_container.name, 'composetest_db_1')
|
2015-05-10 01:38:53 +02:00
|
|
|
self.assertEqual(new_container.get('Volumes')['/etc'], volume_path)
|
|
|
|
self.assertIn(
|
|
|
|
'affinity:container==%s' % old_container.id,
|
|
|
|
new_container.get('Config.Env'))
|
2014-01-15 13:22:55 +01:00
|
|
|
|
2014-04-23 16:46:26 +02:00
|
|
|
self.assertEqual(len(self.client.containers(all=True)), num_containers_before)
|
2014-01-15 14:06:25 +01:00
|
|
|
self.assertNotEqual(old_container.id, new_container.id)
|
2015-01-20 21:01:50 +01:00
|
|
|
self.assertRaises(APIError,
|
|
|
|
self.client.inspect_container,
|
2015-04-24 20:05:17 +02:00
|
|
|
old_container.id)
|
2014-01-03 12:18:59 +01:00
|
|
|
|
2015-06-14 23:11:29 +02:00
|
|
|
def test_execute_convergence_plan_when_containers_are_stopped(self):
|
2014-07-11 23:52:47 +02:00
|
|
|
service = self.create_service(
|
|
|
|
'db',
|
|
|
|
environment={'FOO': '1'},
|
|
|
|
volumes=['/var/db'],
|
2015-05-21 17:19:15 +02:00
|
|
|
entrypoint=['top'],
|
|
|
|
command=['-d', '1']
|
2014-07-11 23:52:47 +02:00
|
|
|
)
|
2015-03-26 07:15:34 +01:00
|
|
|
service.create_container()
|
2014-07-11 23:52:47 +02:00
|
|
|
|
2015-06-14 23:11:29 +02:00
|
|
|
containers = service.containers(stopped=True)
|
|
|
|
self.assertEqual(len(containers), 1)
|
|
|
|
container, = containers
|
|
|
|
self.assertFalse(container.is_running)
|
|
|
|
|
|
|
|
service.execute_convergence_plan(ConvergencePlan('start', [container]))
|
|
|
|
|
|
|
|
containers = service.containers()
|
|
|
|
self.assertEqual(len(containers), 1)
|
|
|
|
container.inspect()
|
|
|
|
self.assertEqual(container, containers[0])
|
|
|
|
self.assertTrue(container.is_running)
|
|
|
|
|
|
|
|
def test_execute_convergence_plan_with_image_declared_volume(self):
|
2015-01-20 14:10:01 +01:00
|
|
|
service = Service(
|
2015-01-27 20:46:58 +01:00
|
|
|
project='composetest',
|
2015-01-20 14:10:01 +01:00
|
|
|
name='db',
|
|
|
|
client=self.client,
|
|
|
|
build='tests/fixtures/dockerfile-with-volume',
|
|
|
|
)
|
|
|
|
|
|
|
|
old_container = create_and_start_container(service)
|
2015-03-26 23:28:02 +01:00
|
|
|
self.assertEqual(list(old_container.get('Volumes').keys()), ['/data'])
|
2015-01-20 14:10:01 +01:00
|
|
|
volume_path = old_container.get('Volumes')['/data']
|
|
|
|
|
2015-06-14 23:11:29 +02:00
|
|
|
new_container, = service.execute_convergence_plan(
|
|
|
|
ConvergencePlan('recreate', [old_container]))
|
2015-10-26 11:27:57 +01:00
|
|
|
|
2015-07-03 04:35:20 +02:00
|
|
|
self.assertEqual(list(new_container.get('Volumes')), ['/data'])
|
2015-01-20 14:10:01 +01:00
|
|
|
self.assertEqual(new_container.get('Volumes')['/data'], volume_path)
|
|
|
|
|
2013-12-10 21:51:55 +01:00
|
|
|
def test_start_container_passes_through_options(self):
|
2013-12-10 22:01:39 +01:00
|
|
|
db = self.create_service('db')
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(db, environment={'FOO': 'BAR'})
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(db.containers()[0].environment['FOO'], 'BAR')
|
2013-12-10 22:01:39 +01:00
|
|
|
|
|
|
|
def test_start_container_inherits_options_from_constructor(self):
|
2013-12-10 21:51:55 +01:00
|
|
|
db = self.create_service('db', environment={'FOO': 'BAR'})
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(db)
|
2013-12-18 19:37:48 +01:00
|
|
|
self.assertEqual(db.containers()[0].environment['FOO'], 'BAR')
|
2013-12-10 21:51:55 +01:00
|
|
|
|
|
|
|
def test_start_container_creates_links(self):
|
2013-12-09 22:39:11 +01:00
|
|
|
db = self.create_service('db')
|
2014-03-01 17:30:33 +01:00
|
|
|
web = self.create_service('web', links=[(db, None)])
|
2014-07-29 03:37:17 +02:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(web)
|
2014-07-29 03:37:17 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(web.containers()[0].links()),
|
|
|
|
set([
|
2015-01-12 15:59:05 +01:00
|
|
|
'composetest_db_1', 'db_1',
|
|
|
|
'composetest_db_2', 'db_2',
|
2015-03-26 04:13:01 +01:00
|
|
|
'db'])
|
2014-07-29 03:37:17 +02:00
|
|
|
)
|
2014-01-19 21:43:31 +01:00
|
|
|
|
2014-03-03 01:54:47 +01:00
|
|
|
def test_start_container_creates_links_with_names(self):
|
|
|
|
db = self.create_service('db')
|
|
|
|
web = self.create_service('web', links=[(db, 'custom_link_name')])
|
2014-07-29 03:37:17 +02:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(web)
|
2014-07-29 03:37:17 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(web.containers()[0].links()),
|
|
|
|
set([
|
2015-01-12 15:59:05 +01:00
|
|
|
'composetest_db_1', 'db_1',
|
|
|
|
'composetest_db_2', 'db_2',
|
2015-03-26 04:13:01 +01:00
|
|
|
'custom_link_name'])
|
2014-07-29 03:37:17 +02:00
|
|
|
)
|
2014-03-03 01:54:47 +01:00
|
|
|
|
2014-10-09 04:31:04 +02:00
|
|
|
def test_start_container_with_external_links(self):
|
|
|
|
db = self.create_service('db')
|
2015-01-12 15:59:05 +01:00
|
|
|
web = self.create_service('web', external_links=['composetest_db_1',
|
|
|
|
'composetest_db_2',
|
|
|
|
'composetest_db_3:db_3'])
|
2014-10-09 04:31:04 +02:00
|
|
|
|
2015-01-08 16:55:40 +01:00
|
|
|
for _ in range(3):
|
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(web)
|
2014-10-09 04:31:04 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(web.containers()[0].links()),
|
|
|
|
set([
|
2015-01-12 15:59:05 +01:00
|
|
|
'composetest_db_1',
|
|
|
|
'composetest_db_2',
|
2015-03-26 07:15:34 +01:00
|
|
|
'db_3']),
|
2014-10-09 04:31:04 +02:00
|
|
|
)
|
2014-03-03 01:54:47 +01:00
|
|
|
|
2014-03-06 19:59:24 +01:00
|
|
|
def test_start_normal_container_does_not_create_links_to_its_own_service(self):
|
|
|
|
db = self.create_service('db')
|
2014-07-29 03:37:17 +02:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(db)
|
2014-07-29 03:37:17 +02:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
c = create_and_start_container(db)
|
2014-07-29 03:37:17 +02:00
|
|
|
self.assertEqual(set(c.links()), set([]))
|
2014-03-06 19:59:24 +01:00
|
|
|
|
|
|
|
def test_start_one_off_container_creates_links_to_its_own_service(self):
|
|
|
|
db = self.create_service('db')
|
2014-07-29 03:37:17 +02:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
create_and_start_container(db)
|
|
|
|
create_and_start_container(db)
|
2014-07-29 03:37:17 +02:00
|
|
|
|
2014-10-26 18:22:16 +01:00
|
|
|
c = create_and_start_container(db, one_off=True)
|
2014-07-29 03:37:17 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(c.links()),
|
|
|
|
set([
|
2015-01-12 15:59:05 +01:00
|
|
|
'composetest_db_1', 'db_1',
|
|
|
|
'composetest_db_2', 'db_2',
|
2015-03-26 04:13:01 +01:00
|
|
|
'db'])
|
2014-07-29 03:37:17 +02:00
|
|
|
)
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2013-12-13 21:36:10 +01:00
|
|
|
def test_start_container_builds_images(self):
|
|
|
|
service = Service(
|
|
|
|
name='test',
|
|
|
|
client=self.client,
|
2013-12-14 17:34:24 +01:00
|
|
|
build='tests/fixtures/simple-dockerfile',
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2013-12-13 21:36:10 +01:00
|
|
|
)
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
2013-12-18 19:37:48 +01:00
|
|
|
container.wait()
|
2014-08-19 23:36:46 +02:00
|
|
|
self.assertIn(b'success', container.logs())
|
2015-01-12 15:59:05 +01:00
|
|
|
self.assertEqual(len(self.client.images(name='composetest_test')), 1)
|
2013-12-20 17:23:40 +01:00
|
|
|
|
|
|
|
def test_start_container_uses_tagged_image_if_it_exists(self):
|
2015-05-30 22:17:21 +02:00
|
|
|
self.check_build('tests/fixtures/simple-dockerfile', tag='composetest_test')
|
2013-12-20 17:23:40 +01:00
|
|
|
service = Service(
|
|
|
|
name='test',
|
|
|
|
client=self.client,
|
|
|
|
build='this/does/not/exist/and/will/throw/error',
|
2015-01-12 15:59:05 +01:00
|
|
|
project='composetest',
|
2013-12-20 17:23:40 +01:00
|
|
|
)
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
2013-12-20 17:23:40 +01:00
|
|
|
container.wait()
|
2014-08-19 23:36:46 +02:00
|
|
|
self.assertIn(b'success', container.logs())
|
2013-12-18 12:14:14 +01:00
|
|
|
|
|
|
|
def test_start_container_creates_ports(self):
|
|
|
|
service = self.create_service('web', ports=[8000])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 18:58:42 +01:00
|
|
|
self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/tcp'])
|
|
|
|
self.assertNotEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000')
|
2013-12-18 12:14:14 +01:00
|
|
|
|
2015-04-30 12:57:46 +02:00
|
|
|
def test_build(self):
|
|
|
|
base_dir = tempfile.mkdtemp()
|
|
|
|
self.addCleanup(shutil.rmtree, base_dir)
|
|
|
|
|
|
|
|
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
|
|
|
|
f.write("FROM busybox\n")
|
|
|
|
|
|
|
|
self.create_service('web', build=base_dir).build()
|
|
|
|
self.assertEqual(len(self.client.images(name='composetest_web')), 1)
|
|
|
|
|
|
|
|
def test_build_non_ascii_filename(self):
|
|
|
|
base_dir = tempfile.mkdtemp()
|
|
|
|
self.addCleanup(shutil.rmtree, base_dir)
|
|
|
|
|
|
|
|
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
|
|
|
|
f.write("FROM busybox\n")
|
|
|
|
|
2015-07-03 04:35:20 +02:00
|
|
|
with open(os.path.join(base_dir.encode('utf8'), b'foo\xE2bar'), 'w') as f:
|
2015-04-30 12:57:46 +02:00
|
|
|
f.write("hello world\n")
|
|
|
|
|
2015-07-22 19:06:43 +02:00
|
|
|
self.create_service('web', build=text_type(base_dir)).build()
|
2015-04-30 12:57:46 +02:00
|
|
|
self.assertEqual(len(self.client.images(name='composetest_web')), 1)
|
|
|
|
|
2014-03-04 00:51:24 +01:00
|
|
|
def test_start_container_stays_unpriviliged(self):
|
|
|
|
service = self.create_service('web')
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 00:51:24 +01:00
|
|
|
self.assertEqual(container['HostConfig']['Privileged'], False)
|
|
|
|
|
|
|
|
def test_start_container_becomes_priviliged(self):
|
2015-03-26 04:13:01 +01:00
|
|
|
service = self.create_service('web', privileged=True)
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 00:51:24 +01:00
|
|
|
self.assertEqual(container['HostConfig']['Privileged'], True)
|
|
|
|
|
2014-03-04 18:59:42 +01:00
|
|
|
def test_expose_does_not_publish_ports(self):
|
2015-09-02 16:41:25 +02:00
|
|
|
service = self.create_service('web', expose=["8000"])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 18:59:42 +01:00
|
|
|
self.assertEqual(container['NetworkSettings']['Ports'], {'8000/tcp': None})
|
|
|
|
|
2014-01-22 18:01:10 +01:00
|
|
|
def test_start_container_creates_port_with_explicit_protocol(self):
|
|
|
|
service = self.create_service('web', ports=['8000/udp'])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 18:58:42 +01:00
|
|
|
self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/udp'])
|
2014-01-22 18:01:10 +01:00
|
|
|
|
2013-12-18 12:14:14 +01:00
|
|
|
def test_start_container_creates_fixed_external_ports(self):
|
|
|
|
service = self.create_service('web', ports=['8000:8000'])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 18:58:42 +01:00
|
|
|
self.assertIn('8000/tcp', container['NetworkSettings']['Ports'])
|
|
|
|
self.assertEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000')
|
2013-12-18 12:14:14 +01:00
|
|
|
|
2014-01-16 02:54:05 +01:00
|
|
|
def test_start_container_creates_fixed_external_ports_when_it_is_different_to_internal_port(self):
|
|
|
|
service = self.create_service('web', ports=['8001:8000'])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-03-04 18:58:42 +01:00
|
|
|
self.assertIn('8000/tcp', container['NetworkSettings']['Ports'])
|
|
|
|
self.assertEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8001')
|
2013-12-09 22:39:11 +01:00
|
|
|
|
2014-06-25 12:49:54 +02:00
|
|
|
def test_port_with_explicit_interface(self):
|
|
|
|
service = self.create_service('web', ports=[
|
|
|
|
'127.0.0.1:8001:8000',
|
2014-07-24 20:48:29 +02:00
|
|
|
'0.0.0.0:9001:9000/udp',
|
2014-06-25 12:49:54 +02:00
|
|
|
])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service).inspect()
|
2014-06-25 12:49:54 +02:00
|
|
|
self.assertEqual(container['NetworkSettings']['Ports'], {
|
|
|
|
'8000/tcp': [
|
|
|
|
{
|
|
|
|
'HostIp': '127.0.0.1',
|
|
|
|
'HostPort': '8001',
|
|
|
|
},
|
|
|
|
],
|
2014-07-24 20:48:29 +02:00
|
|
|
'9000/udp': [
|
2014-06-25 12:49:54 +02:00
|
|
|
{
|
|
|
|
'HostIp': '0.0.0.0',
|
|
|
|
'HostPort': '9001',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
|
2015-06-08 16:58:11 +02:00
|
|
|
def test_create_with_image_id(self):
|
2015-09-22 18:24:56 +02:00
|
|
|
# Get image id for the current busybox:latest
|
|
|
|
pull_busybox(self.client)
|
|
|
|
image_id = self.client.inspect_image('busybox:latest')['Id'][:12]
|
|
|
|
service = self.create_service('foo', image=image_id)
|
2015-06-08 16:58:11 +02:00
|
|
|
service.create_container()
|
2015-04-24 17:11:01 +02:00
|
|
|
|
2014-01-16 18:58:53 +01:00
|
|
|
def test_scale(self):
|
|
|
|
service = self.create_service('web')
|
|
|
|
service.scale(1)
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
2014-12-18 03:31:22 +01:00
|
|
|
|
|
|
|
# Ensure containers don't have stdout or stdin connected
|
|
|
|
container = service.containers()[0]
|
|
|
|
config = container.inspect()['Config']
|
|
|
|
self.assertFalse(config['AttachStderr'])
|
|
|
|
self.assertFalse(config['AttachStdout'])
|
|
|
|
self.assertFalse(config['AttachStdin'])
|
|
|
|
|
2014-01-16 18:58:53 +01:00
|
|
|
service.scale(3)
|
|
|
|
self.assertEqual(len(service.containers()), 3)
|
|
|
|
service.scale(1)
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
service.scale(0)
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
|
2015-08-24 20:49:51 +02:00
|
|
|
def test_scale_with_stopped_containers(self):
|
2015-07-22 19:06:43 +02:00
|
|
|
"""
|
|
|
|
Given there are some stopped containers and scale is called with a
|
|
|
|
desired number that is the same as the number of stopped containers,
|
|
|
|
test that those containers are restarted and not removed/recreated.
|
|
|
|
"""
|
|
|
|
service = self.create_service('web')
|
|
|
|
next_number = service._next_container_number()
|
|
|
|
valid_numbers = [next_number, next_number + 1]
|
2015-08-24 20:49:51 +02:00
|
|
|
service.create_container(number=next_number)
|
|
|
|
service.create_container(number=next_number + 1)
|
2015-07-22 19:06:43 +02:00
|
|
|
|
2015-08-24 20:49:51 +02:00
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
service.scale(2)
|
2015-07-22 19:06:43 +02:00
|
|
|
for container in service.containers():
|
|
|
|
self.assertTrue(container.is_running)
|
|
|
|
self.assertTrue(container.number in valid_numbers)
|
|
|
|
|
|
|
|
captured_output = mock_stdout.getvalue()
|
|
|
|
self.assertNotIn('Creating', captured_output)
|
|
|
|
self.assertIn('Starting', captured_output)
|
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_scale_with_stopped_containers_and_needing_creation(self):
|
2015-07-22 19:06:43 +02:00
|
|
|
"""
|
|
|
|
Given there are some stopped containers and scale is called with a
|
|
|
|
desired number that is greater than the number of stopped containers,
|
|
|
|
test that those containers are restarted and required number are created.
|
|
|
|
"""
|
|
|
|
service = self.create_service('web')
|
|
|
|
next_number = service._next_container_number()
|
|
|
|
service.create_container(number=next_number, quiet=True)
|
|
|
|
|
|
|
|
for container in service.containers():
|
|
|
|
self.assertFalse(container.is_running)
|
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
service.scale(2)
|
2015-07-22 19:06:43 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(service.containers()), 2)
|
|
|
|
for container in service.containers():
|
|
|
|
self.assertTrue(container.is_running)
|
|
|
|
|
|
|
|
captured_output = mock_stdout.getvalue()
|
|
|
|
self.assertIn('Creating', captured_output)
|
|
|
|
self.assertIn('Starting', captured_output)
|
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_scale_with_api_returns_errors(self):
|
2015-07-22 19:06:43 +02:00
|
|
|
"""
|
|
|
|
Test that when scaling if the API returns an error, that error is handled
|
|
|
|
and the remaining threads continue.
|
|
|
|
"""
|
|
|
|
service = self.create_service('web')
|
|
|
|
next_number = service._next_container_number()
|
|
|
|
service.create_container(number=next_number, quiet=True)
|
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
with mock.patch(
|
2015-07-22 19:06:43 +02:00
|
|
|
'compose.container.Container.create',
|
|
|
|
side_effect=APIError(message="testing", response={}, explanation="Boom")):
|
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
|
|
|
|
service.scale(3)
|
2015-07-22 19:06:43 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
self.assertIn("ERROR: for 2 Boom", mock_stdout.getvalue())
|
|
|
|
|
2015-09-09 20:38:46 +02:00
|
|
|
def test_scale_with_api_returns_unexpected_exception(self):
|
2015-08-17 17:31:57 +02:00
|
|
|
"""
|
|
|
|
Test that when scaling if the API returns an error, that is not of type
|
|
|
|
APIError, that error is re-raised.
|
|
|
|
"""
|
|
|
|
service = self.create_service('web')
|
|
|
|
next_number = service._next_container_number()
|
|
|
|
service.create_container(number=next_number, quiet=True)
|
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
with mock.patch(
|
2015-08-17 17:31:57 +02:00
|
|
|
'compose.container.Container.create',
|
2015-09-09 20:38:46 +02:00
|
|
|
side_effect=ValueError("BOOM")
|
|
|
|
):
|
2015-08-17 17:31:57 +02:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
service.scale(3)
|
|
|
|
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
@mock.patch('compose.service.log')
|
2015-07-22 19:06:43 +02:00
|
|
|
def test_scale_with_desired_number_already_achieved(self, mock_log):
|
|
|
|
"""
|
|
|
|
Test that calling scale with a desired number that is equal to the
|
|
|
|
number of containers already running results in no change.
|
|
|
|
"""
|
|
|
|
service = self.create_service('web')
|
|
|
|
next_number = service._next_container_number()
|
|
|
|
container = service.create_container(number=next_number, quiet=True)
|
|
|
|
container.start()
|
|
|
|
|
|
|
|
self.assertTrue(container.is_running)
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
service.scale(1)
|
|
|
|
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
container.inspect()
|
|
|
|
self.assertTrue(container.is_running)
|
|
|
|
|
|
|
|
captured_output = mock_log.info.call_args[0]
|
|
|
|
self.assertIn('Desired container number already achieved', captured_output)
|
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
@mock.patch('compose.service.log')
|
2015-07-22 19:06:43 +02:00
|
|
|
def test_scale_with_custom_container_name_outputs_warning(self, mock_log):
|
|
|
|
"""
|
|
|
|
Test that calling scale on a service that has a custom container name
|
|
|
|
results in warning output.
|
|
|
|
"""
|
2015-08-27 00:55:22 +02:00
|
|
|
service = self.create_service('app', container_name='custom-container')
|
2015-07-22 19:06:43 +02:00
|
|
|
self.assertEqual(service.custom_container_name(), 'custom-container')
|
|
|
|
|
|
|
|
service.scale(3)
|
|
|
|
|
|
|
|
captured_output = mock_log.warn.call_args[0][0]
|
|
|
|
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertIn(
|
|
|
|
"Remove the custom name to scale the service.",
|
|
|
|
captured_output
|
|
|
|
)
|
|
|
|
|
2014-02-17 22:33:05 +01:00
|
|
|
def test_scale_sets_ports(self):
|
|
|
|
service = self.create_service('web', ports=['8000'])
|
|
|
|
service.scale(2)
|
|
|
|
containers = service.containers()
|
|
|
|
self.assertEqual(len(containers), 2)
|
|
|
|
for container in containers:
|
|
|
|
self.assertEqual(list(container.inspect()['HostConfig']['PortBindings'].keys()), ['8000/tcp'])
|
2014-06-19 12:57:55 +02:00
|
|
|
|
|
|
|
def test_network_mode_none(self):
|
2015-09-03 19:13:22 +02:00
|
|
|
service = self.create_service('web', net=Net('none'))
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
2014-08-31 19:07:49 +02:00
|
|
|
self.assertEqual(container.get('HostConfig.NetworkMode'), 'none')
|
2014-06-19 12:57:55 +02:00
|
|
|
|
|
|
|
def test_network_mode_bridged(self):
|
2015-09-03 19:13:22 +02:00
|
|
|
service = self.create_service('web', net=Net('bridge'))
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
2014-08-31 19:07:49 +02:00
|
|
|
self.assertEqual(container.get('HostConfig.NetworkMode'), 'bridge')
|
2014-06-19 12:57:55 +02:00
|
|
|
|
|
|
|
def test_network_mode_host(self):
|
2015-09-03 19:13:22 +02:00
|
|
|
service = self.create_service('web', net=Net('host'))
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
2014-08-31 19:07:49 +02:00
|
|
|
self.assertEqual(container.get('HostConfig.NetworkMode'), 'host')
|
2014-07-06 18:09:30 +02:00
|
|
|
|
2015-02-25 06:42:23 +01:00
|
|
|
def test_pid_mode_none_defined(self):
|
|
|
|
service = self.create_service('web', pid=None)
|
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.PidMode'), '')
|
|
|
|
|
|
|
|
def test_pid_mode_host(self):
|
|
|
|
service = self.create_service('web', pid='host')
|
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.PidMode'), 'host')
|
|
|
|
|
2015-03-25 00:17:05 +01:00
|
|
|
def test_dns_no_value(self):
|
|
|
|
service = self.create_service('web')
|
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertIsNone(container.get('HostConfig.Dns'))
|
|
|
|
|
2014-07-18 03:11:50 +02:00
|
|
|
def test_dns_single_value(self):
|
|
|
|
service = self.create_service('web', dns='8.8.8.8')
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.Dns'), ['8.8.8.8'])
|
2014-07-18 03:11:50 +02:00
|
|
|
|
|
|
|
def test_dns_list(self):
|
|
|
|
service = self.create_service('web', dns=['8.8.8.8', '9.9.9.9'])
|
2014-10-26 18:22:16 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.Dns'), ['8.8.8.8', '9.9.9.9'])
|
2014-07-18 03:11:50 +02:00
|
|
|
|
2014-10-28 16:31:09 +01:00
|
|
|
def test_restart_always_value(self):
|
|
|
|
service = self.create_service('web', restart='always')
|
2014-10-26 18:23:15 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.RestartPolicy.Name'), 'always')
|
2014-10-28 16:31:09 +01:00
|
|
|
|
|
|
|
def test_restart_on_failure_value(self):
|
|
|
|
service = self.create_service('web', restart='on-failure:5')
|
2014-10-26 18:23:15 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.RestartPolicy.Name'), 'on-failure')
|
|
|
|
self.assertEqual(container.get('HostConfig.RestartPolicy.MaximumRetryCount'), 5)
|
2014-10-28 16:31:09 +01:00
|
|
|
|
2014-11-06 20:38:58 +01:00
|
|
|
def test_cap_add_list(self):
|
|
|
|
service = self.create_service('web', cap_add=['SYS_ADMIN', 'NET_ADMIN'])
|
2014-10-26 18:23:15 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.CapAdd'), ['SYS_ADMIN', 'NET_ADMIN'])
|
2014-11-06 20:38:58 +01:00
|
|
|
|
|
|
|
def test_cap_drop_list(self):
|
|
|
|
service = self.create_service('web', cap_drop=['SYS_ADMIN', 'NET_ADMIN'])
|
2014-10-26 18:23:15 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.CapDrop'), ['SYS_ADMIN', 'NET_ADMIN'])
|
2014-11-06 20:38:58 +01:00
|
|
|
|
2015-03-25 00:17:05 +01:00
|
|
|
def test_dns_search_no_value(self):
|
|
|
|
service = self.create_service('web')
|
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertIsNone(container.get('HostConfig.DnsSearch'))
|
|
|
|
|
2014-08-17 15:18:24 +02:00
|
|
|
def test_dns_search_single_value(self):
|
|
|
|
service = self.create_service('web', dns_search='example.com')
|
2014-10-26 18:23:15 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.DnsSearch'), ['example.com'])
|
2014-08-17 15:18:24 +02:00
|
|
|
|
|
|
|
def test_dns_search_list(self):
|
|
|
|
service = self.create_service('web', dns_search=['dc1.example.com', 'dc2.example.com'])
|
2014-10-26 18:23:15 +01:00
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.get('HostConfig.DnsSearch'), ['dc1.example.com', 'dc2.example.com'])
|
2014-08-17 15:18:24 +02:00
|
|
|
|
2014-07-06 18:09:30 +02:00
|
|
|
def test_working_dir_param(self):
|
|
|
|
service = self.create_service('container', working_dir='/working/dir/sample')
|
2014-10-26 18:22:16 +01:00
|
|
|
container = service.create_container()
|
|
|
|
self.assertEqual(container.get('Config.WorkingDir'), '/working/dir/sample')
|
2014-07-11 19:18:05 +02:00
|
|
|
|
|
|
|
def test_split_env(self):
|
|
|
|
service = self.create_service('web', environment=['NORMAL=F1', 'CONTAINS_EQUALS=F=2', 'TRAILING_EQUALS='])
|
2014-10-26 18:22:16 +01:00
|
|
|
env = create_and_start_container(service).environment
|
2015-03-26 04:13:01 +01:00
|
|
|
for k, v in {'NORMAL': 'F1', 'CONTAINS_EQUALS': 'F=2', 'TRAILING_EQUALS': ''}.items():
|
2014-07-11 19:18:05 +02:00
|
|
|
self.assertEqual(env[k], v)
|
|
|
|
|
2014-09-12 05:57:23 +02:00
|
|
|
def test_env_from_file_combined_with_env(self):
|
2015-08-26 18:53:11 +02:00
|
|
|
service = self.create_service(
|
|
|
|
'web',
|
|
|
|
environment=['ONE=1', 'TWO=2', 'THREE=3'],
|
|
|
|
env_file=['tests/fixtures/env/one.env', 'tests/fixtures/env/two.env'])
|
2014-10-26 18:23:15 +01:00
|
|
|
env = create_and_start_container(service).environment
|
2015-03-26 04:13:01 +01:00
|
|
|
for k, v in {'ONE': '1', 'TWO': '2', 'THREE': '3', 'FOO': 'baz', 'DOO': 'dah'}.items():
|
2014-09-12 05:57:23 +02:00
|
|
|
self.assertEqual(env[k], v)
|
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
@mock.patch.dict(os.environ)
|
2014-07-11 19:18:05 +02:00
|
|
|
def test_resolve_env(self):
|
|
|
|
os.environ['FILE_DEF'] = 'E1'
|
|
|
|
os.environ['FILE_DEF_EMPTY'] = 'E2'
|
|
|
|
os.environ['ENV_DEF'] = 'E3'
|
2015-02-27 12:54:57 +01:00
|
|
|
service = self.create_service('web', environment={'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': None, 'NO_DEF': None})
|
|
|
|
env = create_and_start_container(service).environment
|
2015-03-26 04:13:01 +01:00
|
|
|
for k, v in {'FILE_DEF': 'F1', 'FILE_DEF_EMPTY': '', 'ENV_DEF': 'E3', 'NO_DEF': ''}.items():
|
2015-02-27 12:54:57 +01:00
|
|
|
self.assertEqual(env[k], v)
|
2015-03-20 00:10:27 +01:00
|
|
|
|
2015-08-18 17:43:19 +02:00
|
|
|
def test_with_high_enough_api_version_we_get_default_network_mode(self):
|
|
|
|
# TODO: remove this test once minimum docker version is 1.8.x
|
|
|
|
with mock.patch.object(self.client, '_version', '1.20'):
|
|
|
|
service = self.create_service('web')
|
|
|
|
service_config = service._get_container_host_config({})
|
|
|
|
self.assertEquals(service_config['NetworkMode'], 'default')
|
|
|
|
|
2015-03-20 00:10:27 +01:00
|
|
|
def test_labels(self):
|
|
|
|
labels_dict = {
|
|
|
|
'com.example.description': "Accounting webapp",
|
|
|
|
'com.example.department': "Finance",
|
|
|
|
'com.example.label-with-empty-value': "",
|
|
|
|
}
|
|
|
|
|
2015-04-26 23:09:20 +02:00
|
|
|
compose_labels = {
|
|
|
|
LABEL_CONTAINER_NUMBER: '1',
|
|
|
|
LABEL_ONE_OFF: 'False',
|
|
|
|
LABEL_PROJECT: 'composetest',
|
|
|
|
LABEL_SERVICE: 'web',
|
|
|
|
LABEL_VERSION: __version__,
|
|
|
|
}
|
|
|
|
expected = dict(labels_dict, **compose_labels)
|
2015-03-20 00:10:27 +01:00
|
|
|
|
2015-04-26 23:09:20 +02:00
|
|
|
service = self.create_service('web', labels=labels_dict)
|
2015-05-12 12:11:36 +02:00
|
|
|
labels = create_and_start_container(service).labels.items()
|
|
|
|
for pair in expected.items():
|
|
|
|
self.assertIn(pair, labels)
|
|
|
|
|
|
|
|
service.kill()
|
|
|
|
service.remove_stopped()
|
|
|
|
|
|
|
|
labels_list = ["%s=%s" % pair for pair in labels_dict.items()]
|
|
|
|
|
|
|
|
service = self.create_service('web', labels=labels_list)
|
|
|
|
labels = create_and_start_container(service).labels.items()
|
|
|
|
for pair in expected.items():
|
|
|
|
self.assertIn(pair, labels)
|
2015-03-20 00:10:27 +01:00
|
|
|
|
|
|
|
def test_empty_labels(self):
|
|
|
|
labels_list = ['foo', 'bar']
|
|
|
|
|
|
|
|
service = self.create_service('web', labels=labels_list)
|
|
|
|
labels = create_and_start_container(service).labels.items()
|
|
|
|
for name in labels_list:
|
|
|
|
self.assertIn((name, ''), labels)
|
2015-05-06 13:18:58 +02:00
|
|
|
|
2015-07-16 13:51:15 +02:00
|
|
|
def test_custom_container_name(self):
|
|
|
|
service = self.create_service('web', container_name='my-web-container')
|
|
|
|
self.assertEqual(service.custom_container_name(), 'my-web-container')
|
|
|
|
|
|
|
|
container = create_and_start_container(service)
|
|
|
|
self.assertEqual(container.name, 'my-web-container')
|
|
|
|
|
2015-07-22 16:39:56 +02:00
|
|
|
one_off_container = service.create_container(one_off=True)
|
|
|
|
self.assertNotEqual(one_off_container.name, 'my-web-container')
|
|
|
|
|
2015-05-06 13:18:58 +02:00
|
|
|
def test_log_drive_invalid(self):
|
|
|
|
service = self.create_service('web', log_driver='xxx')
|
2015-08-17 14:15:52 +02:00
|
|
|
expected_error_msg = "logger: no log driver named 'xxx' is registered"
|
|
|
|
|
|
|
|
with self.assertRaisesRegexp(APIError, expected_error_msg):
|
|
|
|
create_and_start_container(service)
|
2015-05-06 13:18:58 +02:00
|
|
|
|
|
|
|
def test_log_drive_empty_default_jsonfile(self):
|
|
|
|
service = self.create_service('web')
|
|
|
|
log_config = create_and_start_container(service).log_config
|
|
|
|
|
|
|
|
self.assertEqual('json-file', log_config['Type'])
|
|
|
|
self.assertFalse(log_config['Config'])
|
|
|
|
|
|
|
|
def test_log_drive_none(self):
|
|
|
|
service = self.create_service('web', log_driver='none')
|
|
|
|
log_config = create_and_start_container(service).log_config
|
|
|
|
|
|
|
|
self.assertEqual('none', log_config['Type'])
|
|
|
|
self.assertFalse(log_config['Config'])
|
2015-05-09 01:14:32 +02:00
|
|
|
|
|
|
|
def test_devices(self):
|
|
|
|
service = self.create_service('web', devices=["/dev/random:/dev/mapped-random"])
|
|
|
|
device_config = create_and_start_container(service).get('HostConfig.Devices')
|
|
|
|
|
|
|
|
device_dict = {
|
|
|
|
'PathOnHost': '/dev/random',
|
|
|
|
'CgroupPermissions': 'rwm',
|
|
|
|
'PathInContainer': '/dev/mapped-random'
|
|
|
|
}
|
|
|
|
|
|
|
|
self.assertEqual(1, len(device_config))
|
|
|
|
self.assertDictEqual(device_dict, device_config[0])
|
2015-07-03 11:28:39 +02:00
|
|
|
|
|
|
|
def test_duplicate_containers(self):
|
|
|
|
service = self.create_service('web')
|
|
|
|
|
|
|
|
options = service._get_container_create_options({}, 1)
|
|
|
|
original = Container.create(service.client, **options)
|
|
|
|
|
|
|
|
self.assertEqual(set(service.containers(stopped=True)), set([original]))
|
|
|
|
self.assertEqual(set(service.duplicate_containers()), set())
|
|
|
|
|
|
|
|
options['name'] = 'temporary_container_name'
|
|
|
|
duplicate = Container.create(service.client, **options)
|
|
|
|
|
|
|
|
self.assertEqual(set(service.containers(stopped=True)), set([original, duplicate]))
|
|
|
|
self.assertEqual(set(service.duplicate_containers()), set([duplicate]))
|