mirror of https://github.com/docker/docker-py.git
commit
4bf75ded43
|
@ -574,13 +574,13 @@ class Client(requests.Session):
|
||||||
self._raise_for_status(res)
|
self._raise_for_status(res)
|
||||||
json_ = res.json()
|
json_ = res.json()
|
||||||
s_port = str(private_port)
|
s_port = str(private_port)
|
||||||
f_port = None
|
h_ports = None
|
||||||
if s_port in json_['NetworkSettings']['PortMapping']['Udp']:
|
|
||||||
f_port = json_['NetworkSettings']['PortMapping']['Udp'][s_port]
|
|
||||||
elif s_port in json_['NetworkSettings']['PortMapping']['Tcp']:
|
|
||||||
f_port = json_['NetworkSettings']['PortMapping']['Tcp'][s_port]
|
|
||||||
|
|
||||||
return f_port
|
h_ports = json_['NetworkSettings']['Ports'].get(s_port + '/udp')
|
||||||
|
if h_ports is None:
|
||||||
|
h_ports = json_['NetworkSettings']['Ports'].get(s_port + '/tcp')
|
||||||
|
|
||||||
|
return h_ports
|
||||||
|
|
||||||
def pull(self, repository, tag=None, stream=False):
|
def pull(self, repository, tag=None, stream=False):
|
||||||
registry, repo_name = auth.resolve_repository_name(repository)
|
registry, repo_name = auth.resolve_repository_name(repository)
|
||||||
|
|
|
@ -157,6 +157,34 @@ def get_fake_inspect_image():
|
||||||
return status_code, response
|
return status_code, response
|
||||||
|
|
||||||
|
|
||||||
|
def get_fake_port():
|
||||||
|
status_code = 200
|
||||||
|
response = {
|
||||||
|
'HostConfig': {
|
||||||
|
'Binds': None,
|
||||||
|
'ContainerIDFile': '',
|
||||||
|
'Links': None,
|
||||||
|
'LxcConf': None,
|
||||||
|
'PortBindings': {
|
||||||
|
'1111': None,
|
||||||
|
'1111/tcp': [{'HostIp': '127.0.0.1', 'HostPort': '4567'}],
|
||||||
|
'2222': None
|
||||||
|
},
|
||||||
|
'Privileged': False,
|
||||||
|
'PublishAllPorts': False
|
||||||
|
},
|
||||||
|
'NetworkSettings': {
|
||||||
|
'Bridge': 'docker0',
|
||||||
|
'PortMapping': None,
|
||||||
|
'Ports': {
|
||||||
|
'1111': None,
|
||||||
|
'1111/tcp': [{'HostIp': '127.0.0.1', 'HostPort': '4567'}],
|
||||||
|
'2222': None}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return status_code, response
|
||||||
|
|
||||||
|
|
||||||
def get_fake_insert_image():
|
def get_fake_insert_image():
|
||||||
status_code = 200
|
status_code = 200
|
||||||
response = {'StatusCode': 0}
|
response = {'StatusCode': 0}
|
||||||
|
@ -282,6 +310,8 @@ fake_responses = {
|
||||||
post_fake_stop_container,
|
post_fake_stop_container,
|
||||||
'{1}/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION, prefix):
|
'{1}/{0}/containers/3cc2351ab11b/kill'.format(CURRENT_VERSION, prefix):
|
||||||
post_fake_kill_container,
|
post_fake_kill_container,
|
||||||
|
'{1}/{0}/containers/3cc2351ab11b/json'.format(CURRENT_VERSION, prefix):
|
||||||
|
get_fake_port,
|
||||||
'{1}/{0}/containers/3cc2351ab11b/restart'.format(CURRENT_VERSION, prefix):
|
'{1}/{0}/containers/3cc2351ab11b/restart'.format(CURRENT_VERSION, prefix):
|
||||||
post_fake_restart_container,
|
post_fake_restart_container,
|
||||||
'{1}/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION, prefix):
|
'{1}/{0}/containers/3cc2351ab11b'.format(CURRENT_VERSION, prefix):
|
||||||
|
|
|
@ -422,6 +422,34 @@ class TestKillWithSignal(BaseTestCase):
|
||||||
self.assertEqual(state['Running'], False, state)
|
self.assertEqual(state['Running'], False, state)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPort(BaseTestCase):
|
||||||
|
def runTest(self):
|
||||||
|
|
||||||
|
port_bindings = {
|
||||||
|
1111: ('127.0.0.1', '4567'),
|
||||||
|
2222: ('192.168.0.100', '4568')
|
||||||
|
}
|
||||||
|
|
||||||
|
container = self.client.create_container(
|
||||||
|
'busybox', ['sleep', '60'], ports=port_bindings.keys()
|
||||||
|
)
|
||||||
|
id = container['Id']
|
||||||
|
|
||||||
|
self.client.start(container, port_bindings=port_bindings)
|
||||||
|
|
||||||
|
#Call the port function on each biding and compare expected vs actual
|
||||||
|
for port in port_bindings:
|
||||||
|
actual_bindings = self.client.port(container, port)
|
||||||
|
port_binding = actual_bindings.pop()
|
||||||
|
|
||||||
|
ip, host_port = port_binding['HostIp'], port_binding['HostPort']
|
||||||
|
|
||||||
|
self.assertEqual(ip, port_bindings[port][0])
|
||||||
|
self.assertEqual(host_port, port_bindings[port][1])
|
||||||
|
|
||||||
|
self.client.kill(id)
|
||||||
|
|
||||||
|
|
||||||
class TestRestart(BaseTestCase):
|
class TestRestart(BaseTestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
container = self.client.create_container('busybox', ['sleep', '9999'])
|
container = self.client.create_container('busybox', ['sleep', '9999'])
|
||||||
|
|
|
@ -34,10 +34,6 @@ except ImportError:
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
|
||||||
# FIXME: missing tests for
|
|
||||||
# port;
|
|
||||||
|
|
||||||
|
|
||||||
def response(status_code=200, content='', headers=None, reason=None, elapsed=0,
|
def response(status_code=200, content='', headers=None, reason=None, elapsed=0,
|
||||||
request=None):
|
request=None):
|
||||||
res = requests.Response()
|
res = requests.Response()
|
||||||
|
@ -589,6 +585,17 @@ class DockerClientTest(unittest.TestCase):
|
||||||
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
|
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_port(self):
|
||||||
|
try:
|
||||||
|
self.client.port({'Id': fake_api.FAKE_CONTAINER_ID}, 1111)
|
||||||
|
except Exception as e:
|
||||||
|
self.fail('Command should not raise exception: {0}'.format(e))
|
||||||
|
|
||||||
|
fake_request.assert_called_with(
|
||||||
|
'unix://var/run/docker.sock/v1.6/containers/3cc2351ab11b/json',
|
||||||
|
timeout=docker.client.DEFAULT_TIMEOUT_SECONDS
|
||||||
|
)
|
||||||
|
|
||||||
def test_stop_container(self):
|
def test_stop_container(self):
|
||||||
try:
|
try:
|
||||||
self.client.stop(fake_api.FAKE_CONTAINER_ID, timeout=2)
|
self.client.stop(fake_api.FAKE_CONTAINER_ID, timeout=2)
|
||||||
|
|
Loading…
Reference in New Issue