mirror of
https://github.com/docker/compose.git
synced 2025-07-25 22:54:54 +02:00
Extract docker URL logic, use it in tests as well
This commit is contained in:
parent
ff65a3e1b0
commit
9ed6538693
@ -3,41 +3,18 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import yaml
|
import yaml
|
||||||
import socket
|
|
||||||
|
|
||||||
from ..project import Project
|
from ..project import Project
|
||||||
from .docopt_command import DocoptCommand
|
from .docopt_command import DocoptCommand
|
||||||
from .formatter import Formatter
|
from .formatter import Formatter
|
||||||
from .utils import cached_property
|
from .utils import cached_property, docker_url
|
||||||
from .errors import UserError
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Command(DocoptCommand):
|
class Command(DocoptCommand):
|
||||||
@cached_property
|
@cached_property
|
||||||
def client(self):
|
def client(self):
|
||||||
if os.environ.get('DOCKER_URL'):
|
return Client(docker_url())
|
||||||
return Client(os.environ['DOCKER_URL'])
|
|
||||||
|
|
||||||
socket_path = '/var/run/docker.sock'
|
|
||||||
tcp_host = '127.0.0.1'
|
|
||||||
tcp_port = 4243
|
|
||||||
|
|
||||||
if os.path.exists(socket_path):
|
|
||||||
return Client('unix://%s' % socket_path)
|
|
||||||
|
|
||||||
try:
|
|
||||||
s = socket.socket()
|
|
||||||
s.connect((tcp_host, tcp_port))
|
|
||||||
s.close()
|
|
||||||
return Client('http://%s:%s' % (tcp_host, tcp_port))
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
raise UserError("""
|
|
||||||
Couldn't find Docker daemon - tried %s and %s:%s.
|
|
||||||
If it's running elsewhere, specify a url with DOCKER_URL.
|
|
||||||
""" % (socket_path, tcp_host, tcp_port))
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def project(self):
|
def project(self):
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import socket
|
||||||
|
from .errors import UserError
|
||||||
|
|
||||||
|
|
||||||
def cached_property(f):
|
def cached_property(f):
|
||||||
@ -74,3 +76,28 @@ def mkdir(path, permissions=0700):
|
|||||||
os.chmod(path, permissions)
|
os.chmod(path, permissions)
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def docker_url():
|
||||||
|
if os.environ.get('DOCKER_URL'):
|
||||||
|
return os.environ['DOCKER_URL']
|
||||||
|
|
||||||
|
socket_path = '/var/run/docker.sock'
|
||||||
|
tcp_host = '127.0.0.1'
|
||||||
|
tcp_port = 4243
|
||||||
|
|
||||||
|
if os.path.exists(socket_path):
|
||||||
|
return 'unix://%s' % socket_path
|
||||||
|
|
||||||
|
try:
|
||||||
|
s = socket.socket()
|
||||||
|
s.connect((tcp_host, tcp_port))
|
||||||
|
s.close()
|
||||||
|
return 'http://%s:%s' % (tcp_host, tcp_port)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
raise UserError("""
|
||||||
|
Couldn't find Docker daemon - tried %s and %s:%s.
|
||||||
|
If it's running elsewhere, specify a url with DOCKER_URL.
|
||||||
|
""" % (socket_path, tcp_host, tcp_port))
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
from fig.project import Project
|
from fig.project import Project
|
||||||
from fig.service import Service
|
|
||||||
from .testcases import DockerClientTestCase
|
from .testcases import DockerClientTestCase
|
||||||
|
|
||||||
|
|
||||||
@ -57,7 +56,7 @@ class ProjectTest(DockerClientTestCase):
|
|||||||
self.assertEqual(len(unstarted), 2)
|
self.assertEqual(len(unstarted), 2)
|
||||||
self.assertEqual(unstarted[0][0], web)
|
self.assertEqual(unstarted[0][0], web)
|
||||||
self.assertEqual(unstarted[1][0], db)
|
self.assertEqual(unstarted[1][0], db)
|
||||||
self.assertEqual(len(web.containers(stopped=True)), 2)
|
self.assertEqual(len(web.containers(stopped=True)), 1)
|
||||||
self.assertEqual(len(db.containers(stopped=True)), 1)
|
self.assertEqual(len(db.containers(stopped=True)), 1)
|
||||||
|
|
||||||
def test_up(self):
|
def test_up(self):
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
from docker import Client
|
from docker import Client
|
||||||
from fig.service import Service
|
from fig.service import Service
|
||||||
import os
|
from fig.cli.utils import docker_url
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
|
|
||||||
class DockerClientTestCase(TestCase):
|
class DockerClientTestCase(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
if os.environ.get('DOCKER_URL'):
|
cls.client = Client(docker_url())
|
||||||
cls.client = Client(os.environ['DOCKER_URL'])
|
|
||||||
else:
|
|
||||||
cls.client = Client()
|
|
||||||
cls.client.pull('ubuntu')
|
cls.client.pull('ubuntu')
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user