1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-29 02:08:10 +00:00

API in order to get the list of IOU, Dynamips, Qemu images

This commit is contained in:
Julien Duponchelle 2015-04-13 14:33:13 +02:00
parent 5a4ffae6a2
commit bca90bc563
14 changed files with 218 additions and 36 deletions

View File

@ -25,6 +25,7 @@ from ...schemas.dynamips_vm import VM_CAPTURE_SCHEMA
from ...schemas.dynamips_vm import VM_OBJECT_SCHEMA
from ...schemas.dynamips_vm import VM_NIO_SCHEMA
from ...schemas.dynamips_vm import VM_CONFIGS_SCHEMA
from ...schemas.dynamips_vm import VMS_LIST_SCHEMA
from ...modules.dynamips import Dynamips
from ...modules.project_manager import ProjectManager
@ -421,3 +422,17 @@ class DynamipsVMHandler:
idlepc = yield from dynamips_manager.auto_idlepc(vm)
response.set_status(200)
response.json({"idlepc": idlepc})
@Route.get(
r"/dynamips/vms",
status_codes={
200: "List of Dynamips VM retrieved",
},
description="Retrieve the list of Dynamips VMS",
output=VMS_LIST_SCHEMA)
def list_vms(request, response):
dynamips_manager = Dynamips.instance()
vms = yield from dynamips_manager.list_images()
response.set_status(200)
response.json(vms)

View File

@ -25,6 +25,7 @@ from ...schemas.iou import IOU_OBJECT_SCHEMA
from ...schemas.iou import IOU_NIO_SCHEMA
from ...schemas.iou import IOU_CAPTURE_SCHEMA
from ...schemas.iou import IOU_INITIAL_CONFIG_SCHEMA
from ...schemas.iou import IOU_LIST_VMS_SCHEMA
from ...modules.iou import IOU
@ -310,3 +311,17 @@ class IOUHandler:
project_id=request.match_info["project_id"])
response.set_status(200)
response.json({"content": vm.initial_config})
@Route.get(
r"/iou/vms",
status_codes={
200: "List of IOU VM retrieved",
},
description="Retrieve the list of IOU VMS",
output=IOU_LIST_VMS_SCHEMA)
def list_vms(request, response):
iou_manager = IOU.instance()
vms = yield from iou_manager.list_images()
response.set_status(200)
response.json(vms)

View File

@ -21,6 +21,7 @@ from ...schemas.qemu import QEMU_UPDATE_SCHEMA
from ...schemas.qemu import QEMU_OBJECT_SCHEMA
from ...schemas.qemu import QEMU_NIO_SCHEMA
from ...schemas.qemu import QEMU_BINARY_LIST_SCHEMA
from ...schemas.qemu import QEMU_LIST_IMAGES_SCHEMA
from ...modules.qemu import Qemu
@ -286,3 +287,17 @@ class QEMUHandler:
binaries = yield from Qemu.binary_list()
response.json(binaries)
@Route.get(
r"/qemu/vms",
status_codes={
200: "List of Qemu images retrieved",
},
description="Retrieve the list of Qemu images",
output=QEMU_LIST_IMAGES_SCHEMA)
def list_vms(request, response):
qemu_manager = Qemu.instance()
vms = yield from qemu_manager.list_images()
response.set_status(200)
response.json(vms)

View File

@ -42,7 +42,7 @@ class VirtualBoxHandler:
def show(request, response):
vbox_manager = VirtualBox.instance()
vms = yield from vbox_manager.get_list()
vms = yield from vbox_manager.list_images()
response.json(vms)
@classmethod

View File

@ -403,6 +403,25 @@ class BaseManager:
return os.path.basename(path)
return path
@asyncio.coroutine
def list_images(self):
"""
Return the list of available images for this VM type
:returns: Array of hash
"""
try:
files = os.listdir(self.get_images_directory())
except FileNotFoundError:
return []
files.sort()
images = []
for filename in files:
if filename[0] != ".":
images.append({"filename": filename})
return images
def get_images_directory(self):
"""
Get the image directory on disk

View File

@ -117,7 +117,7 @@ class VirtualBox(BaseManager):
return stdout_data.decode("utf-8", errors="ignore").splitlines()
@asyncio.coroutine
def get_list(self):
def list_images(self):
"""
Gets VirtualBox VM list.
"""

View File

@ -898,3 +898,21 @@ VM_CONFIGS_SCHEMA = {
"additionalProperties": False,
"required": ["startup_config_content", "private_config_content"]
}
VMS_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "List available Dynamips images",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"filename": {
"description": "Image filename",
"type": ["string"]
},
},
}
],
"additionalProperties": False,
}

View File

@ -323,3 +323,21 @@ IOU_INITIAL_CONFIG_SCHEMA = {
"additionalProperties": False,
"required": ["content"]
}
IOU_LIST_VMS_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "List available IOU images",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"filename": {
"description": "Image filename",
"type": ["string"]
},
},
}
],
"additionalProperties": False,
}

View File

@ -397,3 +397,21 @@ QEMU_BINARY_LIST_SCHEMA = {
},
"additionalProperties": False,
}
QEMU_LIST_IMAGES_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "List available QEMU images",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"filename": {
"description": "Image filename",
"type": ["string"]
},
},
}
],
"additionalProperties": False,
}

View File

@ -16,6 +16,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import os
import stat
from unittest.mock import patch
from tests.utils import asyncio_patch
@ -123,3 +127,22 @@ from tests.utils import asyncio_patch
# assert response.status == 200
# assert response.json["name"] == "test"
# assert response.json["console"] == free_console_port
@pytest.fixture
def fake_dynamips(tmpdir):
"""Create a fake IOU image on disk"""
path = str(tmpdir / "7200.bin")
with open(path, "w+") as f:
f.write('1')
os.chmod(path, stat.S_IREAD)
return path
def test_vms(server, tmpdir, fake_dynamips):
with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir), example=True):
response = server.get("/dynamips/vms")
assert response.status == 200
assert response.json == [{"filename": "7200.bin"}]

View File

@ -284,3 +284,11 @@ def test_get_initial_config_with_config_file(server, project, vm):
response = server.get("/projects/{project_id}/iou/vms/{vm_id}/initial_config".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.json["content"] == "TEST"
def test_vms(server, vm, tmpdir, fake_iou_bin):
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir), example=True):
response = server.get("/iou/vms")
assert response.status == 200
assert response.json == [{"filename": "iou.bin"}]

View File

@ -32,12 +32,32 @@ def fake_qemu_bin():
return bin_path
@pytest.fixture
def fake_qemu_vm(tmpdir):
bin_path = os.path.join(str(tmpdir / "linux.img"))
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
return bin_path
@pytest.fixture
def base_params(tmpdir, fake_qemu_bin):
"""Return standard parameters"""
return {"name": "PC TEST 1", "qemu_path": fake_qemu_bin}
@pytest.fixture
def fake_qemu_bin():
bin_path = os.path.join(os.environ["PATH"], "qemu_x42")
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
return bin_path
@pytest.fixture
def vm(server, project, base_params):
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
@ -175,3 +195,11 @@ def test_qemu_list_binaries(server, vm):
assert mock.called
assert response.status == 200
assert response.json == ret
def test_vms(server, tmpdir, fake_qemu_vm):
with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir), example=True):
response = server.get("/qemu/vms")
assert response.status == 200
assert response.json == [{"filename": "linux.img"}]

View File

@ -36,11 +36,7 @@ def iou(port_manager):
return iou
def test_get_application_id(loop, project, port_manager):
# Cleanup the IOU object
IOU._instance = None
iou = IOU.instance()
iou.port_manager = port_manager
def test_get_application_id(loop, project, iou):
vm1_id = str(uuid.uuid4())
vm2_id = str(uuid.uuid4())
vm3_id = str(uuid.uuid4())
@ -54,11 +50,7 @@ def test_get_application_id(loop, project, port_manager):
assert iou.get_application_id(vm3_id) == 1
def test_get_application_id_multiple_project(loop, port_manager):
# Cleanup the IOU object
IOU._instance = None
iou = IOU.instance()
iou.port_manager = port_manager
def test_get_application_id_multiple_project(loop, iou):
vm1_id = str(uuid.uuid4())
vm2_id = str(uuid.uuid4())
vm3_id = str(uuid.uuid4())
@ -72,11 +64,7 @@ def test_get_application_id_multiple_project(loop, port_manager):
assert iou.get_application_id(vm3_id) == 3
def test_get_application_id_no_id_available(loop, project, port_manager):
# Cleanup the IOU object
IOU._instance = None
iou = IOU.instance()
iou.port_manager = port_manager
def test_get_application_id_no_id_available(loop, project, iou):
with pytest.raises(IOUError):
for i in range(1, 513):
vm_id = str(uuid.uuid4())

View File

@ -25,6 +25,14 @@ from gns3server.modules.vpcs import VPCS
from gns3server.modules.iou import IOU
@pytest.fixture(scope="function")
def vpcs(port_manager):
VPCS._instance = None
vpcs = VPCS.instance()
vpcs.port_manager = port_manager
return vpcs
@pytest.fixture(scope="function")
def iou(port_manager):
IOU._instance = None
@ -33,22 +41,14 @@ def iou(port_manager):
return iou
def test_create_vm_new_topology(loop, project, port_manager):
VPCS._instance = None
vpcs = VPCS.instance()
vpcs.port_manager = port_manager
def test_create_vm_new_topology(loop, project, vpcs):
vm_id = str(uuid.uuid4())
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
assert vm in project.vms
def test_create_twice_same_vm_new_topology(loop, project, port_manager):
def test_create_twice_same_vm_new_topology(loop, project, vpcs):
project._vms = set()
VPCS._instance = None
vpcs = VPCS.instance()
vpcs.port_manager = port_manager
vm_id = str(uuid.uuid4())
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id, console=2222))
assert vm in project.vms
@ -57,17 +57,13 @@ def test_create_twice_same_vm_new_topology(loop, project, port_manager):
assert len(project.vms) == 1
def test_create_vm_new_topology_without_uuid(loop, project, port_manager):
VPCS._instance = None
vpcs = VPCS.instance()
vpcs.port_manager = port_manager
def test_create_vm_new_topology_without_uuid(loop, project, vpcs):
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, None))
assert vm in project.vms
assert len(vm.id) == 36
def test_create_vm_old_topology(loop, project, tmpdir, port_manager):
def test_create_vm_old_topology(loop, project, tmpdir, vpcs):
with patch("gns3server.modules.project.Project.is_local", return_value=True):
# Create an old topology directory
@ -79,9 +75,6 @@ def test_create_vm_old_topology(loop, project, tmpdir, port_manager):
with open(os.path.join(vm_dir, "startup.vpc"), "w+") as f:
f.write("1")
VPCS._instance = None
vpcs = VPCS.instance()
vpcs.port_manager = port_manager
vm_id = 1
vm = loop.run_until_complete(vpcs.create_vm("PC 1", project.id, vm_id))
assert len(vm.id) == 36
@ -121,3 +114,27 @@ def test_get_relative_image_path(iou, tmpdir):
assert iou.get_relative_image_path(path2) == "test2.bin"
assert iou.get_relative_image_path("test2.bin") == "test2.bin"
assert iou.get_relative_image_path("../test1.bin") == path1
def test_list_images(loop, iou, tmpdir):
fake_images = ["a.bin", "b.bin", ".blu.bin"]
for image in fake_images:
with open(str(tmpdir / image), "w+") as f:
f.write("1")
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir)):
assert loop.run_until_complete(iou.list_images()) == [
{"filename": "a.bin"},
{"filename": "b.bin"}
]
def test_list_images_empty(loop, iou, tmpdir):
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir)):
assert loop.run_until_complete(iou.list_images()) == []
def test_list_images_directory_not_exist(loop, iou):
with patch("gns3server.modules.IOU.get_images_directory", return_value="/bla"):
assert loop.run_until_complete(iou.list_images()) == []