mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Export non remote server work
This commit is contained in:
6 files changed
+252
-85
No files matched your search
@@ -182,10 +182,6 @@ def test_export(tmpdir):
|
||||
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:
|
||||
@@ -205,86 +201,10 @@ def test_export(tmpdir):
|
||||
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()
|
||||
|
||||
|
||||
def test_export_fix_path(tmpdir):
|
||||
"""
|
||||
Fix absolute image path
|
||||
"""
|
||||
|
||||
project = Project(project_id=str(uuid.uuid4()))
|
||||
path = project.path
|
||||
|
||||
topology = {
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"properties": {
|
||||
"image": "/tmp/c3725-adventerprisek9-mz.124-25d.image"
|
||||
},
|
||||
"type": "C3725"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
with open(os.path.join(path, "test.gns3"), 'w+') as f:
|
||||
json.dump(topology, f)
|
||||
|
||||
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("project.gns3") as myfile:
|
||||
content = myfile.read().decode()
|
||||
topology = json.loads(content)
|
||||
assert topology["topology"]["nodes"][0]["properties"]["image"] == "c3725-adventerprisek9-mz.124-25d.image"
|
||||
|
||||
|
||||
def test_export_with_images(tmpdir):
|
||||
"""
|
||||
Fix absolute image path
|
||||
"""
|
||||
project_id = str(uuid.uuid4())
|
||||
project = Project(project_id=project_id)
|
||||
path = project.path
|
||||
|
||||
os.makedirs(str(tmpdir / "IOS"))
|
||||
with open(str(tmpdir / "IOS" / "test.image"), "w+") as f:
|
||||
f.write("AAA")
|
||||
|
||||
topology = {
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"properties": {
|
||||
"image": "test.image"
|
||||
},
|
||||
"type": "C3725"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
with open(os.path.join(path, "test.gns3"), 'w+') as f:
|
||||
json.dump(topology, f)
|
||||
|
||||
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
|
||||
z = project.export(include_images=True)
|
||||
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:
|
||||
myzip.getinfo("images/IOS/test.image")
|
||||
|
||||
|
||||
def test_export_with_vm(tmpdir):
|
||||
project_id = str(uuid.uuid4())
|
||||
project = Project(project_id=project_id)
|
||||
|
||||
@@ -17,8 +17,11 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import uuid
|
||||
import json
|
||||
import pytest
|
||||
import aiohttp
|
||||
import zipfile
|
||||
from unittest.mock import MagicMock
|
||||
from tests.utils import AsyncioMagicMock
|
||||
from unittest.mock import patch
|
||||
@@ -66,7 +69,6 @@ def test_path_exist(tmpdir):
|
||||
p = Project(name="Test", path=str(tmpdir / "demo"))
|
||||
|
||||
|
||||
|
||||
def test_init_path(tmpdir):
|
||||
|
||||
p = Project(path=str(tmpdir), project_id=str(uuid4()), name="Test")
|
||||
@@ -314,3 +316,107 @@ def test_open_close(async_run, controller):
|
||||
assert project.status == "opened"
|
||||
async_run(project.close())
|
||||
assert project.status == "closed"
|
||||
|
||||
|
||||
def test_export(tmpdir, project):
|
||||
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")
|
||||
|
||||
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()
|
||||
|
||||
|
||||
def test_export_fix_path(tmpdir, project):
|
||||
"""
|
||||
Fix absolute image path
|
||||
"""
|
||||
|
||||
path = project.path
|
||||
|
||||
topology = {
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"properties": {
|
||||
"image": "/tmp/c3725-adventerprisek9-mz.124-25d.image"
|
||||
},
|
||||
"node_type": "dynamips"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
with open(os.path.join(path, "test.gns3"), 'w+') as f:
|
||||
json.dump(topology, f)
|
||||
|
||||
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("project.gns3") as myfile:
|
||||
content = myfile.read().decode()
|
||||
topology = json.loads(content)
|
||||
assert topology["topology"]["nodes"][0]["properties"]["image"] == "c3725-adventerprisek9-mz.124-25d.image"
|
||||
|
||||
|
||||
def test_export_with_images(tmpdir, project):
|
||||
"""
|
||||
Fix absolute image path
|
||||
"""
|
||||
path = project.path
|
||||
|
||||
os.makedirs(str(tmpdir / "IOS"))
|
||||
with open(str(tmpdir / "IOS" / "test.image"), "w+") as f:
|
||||
f.write("AAA")
|
||||
|
||||
topology = {
|
||||
"topology": {
|
||||
"nodes": [
|
||||
{
|
||||
"properties": {
|
||||
"image": "test.image"
|
||||
},
|
||||
"node_type": "dynamips"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
with open(os.path.join(path, "test.gns3"), 'w+') as f:
|
||||
json.dump(topology, f)
|
||||
|
||||
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
|
||||
z = project.export(include_images=True)
|
||||
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:
|
||||
myzip.getinfo("images/IOS/test.image")
|
||||
@@ -24,6 +24,7 @@ import os
|
||||
import asyncio
|
||||
import aiohttp
|
||||
import pytest
|
||||
import zipfile
|
||||
import json
|
||||
|
||||
|
||||
@@ -153,3 +154,23 @@ def test_notification_ws(http_controller, controller, project, async_run):
|
||||
assert answer["action"] == "test"
|
||||
|
||||
async_run(http_controller.close())
|
||||
|
||||
|
||||
def test_export(http_controller, 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_controller.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"
|
||||
Reference in new issue
Block a user