Merge branch '1.5' into 2.0

This commit is contained in:
Julien Duponchelle committed 2016-04-08 17:40:27 +02:00
commit 9e8fcab65c
34 files changed
+895 -188

No files matched your search

+1 -1
View File
@@ -133,7 +133,7 @@ class Query:
if path is None:
return
with open(self._example_file_path(method, route), 'w+') as f:
f.write("curl -i -X {} 'http://localhost:8000/v{}{}{}'".format(method, self._api_version, self._prefix, path))
f.write("curl -i -X {} 'http://localhost:3080/v{}{}{}'".format(method, self._api_version, self._prefix, path))
if body:
f.write(" -d '{}'".format(re.sub(r"\n", "", json.dumps(json.loads(body), sort_keys=True))))
f.write("\n\n")
+2 -1
View File
@@ -30,7 +30,7 @@ from gns3server.hypervisor.docker import Docker
@pytest.fixture
def base_params():
"""Return standard parameters"""
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet"}
return {"name": "PC TEST 1", "image": "nginx", "start_command": "nginx-daemon", "adapters": 2, "environment": "YES=1\nNO=0", "console_type": "telnet", "console_resolution": "1280x1024"}
@pytest.yield_fixture(autouse=True)
@@ -65,6 +65,7 @@ def test_docker_create(http_hypervisor, project, base_params):
assert response.json["image"] == "nginx"
assert response.json["adapters"] == 2
assert response.json["environment"] == "YES=1\nNO=0"
assert response.json["console_resolution"] == "1280x1024"
def test_docker_start(http_hypervisor, vm):
@@ -23,6 +23,7 @@ import uuid
import os
import asyncio
import aiohttp
import zipfile
from unittest.mock import patch
from tests.utils import asyncio_patch
@@ -216,3 +217,40 @@ def test_get_file(http_hypervisor, tmpdir):
response = http_hypervisor.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
assert response.status == 403
def test_export(http_hypervisor, 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_hypervisor.get("/projects/{project_id}/export".format(project_id=project.id), raw=True)
assert response.status == 200
assert response.headers['CONTENT-TYPE'] == 'application/gns3z'
assert response.headers['CONTENT-DISPOSITION'] == 'attachment; filename="{}.gns3z"'.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_hypervisor, tmpdir, loop, project):
with zipfile.ZipFile(str(tmpdir / "test.zip"), 'w') as myzip:
myzip.writestr("demo", b"hello")
project_id = project.id
with open(str(tmpdir / "test.zip"), "rb") as f:
response = http_hypervisor.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"
-1
View File
@@ -219,7 +219,6 @@ def test_backup_projects(http_root, tmpdir, loop):
assert response.headers['CONTENT-TYPE'] == 'application/x-gtar'
with open(str(tmpdir / 'projects.tar'), 'wb+') as f:
print(len(response.body))
f.write(response.body)
tar = tarfile.open(str(tmpdir / 'projects.tar'), 'r')
+71 -9
View File
@@ -57,6 +57,7 @@ def test_json(vm, project):
'adapters': 1,
'console': vm.console,
'console_type': 'telnet',
'console_resolution': '1024x768',
'aux': vm.aux,
'start_command': vm.start_command,
'environment': vm.environment,
@@ -89,7 +90,10 @@ def test_create(loop, project, manager):
"HostConfig":
{
"CapAdd": ["ALL"],
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network"))
],
"Privileged": True
},
"Volumes": {},
@@ -113,7 +117,7 @@ def test_create_vnc(loop, project, manager):
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock:
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", console_type="vnc")
vm = DockerVM("test", str(uuid.uuid4()), project, manager, "ubuntu", console_type="vnc", console=5900)
vm._start_vnc = MagicMock()
vm._display = 42
loop.run_until_complete(asyncio.async(vm.create()))
@@ -126,6 +130,7 @@ def test_create_vnc(loop, project, manager):
"CapAdd": ["ALL"],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network")),
'/tmp/.X11-unix/:/tmp/.X11-unix/'
],
"Privileged": True
@@ -141,6 +146,7 @@ def test_create_vnc(loop, project, manager):
})
assert vm._start_vnc.called
assert vm._cid == "e90e34656806"
assert vm._console_type == "vnc"
def test_create_start_cmd(loop, project, manager):
@@ -161,7 +167,10 @@ def test_create_start_cmd(loop, project, manager):
"HostConfig":
{
"CapAdd": ["ALL"],
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network"))
],
"Privileged": True
},
"Volumes": {},
@@ -194,7 +203,10 @@ def test_create_environment(loop, project, manager):
"HostConfig":
{
"CapAdd": ["ALL"],
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network"))
],
"Privileged": True
},
"Env": ["YES=1", "NO=0"],
@@ -241,7 +253,10 @@ def test_create_image_not_available(loop, project, manager):
"HostConfig":
{
"CapAdd": ["ALL"],
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network"))
],
"Privileged": True
},
"Volumes": {},
@@ -438,6 +453,7 @@ def test_update(loop, vm):
}
original_console = vm.console
original_aux = vm.aux
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
@@ -452,7 +468,10 @@ def test_update(loop, vm):
"HostConfig":
{
"CapAdd": ["ALL"],
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network"))
],
"Privileged": True
},
"Volumes": {},
@@ -465,6 +484,30 @@ def test_update(loop, vm):
"Cmd": ["/bin/sh"]
})
assert vm.console == original_console
assert vm.aux == original_aux
def test_update_vnc(loop, vm):
response = {
"Id": "e90e34656806",
"Warnings": []
}
vm.console_type = "vnc"
vm.console = 5900
vm._display = "display"
original_console = vm.console
original_aux = vm.aux
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._start_vnc"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "ubuntu"}]) as mock_list_images:
with asyncio_patch("gns3server.hypervisor.docker.DockerVM._get_container_state", return_value="stopped"):
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value=response) as mock_query:
loop.run_until_complete(asyncio.async(vm.update()))
assert vm.console == original_console
assert vm.aux == original_aux
def test_update_running(loop, vm):
@@ -490,7 +533,10 @@ def test_update_running(loop, vm):
"HostConfig":
{
"CapAdd": ["ALL"],
"Binds": ["{}:/gns3:ro".format(get_resource("hypervisor/docker/resources"))],
"Binds": [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network"))
],
"Privileged": True
},
"Volumes": {},
@@ -763,6 +809,7 @@ def test_mount_binds(vm, tmpdir):
dst = os.path.join(vm.working_dir, "test/experimental")
assert vm._mount_binds(image_infos) == [
"{}:/gns3:ro".format(get_resource("hypervisor/docker/resources")),
"{}:/etc/network:rw".format(os.path.join(vm.working_dir, "etc", "network")),
"{}:{}".format(dst, "/test/experimental")
]
@@ -770,13 +817,14 @@ def test_mount_binds(vm, tmpdir):
def test_start_vnc(vm, loop):
vm.console_resolution = "1280x1024"
with patch("shutil.which", return_value="/bin/x"):
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.wait_for_file_creation") as mock_wait:
with asyncio_patch("asyncio.create_subprocess_exec") as mock_exec:
loop.run_until_complete(asyncio.async(vm._start_vnc()))
assert vm._display is not None
mock_exec.assert_any_call("Xvfb", "-nolisten", "tcp", ":{}".format(vm._display), "-screen", "0", "1024x768x16")
mock_exec.assert_any_call("x11vnc", "-forever", "-nopw", "-display", "WAIT:{}".format(vm._display), "-rfbport", str(vm.console), "-noncache", "-listen", "127.0.0.1")
mock_exec.assert_any_call("Xvfb", "-nolisten", "tcp", ":{}".format(vm._display), "-screen", "0", "1280x1024x16")
mock_exec.assert_any_call("x11vnc", "-forever", "-nopw", "-shared", "-geometry", "1280x1024", "-display", "WAIT:{}".format(vm._display), "-rfbport", str(vm.console), "-noncache", "-listen", "127.0.0.1")
mock_wait.assert_called_with("/tmp/.X11-unix/X{}".format(vm._display))
@@ -789,3 +837,17 @@ def test_start_aux(vm, loop):
with asyncio_patch("asyncio.subprocess.create_subprocess_exec", return_value=MagicMock()) as mock_exec:
loop.run_until_complete(asyncio.async(vm._start_aux()))
def test_create_network_interfaces(vm):
vm.adapters = 5
network_config = vm._create_network_config()
assert os.path.exists(os.path.join(network_config, "interfaces"))
assert os.path.exists(os.path.join(network_config, "if-up.d"))
with open(os.path.join(network_config, "interfaces")) as f:
content = f.read()
assert "eth0" in content
assert "eth4" in content
assert "eth5" not in content
+25 -1
View File
@@ -323,11 +323,35 @@ def test_disk_options(vm, tmpdir, loop, fake_qemu_img_binary):
open(vm._hda_disk_image, "w+").close()
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
loop.run_until_complete(asyncio.async(vm._disk_options()))
options = loop.run_until_complete(asyncio.async(vm._disk_options()))
assert process.called
args, kwargs = process.call_args
assert args == (fake_qemu_img_binary, "create", "-o", "backing_file={}".format(vm._hda_disk_image), "-f", "qcow2", os.path.join(vm.working_dir, "hda_disk.qcow2"))
assert options == ['-drive', 'file=' + os.path.join(vm.working_dir, "hda_disk.qcow2") + ',if=ide,index=0,media=disk']
def test_disk_options_multiple_disk(vm, tmpdir, loop, fake_qemu_img_binary):
vm._hda_disk_image = str(tmpdir / "test0.qcow2")
vm._hdb_disk_image = str(tmpdir / "test1.qcow2")
vm._hdc_disk_image = str(tmpdir / "test2.qcow2")
vm._hdd_disk_image = str(tmpdir / "test3.qcow2")
open(vm._hda_disk_image, "w+").close()
open(vm._hdb_disk_image, "w+").close()
open(vm._hdc_disk_image, "w+").close()
open(vm._hdd_disk_image, "w+").close()
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()) as process:
options = loop.run_until_complete(asyncio.async(vm._disk_options()))
assert options == [
'-drive', 'file=' + os.path.join(vm.working_dir, "hda_disk.qcow2") + ',if=ide,index=0,media=disk',
'-drive', 'file=' + os.path.join(vm.working_dir, "hdb_disk.qcow2") + ',if=ide,index=1,media=disk',
'-drive', 'file=' + os.path.join(vm.working_dir, "hdc_disk.qcow2") + ',if=ide,index=2,media=disk',
'-drive', 'file=' + os.path.join(vm.working_dir, "hdd_disk.qcow2") + ',if=ide,index=3,media=disk'
]
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
def test_set_process_priority(vm, loop, fake_qemu_img_binary):
+2 -2
View File
@@ -49,8 +49,8 @@ def test_temporary_directory(project, manager):
def test_console(project, manager):
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
vm.console = 2111
assert vm.console == 2111
vm.console = 5011
assert vm.console == 5011
vm.console = None
assert vm.console is None
-4
View File
@@ -32,7 +32,6 @@ def test_reserve_tcp_port():
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(2001, project)
assert port != 2001
assert mock_emit.call_args[0][0] == "log.warning"
def test_reserve_tcp_port_outside_range():
@@ -41,7 +40,6 @@ def test_reserve_tcp_port_outside_range():
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(80, project)
assert port != 80
assert mock_emit.call_args[0][0] == "log.warning"
def test_reserve_tcp_port_already_used_by_another_program():
@@ -65,7 +63,6 @@ def test_reserve_tcp_port_already_used_by_another_program():
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(2001, project)
assert port != 2001
assert mock_emit.call_args[0][0] == "log.warning"
def test_reserve_tcp_port_already_used():
@@ -89,7 +86,6 @@ def test_reserve_tcp_port_already_used():
with patch("gns3server.hypervisor.project.Project.emit") as mock_emit:
port = pm.reserve_tcp_port(2001, project)
assert port != 2001
assert mock_emit.call_args[0][0] == "log.warning"
def test_reserve_udp_port():
+140
View File
@@ -17,9 +17,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import uuid
import json
import asyncio
import pytest
import aiohttp
import zipfile
from uuid import uuid4
from unittest.mock import patch
@@ -269,3 +272,140 @@ 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()
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(tmpdir):
project = Project(project_id=str(uuid.uuid4()))
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
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -77,7 +77,7 @@ def test_parse_arguments(capsys, tmpdir):
assert run.parse_arguments([]).host == "192.168.1.2"
assert run.parse_arguments(["--port", "8002"]).port == 8002
assert run.parse_arguments([]).port == 8000
assert run.parse_arguments([]).port == 3080
server_config["port"] = "8003"
assert run.parse_arguments([]).port == 8003