From e0c5fdcf1c9168ddcf7f840915c679e9a6099301 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 30 Aug 2016 09:58:37 +0200 Subject: [PATCH] Avoid too much ressources usage during tests --- gns3server/controller/__init__.py | 9 ++++++--- gns3server/controller/compute.py | 11 +++++++---- tests/controller/test_controller.py | 22 +++++++++++----------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 00fbaaa1..abc7ebc9 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -208,13 +208,14 @@ class Controller: return Config.instance().get_section_config("Server").getboolean("controller") @asyncio.coroutine - def add_compute(self, compute_id=None, name=None, force=False, **kwargs): + def add_compute(self, compute_id=None, name=None, force=False, connect=True, **kwargs): """ Add a server to the dictionary of compute servers controlled by this controller :param compute_id: Compute server identifier :param name: Compute name :param force: True skip security check + :param connect: True connect to the compute immediately :param kwargs: See the documentation of Compute """ if compute_id not in self._computes: @@ -230,11 +231,13 @@ class Controller: compute = Compute(compute_id=compute_id, controller=self, name=name, **kwargs) self._computes[compute.id] = compute self.save() - yield from compute.connect() + if connect: + yield from compute.connect() self.notification.emit("compute.created", compute.__json__()) return compute else: - yield from self._computes[compute_id].connect() + if connect: + yield from self._computes[compute_id].connect() self.notification.emit("compute.updated", self._computes[compute_id].__json__()) return self._computes[compute_id] diff --git a/gns3server/controller/compute.py b/gns3server/controller/compute.py index 5be1d6d8..701eab96 100644 --- a/gns3server/controller/compute.py +++ b/gns3server/controller/compute.py @@ -21,6 +21,7 @@ import asyncio import socket import json import uuid +import sys import os import io @@ -348,8 +349,9 @@ class Compute: try: response = yield from self._run_http_query("GET", "/capabilities") except (aiohttp.errors.ClientOSError, aiohttp.errors.ClientRequestError, aiohttp.ClientResponseError): - # Try to reconnect after 2 seconds if server unavailable - asyncio.get_event_loop().call_later(2, lambda: asyncio.async(self.connect())) + # Try to reconnect after 2 seconds if server unavailable only if not during tests (otherwise we create a ressources usage bomb) + if not hasattr(sys, "_called_from_test") or not sys._called_from_test: + asyncio.get_event_loop().call_later(2, lambda: asyncio.async(self.connect())) return if "version" not in response.json: @@ -392,8 +394,9 @@ class Compute: if self._ws: yield from self._ws.close() - # Try to reconnect after 1 seconds if server unavailable - asyncio.get_event_loop().call_later(1, lambda: asyncio.async(self.connect())) + # Try to reconnect after 1 seconds if server unavailable only if not during tests (otherwise we create a ressources usage bomb) + if not hasattr(sys, "_called_from_test") or not sys._called_from_test: + asyncio.get_event_loop().call_later(1, lambda: asyncio.async(self.connect())) self._ws = None self._cpu_usage_percent = None self._memory_usage_percent = None diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 7109b4db..3d7a96c6 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -141,26 +141,26 @@ def test_isEnabled(controller): def test_add_compute(controller, controller_config_path, async_run): controller._notification = MagicMock() - c = async_run(controller.add_compute(compute_id="test1")) + c = async_run(controller.add_compute(compute_id="test1", connect=False)) controller._notification.emit.assert_called_with("compute.created", c.__json__()) assert len(controller.computes) == 1 - async_run(controller.add_compute(compute_id="test1")) + async_run(controller.add_compute(compute_id="test1", connect=False)) controller._notification.emit.assert_called_with("compute.updated", c.__json__()) assert len(controller.computes) == 1 - async_run(controller.add_compute(compute_id="test2")) + async_run(controller.add_compute(compute_id="test2", connect=False)) assert len(controller.computes) == 2 def test_addDuplicateCompute(controller, controller_config_path, async_run): controller._notification = MagicMock() - c = async_run(controller.add_compute(compute_id="test1", name="Test")) + c = async_run(controller.add_compute(compute_id="test1", name="Test", connect=False)) assert len(controller.computes) == 1 with pytest.raises(aiohttp.web.HTTPConflict): - async_run(controller.add_compute(compute_id="test2", name="Test")) + async_run(controller.add_compute(compute_id="test2", name="Test", connect=False)) def test_deleteCompute(controller, controller_config_path, async_run): - c = async_run(controller.add_compute(compute_id="test1")) + c = async_run(controller.add_compute(compute_id="test1", connect=False)) assert len(controller.computes) == 1 controller._notification = MagicMock() c._connected = True @@ -177,7 +177,7 @@ def test_deleteComputeProjectOpened(controller, controller_config_path, async_ru """ When you delete a compute the project using it are close """ - c = async_run(controller.add_compute(compute_id="test1")) + c = async_run(controller.add_compute(compute_id="test1", connect=False)) c.post = AsyncioMagicMock() assert len(controller.computes) == 1 @@ -205,7 +205,7 @@ def test_deleteComputeProjectOpened(controller, controller_config_path, async_ru def test_addComputeConfigFile(controller, controller_config_path, async_run): - async_run(controller.add_compute(compute_id="test1", name="Test")) + async_run(controller.add_compute(compute_id="test1", name="Test", connect=False)) assert len(controller.computes) == 1 with open(controller_config_path) as f: data = json.load(f) @@ -223,7 +223,7 @@ def test_addComputeConfigFile(controller, controller_config_path, async_run): def test_getCompute(controller, async_run): - compute = async_run(controller.add_compute(compute_id="test1")) + compute = async_run(controller.add_compute(compute_id="test1", connect=False)) assert controller.get_compute("test1") == compute with pytest.raises(aiohttp.web.HTTPNotFound): @@ -231,7 +231,7 @@ def test_getCompute(controller, async_run): def test_has_compute(controller, async_run): - compute = async_run(controller.add_compute(compute_id="test1")) + compute = async_run(controller.add_compute(compute_id="test1", connect=False)) assert controller.has_compute("test1") assert not controller.has_compute("test2") @@ -311,7 +311,7 @@ def test_start_vm(controller, async_run): def test_stop(controller, async_run): - c = async_run(controller.add_compute(compute_id="test1")) + c = async_run(controller.add_compute(compute_id="test1", connect=False)) c._connected = True async_run(controller.stop()) assert c.connected is False