Skip to content

Commit

Permalink
Some more test adjustments for Swarm support
Browse files Browse the repository at this point in the history
Signed-off-by: Joffrey F <[email protected]>
  • Loading branch information
shin- committed Jul 20, 2017
1 parent 8f0ef26 commit 6a4adb6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ coverage-html
docs/_site
venv
.tox
**/__pycache__
*.pyc
26 changes: 18 additions & 8 deletions tests/acceptance/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@

from .. import mock
from ..helpers import create_host_file
from ..helpers import is_cluster
from ..helpers import no_cluster
from compose.cli.command import get_project
from compose.config.errors import DuplicateOverrideFileFound
from compose.container import Container
from compose.project import OneOffFilter
from compose.utils import nanoseconds_from_time_seconds
from tests.integration.testcases import DockerClientTestCase
from tests.integration.testcases import get_links
from tests.integration.testcases import is_cluster
from tests.integration.testcases import no_cluster
from tests.integration.testcases import pull_busybox
from tests.integration.testcases import SWARM_SKIP_RM_VOLUMES
from tests.integration.testcases import v2_1_only
Expand Down Expand Up @@ -116,7 +116,7 @@ def setUp(self):
def tearDown(self):
if self.base_dir:
self.project.kill()
self.project.remove_stopped()
self.project.down(None, True)

for container in self.project.containers(stopped=True, one_off=OneOffFilter.only):
container.remove(force=True)
Expand Down Expand Up @@ -1214,6 +1214,7 @@ def test_up_handles_abort_on_container_exit_code(self):
self.assertEqual(proc.returncode, 1)

@v2_only()
@no_cluster('Container PID mode does not work across clusters')
def test_up_with_pid_mode(self):
c = self.client.create_container(
'busybox', 'top', name='composetest_pid_mode_container',
Expand Down Expand Up @@ -1244,8 +1245,8 @@ def test_exec_without_tty(self):
self.assertEqual(len(self.project.containers()), 1)

stdout, stderr = self.dispatch(['exec', '-T', 'console', 'ls', '-1d', '/'])
self.assertEqual(stdout, "/\n")
self.assertEqual(stderr, "")
self.assertEqual(stdout, "/\n")

def test_exec_custom_user(self):
self.base_dir = 'tests/fixtures/links-composefile'
Expand Down Expand Up @@ -1826,7 +1827,13 @@ def test_logs_follow(self):

result = self.dispatch(['logs', '-f'])

assert result.stdout.count('\n') == 5
if not is_cluster(self.client):
assert result.stdout.count('\n') == 5
else:
# Sometimes logs are picked up from old containers that haven't yet
# been removed (removal in Swarm is async)
assert result.stdout.count('\n') >= 5

assert 'simple' in result.stdout
assert 'another' in result.stdout
assert 'exited with code 0' in result.stdout
Expand Down Expand Up @@ -1882,7 +1889,10 @@ def test_logs_tail(self):
self.dispatch(['up'])

result = self.dispatch(['logs', '--tail', '2'])
assert result.stdout.count('\n') == 3
assert 'c\n' in result.stdout
assert 'd\n' in result.stdout
assert 'a\n' not in result.stdout
assert 'b\n' not in result.stdout

def test_kill(self):
self.dispatch(['up', '-d'], None)
Expand Down Expand Up @@ -2045,8 +2055,8 @@ def get_port(number):
return result.stdout.rstrip()

assert get_port(3000) == container.get_local_port(3000)
assert ':49152' in get_port(3001)
assert ':49153' in get_port(3002)
assert ':53222' in get_port(3001)
assert ':53223' in get_port(3002)

def test_port_with_scale(self):
self.base_dir = 'tests/fixtures/ports-composefile-scale'
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/ports-composefile/expanded-notation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ services:
ports:
- target: 3000
- target: 3001
published: 49152
published: 53222
- target: 3002
published: 49153
published: 53223
protocol: tcp
- target: 3003
published: 49154
published: 53224
protocol: udp
35 changes: 0 additions & 35 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import functools
import os

from docker.errors import APIError
from pytest import skip

from compose.config.config import ConfigDetails
from compose.config.config import ConfigFile
from compose.config.config import load
Expand Down Expand Up @@ -48,34 +44,3 @@ def create_host_file(client, filename):
"Container exited with code {}:\n{}".format(exitcode, output))
finally:
client.remove_container(container, force=True)


def is_cluster(client):
nodes = None

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 nodes is None:
# Only make the API call if the value hasn't been cached yet
nodes = get_nodes_number()

return nodes > 1


def no_cluster(reason):
def decorator(f):
@functools.wraps(f)
def wrapper(self, *args, **kwargs):
if is_cluster(self.client):
skip("Test will not be run in cluster mode: %s" % reason)
return
return f(self, *args, **kwargs)
return wrapper

return decorator
4 changes: 2 additions & 2 deletions tests/integration/project_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from .. import mock
from ..helpers import build_config as load_config
from ..helpers import create_host_file
from ..helpers import is_cluster
from ..helpers import no_cluster
from .testcases import DockerClientTestCase
from .testcases import SWARM_SKIP_CONTAINERS_ALL
from compose.config import config
Expand All @@ -33,6 +31,8 @@
from compose.project import Project
from compose.project import ProjectError
from compose.service import ConvergenceStrategy
from tests.integration.testcases import is_cluster
from tests.integration.testcases import no_cluster
from tests.integration.testcases import v2_1_only
from tests.integration.testcases import v2_2_only
from tests.integration.testcases import v2_only
Expand Down
23 changes: 19 additions & 4 deletions tests/integration/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from six import text_type

from .. import mock
from ..helpers import is_cluster
from ..helpers import no_cluster
from .testcases import DockerClientTestCase
from .testcases import get_links
from .testcases import pull_busybox
Expand All @@ -38,6 +36,8 @@
from compose.service import NetworkMode
from compose.service import PidMode
from compose.service import Service
from tests.integration.testcases import is_cluster
from tests.integration.testcases import no_cluster
from tests.integration.testcases import v2_1_only
from tests.integration.testcases import v2_2_only
from tests.integration.testcases import v2_only
Expand Down Expand Up @@ -635,7 +635,10 @@ def test_build(self):
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
f.write("FROM busybox\n")

self.create_service('web', build={'context': base_dir}).build()
service = self.create_service('web', build={'context': base_dir})
service.build()
self.addCleanup(self.client.remove_image, service.image_name)

assert self.client.inspect_image('composetest_web')

def test_build_non_ascii_filename(self):
Expand All @@ -648,7 +651,9 @@ def test_build_non_ascii_filename(self):
with open(os.path.join(base_dir.encode('utf8'), b'foo\xE2bar'), 'w') as f:
f.write("hello world\n")

self.create_service('web', build={'context': text_type(base_dir)}).build()
service = self.create_service('web', build={'context': text_type(base_dir)})
service.build()
self.addCleanup(self.client.remove_image, service.image_name)
assert self.client.inspect_image('composetest_web')

def test_build_with_image_name(self):
Expand Down Expand Up @@ -683,6 +688,7 @@ def test_build_with_build_args(self):
build={'context': text_type(base_dir),
'args': {"build_version": "1"}})
service.build()
self.addCleanup(self.client.remove_image, service.image_name)
assert service.image()
assert "build_version=1" in service.image()['ContainerConfig']['Cmd']

Expand All @@ -699,6 +705,8 @@ def test_build_with_build_args_override(self):
build={'context': text_type(base_dir),
'args': {"build_version": "1"}})
service.build(build_args_override={'build_version': '2'})
self.addCleanup(self.client.remove_image, service.image_name)

assert service.image()
assert "build_version=2" in service.image()['ContainerConfig']['Cmd']

Expand All @@ -714,9 +722,12 @@ def test_build_with_build_labels(self):
'labels': {'com.docker.compose.test': 'true'}
})
service.build()
self.addCleanup(self.client.remove_image, service.image_name)

assert service.image()
assert service.image()['Config']['Labels']['com.docker.compose.test'] == 'true'

@no_cluster('Container networks not on Swarm')
def test_build_with_network(self):
base_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, base_dir)
Expand All @@ -739,6 +750,8 @@ def test_build_with_network(self):
})

service.build()
self.addCleanup(self.client.remove_image, service.image_name)

assert service.image()

def test_start_container_stays_unprivileged(self):
Expand Down Expand Up @@ -1130,6 +1143,8 @@ def test_build_with_cachefrom(self):
build={'context': base_dir,
'cache_from': ['build1']})
service.build()
self.addCleanup(self.client.remove_image, service.image_name)

assert service.image()

@mock.patch.dict(os.environ)
Expand Down
36 changes: 35 additions & 1 deletion tests/integration/testcases.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import functools
import os

import pytest
from docker.errors import APIError
from docker.utils import version_lt

from .. import unittest
from ..helpers import is_cluster
from compose.cli.docker_client import docker_client
from compose.config.config import resolve_environment
from compose.config.environment import Environment
Expand All @@ -25,6 +26,7 @@
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'
SWARM_ASSUME_MULTINODE = os.environ.get('SWARM_ASSUME_MULTINODE', '0') != '0'


def pull_busybox(client):
Expand Down Expand Up @@ -141,3 +143,35 @@ def get_volume_data(self, volume_name):
volumes = self.client.volumes(filters={'name': volume_name})['Volumes']
assert len(volumes) > 0
return self.client.inspect_volume(volumes[0]['Name'])


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
2 changes: 1 addition & 1 deletion tests/integration/volume_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from docker.errors import DockerException

from ..helpers import no_cluster
from .testcases import DockerClientTestCase
from .testcases import no_cluster
from compose.const import LABEL_PROJECT
from compose.const import LABEL_VOLUME
from compose.volume import Volume
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ passenv =
DOCKER_CERT_PATH
DOCKER_TLS_VERIFY
DOCKER_VERSION
SWARM_SKIP_*
SWARM_ASSUME_MULTINODE
setenv =
HOME=/tmp
deps =
Expand Down

0 comments on commit 6a4adb6

Please sign in to comment.