Add a new fig command for retrieving the locally bound port of a service.
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
parent
2827786886
commit
c48ee5caef
9 changed files with 164 additions and 51 deletions
|
|
@ -8,18 +8,28 @@ from fig.container import Container
|
|||
|
||||
|
||||
class ContainerTest(unittest.TestCase):
|
||||
|
||||
|
||||
def setUp(self):
|
||||
self.container_dict = {
|
||||
"Id": "abc",
|
||||
"Image": "busybox:latest",
|
||||
"Command": "sleep 300",
|
||||
"Created": 1387384730,
|
||||
"Status": "Up 8 seconds",
|
||||
"Ports": None,
|
||||
"SizeRw": 0,
|
||||
"SizeRootFs": 0,
|
||||
"Names": ["/figtest_db_1"],
|
||||
"NetworkSettings": {
|
||||
"Ports": {},
|
||||
},
|
||||
}
|
||||
|
||||
def test_from_ps(self):
|
||||
container = Container.from_ps(None, {
|
||||
"Id":"abc",
|
||||
"Image":"busybox:latest",
|
||||
"Command":"sleep 300",
|
||||
"Created":1387384730,
|
||||
"Status":"Up 8 seconds",
|
||||
"Ports":None,
|
||||
"SizeRw":0,
|
||||
"SizeRootFs":0,
|
||||
"Names":["/figtest_db_1"]
|
||||
}, has_been_inspected=True)
|
||||
container = Container.from_ps(None,
|
||||
self.container_dict,
|
||||
has_been_inspected=True)
|
||||
self.assertEqual(container.dictionary, {
|
||||
"Id": "abc",
|
||||
"Image":"busybox:latest",
|
||||
|
|
@ -42,35 +52,21 @@ class ContainerTest(unittest.TestCase):
|
|||
})
|
||||
|
||||
def test_number(self):
|
||||
container = Container.from_ps(None, {
|
||||
"Id":"abc",
|
||||
"Image":"busybox:latest",
|
||||
"Command":"sleep 300",
|
||||
"Created":1387384730,
|
||||
"Status":"Up 8 seconds",
|
||||
"Ports":None,
|
||||
"SizeRw":0,
|
||||
"SizeRootFs":0,
|
||||
"Names":["/figtest_db_1"]
|
||||
}, has_been_inspected=True)
|
||||
container = Container.from_ps(None,
|
||||
self.container_dict,
|
||||
has_been_inspected=True)
|
||||
self.assertEqual(container.number, 1)
|
||||
|
||||
def test_name(self):
|
||||
container = Container.from_ps(None, {
|
||||
"Id":"abc",
|
||||
"Image":"busybox:latest",
|
||||
"Command":"sleep 300",
|
||||
"Names":["/figtest_db_1"]
|
||||
}, has_been_inspected=True)
|
||||
container = Container.from_ps(None,
|
||||
self.container_dict,
|
||||
has_been_inspected=True)
|
||||
self.assertEqual(container.name, "figtest_db_1")
|
||||
|
||||
def test_name_without_project(self):
|
||||
container = Container.from_ps(None, {
|
||||
"Id":"abc",
|
||||
"Image":"busybox:latest",
|
||||
"Command":"sleep 300",
|
||||
"Names":["/figtest_db_1"]
|
||||
}, has_been_inspected=True)
|
||||
container = Container.from_ps(None,
|
||||
self.container_dict,
|
||||
has_been_inspected=True)
|
||||
self.assertEqual(container.name_without_project, "db_1")
|
||||
|
||||
def test_inspect_if_not_inspected(self):
|
||||
|
|
@ -85,3 +81,27 @@ class ContainerTest(unittest.TestCase):
|
|||
|
||||
container.inspect_if_not_inspected()
|
||||
self.assertEqual(mock_client.inspect_container.call_count, 1)
|
||||
|
||||
def test_human_readable_ports_none(self):
|
||||
container = Container(None, self.container_dict, has_been_inspected=True)
|
||||
self.assertEqual(container.human_readable_ports, '')
|
||||
|
||||
def test_human_readable_ports_public_and_private(self):
|
||||
self.container_dict['NetworkSettings']['Ports'].update({
|
||||
"45454/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "49197" } ],
|
||||
"45453/tcp": [],
|
||||
})
|
||||
container = Container(None, self.container_dict, has_been_inspected=True)
|
||||
|
||||
expected = "45453/tcp, 0.0.0.0:49197->45454/tcp"
|
||||
self.assertEqual(container.human_readable_ports, expected)
|
||||
|
||||
def test_get_local_port(self):
|
||||
self.container_dict['NetworkSettings']['Ports'].update({
|
||||
"45454/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "49197" } ],
|
||||
})
|
||||
container = Container(None, self.container_dict, has_been_inspected=True)
|
||||
|
||||
self.assertEqual(
|
||||
container.get_local_port(45454, protocol='tcp'),
|
||||
'0.0.0.0:49197')
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import os
|
|||
from .. import unittest
|
||||
import mock
|
||||
|
||||
from fig.packages import docker
|
||||
|
||||
from fig import Service
|
||||
from fig.service import (
|
||||
ConfigError,
|
||||
|
|
@ -97,14 +99,33 @@ class ServiceTest(unittest.TestCase):
|
|||
|
||||
def test_split_domainname_weird(self):
|
||||
service = Service('foo',
|
||||
hostname = 'name.sub',
|
||||
domainname = 'domain.tld',
|
||||
hostname='name.sub',
|
||||
domainname='domain.tld',
|
||||
)
|
||||
service.next_container_name = lambda x: 'foo'
|
||||
opts = service._get_container_create_options({})
|
||||
self.assertEqual(opts['hostname'], 'name.sub', 'hostname')
|
||||
self.assertEqual(opts['domainname'], 'domain.tld', 'domainname')
|
||||
|
||||
def test_get_container_not_found(self):
|
||||
mock_client = mock.create_autospec(docker.Client)
|
||||
mock_client.containers.return_value = []
|
||||
service = Service('foo', client=mock_client)
|
||||
|
||||
self.assertRaises(ValueError, service.get_container)
|
||||
|
||||
@mock.patch('fig.service.Container', autospec=True)
|
||||
def test_get_container(self, mock_container_class):
|
||||
mock_client = mock.create_autospec(docker.Client)
|
||||
container_dict = dict(Name='default_foo_2')
|
||||
mock_client.containers.return_value = [container_dict]
|
||||
service = Service('foo', client=mock_client)
|
||||
|
||||
container = service.get_container(number=2)
|
||||
self.assertEqual(container, mock_container_class.from_ps.return_value)
|
||||
mock_container_class.from_ps.assert_called_once_with(
|
||||
mock_client, container_dict)
|
||||
|
||||
|
||||
class ServiceVolumesTest(unittest.TestCase):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue