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
|
|
|
|
|
2017-07-20 00:01:39 +02:00
|
|
|
import functools
|
2016-01-15 01:10:57 +01:00
|
|
|
import os
|
|
|
|
|
2017-07-07 02:25:41 +02:00
|
|
|
import pytest
|
2017-12-04 04:25:04 +01:00
|
|
|
import six
|
2017-07-20 00:01:39 +02:00
|
|
|
from docker.errors import APIError
|
2015-10-07 17:10:08 +02:00
|
|
|
from docker.utils import version_lt
|
2015-09-22 18:24:56 +02:00
|
|
|
|
2015-08-24 21:25:25 +02:00
|
|
|
from .. import unittest
|
|
|
|
from compose.cli.docker_client import docker_client
|
2015-11-06 23:18:47 +01:00
|
|
|
from compose.config.config import resolve_environment
|
2016-03-08 02:18:19 +01:00
|
|
|
from compose.config.environment import Environment
|
2016-01-15 01:10:57 +01:00
|
|
|
from compose.const import API_VERSIONS
|
2017-03-13 20:57:13 +01:00
|
|
|
from compose.const import COMPOSEFILE_V1 as V1
|
|
|
|
from compose.const import COMPOSEFILE_V2_0 as V2_0
|
|
|
|
from compose.const import COMPOSEFILE_V2_0 as V2_1
|
2017-05-17 13:50:29 +02:00
|
|
|
from compose.const import COMPOSEFILE_V2_2 as V2_2
|
2017-07-10 06:35:34 +02:00
|
|
|
from compose.const import COMPOSEFILE_V2_3 as V2_3
|
2017-07-07 02:25:41 +02:00
|
|
|
from compose.const import COMPOSEFILE_V3_0 as V3_0
|
2017-07-12 04:07:12 +02:00
|
|
|
from compose.const import COMPOSEFILE_V3_2 as V3_2
|
2017-12-05 07:47:33 +01:00
|
|
|
from compose.const import COMPOSEFILE_V3_5 as V3_5
|
2015-05-30 20:52:10 +02:00
|
|
|
from compose.const import LABEL_PROJECT
|
2015-01-12 15:59:05 +01:00
|
|
|
from compose.progress_stream import stream_output
|
2015-08-24 21:25:25 +02:00
|
|
|
from compose.service import Service
|
2013-12-09 18:36:44 +01:00
|
|
|
|
2017-05-22 23:58:51 +02:00
|
|
|
SWARM_SKIP_CONTAINERS_ALL = os.environ.get('SWARM_SKIP_CONTAINERS_ALL', '0') != '0'
|
|
|
|
SWARM_SKIP_CPU_SHARES = os.environ.get('SWARM_SKIP_CPU_SHARES', '0') != '0'
|
|
|
|
SWARM_SKIP_RM_VOLUMES = os.environ.get('SWARM_SKIP_RM_VOLUMES', '0') != '0'
|
2017-07-20 00:01:39 +02:00
|
|
|
SWARM_ASSUME_MULTINODE = os.environ.get('SWARM_ASSUME_MULTINODE', '0') != '0'
|
2017-05-22 23:58:51 +02:00
|
|
|
|
2013-12-09 18:36:44 +01:00
|
|
|
|
2015-09-22 18:24:56 +02:00
|
|
|
def pull_busybox(client):
|
2015-11-20 23:51:36 +01:00
|
|
|
client.pull('busybox:latest', stream=False)
|
2015-09-22 18:24:56 +02:00
|
|
|
|
|
|
|
|
2015-10-20 18:49:45 +02:00
|
|
|
def get_links(container):
|
|
|
|
links = container.get('HostConfig.Links') or []
|
|
|
|
|
|
|
|
def format_link(link):
|
|
|
|
_, alias = link.split(':')
|
|
|
|
return alias.split('/')[-1]
|
|
|
|
|
|
|
|
return [format_link(link) for link in links]
|
|
|
|
|
|
|
|
|
2016-06-28 02:27:31 +02:00
|
|
|
def engine_max_version():
|
2016-01-15 01:10:57 +01:00
|
|
|
if 'DOCKER_VERSION' not in os.environ:
|
2017-12-05 07:47:33 +01:00
|
|
|
return V3_5
|
2016-01-15 01:10:57 +01:00
|
|
|
version = os.environ['DOCKER_VERSION'].partition('-')[0]
|
2016-06-28 02:27:31 +02:00
|
|
|
if version_lt(version, '1.10'):
|
|
|
|
return V1
|
2017-01-05 20:54:35 +01:00
|
|
|
if version_lt(version, '1.12'):
|
2016-06-28 02:27:31 +02:00
|
|
|
return V2_0
|
2017-01-05 20:54:35 +01:00
|
|
|
if version_lt(version, '1.13'):
|
2017-01-20 00:41:31 +01:00
|
|
|
return V2_1
|
2017-07-07 02:25:41 +02:00
|
|
|
if version_lt(version, '17.06'):
|
2017-07-12 04:07:12 +02:00
|
|
|
return V3_2
|
2017-12-05 07:47:33 +01:00
|
|
|
return V3_5
|
2016-01-15 01:10:57 +01:00
|
|
|
|
|
|
|
|
2017-07-07 02:25:41 +02:00
|
|
|
def min_version_skip(version):
|
|
|
|
return pytest.mark.skipif(
|
|
|
|
engine_max_version() < version,
|
|
|
|
reason="Engine version %s is too low" % version
|
|
|
|
)
|
2016-01-15 01:10:57 +01:00
|
|
|
|
|
|
|
|
2016-12-06 23:01:35 +01:00
|
|
|
def v2_only():
|
2017-07-07 02:25:41 +02:00
|
|
|
return min_version_skip(V2_0)
|
2016-12-06 23:01:35 +01:00
|
|
|
|
|
|
|
|
2016-06-28 02:27:31 +02:00
|
|
|
def v2_1_only():
|
2017-07-07 02:25:41 +02:00
|
|
|
return min_version_skip(V2_1)
|
2016-06-28 02:27:31 +02:00
|
|
|
|
2017-05-17 14:21:35 +02:00
|
|
|
|
2017-05-17 13:50:29 +02:00
|
|
|
def v2_2_only():
|
2017-07-10 06:35:34 +02:00
|
|
|
return min_version_skip(V2_2)
|
|
|
|
|
|
|
|
|
|
|
|
def v2_3_only():
|
|
|
|
return min_version_skip(V2_3)
|
2016-12-06 23:01:35 +01:00
|
|
|
|
2017-05-17 14:21:35 +02:00
|
|
|
|
2016-12-06 23:01:35 +01:00
|
|
|
def v3_only():
|
2017-07-07 02:25:41 +02:00
|
|
|
return min_version_skip(V3_0)
|
2016-06-28 02:27:31 +02:00
|
|
|
|
|
|
|
|
2014-01-06 12:22:46 +01:00
|
|
|
class DockerClientTestCase(unittest.TestCase):
|
2013-12-09 18:36:44 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-06-28 02:27:31 +02:00
|
|
|
version = API_VERSIONS[engine_max_version()]
|
2016-03-22 02:32:13 +01:00
|
|
|
cls.client = docker_client(Environment(), version)
|
2013-12-09 18:36:44 +01:00
|
|
|
|
2016-07-20 16:51:22 +02:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
del cls.client
|
|
|
|
|
2015-05-30 22:17:21 +02:00
|
|
|
def tearDown(self):
|
2015-05-30 20:52:10 +02:00
|
|
|
for c in self.client.containers(
|
|
|
|
all=True,
|
|
|
|
filters={'label': '%s=composetest' % LABEL_PROJECT}):
|
2015-10-09 21:42:15 +02:00
|
|
|
self.client.remove_container(c['Id'], force=True)
|
2016-01-12 17:26:20 +01:00
|
|
|
|
2015-05-30 22:17:21 +02:00
|
|
|
for i in self.client.images(
|
|
|
|
filters={'label': 'com.docker.compose.test_image'}):
|
2017-08-03 01:59:43 +02:00
|
|
|
try:
|
|
|
|
self.client.remove_image(i, force=True)
|
|
|
|
except APIError as e:
|
|
|
|
if e.is_server_error():
|
|
|
|
pass
|
2016-01-12 17:26:20 +01:00
|
|
|
|
2015-11-17 04:21:56 +01:00
|
|
|
volumes = self.client.volumes().get('Volumes') or []
|
|
|
|
for v in volumes:
|
2015-12-09 02:21:20 +01:00
|
|
|
if 'composetest_' in v['Name']:
|
2015-11-17 04:21:56 +01:00
|
|
|
self.client.remove_volume(v['Name'])
|
2013-12-09 18:36:44 +01:00
|
|
|
|
2016-01-12 17:26:20 +01:00
|
|
|
networks = self.client.networks()
|
|
|
|
for n in networks:
|
|
|
|
if 'composetest_' in n['Name']:
|
|
|
|
self.client.remove_network(n['Name'])
|
|
|
|
|
2013-12-09 22:39:11 +01:00
|
|
|
def create_service(self, name, **kwargs):
|
2015-04-30 12:57:46 +02:00
|
|
|
if 'image' not in kwargs and 'build' not in kwargs:
|
|
|
|
kwargs['image'] = 'busybox:latest'
|
2015-02-27 12:54:57 +01:00
|
|
|
|
2014-02-21 19:12:51 +01:00
|
|
|
if 'command' not in kwargs:
|
2015-05-21 17:19:15 +02:00
|
|
|
kwargs['command'] = ["top"]
|
2015-02-27 12:54:57 +01:00
|
|
|
|
2016-03-22 23:42:30 +01:00
|
|
|
kwargs['environment'] = resolve_environment(
|
|
|
|
kwargs, Environment.from_env_file(None)
|
|
|
|
)
|
2015-11-14 01:40:10 +01:00
|
|
|
labels = dict(kwargs.setdefault('labels', {}))
|
2015-08-27 00:55:22 +02:00
|
|
|
labels['com.docker.compose.test-name'] = self.id()
|
|
|
|
|
2015-11-14 01:40:10 +01:00
|
|
|
return Service(name, client=self.client, project='composetest', **kwargs)
|
2013-12-09 18:36:44 +01:00
|
|
|
|
2014-09-26 19:58:52 +02:00
|
|
|
def check_build(self, *args, **kwargs):
|
2015-05-30 22:17:21 +02:00
|
|
|
kwargs.setdefault('rm', True)
|
2014-09-26 19:58:52 +02:00
|
|
|
build_output = self.client.build(*args, **kwargs)
|
|
|
|
stream_output(build_output, open('/dev/null', 'w'))
|
2015-10-07 17:10:08 +02:00
|
|
|
|
2015-10-21 21:40:50 +02:00
|
|
|
def require_api_version(self, minimum):
|
|
|
|
api_version = self.client.version()['ApiVersion']
|
|
|
|
if version_lt(api_version, minimum):
|
2017-07-07 02:25:41 +02:00
|
|
|
pytest.skip("API version is too low ({} < {})".format(api_version, minimum))
|
2017-05-22 23:58:51 +02:00
|
|
|
|
|
|
|
def get_volume_data(self, volume_name):
|
|
|
|
if not is_cluster(self.client):
|
|
|
|
return self.client.inspect_volume(volume_name)
|
|
|
|
|
|
|
|
volumes = self.client.volumes(filters={'name': volume_name})['Volumes']
|
|
|
|
assert len(volumes) > 0
|
|
|
|
return self.client.inspect_volume(volumes[0]['Name'])
|
2017-07-20 00:01:39 +02:00
|
|
|
|
|
|
|
|
2017-12-04 04:25:04 +01:00
|
|
|
def if_runtime_available(runtime):
|
|
|
|
if runtime == 'nvidia':
|
|
|
|
command = 'nvidia-container-runtime'
|
|
|
|
if six.PY3:
|
|
|
|
import shutil
|
|
|
|
return pytest.mark.skipif(
|
|
|
|
shutil.which(command) is None,
|
|
|
|
reason="Nvida runtime not exists"
|
|
|
|
)
|
|
|
|
return pytest.mark.skipif(
|
|
|
|
any(
|
|
|
|
os.access(os.path.join(path, command), os.X_OK)
|
|
|
|
for path in os.environ["PATH"].split(os.pathsep)
|
|
|
|
) is False,
|
|
|
|
reason="Nvida runtime not exists"
|
|
|
|
)
|
|
|
|
return pytest.skip("Runtime %s not exists", runtime)
|
|
|
|
|
|
|
|
|
2017-07-20 00:01:39 +02:00
|
|
|
def is_cluster(client):
|
|
|
|
if SWARM_ASSUME_MULTINODE:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_nodes_number():
|
|
|
|
try:
|
|
|
|
return len(client.nodes())
|
|
|
|
except APIError:
|
|
|
|
# If the Engine is not part of a Swarm, the SDK will raise
|
|
|
|
# an APIError
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if not hasattr(is_cluster, 'nodes') or is_cluster.nodes is None:
|
|
|
|
# Only make the API call if the value hasn't been cached yet
|
|
|
|
is_cluster.nodes = get_nodes_number()
|
|
|
|
|
|
|
|
return is_cluster.nodes > 1
|
|
|
|
|
|
|
|
|
|
|
|
def no_cluster(reason):
|
|
|
|
def decorator(f):
|
|
|
|
@functools.wraps(f)
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
if is_cluster(self.client):
|
|
|
|
pytest.skip("Test will not be run in cluster mode: %s" % reason)
|
|
|
|
return
|
|
|
|
return f(self, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|