From 5289c714f46534dbfacf49f0a9f8f8d990f6655f Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Fri, 26 Aug 2016 11:22:09 +0200 Subject: [PATCH] When you delete a compute all project using it are closed Ref #644 --- gns3server/controller/__init__.py | 16 ++++++++------- tests/controller/test_controller.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 1ec3dba2..a17b1d81 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -85,6 +85,8 @@ class Controller: @asyncio.coroutine def stop(self): log.info("Stop controller") + for project in self._projects.values(): + yield from project.close() for compute in self._computes.values(): yield from compute.close() if self.gns3vm.enable and self.gns3vm.auto_stop: @@ -233,12 +235,6 @@ class Controller: self.save() self.notification.emit("compute.created", compute.__json__()) - #FIXME: temporary before the remote GNS3 VM support is back - if "vm" not in self._computes and not hasattr(sys, "_called_from_test"): - compute_vm = Compute(compute_id="vm", controller=self, name="GNS3 VM", **kwargs) - self._computes[compute_vm.id] = compute_vm - self.notification.emit("compute.created", compute_vm.__json__()) - return compute else: self.notification.emit("compute.updated", self._computes[compute_id].__json__()) @@ -247,10 +243,16 @@ class Controller: @asyncio.coroutine def delete_compute(self, compute_id): """ - Delete a compute node + Delete a compute node. Project using this compute will be close + :param compute_id: Compute server identifier """ compute = self.get_compute(compute_id) + + for project in self._projects.values(): + if compute in project.computes: + yield from project.close() + yield from compute.close() del self._computes[compute_id] self.save() diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index a519d8b4..09a0a7a1 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -169,6 +169,37 @@ def test_deleteCompute(controller, controller_config_path, async_run): assert c.connected is False +def test_deleteComputeProjectOpened(controller, controller_config_path, async_run): + """ + When you delete a compute the project using it are close + """ + c = async_run(controller.add_compute(compute_id="test1")) + c.post = AsyncioMagicMock() + assert len(controller.computes) == 1 + + project1 = async_run(controller.add_project(name="Test1")) + async_run(project1.open()) + # We simulate that the project use this compute + project1._project_created_on_compute.add(c) + + project2 = async_run(controller.add_project(name="Test2")) + async_run(project2.open()) + + controller._notification = MagicMock() + c._connected = True + async_run(controller.delete_compute("test1")) + assert len(controller.computes) == 0 + controller._notification.emit.assert_called_with("compute.deleted", c.__json__()) + with open(controller_config_path) as f: + data = json.load(f) + assert len(data["computes"]) == 0 + assert c.connected is False + + # Project 1 use this compute it should be close before deleting the compute + assert project1.status == "closed" + assert project2.status == "opened" + + def test_addComputeConfigFile(controller, controller_config_path, async_run): async_run(controller.add_compute(compute_id="test1", name="Test")) assert len(controller.computes) == 1