Display an upload form (upload is not yet ready)

This commit is contained in:
Julien Duponchelle committed 2015-02-23 11:28:33 +01:00
1 parent 210aa6f12a
commit f7cd09d5fb
30 files changed
+234 -100

No files matched your search

View File
Whitespace-only changes.
+126
View File
@@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Base code use for all API tests"""
import json
import re
import asyncio
import aiohttp
import os
class Query:
def __init__(self, loop, host='localhost', port=8001):
self._loop = loop
self._port = port
self._host = host
def post(self, path, body={}, **kwargs):
return self._fetch("POST", path, body, **kwargs)
def put(self, path, body={}, **kwargs):
return self._fetch("PUT", path, body, **kwargs)
def get(self, path, **kwargs):
return self._fetch("GET", path, **kwargs)
def delete(self, path, **kwargs):
return self._fetch("DELETE", path, **kwargs)
def _get_url(self, path, version):
if version is None:
return "http://{}:{}{}".format(self._host, self._port, path)
return "http://{}:{}/v{}{}".format(self._host, self._port, version, path)
def _fetch(self, method, path, body=None, api_version = 1, **kwargs):
"""Fetch an url, parse the JSON and return response
Options:
- example if True the session is included inside documentation
- raw do not JSON encode the query
- api_version Version of API, None if no version
"""
if body is not None and not kwargs.get("raw", False):
body = json.dumps(body)
@asyncio.coroutine
def go(future):
response = yield from aiohttp.request(method, self._get_url(path, api_version), data=body)
future.set_result(response)
future = asyncio.Future()
asyncio.async(go(future))
self._loop.run_until_complete(future)
response = future.result()
@asyncio.coroutine
def go(future, response):
response = yield from response.read()
future.set_result(response)
future = asyncio.Future()
asyncio.async(go(future, response))
self._loop.run_until_complete(future)
response.body = future.result()
x_route = response.headers.get('X-Route', None)
if x_route is not None:
response.route = x_route.replace("/v1", "")
if response.body is not None:
if response.headers.get("CONTENT-TYPE", "") == "application/json":
try:
response.json = json.loads(response.body.decode("utf-8"))
except ValueError:
response.json = None
else:
response.html = response.body.decode("utf-8")
else:
response.json = {}
response.html = ""
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
self._dump_example(method, response.route, body, response)
return response
def _dump_example(self, method, path, body, response):
"""Dump the request for the documentation"""
if path is None:
return
with open(self._example_file_path(method, path), 'w+') as f:
f.write("curl -i -X {} 'http://localhost:8000{}'".format(method, path))
if body:
f.write(" -d '{}'".format(re.sub(r"\n", "", json.dumps(json.loads(body), sort_keys=True))))
f.write("\n\n")
f.write("{} {} HTTP/1.1\n".format(method, path))
if body:
f.write(json.dumps(json.loads(body), sort_keys=True, indent=4))
f.write("\n\n\n")
f.write("HTTP/1.1 {}\n".format(response.status))
for header, value in sorted(response.headers.items()):
if header == 'DATE':
# We fix the date otherwise the example is always different
value = "Thu, 08 Jan 2015 16:09:15 GMT"
f.write("{}: {}\n".format(header, value))
f.write("\n")
if response.body:
f.write(json.dumps(json.loads(response.body.decode('utf-8')), sort_keys=True, indent=4))
f.write("\n")
def _example_file_path(self, method, path):
path = re.sub('[^a-z0-9]', '', path)
return "docs/api/examples/{}_{}.txt".format(method.lower(), path)
+125
View File
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from tests.utils import asyncio_patch
# @pytest.yield_fixture(scope="module")
# def vm(server, project):
#
# dynamips_path = "/fake/dynamips"
# with asyncio_patch("gns3server.modules.dynamips.nodes.router.Router.create", return_value=True) as mock:
# response = server.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
# "platform": "c3745",
# "image": "somewhere",
# "ram": 128})
# assert mock.called
# assert response.status == 201
#
# with asyncio_patch("gns3server.modules.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
# yield response.json
#
#
# def test_dynamips_vm_create(server, project):
#
# with asyncio_patch("gns3server.modules.dynamips.nodes.router.Router.create", return_value=True):
# response = server.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
# "platform": "c3745",
# "image": "somewhere",
# "ram": 128},
# example=True)
# assert response.status == 201
# assert response.json["name"] == "My router"
# assert response.json["project_id"] == project.id
# assert response.json["dynamips_id"]
#
#
# def test_dynamips_vm_get(server, project, vm):
# response = server.get("/projects/{project_id}/dynamips/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
# assert response.status == 200
# assert response.route == "/projects/{project_id}/dynamips/vms/{vm_id}"
# assert response.json["name"] == "My router"
# assert response.json["project_id"] == project.id
#
#
# def test_dynamips_vm_start(server, vm):
# with asyncio_patch("gns3server.modules.dynamips.nodes.router.Router.start", return_value=True) as mock:
# response = server.post("/projects/{project_id}/dynamips/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_stop(server, vm):
# with asyncio_patch("gns3server.modules.dynamips.nodes.router.Router.stop", return_value=True) as mock:
# response = server.post("/projects/{project_id}/dynamips/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_suspend(server, vm):
# with asyncio_patch("gns3server.modules.dynamips.nodes.router.Router.suspend", return_value=True) as mock:
# response = server.post("/projects/{project_id}/dynamips/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
#
#
# def test_dynamips_vm_resume(server, vm):
# with asyncio_patch("gns3server.modules.dynamips.nodes.router.Router.resume", return_value=True) as mock:
# response = server.post("/projects/{project_id}/dynamips/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
# assert mock.called
# assert response.status == 204
# def test_vbox_nio_create_udp(server, vm):
#
# with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
# response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"],
# vm_id=vm["vm_id"]), {"type": "nio_udp",
# "lport": 4242,
# "rport": 4343,
# "rhost": "127.0.0.1"},
# example=True)
#
# assert mock.called
# args, kwgars = mock.call_args
# assert args[0] == 0
#
# assert response.status == 201
# assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio"
# assert response.json["type"] == "nio_udp"
#
#
# def test_vbox_delete_nio(server, vm):
#
# with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
# response = server.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
#
# assert mock.called
# args, kwgars = mock.call_args
# assert args[0] == 0
#
# assert response.status == 204
# assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio"
#
#
# def test_vbox_update(server, vm, free_console_port):
# response = server.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
# "console": free_console_port})
# assert response.status == 200
# assert response.json["name"] == "test"
# assert response.json["console"] == free_console_port
+249
View File
@@ -0,0 +1,249 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import os
import stat
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture
def fake_iou_bin(tmpdir):
"""Create a fake IOU image on disk"""
path = str(tmpdir / "iou.bin")
with open(path, "w+") as f:
f.write('\x7fELF\x01\x01\x01')
os.chmod(path, stat.S_IREAD | stat.S_IEXEC)
return path
@pytest.fixture
def base_params(tmpdir, fake_iou_bin):
"""Return standard parameters"""
return {"name": "PC TEST 1", "path": fake_iou_bin}
@pytest.fixture
def vm(server, project, base_params):
response = server.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
assert response.status == 201
return response.json
def initial_config_file(project, vm):
directory = os.path.join(project.path, "project-files", "iou", vm["vm_id"])
os.makedirs(directory, exist_ok=True)
return os.path.join(directory, "initial-config.cfg")
def test_iou_create(server, project, base_params):
response = server.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["serial_adapters"] == 2
assert response.json["ethernet_adapters"] == 2
assert response.json["ram"] == 256
assert response.json["nvram"] == 128
assert response.json["l1_keepalives"] is False
def test_iou_create_with_params(server, project, base_params):
params = base_params
params["ram"] = 1024
params["nvram"] = 512
params["serial_adapters"] = 4
params["ethernet_adapters"] = 0
params["l1_keepalives"] = True
params["initial_config_content"] = "hostname test"
response = server.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["serial_adapters"] == 4
assert response.json["ethernet_adapters"] == 0
assert response.json["ram"] == 1024
assert response.json["nvram"] == 512
assert response.json["l1_keepalives"] is True
assert "initial-config.cfg" in response.json["initial_config"]
with open(initial_config_file(project, response.json)) as f:
assert f.read() == params["initial_config_content"]
def test_iou_get(server, project, vm):
response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["serial_adapters"] == 2
assert response.json["ethernet_adapters"] == 2
assert response.json["ram"] == 256
assert response.json["nvram"] == 128
assert response.json["l1_keepalives"] is False
def test_iou_start(server, vm):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_iou_stop(server, vm):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_iou_reload(server, vm):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_iou_delete(server, vm):
with asyncio_patch("gns3server.modules.iou.IOU.delete_vm", return_value=True) as mock:
response = server.delete("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_iou_update(server, vm, tmpdir, free_console_port, project):
params = {
"name": "test",
"console": free_console_port,
"ram": 512,
"nvram": 2048,
"ethernet_adapters": 4,
"serial_adapters": 0,
"l1_keepalives": True,
"initial_config_content": "hostname test"
}
response = server.put("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["ethernet_adapters"] == 4
assert response.json["serial_adapters"] == 0
assert response.json["ram"] == 512
assert response.json["nvram"] == 2048
assert response.json["l1_keepalives"] is True
assert "initial-config.cfg" in response.json["initial_config"]
with open(initial_config_file(project, response.json)) as f:
assert f.read() == "hostname test"
def test_iou_nio_create_udp(server, vm):
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_iou_nio_create_ethernet(server, vm):
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": "eth0",
},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_generic_ethernet"
assert response.json["ethernet_device"] == "eth0"
def test_iou_nio_create_ethernet_different_port(server, vm):
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/3/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": "eth0",
},
example=False)
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_generic_ethernet"
assert response.json["ethernet_device"] == "eth0"
def test_iou_nio_create_tap(server, vm):
with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True):
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap",
"tap_device": "test"})
assert response.status == 201
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_tap"
def test_iou_delete_nio(server, vm):
server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = server.delete("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_iou_start_capture(server, vm, tmpdir):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start_capture", return_value=True) as mock:
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params, example=True)
assert mock.called
assert response.status == 200
assert "test.pcap" in response.json["pcap_file_path"]
def test_iou_stop_capture(server, vm):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.stop_capture", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
assert response.status == 204
def test_get_initial_config_without_config_file(server, vm):
response = server.get("/projects/{project_id}/iou/vms/{vm_id}/initial_config".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.json["content"] == None
assert response.json["path"] == None
def test_get_initial_config_with_config_file(server, project, vm):
path = initial_config_file(project, vm)
with open(path, "w+") as f:
f.write("TEST")
response = server.get("/projects/{project_id}/iou/vms/{vm_id}/initial_config".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.json["content"] == "TEST"
assert response.json["path"] == "initial-config.cfg"
+28
View File
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def test_udp_allocation(server):
response = server.post('/ports/udp', {}, example=True)
assert response.status == 201
assert response.json == {'udp_port': 10000}
def test_interfaces(server):
response = server.get('/interfaces', example=True)
assert response.status == 200
assert isinstance(response.json, list)
+137
View File
@@ -0,0 +1,137 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
This test suite check /project endpoint
"""
import uuid
from unittest.mock import patch
from tests.utils import asyncio_patch
def test_create_project_with_path(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/projects", {"path": str(tmpdir)})
assert response.status == 200
assert response.json["path"] == str(tmpdir)
def test_create_project_without_dir(server):
query = {}
response = server.post("/projects", query)
assert response.status == 200
assert response.json["project_id"] is not None
assert response.json["temporary"] is False
def test_create_temporary_project(server):
query = {"temporary": True}
response = server.post("/projects", query)
assert response.status == 200
assert response.json["project_id"] is not None
assert response.json["temporary"] is True
def test_create_project_with_uuid(server):
query = {"project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
response = server.post("/projects", query)
assert response.status == 200
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
def test_show_project(server):
query = {"project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f", "temporary": False}
response = server.post("/projects", query)
assert response.status == 200
response = server.get("/projects/00010203-0405-0607-0809-0a0b0c0d0e0f", example=True)
assert len(response.json.keys()) == 4
assert len(response.json["location"]) > 0
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
assert response.json["temporary"] is False
def test_show_project_invalid_uuid(server):
response = server.get("/projects/00010203-0405-0607-0809-0a0b0c0d0e42")
assert response.status == 404
def test_update_temporary_project(server):
query = {"temporary": True}
response = server.post("/projects", query)
assert response.status == 200
query = {"temporary": False}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["temporary"] is False
def test_update_path_project(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": True}):
response = server.post("/projects", {})
assert response.status == 200
query = {"path": str(tmpdir)}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 200
assert response.json["path"] == str(tmpdir)
def test_update_path_project_non_local(server, tmpdir):
with patch("gns3server.config.Config.get_section_config", return_value={"local": False}):
response = server.post("/projects", {})
assert response.status == 200
query = {"path": str(tmpdir)}
response = server.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
assert response.status == 403
def test_commit_project(server, project):
with asyncio_patch("gns3server.modules.project.Project.commit", return_value=True) as mock:
response = server.post("/projects/{project_id}/commit".format(project_id=project.id), example=True)
assert response.status == 204
assert mock.called
def test_commit_project_invalid_uuid(server):
response = server.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
assert response.status == 404
def test_delete_project(server, project):
with asyncio_patch("gns3server.modules.project.Project.delete", return_value=True) as mock:
response = server.delete("/projects/{project_id}".format(project_id=project.id), example=True)
assert response.status == 204
assert mock.called
def test_delete_project_invalid_uuid(server):
response = server.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
assert response.status == 404
def test_close_project(server, project):
with asyncio_patch("gns3server.modules.project.Project.close", return_value=True) as mock:
response = server.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
assert response.status == 204
assert mock.called
def test_close_project_invalid_uuid(server, project):
response = server.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
assert response.status == 404
+174
View File
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import os
import stat
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture
def fake_qemu_bin():
bin_path = os.path.join(os.environ["PATH"], "qemu_x42")
with open(bin_path, "w+") as f:
f.write("1")
os.chmod(bin_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
return bin_path
@pytest.fixture
def base_params(tmpdir, fake_qemu_bin):
"""Return standard parameters"""
return {"name": "PC TEST 1", "qemu_path": fake_qemu_bin}
@pytest.fixture
def vm(server, project, base_params):
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
assert response.status == 201
return response.json
def test_qemu_create(server, project, base_params):
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
def test_qemu_create_with_params(server, project, base_params):
params = base_params
params["ram"] = 1024
params["hda_disk_image"] = "hda"
response = server.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), params, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["ram"] == 1024
assert response.json["hda_disk_image"] == "hda"
def test_qemu_get(server, project, vm):
response = server.get("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
def test_qemu_start(server, vm):
with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_qemu_stop(server, vm):
with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.stop", return_value=True) as mock:
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_qemu_reload(server, vm):
with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.reload", return_value=True) as mock:
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_qemu_suspend(server, vm):
with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.suspend", return_value=True) as mock:
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_qemu_resume(server, vm):
with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.resume", return_value=True) as mock:
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_qemu_delete(server, vm):
with asyncio_patch("gns3server.modules.qemu.Qemu.delete_vm", return_value=True) as mock:
response = server.delete("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_qemu_update(server, vm, tmpdir, free_console_port, project):
params = {
"name": "test",
"console": free_console_port,
"ram": 1024,
"hdb_disk_image": "hdb"
}
response = server.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params)
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["hdb_disk_image"] == "hdb"
assert response.json["ram"] == 1024
def test_qemu_nio_create_udp(server, vm):
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_qemu_nio_create_ethernet(server, vm):
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet",
"ethernet_device": "eth0",
},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_generic_ethernet"
assert response.json["ethernet_device"] == "eth0"
def test_qemu_delete_nio(server, vm):
server.post("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = server.delete("/projects/{project_id}/qemu/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/qemu/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_qemu_list_binaries(server, vm):
ret = [{"path": "/tmp/1", "version": "2.2.0"},
{"path": "/tmp/2", "version": "2.1.0"}]
with asyncio_patch("gns3server.modules.qemu.Qemu.binary_list", return_value=ret) as mock:
response = server.get("/qemu/binaries".format(project_id=vm["project_id"]))
assert mock.called
assert response.status == 200
assert response.json == ret
+56
View File
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
This test suite check /version endpoint
It's also used for unittest the HTTP implementation.
"""
from gns3server.version import __version__
def test_version_output(server):
response = server.get('/version', example=True)
assert response.status == 200
assert response.json == {'version': __version__}
def test_version_input(server):
query = {'version': __version__}
response = server.post('/version', query, example=True)
assert response.status == 200
assert response.json == {'version': __version__}
def test_version_invalid_input(server):
query = {'version': "0.4.2"}
response = server.post('/version', query)
assert response.status == 409
assert response.json == {'message': 'Client version 0.4.2 differs with server version {}'.format(__version__),
'status': 409}
def test_version_invalid_input_schema(server):
query = {'version': "0.4.2", "bla": "blu"}
response = server.post('/version', query)
assert response.status == 400
def test_version_invalid_json(server):
query = "BOUM"
response = server.post('/version', query, raw=True)
assert response.status == 400
+130
View File
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
from tests.utils import asyncio_patch
@pytest.yield_fixture(scope="module")
def vm(server, project, monkeypatch):
vboxmanage_path = "/fake/VboxManage"
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
response = server.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VMTEST",
"vmname": "VMTEST",
"linked_clone": False})
assert mock.called
assert response.status == 201
with asyncio_patch("gns3server.modules.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path):
yield response.json
def test_vbox_create(server, project):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
response = server.post("/projects/{project_id}/virtualbox/vms".format(project_id=project.id), {"name": "VM1",
"vmname": "VM1",
"linked_clone": False},
example=True)
assert response.status == 201
assert response.json["name"] == "VM1"
assert response.json["project_id"] == project.id
def test_vbox_get(server, project, vm):
response = server.get("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}"
assert response.json["name"] == "VMTEST"
assert response.json["project_id"] == project.id
def test_vbox_start(server, vm):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vbox_stop(server, vm):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.stop", return_value=True) as mock:
response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vbox_suspend(server, vm):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.suspend", return_value=True) as mock:
response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vbox_resume(server, vm):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.resume", return_value=True) as mock:
response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vbox_reload(server, vm):
with asyncio_patch("gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.reload", return_value=True) as mock:
response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vbox_nio_create_udp(server, vm):
with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
response = server.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"],
vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status == 201
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_vbox_delete_nio(server, vm):
with asyncio_patch('gns3server.modules.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
response = server.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called
args, kwgars = mock.call_args
assert args[0] == 0
assert response.status == 204
assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_vbox_update(server, vm, free_console_port):
response = server.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port})
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
+130
View File
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pytest
import os
from tests.utils import asyncio_patch
from unittest.mock import patch
@pytest.fixture(scope="module")
def vm(server, project):
response = server.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"})
assert response.status == 201
return response.json
def test_vpcs_create(server, project):
response = server.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"}, example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
def test_vpcs_get(server, project, vm):
response = server.get("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 200
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
def test_vpcs_create_startup_script(server, project):
response = server.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "startup_script": "ip 192.168.1.2\necho TEST"})
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["startup_script"] == "ip 192.168.1.2\necho TEST"
def test_vpcs_create_port(server, project, free_console_port):
response = server.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "console": free_console_port})
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms"
assert response.json["name"] == "PC TEST 1"
assert response.json["project_id"] == project.id
assert response.json["console"] == free_console_port
def test_vpcs_nio_create_udp(server, vm):
response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"},
example=True)
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_udp"
def test_vpcs_nio_create_tap(server, vm):
with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True):
response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap",
"tap_device": "test"})
assert response.status == 201
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
assert response.json["type"] == "nio_tap"
def test_vpcs_delete_nio(server, vm):
server.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_udp",
"lport": 4242,
"rport": 4343,
"rhost": "127.0.0.1"})
response = server.delete("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert response.status == 204
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
def test_vpcs_start(server, vm):
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vpcs_stop(server, vm):
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock:
response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vpcs_reload(server, vm):
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.reload", return_value=True) as mock:
response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vpcs_delete(server, vm):
with asyncio_patch("gns3server.modules.vpcs.VPCS.delete_vm", return_value=True) as mock:
response = server.delete("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called
assert response.status == 204
def test_vpcs_update(server, vm, tmpdir, free_console_port):
response = server.put("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
"console": free_console_port,
"startup_script": "ip 192.168.1.1"})
assert response.status == 200
assert response.json["name"] == "test"
assert response.json["console"] == free_console_port
assert response.json["startup_script"] == "ip 192.168.1.1"