Commit dd8b4f39 authored by Samuel GAIST's avatar Samuel GAIST

[dock] Implement support for custom network

Also refactor related tests to avoid spending time discovering
environments when the test doesn't need it.
parent b29a3b75
......@@ -335,6 +335,9 @@ class Host(object):
'-tid',
]
network = container.network
if network:
cmd.append(network)
if container.image in Host.images_cache:
image_infos = Host.images_cache[container.image]
if ('capabilities' in image_infos) and ('gpu' in image_infos['capabilities']):
......@@ -588,6 +591,7 @@ class Container:
def __init__(self, image, command):
self.image = image
self.command = command
self.network_name = None
self.id = None
self._volumes = {}
self._ports = {}
......@@ -673,6 +677,15 @@ class Container:
else:
ports.append('%d:%d' % (v[0], k))
return ports
@property
def network(self):
network = ''
if self.network_name:
network = '--network=' + self.network_name
return network
@property
def command_line(self):
"""Returns the complete docker command to start the container and
execute the specified command.
......@@ -682,6 +695,7 @@ class Container:
"""
cmd = "docker run -ti --rm=true "
cmd += "%s " % self.network
cmd += ' '.join(self.volumes)
cmd += ' '.join(self.ports)
......
......@@ -38,18 +38,22 @@ import time
import docker
import requests
import nose
from ..dock import Host
from . import tmp_prefix
from .utils import slow
from .utils import id_generator
class AsyncTest(unittest.TestCase):
class NoDiscoveryTests(unittest.TestCase):
"""Test cases that don't require the discovery of database and runtime
environments.
"""
@classmethod
def setUpClass(cls):
cls.host = Host(raise_on_errors=False)
cls.test_environment = cls.host.full_environment_name('Python 2.7')
cls.host = Host(raise_on_errors=False, discover=False)
@classmethod
......@@ -62,6 +66,52 @@ class AsyncTest(unittest.TestCase):
assert not self.host.containers # All containers are gone
class NetworkTest(NoDiscoveryTests):
@slow
def test_network(self):
network_name = 'beat_core_test_docker_' + id_generator()
ipam_pool = docker.types.IPAMPool(subnet='193.169.52.0/24',
gateway='193.169.52.254')
ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
client = docker.from_env()
network = client.networks.create(network_name,
driver="bridge",
ipam=ipam_config)
string = "hello world"
container = self.host.create_container('debian:8.4', ["echo", string])
container.network_name = network_name
try:
self.host.start(container)
status = self.host.wait(container)
except Exception as e:
network.remove()
raise
self.assertEqual(status, 0)
self.assertEqual(self.host.logs(container), string + '\n')
network.remove()
@slow
def test_non_existing_network(self):
string = "hello world"
network_name = 'beat.core.fake'
container = self.host.create_container('debian:8.4', ["echo", string])
container.network_name = network_name
try:
self.host.start(container)
except RuntimeError as e:
self.assertTrue(str(e).find('network %s not found' % network_name) >= 0)
class AsyncTest(NoDiscoveryTests):
@slow
def test_echo(self):
......@@ -74,7 +124,6 @@ class AsyncTest(unittest.TestCase):
self.assertEqual(status, 0)
self.assertEqual(self.host.logs(container), string + '\n')
@slow
def test_non_existing(self):
......@@ -123,6 +172,23 @@ class AsyncTest(unittest.TestCase):
self.assertEqual(self.host.logs(container), '')
class AsyncWithEnvironmentTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.host = Host(raise_on_errors=False)
cls.test_environment = cls.host.full_environment_name('Python 2.7')
@classmethod
def tearDownClass(cls):
cls.host.teardown()
def tearDown(self):
self.host.teardown()
assert not self.host.containers # All containers are gone
@slow
def test_memory_limit(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment