Skip to content

Commit

Permalink
If -x-networking is used, set the correct API version.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <[email protected]>
  • Loading branch information
dnephin committed Oct 14, 2015
1 parent d5f5eb1 commit e2f792c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 4 additions & 3 deletions compose/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def get_config_path(file_option):
return [config_file] if config_file else None


def get_client(verbose=False):
client = docker_client()
def get_client(verbose=False, version=None):
client = docker_client(version=version)
if verbose:
version_info = six.iteritems(client.version())
log.info("Compose version %s", __version__)
Expand All @@ -83,11 +83,12 @@ def get_project(base_dir, config_path=None, project_name=None, verbose=False,
use_networking=False, network_driver=None):
config_details = config.find(base_dir, config_path)

api_version = '1.21' if use_networking else None
try:
return Project.from_dicts(
get_project_name(config_details.working_dir, project_name),
config.load(config_details),
get_client(verbose=verbose),
get_client(verbose=verbose, version=api_version),
use_networking=use_networking,
network_driver=network_driver,
)
Expand Down
9 changes: 7 additions & 2 deletions compose/cli/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
log = logging.getLogger(__name__)


def docker_client():
DEFAULT_API_VERSION = '1.19'


def docker_client(version=None):
"""
Returns a docker-py client configured using environment variables
according to the same logic as the official Docker client.
Expand All @@ -18,6 +21,8 @@ def docker_client():
log.warn('The DOCKER_CLIENT_TIMEOUT environment variable is deprecated. Please use COMPOSE_HTTP_TIMEOUT instead.')

kwargs = kwargs_from_env(assert_hostname=False)
kwargs['version'] = os.environ.get('COMPOSE_API_VERSION', '1.19')
kwargs['version'] = version or os.environ.get(
'COMPOSE_API_VERSION',
DEFAULT_API_VERSION)
kwargs['timeout'] = HTTP_TIMEOUT
return Client(**kwargs)
11 changes: 7 additions & 4 deletions tests/integration/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .. import mock
from .testcases import DockerClientTestCase
from compose.cli.command import get_project
from compose.cli.docker_client import docker_client
from compose.cli.errors import UserError
from compose.cli.main import TopLevelCommand
from compose.project import NoSuchService
Expand Down Expand Up @@ -190,8 +191,9 @@ def test_up_without_networking(self):

self.command.base_dir = 'tests/fixtures/links-composefile'
self.command.dispatch(['up', '-d'], None)
client = docker_client(version='1.21')

networks = [n for n in self.client.networks(names=[self.project.name])]
networks = client.networks(names=[self.project.name])
self.assertEqual(len(networks), 0)

for service in self.project.get_services():
Expand All @@ -207,16 +209,17 @@ def test_up_with_networking(self):

self.command.base_dir = 'tests/fixtures/links-composefile'
self.command.dispatch(['--x-networking', 'up', '-d'], None)
client = docker_client(version='1.21')

services = self.project.get_services()

networks = [n for n in self.client.networks(names=[self.project.name])]
networks = client.networks(names=[self.project.name])
for n in networks:
self.addCleanup(self.client.remove_network, n['id'])
self.addCleanup(client.remove_network, n['id'])
self.assertEqual(len(networks), 1)
self.assertEqual(networks[0]['driver'], 'bridge')

network = self.client.inspect_network(networks[0]['id'])
network = client.inspect_network(networks[0]['id'])
self.assertEqual(len(network['containers']), len(services))

for service in services:
Expand Down

0 comments on commit e2f792c

Please sign in to comment.