Commit 5527c8ee authored by Samuel GAIST's avatar Samuel GAIST

[test][docker_execution] Refactor test for custom network

parent 16f29f77
......@@ -35,6 +35,7 @@ import shutil
import tempfile
import pkg_resources
import logging
import docker
if sys.platform == 'darwin':
tmp_prefix = tempfile.mkdtemp(prefix=__name__,
......@@ -51,7 +52,8 @@ else:
prefix = os.path.join(prefix_folder, 'prefix')
network_name = os.environ.get('DOCKER_TEST_NETWORK', 'beat_core_test_network')
network = None
# Setup the logging system
if False:
......@@ -80,9 +82,26 @@ def setup_package():
for path in prefixes:
subprocess.check_call(['rsync', '-arz', path, prefix_folder])
client = docker.from_env()
try:
network = client.networks.get(network_name)
except docker.errors.NotFound:
subnet = os.environ.get('DOCKER_TEST_SUBNET', '193.169.0.0/24')
gateway = os.environ.get('DOCKER_TEST_GATEWAY', '193.169.0.254')
ipam_pool = docker.types.IPAMPool(subnet=subnet,
gateway=gateway)
ipam_config = docker.types.IPAMConfig(pool_configs=[ipam_pool])
network = client.networks.create(network_name,
driver="bridge",
ipam=ipam_config)
def teardown_package():
if os.path.exists(tmp_prefix):
shutil.rmtree(tmp_prefix)
shutil.rmtree(prefix_folder)
if network:
network.remove()
......@@ -36,15 +36,13 @@ import unittest
import pkg_resources
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
from . import network_name
class NoDiscoveryTests(unittest.TestCase):
"""Test cases that don't require the discovery of database and runtime
......@@ -70,17 +68,6 @@ 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
......@@ -95,8 +82,6 @@ class NetworkTest(NoDiscoveryTests):
self.assertEqual(status, 0)
self.assertEqual(self.host.logs(container), string + '\n')
network.remove()
@slow
def test_non_existing_network(self):
......
......@@ -32,8 +32,11 @@ from ..dock import Host
from ..execution import DockerExecutor
from .utils import cleanup
from .utils import slow
from .test_execution import BaseExecutionMixIn
from . import network_name
#----------------------------------------------------------
......@@ -60,6 +63,12 @@ class TestDockerExecution(BaseExecutionMixIn):
return DockerExecutor(self.host, prefix, configuration, tmp_prefix,
dataformat_cache, database_cache, algorithm_cache)
@slow
def test_custom_network(self):
result = self.execute('user/user/integers_addition/1/integers_addition',
[{'sum': 495, 'nb': 9}], network_name=network_name)
assert result is None
# NOT COMPATIBLE YET WITH THE NEW API
# @slow
......
......@@ -34,7 +34,7 @@ import string
import random
import nose
import docker
#----------------------------------------------------------
......@@ -173,3 +173,21 @@ def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
https://stackoverflow.com/a/2257449/5843716
"""
return ''.join(random.choice(chars) for _ in range(size))
#----------------------------------------------------------
def create_network(network_name):
""" Create a docker network with the given name"""
ipam_pool = docker.types.IPAMPool(subnet='193.169.0.0/24',
gateway='193.169.0.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)
return network
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