mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-21 13:45:13 +02:00
Merge pull request #3011 from cristian-ciobanu/feat-#3049
feat(qemu): support two CD/DVD images and consistent ISO handling
This commit is contained in:
22 files changed
+799
-53
No files matched your search
@@ -628,3 +628,52 @@ def test_no_appliance_metadata_when_appliance_has_none():
|
||||
template = ApplianceToTemplate().new_template(appliance, None, "local")
|
||||
|
||||
assert "appliance_metadata" not in template
|
||||
|
||||
|
||||
@pytest.mark.parametrize("version", [6, 8])
|
||||
def test_second_cdrom_survives_appliance_conversion(version):
|
||||
from copy import deepcopy
|
||||
|
||||
from gns3server.schemas.controller.appliances import ApplianceVersionImages
|
||||
|
||||
slots = {"cdrom_image": "installer.iso", "cdrom2_image": "drivers.iso"}
|
||||
validated = ApplianceVersionImages.model_validate(slots).model_dump(exclude_none=True)
|
||||
assert validated == slots
|
||||
appliance = deepcopy(VYOS_V8)
|
||||
appliance["versions"][0]["images"] = validated
|
||||
if version == 6:
|
||||
appliance["registry_version"] = 6
|
||||
appliance["qemu"] = {"arch": "x86_64", "ram": 512, "adapters": 1, "kvm": "allow"}
|
||||
converted = ApplianceToTemplate().new_template(appliance, appliance["versions"][0], "local")
|
||||
assert converted["cdrom_image"] == "installer.iso"
|
||||
assert converted["cdrom2_image"] == "drivers.iso"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["cdrom_interface", "cdrom2_interface"])
|
||||
def test_v8_cdrom_interface_survives_validation_and_conversion(name):
|
||||
import copy
|
||||
|
||||
appliance = copy.deepcopy(VYOS_V8)
|
||||
appliance["settings"][0]["template_properties"][name] = "sata"
|
||||
validated = ApplianceModel.model_validate(appliance).model_dump(mode="json", exclude_none=True)
|
||||
template = ApplianceToTemplate().new_template(validated, validated["versions"][1], "local")
|
||||
assert template[name] == "sata"
|
||||
|
||||
|
||||
def test_legacy_cdrom_interfaces_are_qemu_settings():
|
||||
from gns3server.schemas.controller.appliances import Qemu
|
||||
|
||||
settings = Qemu.model_validate(
|
||||
{
|
||||
"adapter_type": "e1000",
|
||||
"adapters": 1,
|
||||
"ram": 512,
|
||||
"arch": "x86_64",
|
||||
"console_type": "telnet",
|
||||
"kvm": "allow",
|
||||
"cdrom_interface": "sata",
|
||||
"cdrom2_interface": "sata",
|
||||
}
|
||||
)
|
||||
assert settings.model_dump()["cdrom_interface"] == "sata"
|
||||
assert settings.model_dump()["cdrom2_interface"] == "sata"
|
||||
@@ -413,3 +413,49 @@ async def test_export_with_ignoring_snapshots(tmpdir, project):
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / "zipfile.zip")) as myzip:
|
||||
assert os.path.join("snapshots", "snap.gns3project") not in [f.filename for f in myzip.filelist]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_export_two_isos_with_identical_filenames(tmpdir, project):
|
||||
from gns3server.compute.qemu import Qemu
|
||||
from gns3server.controller.import_project import _import_images
|
||||
|
||||
root = Path(str(tmpdir)) / "QEMU"
|
||||
installer = root / "windows/install.iso"
|
||||
drivers = root / "drivers/install.iso"
|
||||
for image, data in ((installer, b"installer"), (drivers, b"drivers")):
|
||||
image.parent.mkdir(parents=True)
|
||||
image.write_bytes(data)
|
||||
topology = {
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"node_type": "qemu",
|
||||
"properties": {
|
||||
"cdrom_image": str(installer),
|
||||
"cdrom2_image": str(drivers),
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Path(project.path, "test.gns3").write_text(json.dumps(topology))
|
||||
archive = str(tmpdir / "iso-project.zip")
|
||||
with aiozipstream.ZipFile() as stream:
|
||||
with patch.object(Qemu, "get_images_directory", return_value=str(root)):
|
||||
await export_project(stream, project, str(tmpdir), include_images=True)
|
||||
await write_file(archive, stream)
|
||||
with zipfile.ZipFile(archive) as stream:
|
||||
properties = json.loads(stream.read("project.gns3"))["topology"]["nodes"][0]["properties"]
|
||||
assert properties["cdrom_image"] != properties["cdrom2_image"]
|
||||
assert stream.read("images/QEMU/" + properties["cdrom_image"]) == b"installer"
|
||||
assert stream.read("images/QEMU/" + properties["cdrom2_image"]) == b"drivers"
|
||||
assert len(stream.namelist()) == len(set(stream.namelist()))
|
||||
destination = Path(str(tmpdir)) / "unpacked"
|
||||
stream.extractall(destination)
|
||||
controller = MagicMock()
|
||||
imported_images = Path(str(tmpdir)) / "imported-images"
|
||||
controller.images_path.return_value = str(imported_images)
|
||||
await _import_images(controller, str(destination / "images"))
|
||||
assert (imported_images / "QEMU" / properties["cdrom_image"]).read_bytes() == b"installer"
|
||||
assert (imported_images / "QEMU" / properties["cdrom2_image"]).read_bytes() == b"drivers"
|
||||
@@ -0,0 +1,56 @@
|
||||
import pytest
|
||||
import sqlalchemy as sa
|
||||
from alembic import command, config
|
||||
from alembic.operations import Operations
|
||||
from alembic.runtime.migration import MigrationContext
|
||||
from fastapi import FastAPI
|
||||
|
||||
from gns3server.db.models import Base
|
||||
from gns3server.db.tasks import connect_to_db
|
||||
from gns3server.db_migrations.versions import e2a7c9d4b601_add_cdrom_interfaces as migration
|
||||
|
||||
|
||||
def test_cdrom_migration_defaults_idempotence_and_downgrade():
|
||||
engine = sa.create_engine("sqlite://")
|
||||
with engine.begin() as connection:
|
||||
connection.exec_driver_sql("CREATE TABLE qemu_templates (id INTEGER PRIMARY KEY)")
|
||||
connection.exec_driver_sql("INSERT INTO qemu_templates (id) VALUES (1)")
|
||||
with Operations.context(MigrationContext.configure(connection)):
|
||||
migration.upgrade()
|
||||
migration.upgrade()
|
||||
assert connection.exec_driver_sql("SELECT cdrom_interface, cdrom2_interface FROM qemu_templates").one() == (
|
||||
"ide",
|
||||
"ide",
|
||||
)
|
||||
migration.downgrade()
|
||||
assert [column["name"] for column in sa.inspect(connection).get_columns("qemu_templates")] == ["id"]
|
||||
engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("versioned", [False, True])
|
||||
async def test_database_startup_adds_cdrom_interfaces(tmp_path, monkeypatch, versioned):
|
||||
path = tmp_path / "controller.db"
|
||||
engine = sa.create_engine(f"sqlite:///{path}")
|
||||
with engine.connect() as connection:
|
||||
Base.metadata.create_all(connection)
|
||||
connection.exec_driver_sql("ALTER TABLE qemu_templates DROP COLUMN cdrom_interface")
|
||||
connection.exec_driver_sql("ALTER TABLE qemu_templates DROP COLUMN cdrom2_interface")
|
||||
if versioned:
|
||||
cfg = config.Config()
|
||||
cfg.set_main_option("script_location", "gns3server:db_migrations")
|
||||
cfg.attributes["connection"] = connection
|
||||
command.stamp(cfg, migration.down_revision)
|
||||
connection.commit()
|
||||
engine.dispose()
|
||||
monkeypatch.setenv("GNS3_DATABASE_URI", f"sqlite+aiosqlite:///{path}")
|
||||
app = FastAPI()
|
||||
await connect_to_db(app)
|
||||
try:
|
||||
async with app.state._db_engine.connect() as connection:
|
||||
columns = await connection.run_sync(lambda conn: sa.inspect(conn).get_columns("qemu_templates"))
|
||||
assert {"cdrom_interface", "cdrom2_interface"} <= {column["name"] for column in columns}
|
||||
revision = await connection.scalar(sa.text("SELECT version_num FROM alembic_version"))
|
||||
assert revision == migration.revision
|
||||
finally:
|
||||
await app.state._db_engine.dispose()
|
||||
Reference in new issue
Block a user