refactor: ruff autofixes

This commit is contained in:
Mark Paronyan committed 2026-09-27 15:55:10 +03:00
1 parent 2ac637c662
commit a0450b6bce
455 files changed
+12322 -14762

No files matched your search

+101 -121
View File
@@ -43,18 +43,17 @@ async def test_import_project(tmpdir, controller):
"name": "test",
"auto_open": True,
"auto_start": True,
"topology": {
},
"version": "2.0.0"
"topology": {},
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
with open(str(tmpdir / "b.png"), 'w+') as f:
with open(str(tmpdir / "b.png"), "w+") as f:
f.write("B")
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
myzip.write(str(tmpdir / "b.png"), "b.png")
myzip.write(str(tmpdir / "b.png"), "project-files/dynamips/test")
@@ -90,19 +89,13 @@ async def test_import_project_override(projects_dir, controller):
tmpdir = Path(projects_dir) / "override-location"
tmpdir.mkdir(parents=True, exist_ok=True)
project_id = str(uuid.uuid4())
topology = {
"project_id": project_id,
"name": "test",
"topology": {
},
"version": "2.0.0"
}
topology = {"project_id": project_id, "name": "test", "topology": {}, "version": "2.0.0"}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
@@ -120,7 +113,7 @@ async def test_import_project_override(projects_dir, controller):
async def write_file(path, z):
with open(path, 'wb') as f:
with open(path, "wb") as f:
async for chunk in z:
f.write(chunk)
@@ -136,12 +129,11 @@ def export_project_with_symlink(tmpdir, controller):
"name": "test",
"auto_open": True,
"auto_start": True,
"topology": {
},
"version": "2.0.0"
"topology": {},
"version": "2.0.0",
}
with open(os.path.join(project.path, "project.gns3"), 'w+') as f:
with open(os.path.join(project.path, "project.gns3"), "w+") as f:
json.dump(topology, f)
os.makedirs(os.path.join(project.path, "vm1", "dynamips"))
@@ -150,7 +142,10 @@ def export_project_with_symlink(tmpdir, controller):
zip_path = str(tmpdir / "project.zip")
with aiozipstream.ZipFile() as z:
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
with patch(
"gns3server.compute.Dynamips.get_images_directory",
return_value=str(tmpdir / "IOS"),
):
await export_project(z, project, str(tmpdir), include_images=False)
await write_file(zip_path, z)
@@ -218,19 +213,13 @@ async def test_import_upgrade(tmpdir, controller):
"""
project_id = str(uuid.uuid4())
topology = {
"project_id": str(uuid.uuid4()),
"name": "test",
"topology": {
},
"version": "1.4.2"
}
topology = {"project_id": str(uuid.uuid4()), "name": "test", "topology": {}, "version": "1.4.2"}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
@@ -245,22 +234,16 @@ async def test_import_upgrade(tmpdir, controller):
async def test_import_with_images(config, tmpdir, controller):
project_id = str(uuid.uuid4())
topology = {
"project_id": str(uuid.uuid4()),
"name": "test",
"topology": {
},
"version": "2.0.0"
}
topology = {"project_id": str(uuid.uuid4()), "name": "test", "topology": {}, "version": "2.0.0"}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
with open(str(tmpdir / "test.image"), 'w+') as f:
with open(str(tmpdir / "test.image"), "w+") as f:
f.write("B")
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
myzip.write(str(tmpdir / "test.image"), "images/IOS/test.image")
@@ -287,27 +270,20 @@ async def test_import_iou_linux_no_vm(linux_platform, tmpdir, controller):
"name": "test",
"type": "topology",
"topology": {
"nodes": [
{
"compute_id": "local",
"node_type": "iou",
"name": "test",
"properties": {}
}
],
"nodes": [{"compute_id": "local", "node_type": "iou", "name": "test", "properties": {}}],
"links": [],
"computes": [],
"drawings": []
"drawings": [],
},
"revision": 5,
"version": "2.0.0"
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
@@ -338,22 +314,22 @@ async def test_import_iou_linux_with_vm(linux_platform, tmpdir, controller):
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"node_type": "iou",
"name": "test",
"properties": {}
"properties": {},
}
],
"links": [],
"computes": [],
"drawings": []
"drawings": [],
},
"revision": 5,
"version": "2.0.0"
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
@@ -384,22 +360,22 @@ async def test_import_nat_non_linux(windows_platform, tmpdir, controller):
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"node_type": "nat",
"name": "test",
"properties": {}
"properties": {},
}
],
"links": [],
"computes": [],
"drawings": []
"drawings": [],
},
"revision": 5,
"version": "2.0.0"
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
@@ -430,41 +406,43 @@ async def test_import_iou_non_linux(windows_platform, tmpdir, controller):
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"node_type": "iou",
"name": "test",
"properties": {}
"properties": {},
},
{
"compute_id": "local",
"node_type": "vpcs",
"name": "test2",
"properties": {}
}
{"compute_id": "local", "node_type": "vpcs", "name": "test2", "properties": {}},
],
"links": [],
"computes": [],
"drawings": []
"drawings": [],
},
"revision": 5,
"version": "2.0.0"
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
with asyncio_patch("gns3server.controller.import_project._move_files_to_compute") as mock:
project = await import_project(controller, project_id, f)
controller._computes["vm"].post.assert_called_with('/projects', data={'name': 'test', 'project_id': project_id})
controller._computes["vm"].post.assert_called_with(
"/projects", data={"name": "test", "project_id": project_id}
)
with open(os.path.join(project.path, "test.gns3")) as f:
topo = json.load(f)
assert topo["topology"]["nodes"][0]["compute_id"] == "vm"
assert topo["topology"]["nodes"][1]["compute_id"] == "local"
mock.assert_called_with(controller._computes["vm"], project_id, project.path, os.path.join('project-files', 'iou', topo["topology"]["nodes"][0]['node_id']))
mock.assert_called_with(
controller._computes["vm"],
project_id,
project.path,
os.path.join("project-files", "iou", topo["topology"]["nodes"][0]["node_id"]),
)
@pytest.mark.asyncio
@@ -487,54 +465,40 @@ async def test_import_node_id(linux_platform, tmpdir, controller):
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"node_type": "iou",
"name": "test",
"properties": {}
"properties": {},
},
{
"compute_id": "local",
"node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d",
"node_type": "iou",
"name": "test2",
"properties": {}
}
"properties": {},
},
],
"links": [
{
"link_id": "b570a150-c09f-47d9-8d32-9ca5b03234d6",
"nodes": [
{
"adapter_number": 0,
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"port_number": 0
},
{
"adapter_number": 0,
"node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d",
"port_number": 0
}
]
{"adapter_number": 0, "node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "port_number": 0},
{"adapter_number": 0, "node_id": "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "port_number": 0},
],
}
],
"computes": [],
"drawings": [
{
"drawing_id": "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3",
"rotation": 0,
"x": -210,
"y": -108,
"z": 0
}
]
{"drawing_id": "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3", "rotation": 0, "x": -210, "y": -108, "z": 0}
],
},
"revision": 5,
"version": "2.0.0"
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
# Fake .gns3project
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
myzip.writestr("project-files/iou/0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b/startup.cfg", "test")
myzip.writestr("project-files/iou/c3ae286c-c81f-40d9-a2d0-5874b2f2478d/startup.cfg", "test")
@@ -545,20 +509,32 @@ async def test_import_node_id(linux_platform, tmpdir, controller):
with open(os.path.join(project.path, "test.gns3")) as f:
topo = json.load(f)
# Node id should have change
assert topo["topology"]["nodes"][0]["node_id"] not in ["0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d"]
assert topo["topology"]["nodes"][0]["node_id"] not in [
"0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"c3ae286c-c81f-40d9-a2d0-5874b2f2478d",
]
# Link should have change
link = topo["topology"]["links"][0]
assert link["link_id"] != "b570a150-c09f-47d9-8d32-9ca5b03234d6"
assert link["nodes"][0]["node_id"] not in ["0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d"]
assert link["nodes"][0]["node_id"] not in [
"0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"c3ae286c-c81f-40d9-a2d0-5874b2f2478d",
]
# Drawing id should change
assert topo["topology"]["drawings"][0]["drawing_id"] != "08d665ba-e982-4d54-82b4-aa0c4d5ba6a3"
# Node files should have moved to the new node id
assert not os.path.exists(os.path.join(project.path, "project-files", "iou", "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "startup.cfg"))
assert not os.path.exists(os.path.join(project.path, "project-files", "iou", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "startup.cfg"))
assert os.path.exists(os.path.join(project.path, "project-files", "iou", topo["topology"]["nodes"][0]["node_id"], "startup.cfg"))
assert not os.path.exists(
os.path.join(project.path, "project-files", "iou", "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b", "startup.cfg")
)
assert not os.path.exists(
os.path.join(project.path, "project-files", "iou", "c3ae286c-c81f-40d9-a2d0-5874b2f2478d", "startup.cfg")
)
assert os.path.exists(
os.path.join(project.path, "project-files", "iou", topo["topology"]["nodes"][0]["node_id"], "startup.cfg")
)
@pytest.mark.asyncio
@@ -581,22 +557,22 @@ async def test_import_keep_compute_ids(windows_platform, tmpdir, controller):
"node_id": "0fd3dd4d-dc93-4a04-a9b9-7396a9e22e8b",
"node_type": "iou",
"name": "test",
"properties": {}
"properties": {},
}
],
"links": [],
"computes": [],
"drawings": []
"drawings": [],
},
"revision": 5,
"version": "2.0.0"
"version": "2.0.0",
}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f:
@@ -619,8 +595,18 @@ async def test_move_files_to_compute(tmpdir):
with asyncio_patch("gns3server.controller.import_project._upload_file") as mock:
await _move_files_to_compute(None, project_id, str(tmpdir), os.path.join("project-files", "docker"))
mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test"), os.path.join("project-files", "docker", "test"))
mock.assert_any_call(None, project_id, str(tmpdir / "project-files" / "docker" / "test2"), os.path.join("project-files", "docker", "test2"))
mock.assert_any_call(
None,
project_id,
str(tmpdir / "project-files" / "docker" / "test"),
os.path.join("project-files", "docker", "test"),
)
mock.assert_any_call(
None,
project_id,
str(tmpdir / "project-files" / "docker" / "test2"),
os.path.join("project-files", "docker", "test2"),
)
assert not os.path.exists(str(tmpdir / "project-files" / "docker"))
@@ -632,19 +618,13 @@ async def test_import_project_name_and_location(projects_dir, controller):
tmpdir = Path(projects_dir)
project_id = str(uuid.uuid4())
topology = {
"project_id": str(uuid.uuid4()),
"name": "test",
"topology": {
},
"version": "2.0.0"
}
topology = {"project_id": str(uuid.uuid4()), "name": "test", "topology": {}, "version": "2.0.0"}
with open(str(tmpdir / "project.gns3"), 'w+') as f:
with open(str(tmpdir / "project.gns3"), "w+") as f:
json.dump(topology, f)
zip_path = str(tmpdir / "project.zip")
with zipfile.ZipFile(zip_path, 'w') as myzip:
with zipfile.ZipFile(zip_path, "w") as myzip:
myzip.write(str(tmpdir / "project.gns3"), "project.gns3")
with open(zip_path, "rb") as f: