diff --git a/gns3server/controller/__init__.py b/gns3server/controller/__init__.py index 53bd864e..1ec3dba2 100644 --- a/gns3server/controller/__init__.py +++ b/gns3server/controller/__init__.py @@ -74,19 +74,21 @@ class Controller: if self.gns3vm.enable: yield from self.gns3vm.start() self._computes["vm"] = Compute(compute_id="vm", - name="GNS3 VM", - controller=self, - protocol=self.gns3vm.protocol, - host=self.gns3vm.ip_address, - port=self.gns3vm.port, - user=self.gns3vm.user, - password=self.gns3vm.password) + name="GNS3 VM", + controller=self, + protocol=self.gns3vm.protocol, + host=self.gns3vm.ip_address, + port=self.gns3vm.port, + user=self.gns3vm.user, + password=self.gns3vm.password) @asyncio.coroutine def stop(self): log.info("Stop controller") for compute in self._computes.values(): yield from compute.close() + if self.gns3vm.enable and self.gns3vm.auto_stop: + yield from self.gns3vm.stop() self._computes = {} self._projects = {} diff --git a/gns3server/controller/gns3vm/__init__.py b/gns3server/controller/gns3vm/__init__.py index 9aad7b3d..24cc61f0 100644 --- a/gns3server/controller/gns3vm/__init__.py +++ b/gns3server/controller/gns3vm/__init__.py @@ -36,7 +36,7 @@ class GNS3VM: self._engines = {} self._settings = { "vmname": None, - "auto_stop": False, + "auto_stop": True, "headless": False, "enable": False, "engine": "vmware" @@ -120,6 +120,13 @@ class GNS3VM: """ return self._settings["enable"] + @property + def auto_stop(self): + """ + The GNSVM should auto stop + """ + return self._settings["auto_stop"] + @property def settings(self): return self._settings @@ -169,3 +176,12 @@ class GNS3VM: engine.vmname = self._settings["vmname"] yield from engine.start() + @asyncio.coroutine + def stop(self): + """ + Stop the GNS3 VM + """ + engine = self._current_engine() + if not engine.running: + log.info("Stop the GNS3 VM") + yield from engine.stop() diff --git a/tests/controller/test_controller.py b/tests/controller/test_controller.py index 382dbd17..a519d8b4 100644 --- a/tests/controller/test_controller.py +++ b/tests/controller/test_controller.py @@ -255,7 +255,7 @@ def test_getProject(controller, async_run): def test_start(controller, async_run): async_run(controller.start()) - assert len(controller.computes) == 1 # Local compute is created + assert len(controller.computes) == 1 # Local compute is created assert controller.computes["local"].name == socket.gethostname() @@ -270,7 +270,7 @@ def test_start_vm(controller, async_run): with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.start") as mock: async_run(controller.start()) assert mock.called - assert len(controller.computes) == 2 # Local compute and vm are created + assert len(controller.computes) == 2 # Local compute and vm are created assert "local" in controller.computes assert "vm" in controller.computes @@ -282,6 +282,21 @@ def test_stop(controller, async_run): assert c.connected is False +def test_stop_vm(controller, async_run): + """ + Start the controller with a GNS3 VM running + """ + controller.gns3vm.settings = { + "enable": True, + "engine": "vmware", + "auto_stop": True + } + controller.gns3vm.running = True + with asyncio_patch("gns3server.controller.gns3vm.vmware_gns3_vm.VMwareGNS3VM.stop") as mock: + async_run(controller.stop()) + assert mock.called + + def test_load_project(controller, async_run, tmpdir): data = { "name": "Experience",