mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-12 19:38:57 +00:00
Fix tests and rename path to iou_path
This commit is contained in:
parent
fb69c693f6
commit
ebc214d6fa
@ -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)
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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"]
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)]
|
||||
|
Loading…
Reference in New Issue
Block a user