From ebc214d6fa8d4cf7f5d13d799851f62b8787a426 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Thu, 12 Feb 2015 15:20:47 +0100 Subject: [PATCH] Fix tests and rename path to iou_path --- gns3server/handlers/iou_handler.py | 4 +-- gns3server/modules/iou/iou_vm.py | 55 ++++++++++++++-------------- gns3server/schemas/iou.py | 10 +++--- tests/api/test_iou.py | 2 +- tests/modules/iou/test_iou_vm.py | 58 ++++++++++++++++-------------- 5 files changed, 68 insertions(+), 61 deletions(-) diff --git a/gns3server/handlers/iou_handler.py b/gns3server/handlers/iou_handler.py index a606efff..ab39622f 100644 --- a/gns3server/handlers/iou_handler.py +++ b/gns3server/handlers/iou_handler.py @@ -50,7 +50,7 @@ class IOUHandler: request.json.get("vm_id"), console=request.json.get("console"), ) - vm.iou_path = request.json.get("iou_path", vm.iou_path) + vm.path = request.json.get("path", vm.path) vm.iourc_path = request.json.get("iourc_path", vm.iourc_path) response.set_status(201) response.json(vm) @@ -97,7 +97,7 @@ class IOUHandler: vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) vm.name = request.json.get("name", vm.name) vm.console = request.json.get("console", vm.console) - vm.iou_path = request.json.get("iou_path", vm.iou_path) + vm.path = request.json.get("path", vm.path) vm.iourc_path = request.json.get("iourc_path", vm.iourc_path) response.json(vm) diff --git a/gns3server/modules/iou/iou_vm.py b/gns3server/modules/iou/iou_vm.py index f243f418..8beb127c 100644 --- a/gns3server/modules/iou/iou_vm.py +++ b/gns3server/modules/iou/iou_vm.py @@ -69,7 +69,7 @@ class IOUVM(BaseVM): self._iou_process = None self._iou_stdout_file = "" self._started = False - self._iou_path = None + self._path = None self._iourc_path = None self._ioucon_thread = None self._console_host = console_host @@ -96,40 +96,40 @@ class IOUVM(BaseVM): self._console = None @property - def iou_path(self): + def path(self): """Path of the iou binary""" - return self._iou_path + return self._path - @iou_path.setter - def iou_path(self, path): + @path.setter + def path(self, path): """ Path of the iou binary :params path: Path to the binary """ - self._iou_path = path - if not os.path.isfile(self._iou_path) or not os.path.exists(self._iou_path): - if os.path.islink(self._iou_path): - raise IOUError("IOU image '{}' linked to '{}' is not accessible".format(self._iou_path, os.path.realpath(self._iou_path))) + self._path = path + if not os.path.isfile(self._path) or not os.path.exists(self._path): + if os.path.islink(self._path): + raise IOUError("IOU image '{}' linked to '{}' is not accessible".format(self._path, os.path.realpath(self._path))) else: - raise IOUError("IOU image '{}' is not accessible".format(self._iou_path)) + raise IOUError("IOU image '{}' is not accessible".format(self._path)) try: - with open(self._iou_path, "rb") as f: + with open(self._path, "rb") as f: # read the first 7 bytes of the file. elf_header_start = f.read(7) except OSError as e: - raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._iou_path, e)) + raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._path, e)) # IOU images must start with the ELF magic number, be 32-bit, little endian # and have an ELF version of 1 normal IOS image are big endian! if elf_header_start != b'\x7fELF\x01\x01\x01': - raise IOUError("'{}' is not a valid IOU image".format(self._iou_path)) + raise IOUError("'{}' is not a valid IOU image".format(self._path)) - if not os.access(self._iou_path, os.X_OK): - raise IOUError("IOU image '{}' is not executable".format(self._iou_path)) + if not os.access(self._path, os.X_OK): + raise IOUError("IOU image '{}' is not executable".format(self._path)) @property def iourc_path(self): @@ -194,7 +194,7 @@ class IOUVM(BaseVM): "console": self._console, "project_id": self.project.id, "iourc_path": self._iourc_path, - "iou_path": self.iou_path + "path": self.path } @property @@ -245,7 +245,7 @@ class IOUVM(BaseVM): """ try: - output = subprocess.check_output(["ldd", self._iou_path]) + output = subprocess.check_output(["ldd", self._path]) except (FileNotFoundError, subprocess.SubprocessError) as e: log.warn("could not determine the shared library dependencies for {}: {}".format(self._path, e)) return @@ -296,8 +296,8 @@ class IOUVM(BaseVM): raise IOUError("could not start IOU: {}: 32-bit binary support is probably not installed".format(e)) except (OSError, subprocess.SubprocessError) as e: iou_stdout = self.read_iou_stdout() - log.error("could not start IOU {}: {}\n{}".format(self._iou_path, e, iou_stdout)) - raise IOUError("could not start IOU {}: {}\n{}".format(self._iou_path, e, iou_stdout)) + log.error("could not start IOU {}: {}\n{}".format(self._path, e, iou_stdout)) + raise IOUError("could not start IOU {}: {}\n{}".format(self._path, e, iou_stdout)) # start console support self._start_ioucon() @@ -412,13 +412,14 @@ class IOUVM(BaseVM): self._iou_process = None - self._terminate_process_iouyap() - try: - yield from asyncio.wait_for(self._iouyap_process.wait(), timeout=3) - except asyncio.TimeoutError: - self._iou_process.kill() - if self._iouyap_process.returncode is None: - log.warn("IOUYAP process {} is still running".format(self._iou_process.pid)) + if self._iouyap_process is not None: + self._terminate_process_iouyap() + try: + yield from asyncio.wait_for(self._iouyap_process.wait(), timeout=3) + except asyncio.TimeoutError: + self._iou_process.kill() + if self._iouyap_process.returncode is None: + log.warn("IOUYAP process {} is still running".format(self._iou_process.pid)) self._started = False @@ -512,7 +513,7 @@ class IOUVM(BaseVM): -N Ignore the NETMAP file """ - command = [self._iou_path] + command = [self._path] if len(self._ethernet_adapters) != 2: command.extend(["-e", str(len(self._ethernet_adapters))]) if len(self._serial_adapters) != 2: diff --git a/gns3server/schemas/iou.py b/gns3server/schemas/iou.py index 562ac0f7..5c3434b6 100644 --- a/gns3server/schemas/iou.py +++ b/gns3server/schemas/iou.py @@ -42,7 +42,7 @@ IOU_CREATE_SCHEMA = { "maximum": 65535, "type": ["integer", "null"] }, - "iou_path": { + "path": { "description": "Path of iou binary", "type": "string" }, @@ -52,7 +52,7 @@ IOU_CREATE_SCHEMA = { }, }, "additionalProperties": False, - "required": ["name", "iou_path", "iourc_path"] + "required": ["name", "path", "iourc_path"] } IOU_UPDATE_SCHEMA = { @@ -71,7 +71,7 @@ IOU_UPDATE_SCHEMA = { "maximum": 65535, "type": ["integer", "null"] }, - "iou_path": { + "path": { "description": "Path of iou binary", "type": "string" }, @@ -113,7 +113,7 @@ IOU_OBJECT_SCHEMA = { "maxLength": 36, "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" }, - "iou_path": { + "path": { "description": "Path of iou binary", "type": "string" }, @@ -123,5 +123,5 @@ IOU_OBJECT_SCHEMA = { }, }, "additionalProperties": False, - "required": ["name", "vm_id", "console", "project_id", "iou_path", "iourc_path"] + "required": ["name", "vm_id", "console", "project_id", "path", "iourc_path"] } diff --git a/tests/api/test_iou.py b/tests/api/test_iou.py index 2e0a6fb7..75188cb3 100644 --- a/tests/api/test_iou.py +++ b/tests/api/test_iou.py @@ -36,7 +36,7 @@ def fake_iou_bin(tmpdir): @pytest.fixture def base_params(tmpdir, fake_iou_bin): """Return standard parameters""" - return {"name": "PC TEST 1", "iou_path": fake_iou_bin, "iourc_path": str(tmpdir / "iourc")} + return {"name": "PC TEST 1", "path": fake_iou_bin, "iourc_path": str(tmpdir / "iourc")} @pytest.fixture diff --git a/tests/modules/iou/test_iou_vm.py b/tests/modules/iou/test_iou_vm.py index 5f5d4093..8f8a0181 100644 --- a/tests/modules/iou/test_iou_vm.py +++ b/tests/modules/iou/test_iou_vm.py @@ -47,8 +47,8 @@ def vm(project, manager, tmpdir, fake_iou_bin): config["iouyap_path"] = fake_file manager.config.set_section_config("IOU", config) - vm.iou_path = fake_iou_bin - vm.iourc = fake_file + vm.path = fake_iou_bin + vm.iourc_path = fake_file return vm @@ -76,11 +76,13 @@ def test_vm_invalid_iouyap_path(project, manager, loop): loop.run_until_complete(asyncio.async(vm.start())) -def test_start(loop, vm): +def test_start(loop, vm, monkeypatch): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True): - with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()): - loop.run_until_complete(asyncio.async(vm.start())) - assert vm.is_running() + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True): + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True): + with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()): + loop.run_until_complete(asyncio.async(vm.start())) + assert vm.is_running() def test_stop(loop, vm): @@ -92,12 +94,14 @@ def test_stop(loop, vm): process.wait.return_value = future with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True): - with asyncio_patch("asyncio.create_subprocess_exec", return_value=process): - loop.run_until_complete(asyncio.async(vm.start())) - assert vm.is_running() - loop.run_until_complete(asyncio.async(vm.stop())) - assert vm.is_running() is False - process.terminate.assert_called_with() + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True): + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True): + with asyncio_patch("asyncio.create_subprocess_exec", return_value=process): + loop.run_until_complete(asyncio.async(vm.start())) + assert vm.is_running() + loop.run_until_complete(asyncio.async(vm.stop())) + assert vm.is_running() is False + process.terminate.assert_called_with() def test_reload(loop, vm, fake_iou_bin): @@ -109,12 +113,14 @@ def test_reload(loop, vm, fake_iou_bin): process.wait.return_value = future with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True): - with asyncio_patch("asyncio.create_subprocess_exec", return_value=process): - loop.run_until_complete(asyncio.async(vm.start())) - assert vm.is_running() - loop.run_until_complete(asyncio.async(vm.reload())) - assert vm.is_running() is True - process.terminate.assert_called_with() + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True): + with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True): + with asyncio_patch("asyncio.create_subprocess_exec", return_value=process): + loop.run_until_complete(asyncio.async(vm.start())) + assert vm.is_running() + loop.run_until_complete(asyncio.async(vm.reload())) + assert vm.is_running() is True + process.terminate.assert_called_with() def test_close(vm, port_manager): @@ -128,23 +134,23 @@ def test_close(vm, port_manager): assert vm.is_running() is False -def test_iou_path(vm, fake_iou_bin): +def test_path(vm, fake_iou_bin): - vm.iou_path = fake_iou_bin - assert vm.iou_path == fake_iou_bin + vm.path = fake_iou_bin + assert vm.path == fake_iou_bin def test_path_invalid_bin(vm, tmpdir): - iou_path = str(tmpdir / "test.bin") + path = str(tmpdir / "test.bin") with pytest.raises(IOUError): - vm.iou_path = iou_path + vm.path = path - with open(iou_path, "w+") as f: + with open(path, "w+") as f: f.write("BUG") with pytest.raises(IOUError): - vm.iou_path = iou_path + vm.path = path def test_create_netmap_config(vm): @@ -161,4 +167,4 @@ def test_create_netmap_config(vm): def test_build_command(vm): - assert vm._build_command() == [vm.iou_path, '-L', str(vm.application_id)] + assert vm._build_command() == [vm.path, '-L', str(vm.application_id)]