2014-01-09 16:30:36 +01:00
|
|
|
from __future__ import absolute_import
|
2015-10-30 21:22:51 +01:00
|
|
|
from __future__ import unicode_literals
|
2015-08-24 21:25:25 +02:00
|
|
|
|
2016-01-08 22:54:35 +01:00
|
|
|
import datetime
|
2015-08-31 20:31:20 +02:00
|
|
|
import json
|
2015-03-12 14:59:23 +01:00
|
|
|
import os
|
2015-06-02 15:28:21 +02:00
|
|
|
import shlex
|
2015-11-11 00:52:21 +01:00
|
|
|
import signal
|
2015-11-02 22:46:49 +01:00
|
|
|
import subprocess
|
2015-11-11 00:52:21 +01:00
|
|
|
import time
|
2015-11-02 22:46:49 +01:00
|
|
|
from collections import namedtuple
|
2015-08-24 21:25:25 +02:00
|
|
|
from operator import attrgetter
|
2015-10-10 20:41:17 +02:00
|
|
|
from textwrap import dedent
|
2014-08-08 18:41:52 +02:00
|
|
|
|
2015-11-11 01:43:05 +01:00
|
|
|
from docker import errors
|
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
from .. import mock
|
2015-09-12 02:50:31 +02:00
|
|
|
from compose.cli.command import get_project
|
2015-10-14 18:09:50 +02:00
|
|
|
from compose.cli.docker_client import docker_client
|
2015-10-27 10:00:51 +01:00
|
|
|
from compose.container import Container
|
2015-11-02 23:52:37 +01:00
|
|
|
from tests.integration.testcases import DockerClientTestCase
|
2015-10-20 18:49:45 +02:00
|
|
|
from tests.integration.testcases import get_links
|
2015-11-20 23:51:36 +01:00
|
|
|
from tests.integration.testcases import pull_busybox
|
2015-11-02 22:46:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
ProcessResult = namedtuple('ProcessResult', 'stdout stderr')
|
|
|
|
|
|
|
|
|
|
|
|
BUILD_CACHE_TEXT = 'Using cache'
|
|
|
|
BUILD_PULL_TEXT = 'Status: Image is up to date for busybox:latest'
|
2014-01-09 16:30:36 +01:00
|
|
|
|
2014-07-30 07:42:58 +02:00
|
|
|
|
2015-11-11 00:52:21 +01:00
|
|
|
def start_process(base_dir, options):
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
['docker-compose'] + options,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
cwd=base_dir)
|
|
|
|
print("Running process: %s" % proc.pid)
|
|
|
|
return proc
|
|
|
|
|
|
|
|
|
|
|
|
def wait_on_process(proc, returncode=0):
|
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
if proc.returncode != returncode:
|
2015-11-14 01:40:10 +01:00
|
|
|
print(stderr.decode('utf-8'))
|
2015-11-11 00:52:21 +01:00
|
|
|
assert proc.returncode == returncode
|
|
|
|
return ProcessResult(stdout.decode('utf-8'), stderr.decode('utf-8'))
|
|
|
|
|
|
|
|
|
2015-11-27 19:54:00 +01:00
|
|
|
def wait_on_condition(condition, delay=0.1, timeout=20):
|
2015-11-11 00:52:21 +01:00
|
|
|
start_time = time.time()
|
|
|
|
while not condition():
|
|
|
|
if time.time() - start_time > timeout:
|
|
|
|
raise AssertionError("Timeout: %s" % condition)
|
|
|
|
time.sleep(delay)
|
|
|
|
|
|
|
|
|
2015-12-08 21:11:05 +01:00
|
|
|
def kill_service(service):
|
|
|
|
for container in service.containers():
|
|
|
|
container.kill()
|
|
|
|
|
|
|
|
|
2015-11-11 00:52:21 +01:00
|
|
|
class ContainerCountCondition(object):
|
|
|
|
|
|
|
|
def __init__(self, project, expected):
|
|
|
|
self.project = project
|
|
|
|
self.expected = expected
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
return len(self.project.containers()) == self.expected
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "waiting for counter count == %s" % self.expected
|
|
|
|
|
|
|
|
|
2015-11-11 01:43:05 +01:00
|
|
|
class ContainerStateCondition(object):
|
|
|
|
|
|
|
|
def __init__(self, client, name, running):
|
|
|
|
self.client = client
|
|
|
|
self.name = name
|
|
|
|
self.running = running
|
|
|
|
|
|
|
|
# State.Running == true
|
|
|
|
def __call__(self):
|
|
|
|
try:
|
|
|
|
container = self.client.inspect_container(self.name)
|
|
|
|
return container['State']['Running'] == self.running
|
|
|
|
except errors.APIError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __str__(self):
|
2015-12-28 22:57:55 +01:00
|
|
|
state = 'running' if self.running else 'stopped'
|
|
|
|
return "waiting for container to be %s" % state
|
2015-11-11 01:43:05 +01:00
|
|
|
|
|
|
|
|
2014-01-26 21:28:37 +01:00
|
|
|
class CLITestCase(DockerClientTestCase):
|
2015-11-02 22:46:49 +01:00
|
|
|
|
2014-01-09 16:30:36 +01:00
|
|
|
def setUp(self):
|
2014-01-26 21:28:37 +01:00
|
|
|
super(CLITestCase, self).setUp()
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/simple-composefile'
|
2014-01-09 16:30:36 +01:00
|
|
|
|
2014-01-20 17:47:58 +01:00
|
|
|
def tearDown(self):
|
2015-10-10 20:41:17 +02:00
|
|
|
if self.base_dir:
|
|
|
|
self.project.kill()
|
|
|
|
self.project.remove_stopped()
|
|
|
|
for container in self.project.containers(stopped=True, one_off=True):
|
|
|
|
container.remove(force=True)
|
2015-05-30 22:17:21 +02:00
|
|
|
super(CLITestCase, self).tearDown()
|
2014-07-30 07:42:58 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def project(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
# Hack: allow project to be overridden
|
|
|
|
if not hasattr(self, '_project'):
|
|
|
|
self._project = get_project(self.base_dir)
|
|
|
|
return self._project
|
|
|
|
|
|
|
|
def dispatch(self, options, project_options=None, returncode=0):
|
|
|
|
project_options = project_options or []
|
2015-11-11 00:52:21 +01:00
|
|
|
proc = start_process(self.base_dir, project_options + options)
|
|
|
|
return wait_on_process(proc, returncode=returncode)
|
2014-01-20 17:47:58 +01:00
|
|
|
|
2014-11-02 00:22:22 +01:00
|
|
|
def test_help(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/no-composefile'
|
|
|
|
result = self.dispatch(['help', 'up'], returncode=1)
|
|
|
|
assert 'Usage: up [options] [SERVICE...]' in result.stderr
|
2015-10-10 20:41:17 +02:00
|
|
|
# Prevent tearDown from trying to create a project
|
|
|
|
self.base_dir = None
|
|
|
|
|
|
|
|
def test_config_list_services(self):
|
|
|
|
result = self.dispatch(['config', '--services'])
|
|
|
|
assert set(result.stdout.rstrip().split('\n')) == {'simple', 'another'}
|
|
|
|
|
|
|
|
def test_config_quiet_with_error(self):
|
|
|
|
self.base_dir = None
|
|
|
|
result = self.dispatch([
|
|
|
|
'-f', 'tests/fixtures/invalid-composefile/invalid.yml',
|
|
|
|
'config', '-q'
|
|
|
|
], returncode=1)
|
|
|
|
assert "'notaservice' doesn't have any configuration" in result.stderr
|
|
|
|
|
|
|
|
def test_config_quiet(self):
|
|
|
|
assert self.dispatch(['config', '-q']).stdout == ''
|
|
|
|
|
|
|
|
def test_config_default(self):
|
|
|
|
result = self.dispatch(['config'])
|
|
|
|
assert dedent("""
|
|
|
|
simple:
|
|
|
|
command: top
|
|
|
|
image: busybox:latest
|
|
|
|
""").lstrip() in result.stdout
|
|
|
|
assert dedent("""
|
|
|
|
another:
|
|
|
|
command: top
|
|
|
|
image: busybox:latest
|
|
|
|
""").lstrip() in result.stdout
|
2014-11-02 00:22:22 +01:00
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_ps(self):
|
2014-07-30 07:42:58 +02:00
|
|
|
self.project.get_service('simple').create_container()
|
2015-11-02 22:46:49 +01:00
|
|
|
result = self.dispatch(['ps'])
|
|
|
|
assert 'simplecomposefile_simple_1' in result.stdout
|
2014-01-16 18:58:53 +01:00
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_ps_default_composefile(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/multiple-composefiles'
|
|
|
|
self.dispatch(['up', '-d'])
|
|
|
|
result = self.dispatch(['ps'])
|
2014-02-26 16:45:14 +01:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.assertIn('multiplecomposefiles_simple_1', result.stdout)
|
|
|
|
self.assertIn('multiplecomposefiles_another_1', result.stdout)
|
|
|
|
self.assertNotIn('multiplecomposefiles_yetanother_1', result.stdout)
|
2014-02-26 16:45:14 +01:00
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_ps_alternate_composefile(self):
|
2015-04-27 03:21:55 +02:00
|
|
|
config_path = os.path.abspath(
|
|
|
|
'tests/fixtures/multiple-composefiles/compose2.yml')
|
2015-11-02 22:46:49 +01:00
|
|
|
self._project = get_project(self.base_dir, [config_path])
|
2015-09-21 11:59:23 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/multiple-composefiles'
|
|
|
|
self.dispatch(['-f', 'compose2.yml', 'up', '-d'])
|
|
|
|
result = self.dispatch(['-f', 'compose2.yml', 'ps'])
|
|
|
|
|
|
|
|
self.assertNotIn('multiplecomposefiles_simple_1', result.stdout)
|
|
|
|
self.assertNotIn('multiplecomposefiles_another_1', result.stdout)
|
|
|
|
self.assertIn('multiplecomposefiles_yetanother_1', result.stdout)
|
|
|
|
|
|
|
|
def test_pull(self):
|
|
|
|
result = self.dispatch(['pull'])
|
|
|
|
assert sorted(result.stderr.split('\n'))[1:] == [
|
|
|
|
'Pulling another (busybox:latest)...',
|
|
|
|
'Pulling simple (busybox:latest)...',
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_pull_with_digest(self):
|
|
|
|
result = self.dispatch(['-f', 'digest.yml', 'pull'])
|
2014-07-24 20:47:39 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
assert 'Pulling simple (busybox:latest)...' in result.stderr
|
|
|
|
assert ('Pulling digest (busybox@'
|
|
|
|
'sha256:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b520'
|
|
|
|
'04ee8502d)...') in result.stderr
|
2015-10-03 07:24:28 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_pull_with_ignore_pull_failures(self):
|
|
|
|
result = self.dispatch([
|
|
|
|
'-f', 'ignore-pull-failures.yml',
|
|
|
|
'pull', '--ignore-pull-failures'])
|
|
|
|
|
|
|
|
assert 'Pulling simple (busybox:latest)...' in result.stderr
|
|
|
|
assert 'Pulling another (nonexisting-image:latest)...' in result.stderr
|
|
|
|
assert 'Error: image library/nonexisting-image:latest not found' in result.stderr
|
|
|
|
|
|
|
|
def test_build_plain(self):
|
|
|
|
self.base_dir = 'tests/fixtures/simple-dockerfile'
|
|
|
|
self.dispatch(['build', 'simple'])
|
|
|
|
|
|
|
|
result = self.dispatch(['build', 'simple'])
|
|
|
|
assert BUILD_CACHE_TEXT in result.stdout
|
|
|
|
assert BUILD_PULL_TEXT not in result.stdout
|
2015-09-14 15:02:15 +02:00
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_build_no_cache(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/simple-dockerfile'
|
|
|
|
self.dispatch(['build', 'simple'])
|
2014-07-24 20:47:39 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
result = self.dispatch(['build', '--no-cache', 'simple'])
|
|
|
|
assert BUILD_CACHE_TEXT not in result.stdout
|
|
|
|
assert BUILD_PULL_TEXT not in result.stdout
|
2015-09-14 15:02:15 +02:00
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_build_pull(self):
|
2015-11-20 23:51:36 +01:00
|
|
|
# Make sure we have the latest busybox already
|
|
|
|
pull_busybox(self.client)
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/simple-dockerfile'
|
|
|
|
self.dispatch(['build', 'simple'], None)
|
2015-09-14 15:02:15 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
result = self.dispatch(['build', '--pull', 'simple'])
|
|
|
|
assert BUILD_CACHE_TEXT in result.stdout
|
|
|
|
assert BUILD_PULL_TEXT in result.stdout
|
2015-09-14 15:02:15 +02:00
|
|
|
|
2015-10-03 07:24:28 +02:00
|
|
|
def test_build_no_cache_pull(self):
|
2015-11-20 23:51:36 +01:00
|
|
|
# Make sure we have the latest busybox already
|
|
|
|
pull_busybox(self.client)
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/simple-dockerfile'
|
|
|
|
self.dispatch(['build', 'simple'])
|
2015-09-14 15:02:15 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
result = self.dispatch(['build', '--no-cache', '--pull', 'simple'])
|
|
|
|
assert BUILD_CACHE_TEXT not in result.stdout
|
|
|
|
assert BUILD_PULL_TEXT in result.stdout
|
2014-11-03 23:46:01 +01:00
|
|
|
|
2015-10-27 10:00:51 +01:00
|
|
|
def test_build_failed(self):
|
|
|
|
self.base_dir = 'tests/fixtures/simple-failing-dockerfile'
|
|
|
|
self.dispatch(['build', 'simple'], returncode=1)
|
|
|
|
|
|
|
|
labels = ["com.docker.compose.test_failing_image=true"]
|
|
|
|
containers = [
|
|
|
|
Container.from_ps(self.project.client, c)
|
|
|
|
for c in self.project.client.containers(
|
|
|
|
all=True,
|
|
|
|
filters={"label": labels})
|
|
|
|
]
|
|
|
|
assert len(containers) == 1
|
|
|
|
|
|
|
|
def test_build_failed_forcerm(self):
|
|
|
|
self.base_dir = 'tests/fixtures/simple-failing-dockerfile'
|
|
|
|
self.dispatch(['build', '--force-rm', 'simple'], returncode=1)
|
|
|
|
|
|
|
|
labels = ["com.docker.compose.test_failing_image=true"]
|
|
|
|
|
|
|
|
containers = [
|
|
|
|
Container.from_ps(self.project.client, c)
|
|
|
|
for c in self.project.client.containers(
|
|
|
|
all=True,
|
|
|
|
filters={"label": labels})
|
|
|
|
]
|
|
|
|
assert not containers
|
|
|
|
|
2015-12-14 22:46:13 +01:00
|
|
|
def test_create(self):
|
|
|
|
self.dispatch(['create'])
|
|
|
|
service = self.project.get_service('simple')
|
|
|
|
another = self.project.get_service('another')
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(another.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
self.assertEqual(len(another.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
def test_create_with_force_recreate(self):
|
|
|
|
self.dispatch(['create'], None)
|
|
|
|
service = self.project.get_service('simple')
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
old_ids = [c.id for c in service.containers(stopped=True)]
|
|
|
|
|
|
|
|
self.dispatch(['create', '--force-recreate'], None)
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
new_ids = [c.id for c in service.containers(stopped=True)]
|
|
|
|
|
|
|
|
self.assertNotEqual(old_ids, new_ids)
|
|
|
|
|
|
|
|
def test_create_with_no_recreate(self):
|
|
|
|
self.dispatch(['create'], None)
|
|
|
|
service = self.project.get_service('simple')
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
old_ids = [c.id for c in service.containers(stopped=True)]
|
|
|
|
|
|
|
|
self.dispatch(['create', '--no-recreate'], None)
|
|
|
|
self.assertEqual(len(service.containers()), 0)
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
|
|
|
|
new_ids = [c.id for c in service.containers(stopped=True)]
|
|
|
|
|
|
|
|
self.assertEqual(old_ids, new_ids)
|
|
|
|
|
|
|
|
def test_create_with_force_recreate_and_no_recreate(self):
|
|
|
|
self.dispatch(
|
|
|
|
['create', '--force-recreate', '--no-recreate'],
|
|
|
|
returncode=1)
|
|
|
|
|
2015-09-02 22:08:18 +02:00
|
|
|
def test_up_detached(self):
|
2015-11-02 23:52:37 +01:00
|
|
|
self.dispatch(['up', '-d'])
|
2014-07-30 07:42:58 +02:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
another = self.project.get_service('another')
|
2014-06-07 15:44:07 +02:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
2014-06-21 12:30:36 +02:00
|
|
|
self.assertEqual(len(another.containers()), 1)
|
2014-06-07 15:44:07 +02:00
|
|
|
|
2014-12-18 03:31:22 +01:00
|
|
|
# Ensure containers don't have stdin and stdout connected in -d mode
|
2015-09-02 22:08:18 +02:00
|
|
|
container, = service.containers()
|
|
|
|
self.assertFalse(container.get('Config.AttachStderr'))
|
|
|
|
self.assertFalse(container.get('Config.AttachStdout'))
|
|
|
|
self.assertFalse(container.get('Config.AttachStdin'))
|
|
|
|
|
|
|
|
def test_up_attached(self):
|
2015-11-02 23:52:37 +01:00
|
|
|
self.base_dir = 'tests/fixtures/echo-services'
|
|
|
|
result = self.dispatch(['up', '--no-color'])
|
2015-09-02 22:08:18 +02:00
|
|
|
|
2015-11-02 23:52:37 +01:00
|
|
|
assert 'simple_1 | simple' in result.stdout
|
|
|
|
assert 'another_1 | another' in result.stdout
|
2014-12-18 03:31:22 +01:00
|
|
|
|
2015-10-07 17:10:08 +02:00
|
|
|
def test_up_without_networking(self):
|
2015-10-21 21:40:50 +02:00
|
|
|
self.require_api_version('1.21')
|
2015-10-07 17:10:08 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['up', '-d'], None)
|
2015-10-14 18:09:50 +02:00
|
|
|
client = docker_client(version='1.21')
|
2015-10-07 17:10:08 +02:00
|
|
|
|
2015-10-14 18:09:50 +02:00
|
|
|
networks = client.networks(names=[self.project.name])
|
2015-10-07 17:10:08 +02:00
|
|
|
self.assertEqual(len(networks), 0)
|
|
|
|
|
|
|
|
for service in self.project.get_services():
|
|
|
|
containers = service.containers()
|
|
|
|
self.assertEqual(len(containers), 1)
|
|
|
|
self.assertNotEqual(containers[0].get('Config.Hostname'), service.name)
|
|
|
|
|
|
|
|
web_container = self.project.get_service('web').containers()[0]
|
|
|
|
self.assertTrue(web_container.get('HostConfig.Links'))
|
|
|
|
|
|
|
|
def test_up_with_networking(self):
|
2015-10-21 21:40:50 +02:00
|
|
|
self.require_api_version('1.21')
|
2015-10-07 17:10:08 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['--x-networking', 'up', '-d'], None)
|
2015-10-14 18:09:50 +02:00
|
|
|
client = docker_client(version='1.21')
|
2015-10-07 17:10:08 +02:00
|
|
|
|
|
|
|
services = self.project.get_services()
|
|
|
|
|
2015-10-14 18:09:50 +02:00
|
|
|
networks = client.networks(names=[self.project.name])
|
2015-10-07 17:10:08 +02:00
|
|
|
for n in networks:
|
2015-10-30 20:26:56 +01:00
|
|
|
self.addCleanup(client.remove_network, n['Id'])
|
2015-10-07 17:10:08 +02:00
|
|
|
self.assertEqual(len(networks), 1)
|
2015-10-30 20:26:56 +01:00
|
|
|
self.assertEqual(networks[0]['Driver'], 'bridge')
|
2015-10-07 17:10:08 +02:00
|
|
|
|
2015-10-30 20:26:56 +01:00
|
|
|
network = client.inspect_network(networks[0]['Id'])
|
|
|
|
self.assertEqual(len(network['Containers']), len(services))
|
2015-10-07 17:10:08 +02:00
|
|
|
|
|
|
|
for service in services:
|
|
|
|
containers = service.containers()
|
|
|
|
self.assertEqual(len(containers), 1)
|
2015-10-30 20:26:56 +01:00
|
|
|
self.assertIn(containers[0].id, network['Containers'])
|
2015-10-07 17:10:08 +02:00
|
|
|
|
|
|
|
web_container = self.project.get_service('web').containers()[0]
|
|
|
|
self.assertFalse(web_container.get('HostConfig.Links'))
|
|
|
|
|
2014-06-08 14:33:08 +02:00
|
|
|
def test_up_with_links(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['up', '-d', 'web'], None)
|
2014-07-30 07:42:58 +02:00
|
|
|
web = self.project.get_service('web')
|
|
|
|
db = self.project.get_service('db')
|
|
|
|
console = self.project.get_service('console')
|
2014-06-08 14:33:08 +02:00
|
|
|
self.assertEqual(len(web.containers()), 1)
|
|
|
|
self.assertEqual(len(db.containers()), 1)
|
|
|
|
self.assertEqual(len(console.containers()), 0)
|
|
|
|
|
2014-06-21 12:30:36 +02:00
|
|
|
def test_up_with_no_deps(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['up', '-d', '--no-deps', 'web'], None)
|
2014-07-30 07:42:58 +02:00
|
|
|
web = self.project.get_service('web')
|
|
|
|
db = self.project.get_service('db')
|
|
|
|
console = self.project.get_service('console')
|
2014-06-08 14:33:08 +02:00
|
|
|
self.assertEqual(len(web.containers()), 1)
|
|
|
|
self.assertEqual(len(db.containers()), 0)
|
|
|
|
self.assertEqual(len(console.containers()), 0)
|
|
|
|
|
2015-07-15 17:19:39 +02:00
|
|
|
def test_up_with_force_recreate(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2014-07-30 07:42:58 +02:00
|
|
|
service = self.project.get_service('simple')
|
2014-06-08 15:16:14 +02:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
old_ids = [c.id for c in service.containers()]
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d', '--force-recreate'], None)
|
2014-06-08 15:16:14 +02:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
new_ids = [c.id for c in service.containers()]
|
|
|
|
|
|
|
|
self.assertNotEqual(old_ids, new_ids)
|
|
|
|
|
2015-07-15 17:19:39 +02:00
|
|
|
def test_up_with_no_recreate(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2014-07-30 07:42:58 +02:00
|
|
|
service = self.project.get_service('simple')
|
2014-06-08 15:16:14 +02:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
old_ids = [c.id for c in service.containers()]
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d', '--no-recreate'], None)
|
2014-06-08 15:16:14 +02:00
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
|
|
|
|
new_ids = [c.id for c in service.containers()]
|
|
|
|
|
|
|
|
self.assertEqual(old_ids, new_ids)
|
|
|
|
|
2015-07-15 17:19:39 +02:00
|
|
|
def test_up_with_force_recreate_and_no_recreate(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(
|
|
|
|
['up', '-d', '--force-recreate', '--no-recreate'],
|
|
|
|
returncode=1)
|
2015-07-15 17:19:39 +02:00
|
|
|
|
2015-05-28 15:28:02 +02:00
|
|
|
def test_up_with_timeout(self):
|
2015-11-11 00:52:21 +01:00
|
|
|
self.dispatch(['up', '-d', '-t', '1'])
|
2015-05-28 15:28:02 +02:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
another = self.project.get_service('another')
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertEqual(len(another.containers()), 1)
|
|
|
|
|
|
|
|
# Ensure containers don't have stdin and stdout connected in -d mode
|
|
|
|
config = service.containers()[0].inspect()['Config']
|
|
|
|
self.assertFalse(config['AttachStderr'])
|
|
|
|
self.assertFalse(config['AttachStdout'])
|
|
|
|
self.assertFalse(config['AttachStdin'])
|
|
|
|
|
2015-11-11 00:52:21 +01:00
|
|
|
def test_up_handles_sigint(self):
|
|
|
|
proc = start_process(self.base_dir, ['up', '-t', '2'])
|
|
|
|
wait_on_condition(ContainerCountCondition(self.project, 2))
|
|
|
|
|
|
|
|
os.kill(proc.pid, signal.SIGINT)
|
|
|
|
wait_on_condition(ContainerCountCondition(self.project, 0))
|
|
|
|
|
|
|
|
def test_up_handles_sigterm(self):
|
|
|
|
proc = start_process(self.base_dir, ['up', '-t', '2'])
|
|
|
|
wait_on_condition(ContainerCountCondition(self.project, 2))
|
|
|
|
|
|
|
|
os.kill(proc.pid, signal.SIGTERM)
|
|
|
|
wait_on_condition(ContainerCountCondition(self.project, 0))
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_without_links(self):
|
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['run', 'console', '/bin/true'])
|
2014-07-30 07:42:58 +02:00
|
|
|
self.assertEqual(len(self.project.containers()), 0)
|
2014-06-08 15:03:58 +02:00
|
|
|
|
2014-12-18 03:31:22 +01:00
|
|
|
# Ensure stdin/out was open
|
|
|
|
container = self.project.containers(stopped=True, one_off=True)[0]
|
|
|
|
config = container.inspect()['Config']
|
|
|
|
self.assertTrue(config['AttachStderr'])
|
|
|
|
self.assertTrue(config['AttachStdout'])
|
|
|
|
self.assertTrue(config['AttachStdin'])
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_links(self):
|
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['run', 'web', '/bin/true'], None)
|
2014-07-30 07:42:58 +02:00
|
|
|
db = self.project.get_service('db')
|
|
|
|
console = self.project.get_service('console')
|
2014-06-08 15:03:58 +02:00
|
|
|
self.assertEqual(len(db.containers()), 1)
|
|
|
|
self.assertEqual(len(console.containers()), 0)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_with_no_deps(self):
|
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['run', '--no-deps', 'web', '/bin/true'])
|
2014-07-30 07:42:58 +02:00
|
|
|
db = self.project.get_service('db')
|
2014-06-08 15:03:58 +02:00
|
|
|
self.assertEqual(len(db.containers()), 0)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_does_not_recreate_linked_containers(self):
|
|
|
|
self.base_dir = 'tests/fixtures/links-composefile'
|
|
|
|
self.dispatch(['up', '-d', 'db'])
|
2014-07-30 07:42:58 +02:00
|
|
|
db = self.project.get_service('db')
|
2014-06-08 15:16:14 +02:00
|
|
|
self.assertEqual(len(db.containers()), 1)
|
|
|
|
|
|
|
|
old_ids = [c.id for c in db.containers()]
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', 'web', '/bin/true'], None)
|
2014-06-08 15:16:14 +02:00
|
|
|
self.assertEqual(len(db.containers()), 1)
|
|
|
|
|
|
|
|
new_ids = [c.id for c in db.containers()]
|
|
|
|
|
|
|
|
self.assertEqual(old_ids, new_ids)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_without_command(self):
|
|
|
|
self.base_dir = 'tests/fixtures/commands-composefile'
|
2015-01-12 15:59:05 +01:00
|
|
|
self.check_build('tests/fixtures/simple-dockerfile', tag='composetest_test')
|
2014-07-05 10:19:54 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', 'implicit'])
|
2014-07-30 07:42:58 +02:00
|
|
|
service = self.project.get_service('implicit')
|
2014-07-05 10:19:54 +02:00
|
|
|
containers = service.containers(stopped=True, one_off=True)
|
|
|
|
self.assertEqual(
|
|
|
|
[c.human_readable_command for c in containers],
|
|
|
|
[u'/bin/sh -c echo "success"'],
|
|
|
|
)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', 'explicit'])
|
2014-07-30 07:42:58 +02:00
|
|
|
service = self.project.get_service('explicit')
|
2014-07-05 10:19:54 +02:00
|
|
|
containers = service.containers(stopped=True, one_off=True)
|
|
|
|
self.assertEqual(
|
|
|
|
[c.human_readable_command for c in containers],
|
|
|
|
[u'/bin/true'],
|
|
|
|
)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_entrypoint_overridden(self):
|
|
|
|
self.base_dir = 'tests/fixtures/dockerfile_with_entrypoint'
|
2014-08-17 14:13:28 +02:00
|
|
|
name = 'service'
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', '--entrypoint', '/bin/echo', name, 'helloworld'])
|
2014-08-17 14:13:28 +02:00
|
|
|
service = self.project.get_service(name)
|
|
|
|
container = service.containers(stopped=True, one_off=True)[0]
|
|
|
|
self.assertEqual(
|
2015-06-02 15:28:21 +02:00
|
|
|
shlex.split(container.human_readable_command),
|
|
|
|
[u'/bin/echo', u'helloworld'],
|
2014-08-17 14:13:28 +02:00
|
|
|
)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_user_overridden(self):
|
|
|
|
self.base_dir = 'tests/fixtures/user-composefile'
|
2015-02-15 03:08:47 +01:00
|
|
|
name = 'service'
|
|
|
|
user = 'sshd'
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', '--user={user}'.format(user=user), name], returncode=1)
|
2015-02-15 03:08:47 +01:00
|
|
|
service = self.project.get_service(name)
|
|
|
|
container = service.containers(stopped=True, one_off=True)[0]
|
|
|
|
self.assertEqual(user, container.get('Config.User'))
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_user_overridden_short_form(self):
|
|
|
|
self.base_dir = 'tests/fixtures/user-composefile'
|
2015-02-15 03:08:47 +01:00
|
|
|
name = 'service'
|
|
|
|
user = 'sshd'
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', '-u', user, name], returncode=1)
|
2015-02-15 03:08:47 +01:00
|
|
|
service = self.project.get_service(name)
|
|
|
|
container = service.containers(stopped=True, one_off=True)[0]
|
|
|
|
self.assertEqual(user, container.get('Config.User'))
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_environement_overridden(self):
|
2014-10-07 17:03:40 +02:00
|
|
|
name = 'service'
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/environment-composefile'
|
|
|
|
self.dispatch([
|
|
|
|
'run', '-e', 'foo=notbar',
|
|
|
|
'-e', 'allo=moto=bobo',
|
|
|
|
'-e', 'alpha=beta',
|
|
|
|
name,
|
|
|
|
'/bin/true',
|
|
|
|
])
|
2014-10-07 17:03:40 +02:00
|
|
|
service = self.project.get_service(name)
|
|
|
|
container = service.containers(stopped=True, one_off=True)[0]
|
|
|
|
# env overriden
|
|
|
|
self.assertEqual('notbar', container.environment['foo'])
|
|
|
|
# keep environement from yaml
|
|
|
|
self.assertEqual('world', container.environment['hello'])
|
|
|
|
# added option from command line
|
|
|
|
self.assertEqual('beta', container.environment['alpha'])
|
|
|
|
# make sure a value with a = don't crash out
|
|
|
|
self.assertEqual('moto=bobo', container.environment['allo'])
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_without_map_ports(self):
|
2014-09-16 00:18:03 +02:00
|
|
|
# create one off container
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/ports-composefile'
|
|
|
|
self.dispatch(['run', '-d', 'simple'])
|
2014-09-16 00:18:03 +02:00
|
|
|
container = self.project.get_service('simple').containers(one_off=True)[0]
|
|
|
|
|
|
|
|
# get port information
|
|
|
|
port_random = container.get_local_port(3000)
|
|
|
|
port_assigned = container.get_local_port(3001)
|
|
|
|
|
|
|
|
# close all one off containers we just created
|
|
|
|
container.stop()
|
|
|
|
|
|
|
|
# check the ports
|
|
|
|
self.assertEqual(port_random, None)
|
|
|
|
self.assertEqual(port_assigned, None)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_map_ports(self):
|
2014-09-16 00:18:03 +02:00
|
|
|
# create one off container
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/ports-composefile'
|
|
|
|
self.dispatch(['run', '-d', '--service-ports', 'simple'])
|
2014-09-16 00:18:03 +02:00
|
|
|
container = self.project.get_service('simple').containers(one_off=True)[0]
|
|
|
|
|
|
|
|
# get port information
|
|
|
|
port_random = container.get_local_port(3000)
|
|
|
|
port_assigned = container.get_local_port(3001)
|
2015-07-06 18:15:50 +02:00
|
|
|
port_range = container.get_local_port(3002), container.get_local_port(3003)
|
2014-09-16 00:18:03 +02:00
|
|
|
|
|
|
|
# close all one off containers we just created
|
|
|
|
container.stop()
|
|
|
|
|
|
|
|
# check the ports
|
|
|
|
self.assertNotEqual(port_random, None)
|
|
|
|
self.assertIn("0.0.0.0", port_random)
|
2015-03-17 00:21:29 +01:00
|
|
|
self.assertEqual(port_assigned, "0.0.0.0:49152")
|
2015-07-06 18:15:50 +02:00
|
|
|
self.assertEqual(port_range[0], "0.0.0.0:49153")
|
|
|
|
self.assertEqual(port_range[1], "0.0.0.0:49154")
|
2014-09-16 00:18:03 +02:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_explicitly_maped_ports(self):
|
2015-08-07 14:42:37 +02:00
|
|
|
# create one off container
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/ports-composefile'
|
|
|
|
self.dispatch(['run', '-d', '-p', '30000:3000', '--publish', '30001:3001', 'simple'])
|
2015-08-07 14:42:37 +02:00
|
|
|
container = self.project.get_service('simple').containers(one_off=True)[0]
|
|
|
|
|
|
|
|
# get port information
|
|
|
|
port_short = container.get_local_port(3000)
|
|
|
|
port_full = container.get_local_port(3001)
|
|
|
|
|
|
|
|
# close all one off containers we just created
|
|
|
|
container.stop()
|
|
|
|
|
|
|
|
# check the ports
|
|
|
|
self.assertEqual(port_short, "0.0.0.0:30000")
|
|
|
|
self.assertEqual(port_full, "0.0.0.0:30001")
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_service_with_explicitly_maped_ip_ports(self):
|
2015-08-07 14:42:37 +02:00
|
|
|
# create one off container
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/ports-composefile'
|
|
|
|
self.dispatch(['run', '-d', '-p', '127.0.0.1:30000:3000', '--publish', '127.0.0.1:30001:3001', 'simple'], None)
|
2015-08-07 14:42:37 +02:00
|
|
|
container = self.project.get_service('simple').containers(one_off=True)[0]
|
|
|
|
|
|
|
|
# get port information
|
|
|
|
port_short = container.get_local_port(3000)
|
|
|
|
port_full = container.get_local_port(3001)
|
|
|
|
|
|
|
|
# close all one off containers we just created
|
|
|
|
container.stop()
|
|
|
|
|
|
|
|
# check the ports
|
|
|
|
self.assertEqual(port_short, "127.0.0.1:30000")
|
|
|
|
self.assertEqual(port_full, "127.0.0.1:30001")
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_with_custom_name(self):
|
|
|
|
self.base_dir = 'tests/fixtures/environment-composefile'
|
2015-08-26 19:33:03 +02:00
|
|
|
name = 'the-container-name'
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['run', '--name', name, 'service', '/bin/true'])
|
2015-08-26 19:33:03 +02:00
|
|
|
|
|
|
|
service = self.project.get_service('service')
|
|
|
|
container, = service.containers(stopped=True, one_off=True)
|
|
|
|
self.assertEqual(container.name, name)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def test_run_with_networking(self):
|
2015-10-30 14:15:47 +01:00
|
|
|
self.require_api_version('1.21')
|
|
|
|
client = docker_client(version='1.21')
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/simple-dockerfile'
|
|
|
|
self.dispatch(['--x-networking', 'run', 'simple', 'true'], None)
|
2015-10-30 14:15:47 +01:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
container, = service.containers(stopped=True, one_off=True)
|
|
|
|
networks = client.networks(names=[self.project.name])
|
|
|
|
for n in networks:
|
2015-10-30 20:26:56 +01:00
|
|
|
self.addCleanup(client.remove_network, n['Id'])
|
2015-10-30 14:15:47 +01:00
|
|
|
self.assertEqual(len(networks), 1)
|
|
|
|
self.assertEqual(container.human_readable_command, u'true')
|
|
|
|
|
2015-11-11 01:43:05 +01:00
|
|
|
def test_run_handles_sigint(self):
|
|
|
|
proc = start_process(self.base_dir, ['run', '-T', 'simple', 'top'])
|
|
|
|
wait_on_condition(ContainerStateCondition(
|
|
|
|
self.project.client,
|
|
|
|
'simplecomposefile_simple_run_1',
|
|
|
|
running=True))
|
|
|
|
|
|
|
|
os.kill(proc.pid, signal.SIGINT)
|
|
|
|
wait_on_condition(ContainerStateCondition(
|
|
|
|
self.project.client,
|
|
|
|
'simplecomposefile_simple_run_1',
|
|
|
|
running=False))
|
|
|
|
|
|
|
|
def test_run_handles_sigterm(self):
|
|
|
|
proc = start_process(self.base_dir, ['run', '-T', 'simple', 'top'])
|
|
|
|
wait_on_condition(ContainerStateCondition(
|
|
|
|
self.project.client,
|
|
|
|
'simplecomposefile_simple_run_1',
|
|
|
|
running=True))
|
|
|
|
|
|
|
|
os.kill(proc.pid, signal.SIGTERM)
|
|
|
|
wait_on_condition(ContainerStateCondition(
|
|
|
|
self.project.client,
|
|
|
|
'simplecomposefile_simple_run_1',
|
|
|
|
running=False))
|
|
|
|
|
2014-03-04 11:49:07 +01:00
|
|
|
def test_rm(self):
|
2014-07-30 07:42:58 +02:00
|
|
|
service = self.project.get_service('simple')
|
2014-03-04 11:49:07 +01:00
|
|
|
service.create_container()
|
2015-12-08 21:11:05 +01:00
|
|
|
kill_service(service)
|
2014-03-04 11:49:07 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['rm', '--force'], None)
|
2014-03-04 11:49:07 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 0)
|
2015-02-23 00:13:15 +01:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
service.create_container()
|
2015-12-08 21:11:05 +01:00
|
|
|
kill_service(service)
|
2015-02-23 00:13:15 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['rm', '-f'], None)
|
2015-02-23 00:13:15 +01:00
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 0)
|
2014-03-04 11:49:07 +01:00
|
|
|
|
2015-04-21 12:58:44 +02:00
|
|
|
def test_stop(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2015-04-21 12:58:44 +02:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['stop', '-t', '1'], None)
|
2015-04-21 12:58:44 +02:00
|
|
|
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
self.assertFalse(service.containers(stopped=True)[0].is_running)
|
|
|
|
|
2015-12-15 19:05:22 +01:00
|
|
|
def test_start_no_containers(self):
|
|
|
|
result = self.dispatch(['start'], returncode=1)
|
|
|
|
assert 'No containers to start' in result.stderr
|
|
|
|
|
2015-12-17 22:23:00 +01:00
|
|
|
def test_up_logging(self):
|
|
|
|
self.base_dir = 'tests/fixtures/logging-composefile'
|
|
|
|
self.dispatch(['up', '-d'])
|
|
|
|
simple = self.project.get_service('simple').containers()[0]
|
|
|
|
log_config = simple.get('HostConfig.LogConfig')
|
|
|
|
self.assertTrue(log_config)
|
|
|
|
self.assertEqual(log_config.get('Type'), 'none')
|
|
|
|
|
|
|
|
another = self.project.get_service('another').containers()[0]
|
|
|
|
log_config = another.get('HostConfig.LogConfig')
|
|
|
|
self.assertTrue(log_config)
|
|
|
|
self.assertEqual(log_config.get('Type'), 'json-file')
|
|
|
|
self.assertEqual(log_config.get('Config')['max-size'], '10m')
|
|
|
|
|
2016-01-08 23:37:07 +01:00
|
|
|
def test_up_logging_legacy(self):
|
|
|
|
self.base_dir = 'tests/fixtures/logging-composefile-legacy'
|
|
|
|
self.dispatch(['up', '-d'])
|
|
|
|
simple = self.project.get_service('simple').containers()[0]
|
|
|
|
log_config = simple.get('HostConfig.LogConfig')
|
|
|
|
self.assertTrue(log_config)
|
|
|
|
self.assertEqual(log_config.get('Type'), 'none')
|
|
|
|
|
|
|
|
another = self.project.get_service('another').containers()[0]
|
|
|
|
log_config = another.get('HostConfig.LogConfig')
|
|
|
|
self.assertTrue(log_config)
|
|
|
|
self.assertEqual(log_config.get('Type'), 'json-file')
|
|
|
|
self.assertEqual(log_config.get('Config')['max-size'], '10m')
|
|
|
|
|
2015-07-25 22:20:58 +02:00
|
|
|
def test_pause_unpause(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2015-07-25 22:20:58 +02:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
self.assertFalse(service.containers()[0].is_paused)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['pause'], None)
|
2015-07-25 22:20:58 +02:00
|
|
|
self.assertTrue(service.containers()[0].is_paused)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['unpause'], None)
|
2015-07-25 22:20:58 +02:00
|
|
|
self.assertFalse(service.containers()[0].is_paused)
|
|
|
|
|
2015-12-15 19:05:22 +01:00
|
|
|
def test_pause_no_containers(self):
|
|
|
|
result = self.dispatch(['pause'], returncode=1)
|
|
|
|
assert 'No containers to pause' in result.stderr
|
|
|
|
|
|
|
|
def test_unpause_no_containers(self):
|
|
|
|
result = self.dispatch(['unpause'], returncode=1)
|
|
|
|
assert 'No containers to unpause' in result.stderr
|
|
|
|
|
2015-06-08 23:04:42 +02:00
|
|
|
def test_logs_invalid_service_name(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['logs', 'madeupname'], returncode=1)
|
2015-06-08 23:04:42 +02:00
|
|
|
|
2014-11-03 23:46:01 +01:00
|
|
|
def test_kill(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['kill'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
self.assertFalse(service.containers(stopped=True)[0].is_running)
|
|
|
|
|
2015-05-29 12:13:37 +02:00
|
|
|
def test_kill_signal_sigstop(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
service = self.project.get_service('simple')
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['kill', '-s', 'SIGSTOP'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(service.containers()), 1)
|
2015-05-29 12:13:37 +02:00
|
|
|
# The container is still running. It has only been paused
|
2014-11-03 23:46:01 +01:00
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
|
2015-05-29 12:13:37 +02:00
|
|
|
def test_kill_stopped_service(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['up', '-d'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
service = self.project.get_service('simple')
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['kill', '-s', 'SIGSTOP'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
self.assertTrue(service.containers()[0].is_running)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['kill', '-s', 'SIGKILL'], None)
|
2014-11-03 23:46:01 +01:00
|
|
|
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
self.assertFalse(service.containers(stopped=True)[0].is_running)
|
|
|
|
|
2014-08-08 22:11:34 +02:00
|
|
|
def test_restart(self):
|
|
|
|
service = self.project.get_service('simple')
|
|
|
|
container = service.create_container()
|
2015-11-02 19:33:13 +01:00
|
|
|
container.start()
|
2014-08-08 22:11:34 +02:00
|
|
|
started_at = container.dictionary['State']['StartedAt']
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['restart', '-t', '1'], None)
|
2014-08-08 22:11:34 +02:00
|
|
|
container.inspect()
|
|
|
|
self.assertNotEqual(
|
|
|
|
container.dictionary['State']['FinishedAt'],
|
|
|
|
'0001-01-01T00:00:00Z',
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
container.dictionary['State']['StartedAt'],
|
|
|
|
started_at,
|
|
|
|
)
|
|
|
|
|
2015-11-14 12:19:57 +01:00
|
|
|
def test_restart_stopped_container(self):
|
|
|
|
service = self.project.get_service('simple')
|
|
|
|
container = service.create_container()
|
|
|
|
container.start()
|
|
|
|
container.kill()
|
|
|
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
|
|
|
self.dispatch(['restart', '-t', '1'], None)
|
|
|
|
self.assertEqual(len(service.containers(stopped=False)), 1)
|
|
|
|
|
2015-12-15 19:05:22 +01:00
|
|
|
def test_restart_no_containers(self):
|
|
|
|
result = self.dispatch(['restart'], returncode=1)
|
|
|
|
assert 'No containers to restart' in result.stderr
|
|
|
|
|
2014-01-16 18:58:53 +01:00
|
|
|
def test_scale(self):
|
2014-07-30 07:42:58 +02:00
|
|
|
project = self.project
|
2014-01-16 18:58:53 +01:00
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['scale', 'simple=1'])
|
2014-01-16 18:58:53 +01:00
|
|
|
self.assertEqual(len(project.get_service('simple').containers()), 1)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['scale', 'simple=3', 'another=2'])
|
2014-01-16 18:58:53 +01:00
|
|
|
self.assertEqual(len(project.get_service('simple').containers()), 3)
|
|
|
|
self.assertEqual(len(project.get_service('another').containers()), 2)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['scale', 'simple=1', 'another=1'])
|
2014-01-16 18:58:53 +01:00
|
|
|
self.assertEqual(len(project.get_service('simple').containers()), 1)
|
|
|
|
self.assertEqual(len(project.get_service('another').containers()), 1)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['scale', 'simple=1', 'another=1'])
|
2014-01-16 18:58:53 +01:00
|
|
|
self.assertEqual(len(project.get_service('simple').containers()), 1)
|
|
|
|
self.assertEqual(len(project.get_service('another').containers()), 1)
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['scale', 'simple=0', 'another=0'])
|
2014-01-16 18:58:53 +01:00
|
|
|
self.assertEqual(len(project.get_service('simple').containers()), 0)
|
|
|
|
self.assertEqual(len(project.get_service('another').containers()), 0)
|
2014-08-08 18:41:52 +02:00
|
|
|
|
|
|
|
def test_port(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/ports-composefile'
|
|
|
|
self.dispatch(['up', '-d'], None)
|
2014-08-08 18:41:52 +02:00
|
|
|
container = self.project.get_service('simple').get_container()
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def get_port(number):
|
|
|
|
result = self.dispatch(['port', 'simple', str(number)])
|
|
|
|
return result.stdout.rstrip()
|
2014-08-08 18:41:52 +02:00
|
|
|
|
|
|
|
self.assertEqual(get_port(3000), container.get_local_port(3000))
|
2015-03-17 00:21:29 +01:00
|
|
|
self.assertEqual(get_port(3001), "0.0.0.0:49152")
|
2015-07-06 18:15:50 +02:00
|
|
|
self.assertEqual(get_port(3002), "0.0.0.0:49153")
|
2015-03-12 14:59:23 +01:00
|
|
|
|
2015-05-12 13:44:52 +02:00
|
|
|
def test_port_with_scale(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/ports-composefile-scale'
|
|
|
|
self.dispatch(['scale', 'simple=2'], None)
|
2015-05-12 13:44:52 +02:00
|
|
|
containers = sorted(
|
|
|
|
self.project.containers(service_names=['simple']),
|
|
|
|
key=attrgetter('name'))
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
def get_port(number, index=None):
|
2015-05-12 13:44:52 +02:00
|
|
|
if index is None:
|
2015-11-02 22:46:49 +01:00
|
|
|
result = self.dispatch(['port', 'simple', str(number)])
|
2015-05-12 13:44:52 +02:00
|
|
|
else:
|
2015-11-02 22:46:49 +01:00
|
|
|
result = self.dispatch(['port', '--index=' + str(index), 'simple', str(number)])
|
|
|
|
return result.stdout.rstrip()
|
2015-05-12 13:44:52 +02:00
|
|
|
|
|
|
|
self.assertEqual(get_port(3000), containers[0].get_local_port(3000))
|
|
|
|
self.assertEqual(get_port(3000, index=1), containers[0].get_local_port(3000))
|
|
|
|
self.assertEqual(get_port(3000, index=2), containers[1].get_local_port(3000))
|
|
|
|
self.assertEqual(get_port(3002), "")
|
|
|
|
|
2015-08-31 20:31:20 +02:00
|
|
|
def test_events_json(self):
|
|
|
|
events_proc = start_process(self.base_dir, ['events', '--json'])
|
|
|
|
self.dispatch(['up', '-d'])
|
|
|
|
wait_on_condition(ContainerCountCondition(self.project, 2))
|
|
|
|
|
|
|
|
os.kill(events_proc.pid, signal.SIGINT)
|
|
|
|
result = wait_on_process(events_proc, returncode=1)
|
|
|
|
lines = [json.loads(line) for line in result.stdout.rstrip().split('\n')]
|
2016-01-08 22:54:35 +01:00
|
|
|
assert [e['action'] for e in lines] == ['create', 'start', 'create', 'start']
|
|
|
|
|
|
|
|
def test_events_human_readable(self):
|
|
|
|
events_proc = start_process(self.base_dir, ['events'])
|
|
|
|
self.dispatch(['up', '-d', 'simple'])
|
|
|
|
wait_on_condition(ContainerCountCondition(self.project, 1))
|
|
|
|
|
|
|
|
os.kill(events_proc.pid, signal.SIGINT)
|
|
|
|
result = wait_on_process(events_proc, returncode=1)
|
|
|
|
lines = result.stdout.rstrip().split('\n')
|
|
|
|
assert len(lines) == 2
|
|
|
|
|
|
|
|
container, = self.project.containers()
|
|
|
|
expected_template = (
|
|
|
|
' container {} {} (image=busybox:latest, '
|
|
|
|
'name=simplecomposefile_simple_1)')
|
|
|
|
|
|
|
|
assert expected_template.format('create', container.id) in lines[0]
|
|
|
|
assert expected_template.format('start', container.id) in lines[1]
|
|
|
|
assert lines[0].startswith(datetime.date.today().isoformat())
|
2015-08-31 20:31:20 +02:00
|
|
|
|
2015-03-12 14:59:23 +01:00
|
|
|
def test_env_file_relative_to_compose_file(self):
|
|
|
|
config_path = os.path.abspath('tests/fixtures/env-file/docker-compose.yml')
|
2015-11-02 22:46:49 +01:00
|
|
|
self.dispatch(['-f', config_path, 'up', '-d'], None)
|
|
|
|
self._project = get_project(self.base_dir, [config_path])
|
2015-03-12 14:59:23 +01:00
|
|
|
|
|
|
|
containers = self.project.containers(stopped=True)
|
|
|
|
self.assertEqual(len(containers), 1)
|
|
|
|
self.assertIn("FOO=1", containers[0].get('Config.Env'))
|
2015-03-18 21:51:27 +01:00
|
|
|
|
2014-08-19 23:36:46 +02:00
|
|
|
@mock.patch.dict(os.environ)
|
2015-07-24 16:58:18 +02:00
|
|
|
def test_home_and_env_var_in_volume_path(self):
|
|
|
|
os.environ['VOLUME_NAME'] = 'my-volume'
|
|
|
|
os.environ['HOME'] = '/tmp/home-dir'
|
|
|
|
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/volume-path-interpolation'
|
|
|
|
self.dispatch(['up', '-d'], None)
|
2015-07-24 16:58:18 +02:00
|
|
|
|
|
|
|
container = self.project.containers(stopped=True)[0]
|
2015-10-03 01:04:39 +02:00
|
|
|
actual_host_path = container.get_mount('/container-path')['Source']
|
2015-07-24 16:58:18 +02:00
|
|
|
components = actual_host_path.split('/')
|
2015-11-02 22:46:49 +01:00
|
|
|
assert components[-2:] == ['home-dir', 'my-volume']
|
2015-07-24 16:58:18 +02:00
|
|
|
|
2015-09-15 20:43:18 +02:00
|
|
|
def test_up_with_default_override_file(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/override-files'
|
|
|
|
self.dispatch(['up', '-d'], None)
|
2015-09-15 20:43:18 +02:00
|
|
|
|
|
|
|
containers = self.project.containers()
|
|
|
|
self.assertEqual(len(containers), 2)
|
|
|
|
|
|
|
|
web, db = containers
|
|
|
|
self.assertEqual(web.human_readable_command, 'top')
|
|
|
|
self.assertEqual(db.human_readable_command, 'top')
|
|
|
|
|
|
|
|
def test_up_with_multiple_files(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/override-files'
|
2015-09-15 20:43:18 +02:00
|
|
|
config_paths = [
|
|
|
|
'docker-compose.yml',
|
|
|
|
'docker-compose.override.yml',
|
|
|
|
'extra.yml',
|
|
|
|
|
|
|
|
]
|
2015-11-02 22:46:49 +01:00
|
|
|
self._project = get_project(self.base_dir, config_paths)
|
|
|
|
self.dispatch(
|
2015-09-15 20:43:18 +02:00
|
|
|
[
|
|
|
|
'-f', config_paths[0],
|
|
|
|
'-f', config_paths[1],
|
|
|
|
'-f', config_paths[2],
|
|
|
|
'up', '-d',
|
|
|
|
],
|
|
|
|
None)
|
|
|
|
|
|
|
|
containers = self.project.containers()
|
|
|
|
self.assertEqual(len(containers), 3)
|
|
|
|
|
|
|
|
web, other, db = containers
|
|
|
|
self.assertEqual(web.human_readable_command, 'top')
|
2015-10-20 18:49:45 +02:00
|
|
|
self.assertTrue({'db', 'other'} <= set(get_links(web)))
|
2015-09-15 20:43:18 +02:00
|
|
|
self.assertEqual(db.human_readable_command, 'top')
|
|
|
|
self.assertEqual(other.human_readable_command, 'top')
|
|
|
|
|
2015-03-18 21:51:27 +01:00
|
|
|
def test_up_with_extends(self):
|
2015-11-02 22:46:49 +01:00
|
|
|
self.base_dir = 'tests/fixtures/extends'
|
|
|
|
self.dispatch(['up', '-d'], None)
|
2015-03-18 21:51:27 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set([s.name for s in self.project.services]),
|
|
|
|
set(['mydb', 'myweb']),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Sort by name so we get [db, web]
|
|
|
|
containers = sorted(
|
|
|
|
self.project.containers(stopped=True),
|
|
|
|
key=lambda c: c.name,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(len(containers), 2)
|
|
|
|
web = containers[1]
|
|
|
|
|
2015-10-20 18:49:45 +02:00
|
|
|
self.assertEqual(
|
|
|
|
set(get_links(web)),
|
|
|
|
set(['db', 'mydb_1', 'extends_mydb_1']))
|
2015-03-18 21:51:27 +01:00
|
|
|
|
|
|
|
expected_env = set([
|
|
|
|
"FOO=1",
|
|
|
|
"BAR=2",
|
|
|
|
"BAZ=2",
|
|
|
|
])
|
|
|
|
self.assertTrue(expected_env <= set(web.get('Config.Env')))
|