Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to use virtual docker client in some tests #153

Merged
merged 2 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions tests/cli/test_remoteappdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from remoteappmanager.cli.remoteappdb import __main__ as remoteappdb
from tests.temp_mixin import TempMixin
from tests.utils import mock_docker_client
from tests.mocking.virtual.docker_client import create_docker_client


class TestRemoteAppDbCLI(TempMixin, unittest.TestCase):
Expand Down Expand Up @@ -54,19 +54,19 @@ def test_user_create(self):
self.assertIn("foo", out)
self.assertIn("bar", out)

@mock.patch('remoteappmanager.cli.remoteappdb.__main__.get_docker_client', # noqa
create_docker_client)
def test_app_create_with_verify(self):
with mock.patch('remoteappmanager.cli.remoteappdb.__main__.get_docker_client', # noqa
mock_docker_client):
# docker.client.inspect_image is mocked to always return
# something, so verification would pass
exit_code, output = self._remoteappdb("app create anything")
exit_code, output = self._remoteappdb("app create image_id1")

self.assertEqual(exit_code, 0)
self.assertEqual(exit_code, 0)

# Check that the app is created
exit_code, output = self._remoteappdb("app list")
self.assertIn('anything', output)
# Check that the app is created
exit_code, output = self._remoteappdb("app list")
self.assertIn('image_id1', output)

@mock.patch('remoteappmanager.cli.remoteappdb.__main__.get_docker_client', # noqa
create_docker_client)
def test_app_create_wrong_name_with_verify(self):
# create an application with a wrong image name
exit_code, output = self._remoteappdb('app create wrong')
Expand All @@ -77,6 +77,8 @@ def test_app_create_wrong_name_with_verify(self):
exit_code, output = self._remoteappdb("app list")
self.assertNotIn('wrong', output)

@mock.patch('remoteappmanager.cli.remoteappdb.__main__.get_docker_client', # noqa
create_docker_client)
def test_app_create_wrong_name_without_verify(self):
# create an application with a wrong image name
exit_code, output = self._remoteappdb("app create wrong2 --no-verify")
Expand Down
4 changes: 2 additions & 2 deletions tests/docker/test_async_docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from docker.utils import kwargs_from_env

from remoteappmanager.docker.async_docker_client import AsyncDockerClient
from tests import utils
from tests.mocking.virtual.docker_client import create_docker_client


class TestAsyncDockerClient(AsyncTestCase):
Expand All @@ -26,7 +26,7 @@ def tearDown(self):
@gen_test
def test_info(self):
client = AsyncDockerClient()
client._sync_client = utils.mock_docker_client()
client._sync_client = create_docker_client()
response = yield client.info()
# Test contents of response
self.assertIsInstance(response, dict)
Expand Down
75 changes: 38 additions & 37 deletions tests/docker/test_container.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from unittest import TestCase

from remoteappmanager.docker.container import Container
from tests.utils import assert_containers_equal, \
mock_docker_client_with_running_containers
from tests.utils import assert_containers_equal
from tests.mocking.virtual.docker_client import create_docker_client


class TestContainer(TestCase):
Expand Down Expand Up @@ -57,60 +57,61 @@ def test_from_docker_dict_with_public_port(self):

def test_from_docker_dict_without_public_port(self):
'''Test convertion from "docker ps" to Container with public port'''
# With public port
container_dict = {
'Command': '/startup.sh',
'Created': 1466756760,
'HostConfig': {'NetworkMode': 'default'},
'Id': '812c765d0549be0ab831ae8348',
'Image': 'novnc-ubuntu:latest',
'ImageID': 'sha256:f4610c75d3c0dfa25d3c0dfa25d3c0dfa2',
'Labels': {'eu.simphony-project.docker.ui_name': 'Empty Ubuntu',
'eu.simphony-project.docker.user': 'user',
'eu.simphony-project.docker.url_id': "8e2fe66d5de74db9bbab50c0d2f92b33"}, # noqa
'Names': ['/remoteexec-user-empty-ubuntu_3Alatest'],
'Ports': [{'IP': '0.0.0.0',
'PrivatePort': 8888,
'Type': 'tcp'}],
'State': 'running',
'Status': 'Up 56 minutes'}
client = create_docker_client(container_ids=('container_id1',),
container_names=('container_name1',),
container_ports=([{'IP': '0.0.0.0',
'PrivatePort': 8888,
'Type': 'tcp'}],))
container_dict = client.containers()[0]

# Container without public port
actual = Container.from_docker_dict(container_dict)
expected = Container(
docker_id='812c765d0549be0ab831ae8348',
name='/remoteexec-user-empty-ubuntu_3Alatest',
image_name='novnc-ubuntu:latest',
image_id='sha256:f4610c75d3c0dfa25d3c0dfa25d3c0dfa2',
docker_id='container_id1',
name='/container_name1',
image_name='image_name1',
image_id='image_id1',
ip='0.0.0.0',
port=80,
url_id="8e2fe66d5de74db9bbab50c0d2f92b33")
url_id="url_id",
mapping_id="mapping_id")

assert_containers_equal(self, actual, expected)

def test_from_docker_dict_inspect_container(self):
client = mock_docker_client_with_running_containers()
client = create_docker_client()
actual = Container.from_docker_dict(
client.inspect_container("id"))
client.inspect_container('container_id1'))

expected = Container(
docker_id='35d88fe321c3d575ec3be64f54b8967ef49c0dc92395bf4c1e511ed3e6ae0c79', # noqa
name='/remoteexec-username-simphonyproject_2Fsimphonic-mayavi_5F1',
image_name='simphonyproject/simphonic-mayavi',
image_id='sha256:f43b749341ee37b56e7bd8d99f09629f311aaec35a8045a39185b5659edef169', # noqa
docker_id='container_id1',
name='/remoteexec-username-mapping_5Fid',
image_name='image_name1',
image_id='image_id1',
ip='0.0.0.0',
port=32782,
url_id="55555555555555555555555555555555",
mapping_id="1c08c87878634e90af43d799e90f61d2")
port=666,
url_id="url_id",
mapping_id="mapping_id")

assert_containers_equal(self, actual, expected)

def test_multiple_ports_data(self):
client = mock_docker_client_with_running_containers()
docker_dict = client.inspect_container("id")
client = create_docker_client(container_names=('container_name1'),
container_ids=('container_id1'),
container_ports=(
[{'IP': '0.0.0.0',
'PublicPort': 666,
'PrivatePort': 8888,
'Type': 'tcp'},
{'IP': '0.0.0.0',
'PublicPort': 667,
'PrivatePort': 8889,
'Type': 'tcp'}],))

docker_dict = client.inspect_container("container_id1")
docker_dict["NetworkSettings"]["Ports"] = {
'8888/tcp': [{'HostIp': '0.0.0.0', 'HostPort': '32782'}],
'8889/tcp': [{'HostIp': '0.0.0.0', 'HostPort': '32783'}]
'8888/tcp': [{'HostIp': '0.0.0.0', 'HostPort': '666'}],
'8889/tcp': [{'HostIp': '0.0.0.0', 'HostPort': '667'}]
}
with self.assertRaises(ValueError):
Container.from_docker_dict(docker_dict)
Expand Down
2 changes: 1 addition & 1 deletion tests/docker/test_container_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_start_stop(self):
@gen_test
def test_containers_from_mapping_id(self):
''' Test containers_for_mapping_id returns a list of Container '''
result = yield self.manager.containers_from_mapping_id("username",
result = yield self.manager.containers_from_mapping_id("user_name",
"mapping_id")
expected = Container(docker_id='container_id1',
mapping_id="mapping_id",
Expand Down
8 changes: 4 additions & 4 deletions tests/docker/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from remoteappmanager.docker import docker_labels
from remoteappmanager.docker.image import Image
from tests.utils import mock_docker_client
from tests.mocking.virtual.docker_client import create_docker_client


class TestImage(TestCase):
def test_from_docker_dict_images(self):
docker_client = mock_docker_client()
docker_client = create_docker_client()
image_dict = docker_client.images()[0]
image = Image.from_docker_dict(image_dict)

Expand All @@ -19,8 +19,8 @@ def test_from_docker_dict_images(self):
image_dict["Labels"][docker_labels.UI_NAME])

def test_from_docker_dict_inspect_image(self):
docker_client = mock_docker_client()
image_dict = docker_client.inspect_image()
docker_client = create_docker_client()
image_dict = docker_client.inspect_image('image_id1')
image = Image.from_docker_dict(image_dict)

self.assertEqual(image.docker_id, image_dict["Id"])
Expand Down
39 changes: 26 additions & 13 deletions tests/mocking/virtual/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def get_fake_image_labels(num=2):
def get_fake_container_ports(num=3):
samples = cycle(([{'IP': '0.0.0.0',
'PublicPort': 666,
'PrivatePort': 22,
'PrivatePort': 8888,
'Type': 'tcp'}],
[{'PrivatePort': 22,
[{'PrivatePort': 8889,
'Type': 'tcp'}],
[]))
return tuple(next(samples) for _ in range(num))
Expand Down Expand Up @@ -87,11 +87,16 @@ def containers(**kwargs):
all_labels.update(labels)

# Apply filters for labels
if 'filters' in kwargs and 'labels' in kwargs['filters']:
label_filters = kwargs['filters']['labels']
if 'filters' in kwargs and 'label' in kwargs['filters']:
label_filters = kwargs['filters']['label']

if not isinstance(label_filters, (list, tuple)):
label_filters = [label_filters]

label_filters = (label.split('=') for label in label_filters)
if any(label_name not in all_labels or
all_labels[label_name] != label_value
for label_name, label_value in label_filters.items()):
for label_name, label_value in label_filters):
continue

results.append(
Expand Down Expand Up @@ -133,20 +138,28 @@ def inspect_container(container_name_or_id):
all_labels = image_labels.copy()
all_labels.update(labels)

network_settings = {}

if container_port:
host_ip = container_port[0].get('IP', '')
host_port = container_port[0].get('PublicPort', '')
else:
host_ip = host_port = ''
network_settings['Ports'] = {}

for port_settings in container_port:
target = '{}/{}'.format(port_settings.get('PrivatePort', ''),
port_settings.get('Type', 'tcp'))
host_ip = port_settings.get('IP', '')
host_port = port_settings.get('PublicPort', '')
network_settings['Ports'][target] = [
{'HostIp': str(host_ip),
'HostPort': str(host_port)}]

return {'State': container_state,
'Name': '/'+container_name,
'Image': image_id,
'Image': image_name,
'Labels': all_labels,
'Config': {
'Image': image_name,
'Labels': all_labels},
'Id': container_id,
'Ports': {'8888/tcp': [
{'HostIp': host_ip, 'HostPort': host_port}]}
'NetworkSettings': network_settings
}

return inspect_container
Expand Down
Loading