mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Strip the old / import export project code controller will do everything
This commit is contained in:
5 files changed
-372
No files matched your search
@@ -175,155 +175,3 @@ def test_emit(async_run):
|
||||
(action, event, context) = async_run(queue.get(0.5))
|
||||
assert action == "test"
|
||||
assert context["project_id"] == project.id
|
||||
|
||||
|
||||
def test_export(tmpdir):
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
path = project.path
|
||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test"), 'w+') as f:
|
||||
f.write("HELLO")
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test_log.txt"), 'w+') as f:
|
||||
f.write("LOG")
|
||||
os.makedirs(os.path.join(path, "project-files", "snapshots"))
|
||||
with open(os.path.join(path, "project-files", "snapshots", "test"), 'w+') as f:
|
||||
f.write("WORLD")
|
||||
|
||||
z = project.export()
|
||||
|
||||
with open(str(tmpdir / 'zipfile.zip'), 'wb') as f:
|
||||
for data in z:
|
||||
f.write(data)
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
|
||||
with myzip.open("vm-1/dynamips/test") as myfile:
|
||||
content = myfile.read()
|
||||
assert content == b"HELLO"
|
||||
|
||||
assert 'project-files/snapshots/test' not in myzip.namelist()
|
||||
assert 'vm-1/dynamips/test_log.txt' not in myzip.namelist()
|
||||
|
||||
|
||||
def test_export_with_vm(tmpdir):
|
||||
project_id = str(uuid.uuid4())
|
||||
project = Project(project_id=project_id)
|
||||
path = project.path
|
||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||
|
||||
# The .gns3 should be renamed project.gns3 in order to simplify import
|
||||
with open(os.path.join(path, "test.gns3"), 'w+') as f:
|
||||
f.write("{}")
|
||||
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test"), 'w+') as f:
|
||||
f.write("HELLO")
|
||||
with open(os.path.join(path, "vm-1", "dynamips", "test_log.txt"), 'w+') as f:
|
||||
f.write("LOG")
|
||||
os.makedirs(os.path.join(path, "project-files", "snapshots"))
|
||||
with open(os.path.join(path, "project-files", "snapshots", "test"), 'w+') as f:
|
||||
f.write("WORLD")
|
||||
|
||||
os.makedirs(os.path.join(path, "servers", "vm", "project-files", "docker"))
|
||||
with open(os.path.join(path, "servers", "vm", "project-files", "docker", "busybox"), 'w+') as f:
|
||||
f.write("DOCKER")
|
||||
|
||||
z = project.export()
|
||||
|
||||
with open(str(tmpdir / 'zipfile.zip'), 'wb') as f:
|
||||
for data in z:
|
||||
f.write(data)
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
|
||||
with myzip.open("vm-1/dynamips/test") as myfile:
|
||||
content = myfile.read()
|
||||
assert content == b"HELLO"
|
||||
|
||||
assert 'test.gns3' not in myzip.namelist()
|
||||
assert 'project.gns3' in myzip.namelist()
|
||||
assert 'project-files/snapshots/test' not in myzip.namelist()
|
||||
assert 'vm-1/dynamips/test_log.txt' not in myzip.namelist()
|
||||
assert 'servers/vm/project-files/docker/busybox' not in myzip.namelist()
|
||||
assert 'project-files/docker/busybox' in myzip.namelist()
|
||||
|
||||
|
||||
def test_import(tmpdir):
|
||||
|
||||
project_id = str(uuid.uuid4())
|
||||
project = Project(name="test", project_id=project_id)
|
||||
|
||||
topology = {
|
||||
"project_id": str(uuid.uuid4()),
|
||||
"name": "testtest",
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"server_id": 3,
|
||||
"type": "VPCSDevice"
|
||||
},
|
||||
{
|
||||
"server_id": 3,
|
||||
"type": "QemuVM"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
with open(str(tmpdir / "project.gns3"), 'w+') as f:
|
||||
json.dump(topology, 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:
|
||||
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")
|
||||
myzip.write(str(tmpdir / "b.png"), "project-files/qemu/test")
|
||||
|
||||
with open(zip_path, "rb") as f:
|
||||
project.import_zip(f)
|
||||
|
||||
assert os.path.exists(os.path.join(project.path, "b.png"))
|
||||
assert os.path.exists(os.path.join(project.path, "test.gns3"))
|
||||
assert os.path.exists(os.path.join(project.path, "project-files/dynamips/test"))
|
||||
assert os.path.exists(os.path.join(project.path, "servers/vm/project-files/qemu/test"))
|
||||
|
||||
with open(os.path.join(project.path, "test.gns3")) as f:
|
||||
content = json.load(f)
|
||||
|
||||
assert content["name"] == "test"
|
||||
assert content["project_id"] == project_id
|
||||
assert content["topology"]["servers"] == [
|
||||
{
|
||||
"id": 1,
|
||||
"local": True,
|
||||
"vm": False
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"local": False,
|
||||
"vm": True
|
||||
},
|
||||
]
|
||||
assert content["topology"]["nodes"][0]["server_id"] == 1
|
||||
assert content["topology"]["nodes"][1]["server_id"] == 2
|
||||
|
||||
|
||||
def test_import_with_images(tmpdir):
|
||||
|
||||
project_id = str(uuid.uuid4())
|
||||
project = Project(name="test", project_id=project_id)
|
||||
|
||||
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:
|
||||
myzip.write(str(tmpdir / "test.image"), "images/IOS/test.image")
|
||||
|
||||
with open(zip_path, "rb") as f:
|
||||
project.import_zip(f)
|
||||
|
||||
# TEST import images
|
||||
path = os.path.join(project._config().get("images_path"), "IOS", "test.image")
|
||||
assert os.path.exists(path), path
|
||||
@@ -199,5 +199,3 @@ def test_export_with_images(tmpdir, project):
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
|
||||
myzip.getinfo("images/IOS/test.image")
|
||||
|
||||
|
||||
@@ -199,40 +199,3 @@ def test_stream_file(http_compute, tmpdir):
|
||||
|
||||
response = http_compute.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
|
||||
assert response.status == 403
|
||||
|
||||
|
||||
def test_export(http_compute, tmpdir, loop, project):
|
||||
|
||||
os.makedirs(project.path, exist_ok=True)
|
||||
with open(os.path.join(project.path, 'a'), 'w+') as f:
|
||||
f.write('hello')
|
||||
|
||||
response = http_compute.get("/projects/{project_id}/export".format(project_id=project.id), raw=True)
|
||||
assert response.status == 200
|
||||
assert response.headers['CONTENT-TYPE'] == 'application/gns3project'
|
||||
assert response.headers['CONTENT-DISPOSITION'] == 'attachment; filename="{}.gns3project"'.format(project.name)
|
||||
|
||||
with open(str(tmpdir / 'project.zip'), 'wb+') as f:
|
||||
f.write(response.body)
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / 'project.zip')) as myzip:
|
||||
with myzip.open("a") as myfile:
|
||||
content = myfile.read()
|
||||
assert content == b"hello"
|
||||
|
||||
|
||||
def test_import(http_compute, tmpdir, loop):
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / "test.zip"), 'w') as myzip:
|
||||
myzip.writestr("demo", b"hello")
|
||||
|
||||
project_id = str(uuid.uuid4())
|
||||
|
||||
with open(str(tmpdir / "test.zip"), "rb") as f:
|
||||
response = http_compute.post("/projects/{project_id}/import".format(project_id=project_id), body=f.read(), raw=True)
|
||||
assert response.status == 201
|
||||
|
||||
project = ProjectManager.instance().get_project(project_id=project_id)
|
||||
with open(os.path.join(project.path, "demo")) as f:
|
||||
content = f.read()
|
||||
assert content == "hello"
|
||||
Reference in new issue
Block a user