mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
186 files changed
+1173
-1206
No files matched your search
@@ -32,7 +32,7 @@ class Query:
|
||||
|
||||
def __init__(self, loop, host='localhost', port=8001, prefix='', api_version=None):
|
||||
"""
|
||||
:param prefix: Prefix added before path (ex: /hypervisor)
|
||||
:param prefix: Prefix added before path (ex: /compute)
|
||||
:param api_version: Version of the api
|
||||
"""
|
||||
self._loop = loop
|
||||
@@ -110,6 +110,8 @@ class Query:
|
||||
response.route = x_route.replace("/v{}".format(self._api_version), "")
|
||||
response.route = response.route .replace(self._prefix, "")
|
||||
|
||||
response.json = {}
|
||||
response.html = ""
|
||||
if response.body is not None:
|
||||
if response.headers.get("CONTENT-TYPE", "") == "application/json":
|
||||
try:
|
||||
@@ -121,9 +123,7 @@ class Query:
|
||||
response.html = response.body.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
response.html = None
|
||||
else:
|
||||
response.json = {}
|
||||
response.html = ""
|
||||
|
||||
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
|
||||
self._dump_example(method, response.route, path, body, response)
|
||||
return response
|
||||
|
||||
File renamed without changes.
+4
-4
@@ -19,7 +19,7 @@ from unittest.mock import MagicMock, patch
|
||||
from gns3server.config import Config
|
||||
|
||||
|
||||
def test_reload_accepted(http_hypervisor):
|
||||
def test_reload_accepted(http_compute):
|
||||
|
||||
gns_config = MagicMock()
|
||||
config = Config.instance()
|
||||
@@ -27,17 +27,17 @@ def test_reload_accepted(http_hypervisor):
|
||||
gns_config.get_section_config.return_value = config.get_section_config("Server")
|
||||
|
||||
with patch("gns3server.config.Config.instance", return_value=gns_config):
|
||||
response = http_hypervisor.post('/config/reload', example=True)
|
||||
response = http_compute.post('/config/reload', example=True)
|
||||
|
||||
assert response.status == 201
|
||||
assert gns_config.reload.called
|
||||
|
||||
|
||||
def test_reload_forbidden(http_hypervisor):
|
||||
def test_reload_forbidden(http_compute):
|
||||
|
||||
config = Config.instance()
|
||||
config.set("Server", "local", "false")
|
||||
|
||||
response = http_hypervisor.post('/config/reload')
|
||||
response = http_compute.post('/config/reload')
|
||||
|
||||
assert response.status == 403
|
||||
@@ -0,0 +1,185 @@
|
||||
# -*- 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
|
||||
import sys
|
||||
import uuid
|
||||
import aiohttp
|
||||
|
||||
from tests.utils import asyncio_patch
|
||||
from unittest.mock import patch, MagicMock, PropertyMock
|
||||
from gns3server.compute.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", "console_resolution": "1280x1024"}
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
def mock_connection():
|
||||
docker = Docker.instance()
|
||||
docker._connected = True
|
||||
docker._connector = MagicMock()
|
||||
yield
|
||||
Docker._instance = None
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vm(http_compute, project, base_params):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
|
||||
if response.status != 201:
|
||||
print(response.body)
|
||||
assert response.status == 201
|
||||
return response.json
|
||||
|
||||
|
||||
def test_docker_create(http_compute, project, base_params):
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
|
||||
with asyncio_patch("gns3server.compute.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
|
||||
assert response.status == 201
|
||||
assert response.route == "/projects/{project_id}/docker/vms"
|
||||
assert response.json["name"] == "PC TEST 1"
|
||||
assert response.json["project_id"] == project.id
|
||||
assert response.json["container_id"] == "8bd8153ea8f5"
|
||||
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_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_stop(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_reload(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.restart", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_delete(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.delete", return_value=True) as mock:
|
||||
response = http_compute.delete("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_reload(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.pause", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_nio_create_udp(http_compute, vm):
|
||||
response = http_compute.post("/projects/{project_id}/docker/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}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
||||
assert response.json["type"] == "nio_udp"
|
||||
|
||||
|
||||
def test_docker_delete_nio(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.adapter_remove_nio_binding") as mock:
|
||||
response = http_compute.delete("/projects/{project_id}/docker/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}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
||||
|
||||
|
||||
def test_docker_update(http_compute, vm, tmpdir, free_console_port):
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.update") as mock:
|
||||
response = http_compute.put("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
|
||||
"console": free_console_port,
|
||||
"start_command": "yes",
|
||||
"environment": "GNS3=1\nGNS4=0"},
|
||||
example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
assert response.json["start_command"] == "yes"
|
||||
assert response.json["environment"] == "GNS3=1\nGNS4=0"
|
||||
|
||||
|
||||
def test_docker_start_capture(http_compute, vm, tmpdir, project):
|
||||
|
||||
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start_capture") as start_capture:
|
||||
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
response = http_compute.post("/projects/{project_id}/docker/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 response.status == 200
|
||||
|
||||
assert start_capture.called
|
||||
assert "test.pcap" in response.json["pcap_file_path"]
|
||||
|
||||
|
||||
def test_docker_start_capture_not_started(http_compute, vm, tmpdir):
|
||||
|
||||
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.start_capture") as start_capture:
|
||||
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params)
|
||||
|
||||
assert not start_capture.called
|
||||
assert response.status == 409
|
||||
|
||||
|
||||
def test_docker_stop_capture(http_compute, vm, tmpdir, project):
|
||||
|
||||
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
|
||||
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
|
||||
assert response.status == 204
|
||||
|
||||
assert stop_capture.called
|
||||
|
||||
|
||||
def test_docker_stop_capture_not_started(http_compute, vm, tmpdir):
|
||||
|
||||
with patch("gns3server.compute.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.compute.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
|
||||
|
||||
response = http_compute.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
|
||||
assert not stop_capture.called
|
||||
assert response.status == 409
|
||||
+38
-38
@@ -24,25 +24,25 @@ from tests.utils import asyncio_patch
|
||||
|
||||
|
||||
# @pytest.yield_fixture(scope="module")
|
||||
# def vm(http_hypervisor, project):
|
||||
# def vm(http_compute, project):
|
||||
#
|
||||
# dynamips_path = "/fake/dynamips"
|
||||
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.create", return_value=True) as mock:
|
||||
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
|
||||
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.create", return_value=True) as mock:
|
||||
# response = http_compute.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.hypervisor.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
|
||||
# with asyncio_patch("gns3server.compute.dynamips.Dynamips.find_dynamips", return_value=dynamips_path):
|
||||
# yield response.json
|
||||
#
|
||||
#
|
||||
# def test_dynamips_vm_create(http_hypervisor, project):
|
||||
# def test_dynamips_vm_create(http_compute, project):
|
||||
#
|
||||
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.create", return_value=True):
|
||||
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
|
||||
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.create", return_value=True):
|
||||
# response = http_compute.post("/projects/{project_id}/dynamips/vms".format(project_id=project.id), {"name": "My router",
|
||||
# "platform": "c3745",
|
||||
# "image": "somewhere",
|
||||
# "ram": 128},
|
||||
@@ -53,46 +53,46 @@ from tests.utils import asyncio_patch
|
||||
# assert response.json["dynamips_id"]
|
||||
#
|
||||
#
|
||||
# def test_dynamips_vm_get(http_hypervisor, project, vm):
|
||||
# response = http_hypervisor.get("/projects/{project_id}/dynamips/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
# def test_dynamips_vm_get(http_compute, project, vm):
|
||||
# response = http_compute.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(http_hypervisor, vm):
|
||||
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.start", return_value=True) as mock:
|
||||
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
# def test_dynamips_vm_start(http_compute, vm):
|
||||
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.start", return_value=True) as mock:
|
||||
# response = http_compute.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(http_hypervisor, vm):
|
||||
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.stop", return_value=True) as mock:
|
||||
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
# def test_dynamips_vm_stop(http_compute, vm):
|
||||
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.stop", return_value=True) as mock:
|
||||
# response = http_compute.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(http_hypervisor, vm):
|
||||
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.suspend", return_value=True) as mock:
|
||||
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
# def test_dynamips_vm_suspend(http_compute, vm):
|
||||
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.suspend", return_value=True) as mock:
|
||||
# response = http_compute.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(http_hypervisor, vm):
|
||||
# with asyncio_patch("gns3server.hypervisor.dynamips.nodes.router.Router.resume", return_value=True) as mock:
|
||||
# response = http_hypervisor.post("/projects/{project_id}/dynamips/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
# def test_dynamips_vm_resume(http_compute, vm):
|
||||
# with asyncio_patch("gns3server.compute.dynamips.nodes.router.Router.resume", return_value=True) as mock:
|
||||
# response = http_compute.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(http_hypervisor, vm):
|
||||
# def test_vbox_nio_create_udp(http_compute, vm):
|
||||
#
|
||||
# with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
|
||||
# response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"],
|
||||
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
|
||||
# response = http_compute.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,
|
||||
@@ -108,10 +108,10 @@ from tests.utils import asyncio_patch
|
||||
# assert response.json["type"] == "nio_udp"
|
||||
#
|
||||
#
|
||||
# def test_vbox_delete_nio(http_hypervisor, vm):
|
||||
# def test_vbox_delete_nio(http_compute, vm):
|
||||
#
|
||||
# with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
|
||||
# response = http_hypervisor.delete("/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
# with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
|
||||
# response = http_compute.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
|
||||
@@ -121,8 +121,8 @@ from tests.utils import asyncio_patch
|
||||
# assert response.route == "/projects/{project_id}/virtualbox/vms/{vm_id}/adapters/{adapter_id:\d+}/nio"
|
||||
#
|
||||
#
|
||||
# def test_vbox_update(http_hypervisor, vm, free_console_port):
|
||||
# response = http_hypervisor.put("/projects/{project_id}/virtualbox/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
|
||||
# def test_vbox_update(http_compute, vm, free_console_port):
|
||||
# response = http_compute.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"
|
||||
@@ -151,17 +151,17 @@ def fake_file(tmpdir):
|
||||
return path
|
||||
|
||||
|
||||
def test_vms(http_hypervisor, tmpdir, fake_dynamips, fake_file):
|
||||
def test_vms(http_compute, tmpdir, fake_dynamips, fake_file):
|
||||
|
||||
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir), example=True):
|
||||
response = http_hypervisor.get("/dynamips/vms")
|
||||
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir), example=True):
|
||||
response = http_compute.get("/dynamips/vms")
|
||||
assert response.status == 200
|
||||
assert response.json == [{"filename": "7200.bin", "path": "7200.bin"}]
|
||||
|
||||
|
||||
def test_upload_vm(http_hypervisor, tmpdir):
|
||||
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/dynamips/vms/test2", body="TEST", raw=True)
|
||||
def test_upload_vm(http_compute, tmpdir):
|
||||
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/dynamips/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 204
|
||||
|
||||
with open(str(tmpdir / "test2")) as f:
|
||||
@@ -172,11 +172,11 @@ def test_upload_vm(http_hypervisor, tmpdir):
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
def test_upload_vm_permission_denied(http_hypervisor, tmpdir):
|
||||
def test_upload_vm_permission_denied(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2"), "w+") as f:
|
||||
f.write("")
|
||||
os.chmod(str(tmpdir / "test2"), 0)
|
||||
|
||||
with patch("gns3server.hypervisor.Dynamips.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/dynamips/vms/test2", body="TEST", raw=True)
|
||||
with patch("gns3server.compute.Dynamips.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/dynamips/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 409
|
||||
+4
-4
@@ -26,14 +26,14 @@ import aiohttp
|
||||
from gns3server.version import __version__
|
||||
|
||||
|
||||
def test_stream(http_hypervisor, tmpdir, loop):
|
||||
def test_stream(http_compute, tmpdir, loop):
|
||||
with open(str(tmpdir / "test"), 'w+') as f:
|
||||
f.write("hello")
|
||||
|
||||
def go(future):
|
||||
query = json.dumps({"location": str(tmpdir / "test")})
|
||||
headers = {'content-type': 'application/json'}
|
||||
response = yield from aiohttp.request("GET", http_hypervisor.get_url("/files/stream"), data=query, headers=headers)
|
||||
response = yield from aiohttp.request("GET", http_compute.get_url("/files/stream"), data=query, headers=headers)
|
||||
response.body = yield from response.content.read(5)
|
||||
with open(str(tmpdir / "test"), 'a') as f:
|
||||
f.write("world")
|
||||
@@ -48,11 +48,11 @@ def test_stream(http_hypervisor, tmpdir, loop):
|
||||
assert response.body == b'helloworld'
|
||||
|
||||
|
||||
def test_stream_file_not_found(http_hypervisor, tmpdir, loop):
|
||||
def test_stream_file_not_found(http_compute, tmpdir, loop):
|
||||
def go(future):
|
||||
query = json.dumps({"location": str(tmpdir / "test")})
|
||||
headers = {'content-type': 'application/json'}
|
||||
response = yield from aiohttp.request("GET", http_hypervisor.get_url("/files/stream"), data=query, headers=headers)
|
||||
response = yield from aiohttp.request("GET", http_compute.get_url("/files/stream"), data=query, headers=headers)
|
||||
response.close()
|
||||
future.set_result(response)
|
||||
|
||||
+83
-83
@@ -46,8 +46,8 @@ def base_params(tmpdir, fake_iou_bin):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vm(http_hypervisor, project, base_params):
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
|
||||
def vm(http_compute, project, base_params):
|
||||
response = http_compute.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
|
||||
assert response.status == 201
|
||||
return response.json
|
||||
|
||||
@@ -58,8 +58,8 @@ def startup_config_file(project, vm):
|
||||
return os.path.join(directory, "startup-config.cfg")
|
||||
|
||||
|
||||
def test_iou_create(http_hypervisor, project, base_params):
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), base_params)
|
||||
def test_iou_create(http_compute, project, base_params):
|
||||
response = http_compute.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"
|
||||
@@ -71,7 +71,7 @@ def test_iou_create(http_hypervisor, project, base_params):
|
||||
assert response.json["l1_keepalives"] is False
|
||||
|
||||
|
||||
def test_iou_create_with_params(http_hypervisor, project, base_params):
|
||||
def test_iou_create_with_params(http_compute, project, base_params):
|
||||
params = base_params
|
||||
params["ram"] = 1024
|
||||
params["nvram"] = 512
|
||||
@@ -82,7 +82,7 @@ def test_iou_create_with_params(http_hypervisor, project, base_params):
|
||||
params["use_default_iou_values"] = True
|
||||
params["iourc_content"] = "test"
|
||||
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
|
||||
response = http_compute.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"
|
||||
@@ -101,7 +101,7 @@ def test_iou_create_with_params(http_hypervisor, project, base_params):
|
||||
assert "iourc" in response.json["iourc_path"]
|
||||
|
||||
|
||||
def test_iou_create_startup_config_already_exist(http_hypervisor, project, base_params):
|
||||
def test_iou_create_startup_config_already_exist(http_compute, project, base_params):
|
||||
"""We don't erase a startup-config if already exist at project creation"""
|
||||
|
||||
vm_id = str(uuid.uuid4())
|
||||
@@ -113,7 +113,7 @@ def test_iou_create_startup_config_already_exist(http_hypervisor, project, base_
|
||||
params["vm_id"] = vm_id
|
||||
params["startup_config_content"] = "hostname test"
|
||||
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
|
||||
response = http_compute.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"
|
||||
|
||||
@@ -122,8 +122,8 @@ def test_iou_create_startup_config_already_exist(http_hypervisor, project, base_
|
||||
assert f.read() == "echo hello"
|
||||
|
||||
|
||||
def test_iou_get(http_hypervisor, project, vm):
|
||||
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_iou_get(http_compute, project, vm):
|
||||
response = http_compute.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"
|
||||
@@ -135,50 +135,50 @@ def test_iou_get(http_hypervisor, project, vm):
|
||||
assert response.json["l1_keepalives"] is False
|
||||
|
||||
|
||||
def test_iou_start(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
def test_iou_start(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
|
||||
response = http_compute.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 == 200
|
||||
assert response.json["name"] == "PC TEST 1"
|
||||
|
||||
|
||||
def test_iou_start_with_iourc(http_hypervisor, vm, tmpdir):
|
||||
def test_iou_start_with_iourc(http_compute, vm, tmpdir):
|
||||
body = {"iourc_content": "test"}
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True)
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
|
||||
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert response.status == 200
|
||||
with open(response.json["iourc_path"]) as f:
|
||||
assert f.read() == "test"
|
||||
|
||||
|
||||
def test_iou_stop(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_iou_stop(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_iou_reload(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/iou/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_iou_reload(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.reload", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/iou/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_iou_delete(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.iou.IOU.delete_vm", return_value=True) as mock:
|
||||
response = http_hypervisor.delete("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_iou_delete(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.iou.IOU.delete_vm", return_value=True) as mock:
|
||||
response = http_compute.delete("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_iou_update(http_hypervisor, vm, tmpdir, free_console_port, project):
|
||||
def test_iou_update(http_compute, vm, tmpdir, free_console_port, project):
|
||||
params = {
|
||||
"name": "test",
|
||||
"console": free_console_port,
|
||||
@@ -191,7 +191,7 @@ def test_iou_update(http_hypervisor, vm, tmpdir, free_console_port, project):
|
||||
"use_default_iou_values": True,
|
||||
"iourc_content": "test"
|
||||
}
|
||||
response = http_hypervisor.put("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
|
||||
response = http_compute.put("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
@@ -208,65 +208,65 @@ def test_iou_update(http_hypervisor, vm, tmpdir, free_console_port, project):
|
||||
assert "iourc" in response.json["iourc_path"]
|
||||
|
||||
|
||||
def test_iou_nio_create_udp(http_hypervisor, vm):
|
||||
response = http_hypervisor.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)
|
||||
def test_iou_nio_create_udp(http_compute, vm):
|
||||
response = http_compute.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(http_hypervisor, vm, ethernet_device):
|
||||
response = http_hypervisor.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": ethernet_device,
|
||||
},
|
||||
example=True)
|
||||
def test_iou_nio_create_ethernet(http_compute, vm, ethernet_device):
|
||||
response = http_compute.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": ethernet_device,
|
||||
},
|
||||
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"] == ethernet_device
|
||||
|
||||
|
||||
def test_iou_nio_create_ethernet_different_port(http_hypervisor, vm, ethernet_device):
|
||||
response = http_hypervisor.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": ethernet_device,
|
||||
},
|
||||
example=False)
|
||||
def test_iou_nio_create_ethernet_different_port(http_compute, vm, ethernet_device):
|
||||
response = http_compute.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": ethernet_device,
|
||||
},
|
||||
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"] == ethernet_device
|
||||
|
||||
|
||||
def test_iou_nio_create_tap(http_hypervisor, vm, ethernet_device):
|
||||
with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=True):
|
||||
response = http_hypervisor.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": ethernet_device})
|
||||
def test_iou_nio_create_tap(http_compute, vm, ethernet_device):
|
||||
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
|
||||
response = http_compute.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": ethernet_device})
|
||||
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(http_hypervisor, vm):
|
||||
http_hypervisor.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 = http_hypervisor.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)
|
||||
def test_iou_delete_nio(http_compute, vm):
|
||||
http_compute.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 = http_compute.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(http_hypervisor, vm, tmpdir, project):
|
||||
def test_iou_start_capture(http_compute, vm, tmpdir, project):
|
||||
|
||||
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start_capture") as start_capture:
|
||||
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start_capture") as start_capture:
|
||||
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
response = http_hypervisor.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)
|
||||
response = http_compute.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 response.status == 200
|
||||
|
||||
@@ -274,71 +274,71 @@ def test_iou_start_capture(http_hypervisor, vm, tmpdir, project):
|
||||
assert "test.pcap" in response.json["pcap_file_path"]
|
||||
|
||||
|
||||
def test_iou_start_capture_not_started(http_hypervisor, vm, tmpdir):
|
||||
def test_iou_start_capture_not_started(http_compute, vm, tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.start_capture") as start_capture:
|
||||
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.start_capture") as start_capture:
|
||||
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
response = http_hypervisor.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)
|
||||
response = http_compute.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)
|
||||
|
||||
assert not start_capture.called
|
||||
assert response.status == 409
|
||||
|
||||
|
||||
def test_iou_stop_capture(http_hypervisor, vm, tmpdir, project):
|
||||
def test_iou_stop_capture(http_compute, vm, tmpdir, project):
|
||||
|
||||
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
|
||||
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
|
||||
|
||||
response = http_hypervisor.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)
|
||||
response = http_compute.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 response.status == 204
|
||||
|
||||
assert stop_capture.called
|
||||
|
||||
|
||||
def test_iou_stop_capture_not_started(http_hypervisor, vm, tmpdir):
|
||||
def test_iou_stop_capture_not_started(http_compute, vm, tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
|
||||
with patch("gns3server.compute.iou.iou_vm.IOUVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM.stop_capture") as stop_capture:
|
||||
|
||||
response = http_hypervisor.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"]))
|
||||
response = http_compute.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"]))
|
||||
|
||||
assert not stop_capture.called
|
||||
assert response.status == 409
|
||||
|
||||
|
||||
def test_get_configs_without_configs_file(http_hypervisor, vm):
|
||||
def test_get_configs_without_configs_file(http_compute, vm):
|
||||
|
||||
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert response.status == 200
|
||||
assert "startup_config" not in response.json
|
||||
assert "private_config" not in response.json
|
||||
|
||||
|
||||
def test_get_configs_with_startup_config_file(http_hypervisor, project, vm):
|
||||
def test_get_configs_with_startup_config_file(http_compute, project, vm):
|
||||
|
||||
path = startup_config_file(project, vm)
|
||||
with open(path, "w+") as f:
|
||||
f.write("TEST")
|
||||
|
||||
response = http_hypervisor.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
response = http_compute.get("/projects/{project_id}/iou/vms/{vm_id}/configs".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["startup_config_content"] == "TEST"
|
||||
|
||||
|
||||
def test_vms(http_hypervisor, tmpdir, fake_iou_bin):
|
||||
def test_vms(http_compute, tmpdir, fake_iou_bin):
|
||||
|
||||
with patch("gns3server.hypervisor.IOU.get_images_directory", return_value=str(tmpdir)):
|
||||
response = http_hypervisor.get("/iou/vms", example=True)
|
||||
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir)):
|
||||
response = http_compute.get("/iou/vms", example=True)
|
||||
assert response.status == 200
|
||||
assert response.json == [{"filename": "iou.bin", "path": "iou.bin"}]
|
||||
|
||||
|
||||
def test_upload_vm(http_hypervisor, tmpdir):
|
||||
with patch("gns3server.hypervisor.IOU.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/iou/vms/test2", body="TEST", raw=True)
|
||||
def test_upload_vm(http_compute, tmpdir):
|
||||
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/iou/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 204
|
||||
|
||||
with open(str(tmpdir / "test2")) as f:
|
||||
@@ -349,11 +349,11 @@ def test_upload_vm(http_hypervisor, tmpdir):
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
def test_upload_vm_permission_denied(http_hypervisor, tmpdir):
|
||||
def test_upload_vm_permission_denied(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2"), "w+") as f:
|
||||
f.write("")
|
||||
os.chmod(str(tmpdir / "test2"), 0)
|
||||
|
||||
with patch("gns3server.hypervisor.IOU.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/iou/vms/test2", body="TEST", raw=True)
|
||||
with patch("gns3server.compute.IOU.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/iou/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 409
|
||||
+4
-4
@@ -19,15 +19,15 @@ import os
|
||||
import pytest
|
||||
|
||||
|
||||
def test_udp_allocation(http_hypervisor, project):
|
||||
response = http_hypervisor.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
|
||||
def test_udp_allocation(http_compute, project):
|
||||
response = http_compute.post('/projects/{}/ports/udp'.format(project.id), {}, example=True)
|
||||
assert response.status == 201
|
||||
assert response.json['udp_port'] is not None
|
||||
|
||||
|
||||
# Netfifaces is not available on Travis
|
||||
@pytest.mark.skipif(os.environ.get("TRAVIS", False) is not False, reason="Not supported on Travis")
|
||||
def test_interfaces(http_hypervisor):
|
||||
response = http_hypervisor.get('/interfaces', example=True)
|
||||
def test_interfaces(http_compute):
|
||||
response = http_compute.get('/interfaces', example=True)
|
||||
assert response.status == 200
|
||||
assert isinstance(response.json, list)
|
||||
+4
-4
@@ -17,11 +17,11 @@
|
||||
|
||||
import json
|
||||
|
||||
from gns3server.hypervisor.notification_manager import NotificationManager
|
||||
from gns3server.compute.notification_manager import NotificationManager
|
||||
|
||||
|
||||
def test_notification_ws(http_hypervisor, async_run):
|
||||
ws = http_hypervisor.websocket("/notifications/ws")
|
||||
def test_notification_ws(http_compute, async_run):
|
||||
ws = http_compute.websocket("/notifications/ws")
|
||||
answer = async_run(ws.receive())
|
||||
answer = json.loads(answer.data)
|
||||
assert answer["action"] == "ping"
|
||||
@@ -32,4 +32,4 @@ def test_notification_ws(http_hypervisor, async_run):
|
||||
answer = json.loads(answer.data)
|
||||
assert answer["action"] == "test"
|
||||
|
||||
async_run(http_hypervisor.close())
|
||||
async_run(http_compute.close())
|
||||
+61
-62
@@ -28,97 +28,96 @@ import zipfile
|
||||
from unittest.mock import patch
|
||||
from tests.utils import asyncio_patch
|
||||
|
||||
from gns3server.handlers.api.hypervisor.project_handler import ProjectHandler
|
||||
from gns3server.hypervisor.project_manager import ProjectManager
|
||||
from gns3server.handlers.api.compute.project_handler import ProjectHandler
|
||||
from gns3server.compute.project_manager import ProjectManager
|
||||
|
||||
|
||||
def test_create_project_with_path(http_hypervisor, tmpdir):
|
||||
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
|
||||
response = http_hypervisor.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"})
|
||||
def test_create_project_with_path(http_compute, tmpdir):
|
||||
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
||||
response = http_compute.post("/projects", {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"})
|
||||
assert response.status == 201
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["project_id"] == "00010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
|
||||
|
||||
def test_create_project_without_dir(http_hypervisor):
|
||||
def test_create_project_without_dir(http_compute):
|
||||
query = {"name": "test", "project_id": "10010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query, example=True)
|
||||
response = http_compute.post("/projects", query, example=True)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] == "10010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
assert response.json["temporary"] is False
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_create_temporary_project(http_hypervisor):
|
||||
def test_create_temporary_project(http_compute):
|
||||
query = {"name": "test", "temporary": True, "project_id": "20010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] == "20010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
assert response.json["temporary"] is True
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_create_project_with_uuid(http_hypervisor):
|
||||
def test_create_project_with_uuid(http_compute):
|
||||
query = {"name": "test", "project_id": "30010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
assert response.json["project_id"] == "30010203-0405-0607-0809-0a0b0c0d0e0f"
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_show_project(http_hypervisor):
|
||||
def test_show_project(http_compute):
|
||||
query = {"name": "test", "project_id": "40010203-0405-0607-0809-0a0b0c0d0e02", "temporary": False}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
response = http_hypervisor.get("/projects/40010203-0405-0607-0809-0a0b0c0d0e02", example=True)
|
||||
response = http_compute.get("/projects/40010203-0405-0607-0809-0a0b0c0d0e02", example=True)
|
||||
assert len(response.json.keys()) == 3
|
||||
assert response.json["project_id"] == "40010203-0405-0607-0809-0a0b0c0d0e02"
|
||||
assert response.json["temporary"] is False
|
||||
assert response.json["name"] == "test"
|
||||
|
||||
|
||||
def test_show_project_invalid_uuid(http_hypervisor):
|
||||
response = http_hypervisor.get("/projects/50010203-0405-0607-0809-0a0b0c0d0e42")
|
||||
def test_show_project_invalid_uuid(http_compute):
|
||||
response = http_compute.get("/projects/50010203-0405-0607-0809-0a0b0c0d0e42")
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_list_projects(http_hypervisor):
|
||||
def test_list_projects(http_compute):
|
||||
ProjectManager.instance()._projects = {}
|
||||
|
||||
query = {"name": "test", "project_id": "51010203-0405-0607-0809-0a0b0c0d0e0f"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
query = {"name": "test", "project_id": "52010203-0405-0607-0809-0a0b0c0d0e0b"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
|
||||
response = http_hypervisor.get("/projects", example=True)
|
||||
response = http_compute.get("/projects", example=True)
|
||||
assert response.status == 200
|
||||
assert len(response.json) == 2
|
||||
assert "51010203-0405-0607-0809-0a0b0c0d0e0f" in [p["project_id"] for p in response.json]
|
||||
|
||||
|
||||
def test_update_temporary_project(http_hypervisor):
|
||||
def test_update_temporary_project(http_compute):
|
||||
query = {"name": "test", "temporary": True, "project_id": "60010203-0405-0607-0809-0a0b0c0d0e0b"}
|
||||
response = http_hypervisor.post("/projects", query)
|
||||
response = http_compute.post("/projects", query)
|
||||
assert response.status == 201
|
||||
query = {"name": "test", "temporary": False}
|
||||
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
response = http_compute.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_temporary(http_hypervisor, tmpdir):
|
||||
def test_update_path_project_temporary(http_compute, tmpdir):
|
||||
|
||||
os.makedirs(str(tmpdir / "a"))
|
||||
os.makedirs(str(tmpdir / "b"))
|
||||
|
||||
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
|
||||
response = http_hypervisor.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "temporary": True, "project_id": "70010203-0405-0607-0809-0a0b0c0d0e0b"})
|
||||
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
||||
response = http_compute.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "temporary": True, "project_id": "70010203-0405-0607-0809-0a0b0c0d0e0b"})
|
||||
assert response.status == 201
|
||||
assert response.json["name"] == "first_name"
|
||||
query = {"name": "second_name", "path": str(tmpdir / "b")}
|
||||
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "second_name"
|
||||
|
||||
@@ -126,17 +125,17 @@ def test_update_path_project_temporary(http_hypervisor, tmpdir):
|
||||
assert os.path.exists(str(tmpdir / "b"))
|
||||
|
||||
|
||||
def test_update_path_project_non_temporary(http_hypervisor, tmpdir):
|
||||
def test_update_path_project_non_temporary(http_compute, tmpdir):
|
||||
|
||||
os.makedirs(str(tmpdir / "a"))
|
||||
os.makedirs(str(tmpdir / "b"))
|
||||
|
||||
with patch("gns3server.hypervisor.project.Project.is_local", return_value=True):
|
||||
response = http_hypervisor.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "project_id": "80010203-0405-0607-0809-0a0b0c0d0e0b"})
|
||||
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
||||
response = http_compute.post("/projects", {"name": "first_name", "path": str(tmpdir / "a"), "project_id": "80010203-0405-0607-0809-0a0b0c0d0e0b"})
|
||||
assert response.status == 201
|
||||
assert response.json["name"] == "first_name"
|
||||
query = {"name": "second_name", "path": str(tmpdir / "b")}
|
||||
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "second_name"
|
||||
|
||||
@@ -144,63 +143,63 @@ def test_update_path_project_non_temporary(http_hypervisor, tmpdir):
|
||||
assert os.path.exists(str(tmpdir / "b"))
|
||||
|
||||
|
||||
def test_update_path_project_non_local(http_hypervisor, tmpdir):
|
||||
def test_update_path_project_non_local(http_compute, tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.project.Project.is_local", return_value=False):
|
||||
response = http_hypervisor.post("/projects", {"name": "first_name", "project_id": "90010203-0405-0607-0809-0a0b0c0d0e0b"})
|
||||
with patch("gns3server.compute.project.Project.is_local", return_value=False):
|
||||
response = http_compute.post("/projects", {"name": "first_name", "project_id": "90010203-0405-0607-0809-0a0b0c0d0e0b"})
|
||||
assert response.status == 201
|
||||
query = {"name": "second_name", "path": str(tmpdir)}
|
||||
response = http_hypervisor.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
response = http_compute.put("/projects/{project_id}".format(project_id=response.json["project_id"]), query, example=True)
|
||||
assert response.status == 403
|
||||
|
||||
|
||||
def test_commit_project(http_hypervisor, project):
|
||||
with asyncio_patch("gns3server.hypervisor.project.Project.commit", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/commit".format(project_id=project.id), example=True)
|
||||
def test_commit_project(http_compute, project):
|
||||
with asyncio_patch("gns3server.compute.project.Project.commit", return_value=True) as mock:
|
||||
response = http_compute.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(http_hypervisor):
|
||||
response = http_hypervisor.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
|
||||
def test_commit_project_invalid_uuid(http_compute):
|
||||
response = http_compute.post("/projects/{project_id}/commit".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_delete_project(http_hypervisor, project):
|
||||
with asyncio_patch("gns3server.hypervisor.project.Project.delete", return_value=True) as mock:
|
||||
response = http_hypervisor.delete("/projects/{project_id}".format(project_id=project.id), example=True)
|
||||
def test_delete_project(http_compute, project):
|
||||
with asyncio_patch("gns3server.compute.project.Project.delete", return_value=True) as mock:
|
||||
response = http_compute.delete("/projects/{project_id}".format(project_id=project.id), example=True)
|
||||
assert response.status == 204
|
||||
assert mock.called
|
||||
|
||||
|
||||
def test_delete_project_invalid_uuid(http_hypervisor):
|
||||
response = http_hypervisor.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
|
||||
def test_delete_project_invalid_uuid(http_compute):
|
||||
response = http_compute.delete("/projects/{project_id}".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_close_project(http_hypervisor, project):
|
||||
with asyncio_patch("gns3server.hypervisor.project.Project.close", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
|
||||
def test_close_project(http_compute, project):
|
||||
with asyncio_patch("gns3server.compute.project.Project.close", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
|
||||
assert response.status == 204
|
||||
assert mock.called
|
||||
|
||||
|
||||
def test_close_project_two_client_connected(http_hypervisor, project):
|
||||
def test_close_project_two_client_connected(http_compute, project):
|
||||
|
||||
ProjectHandler._notifications_listening = {project.id: 2}
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.project.Project.close", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
|
||||
with asyncio_patch("gns3server.compute.project.Project.close", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/close".format(project_id=project.id), example=True)
|
||||
assert response.status == 204
|
||||
assert not mock.called
|
||||
|
||||
|
||||
def test_close_project_invalid_uuid(http_hypervisor):
|
||||
response = http_hypervisor.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
|
||||
def test_close_project_invalid_uuid(http_compute):
|
||||
response = http_compute.post("/projects/{project_id}/close".format(project_id=uuid.uuid4()))
|
||||
assert response.status == 404
|
||||
|
||||
|
||||
def test_get_file(http_hypervisor, tmpdir):
|
||||
def test_get_file(http_compute, tmpdir):
|
||||
|
||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
||||
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
|
||||
@@ -208,24 +207,24 @@ def test_get_file(http_hypervisor, tmpdir):
|
||||
with open(os.path.join(project.path, "hello"), "w+") as f:
|
||||
f.write("world")
|
||||
|
||||
response = http_hypervisor.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True)
|
||||
response = http_compute.get("/projects/{project_id}/files/hello".format(project_id=project.id), raw=True)
|
||||
assert response.status == 200
|
||||
assert response.body == b"world"
|
||||
|
||||
response = http_hypervisor.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
|
||||
response = http_compute.get("/projects/{project_id}/files/false".format(project_id=project.id), raw=True)
|
||||
assert response.status == 404
|
||||
|
||||
response = http_hypervisor.get("/projects/{project_id}/files/../hello".format(project_id=project.id), raw=True)
|
||||
response = http_compute.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):
|
||||
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_hypervisor.get("/projects/{project_id}/export".format(project_id=project.id), raw=True)
|
||||
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/gns3z'
|
||||
assert response.headers['CONTENT-DISPOSITION'] == 'attachment; filename="{}.gns3z"'.format(project.name)
|
||||
@@ -239,7 +238,7 @@ def test_export(http_hypervisor, tmpdir, loop, project):
|
||||
assert content == b"hello"
|
||||
|
||||
|
||||
def test_import(http_hypervisor, tmpdir, loop, project):
|
||||
def test_import(http_compute, tmpdir, loop, project):
|
||||
|
||||
with zipfile.ZipFile(str(tmpdir / "test.zip"), 'w') as myzip:
|
||||
myzip.writestr("demo", b"hello")
|
||||
@@ -247,7 +246,7 @@ def test_import(http_hypervisor, tmpdir, loop, project):
|
||||
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)
|
||||
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)
|
||||
+82
-82
@@ -57,14 +57,14 @@ def base_params(tmpdir, fake_qemu_bin):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vm(http_hypervisor, project, base_params):
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
||||
def vm(http_compute, project, base_params):
|
||||
response = http_compute.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
||||
assert response.status == 201
|
||||
return response.json
|
||||
|
||||
|
||||
def test_qemu_create(http_hypervisor, project, base_params, fake_qemu_bin):
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
||||
def test_qemu_create(http_compute, project, base_params, fake_qemu_bin):
|
||||
response = http_compute.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"
|
||||
@@ -73,11 +73,11 @@ def test_qemu_create(http_hypervisor, project, base_params, fake_qemu_bin):
|
||||
assert response.json["platform"] == "x86_64"
|
||||
|
||||
|
||||
def test_qemu_create_platform(http_hypervisor, project, base_params, fake_qemu_bin):
|
||||
def test_qemu_create_platform(http_compute, project, base_params, fake_qemu_bin):
|
||||
base_params["qemu_path"] = None
|
||||
base_params["platform"] = "x86_64"
|
||||
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), base_params)
|
||||
response = http_compute.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"
|
||||
@@ -86,12 +86,12 @@ def test_qemu_create_platform(http_hypervisor, project, base_params, fake_qemu_b
|
||||
assert response.json["platform"] == "x86_64"
|
||||
|
||||
|
||||
def test_qemu_create_with_params(http_hypervisor, project, base_params, fake_qemu_vm):
|
||||
def test_qemu_create_with_params(http_compute, project, base_params, fake_qemu_vm):
|
||||
params = base_params
|
||||
params["ram"] = 1024
|
||||
params["hda_disk_image"] = "linux载.img"
|
||||
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms".format(project_id=project.id), params, example=True)
|
||||
response = http_compute.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"
|
||||
@@ -102,8 +102,8 @@ def test_qemu_create_with_params(http_hypervisor, project, base_params, fake_qem
|
||||
assert response.json["hda_disk_image_md5sum"] == "c4ca4238a0b923820dcc509a6f75849b"
|
||||
|
||||
|
||||
def test_qemu_get(http_hypervisor, project, vm):
|
||||
response = http_hypervisor.get("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_get(http_compute, project, vm):
|
||||
response = http_compute.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"
|
||||
@@ -111,57 +111,57 @@ def test_qemu_get(http_hypervisor, project, vm):
|
||||
assert response.json["vm_directory"] == os.path.join(project.path, "project-files", "qemu", vm["vm_id"])
|
||||
|
||||
|
||||
def test_qemu_start(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_start(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "PC TEST 1"
|
||||
|
||||
|
||||
def test_qemu_stop(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.stop", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_stop(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.stop", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_qemu_reload(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.reload", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_reload(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.reload", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_qemu_suspend(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.suspend", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_suspend(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.suspend", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_qemu_resume(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.qemu_vm.QemuVM.resume", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/qemu/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_resume(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.qemu.qemu_vm.QemuVM.resume", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/qemu/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_qemu_delete(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.Qemu.delete_vm", return_value=True) as mock:
|
||||
response = http_hypervisor.delete("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_qemu_delete(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.qemu.Qemu.delete_vm", return_value=True) as mock:
|
||||
response = http_compute.delete("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_qemu_update(http_hypervisor, vm, tmpdir, free_console_port, project, fake_qemu_vm):
|
||||
def test_qemu_update(http_compute, vm, tmpdir, free_console_port, project, fake_qemu_vm):
|
||||
params = {
|
||||
"name": "test",
|
||||
"console": free_console_port,
|
||||
"ram": 1024,
|
||||
"hdb_disk_image": "linux.img"
|
||||
}
|
||||
response = http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
|
||||
response = http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), params, example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
@@ -169,71 +169,71 @@ def test_qemu_update(http_hypervisor, vm, tmpdir, free_console_port, project, fa
|
||||
assert response.json["ram"] == 1024
|
||||
|
||||
|
||||
def test_qemu_nio_create_udp(http_hypervisor, vm):
|
||||
http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
|
||||
response = http_hypervisor.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)
|
||||
def test_qemu_nio_create_udp(http_compute, vm):
|
||||
http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
|
||||
response = http_compute.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(http_hypervisor, vm):
|
||||
http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
|
||||
response = http_hypervisor.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)
|
||||
def test_qemu_nio_create_ethernet(http_compute, vm):
|
||||
http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
|
||||
response = http_compute.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 == 409
|
||||
|
||||
|
||||
def test_qemu_delete_nio(http_hypervisor, vm):
|
||||
http_hypervisor.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
|
||||
http_hypervisor.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 = http_hypervisor.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)
|
||||
def test_qemu_delete_nio(http_compute, vm):
|
||||
http_compute.put("/projects/{project_id}/qemu/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"adapters": 2})
|
||||
http_compute.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 = http_compute.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(http_hypervisor, vm):
|
||||
def test_qemu_list_binaries(http_compute, vm):
|
||||
ret = [{"path": "/tmp/1", "version": "2.2.0"},
|
||||
{"path": "/tmp/2", "version": "2.1.0"}]
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.Qemu.binary_list", return_value=ret) as mock:
|
||||
response = http_hypervisor.get("/qemu/binaries".format(project_id=vm["project_id"]), example=True)
|
||||
with asyncio_patch("gns3server.compute.qemu.Qemu.binary_list", return_value=ret) as mock:
|
||||
response = http_compute.get("/qemu/binaries".format(project_id=vm["project_id"]), example=True)
|
||||
assert mock.called_with(None)
|
||||
assert response.status == 200
|
||||
assert response.json == ret
|
||||
|
||||
|
||||
def test_qemu_list_binaries_filter(http_hypervisor, vm):
|
||||
def test_qemu_list_binaries_filter(http_compute, vm):
|
||||
ret = [
|
||||
{"path": "/tmp/x86_64", "version": "2.2.0"},
|
||||
{"path": "/tmp/alpha", "version": "2.1.0"},
|
||||
{"path": "/tmp/i386", "version": "2.1.0"}
|
||||
]
|
||||
with asyncio_patch("gns3server.hypervisor.qemu.Qemu.binary_list", return_value=ret) as mock:
|
||||
response = http_hypervisor.get("/qemu/binaries".format(project_id=vm["project_id"]), body={"archs": ["i386"]}, example=True)
|
||||
with asyncio_patch("gns3server.compute.qemu.Qemu.binary_list", return_value=ret) as mock:
|
||||
response = http_compute.get("/qemu/binaries".format(project_id=vm["project_id"]), body={"archs": ["i386"]}, example=True)
|
||||
assert response.status == 200
|
||||
assert mock.called_with(["i386"])
|
||||
assert response.json == ret
|
||||
|
||||
|
||||
def test_vms(http_hypervisor, tmpdir, fake_qemu_vm):
|
||||
def test_vms(http_compute, tmpdir, fake_qemu_vm):
|
||||
|
||||
response = http_hypervisor.get("/qemu/vms")
|
||||
response = http_compute.get("/qemu/vms")
|
||||
assert response.status == 200
|
||||
assert response.json == [{"filename": "linux载.img", "path": "linux载.img"}]
|
||||
|
||||
|
||||
def test_upload_vm(http_hypervisor, tmpdir):
|
||||
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/qemu/vms/test2", body="TEST", raw=True)
|
||||
def test_upload_vm(http_compute, tmpdir):
|
||||
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/qemu/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 204
|
||||
|
||||
with open(str(tmpdir / "test2")) as f:
|
||||
@@ -244,9 +244,9 @@ def test_upload_vm(http_hypervisor, tmpdir):
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
def test_upload_vm_ova(http_hypervisor, tmpdir):
|
||||
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/qemu/vms/test2.ova/test2.vmdk", body="TEST", raw=True)
|
||||
def test_upload_vm_ova(http_compute, tmpdir):
|
||||
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/qemu/vms/test2.ova/test2.vmdk", body="TEST", raw=True)
|
||||
assert response.status == 204
|
||||
|
||||
with open(str(tmpdir / "test2.ova" / "test2.vmdk")) as f:
|
||||
@@ -257,23 +257,23 @@ def test_upload_vm_ova(http_hypervisor, tmpdir):
|
||||
assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf"
|
||||
|
||||
|
||||
def test_upload_vm_forbiden_location(http_hypervisor, tmpdir):
|
||||
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/qemu/vms/../../test2", body="TEST", raw=True)
|
||||
def test_upload_vm_forbiden_location(http_compute, tmpdir):
|
||||
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/qemu/vms/../../test2", body="TEST", raw=True)
|
||||
assert response.status == 403
|
||||
|
||||
|
||||
def test_upload_vm_permission_denied(http_hypervisor, tmpdir):
|
||||
def test_upload_vm_permission_denied(http_compute, tmpdir):
|
||||
with open(str(tmpdir / "test2"), "w+") as f:
|
||||
f.write("")
|
||||
os.chmod(str(tmpdir / "test2"), 0)
|
||||
|
||||
with patch("gns3server.hypervisor.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_hypervisor.post("/qemu/vms/test2", body="TEST", raw=True)
|
||||
with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir),):
|
||||
response = http_compute.post("/qemu/vms/test2", body="TEST", raw=True)
|
||||
assert response.status == 409
|
||||
|
||||
|
||||
def test_create_img_relative(http_hypervisor):
|
||||
def test_create_img_relative(http_compute):
|
||||
body = {
|
||||
"qemu_img": "/tmp/qemu-img",
|
||||
"path": "hda.qcow2",
|
||||
@@ -284,13 +284,13 @@ def test_create_img_relative(http_hypervisor):
|
||||
"lazy_refcounts": "off",
|
||||
"size": 100
|
||||
}
|
||||
with asyncio_patch("gns3server.hypervisor.Qemu.create_disk"):
|
||||
response = http_hypervisor.post("/qemu/img", body=body, example=True)
|
||||
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
|
||||
response = http_compute.post("/qemu/img", body=body, example=True)
|
||||
|
||||
assert response.status == 201
|
||||
|
||||
|
||||
def test_create_img_absolute_non_local(http_hypervisor):
|
||||
def test_create_img_absolute_non_local(http_compute):
|
||||
|
||||
config = Config.instance()
|
||||
config.set("Server", "local", "false")
|
||||
@@ -305,13 +305,13 @@ def test_create_img_absolute_non_local(http_hypervisor):
|
||||
"lazy_refcounts": "off",
|
||||
"size": 100
|
||||
}
|
||||
with asyncio_patch("gns3server.hypervisor.Qemu.create_disk"):
|
||||
response = http_hypervisor.post("/qemu/img", body=body, example=True)
|
||||
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
|
||||
response = http_compute.post("/qemu/img", body=body, example=True)
|
||||
|
||||
assert response.status == 403
|
||||
|
||||
|
||||
def test_create_img_absolute_local(http_hypervisor):
|
||||
def test_create_img_absolute_local(http_compute):
|
||||
|
||||
config = Config.instance()
|
||||
config.set("Server", "local", "true")
|
||||
@@ -326,13 +326,13 @@ def test_create_img_absolute_local(http_hypervisor):
|
||||
"lazy_refcounts": "off",
|
||||
"size": 100
|
||||
}
|
||||
with asyncio_patch("gns3server.hypervisor.Qemu.create_disk"):
|
||||
response = http_hypervisor.post("/qemu/img", body=body, example=True)
|
||||
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
|
||||
response = http_compute.post("/qemu/img", body=body, example=True)
|
||||
|
||||
assert response.status == 201
|
||||
|
||||
|
||||
def test_capabilities(http_hypervisor):
|
||||
with asyncio_patch("gns3server.hypervisor.Qemu.get_kvm_archs", return_value=["x86_64"]):
|
||||
response = http_hypervisor.get("/qemu/capabilities", example=True)
|
||||
def test_capabilities(http_compute):
|
||||
with asyncio_patch("gns3server.compute.Qemu.get_kvm_archs", return_value=["x86_64"]):
|
||||
response = http_compute.get("/qemu/capabilities", example=True)
|
||||
assert response.json["kvm"] == ["x86_64"]
|
||||
+10
-10
@@ -25,37 +25,37 @@ from gns3server.config import Config
|
||||
from gns3server.version import __version__
|
||||
|
||||
|
||||
def test_version_output(http_hypervisor):
|
||||
def test_version_output(http_compute):
|
||||
config = Config.instance()
|
||||
config.set("Server", "local", "true")
|
||||
|
||||
response = http_hypervisor.get('/version', example=True)
|
||||
response = http_compute.get('/version', example=True)
|
||||
assert response.status == 200
|
||||
assert response.json == {'local': True, 'version': __version__}
|
||||
|
||||
|
||||
def test_version_input(http_hypervisor):
|
||||
def test_version_input(http_compute):
|
||||
query = {'version': __version__}
|
||||
response = http_hypervisor.post('/version', query, example=True)
|
||||
response = http_compute.post('/version', query, example=True)
|
||||
assert response.status == 200
|
||||
assert response.json == {'version': __version__}
|
||||
|
||||
|
||||
def test_version_invalid_input(http_hypervisor):
|
||||
def test_version_invalid_input(http_compute):
|
||||
query = {'version': "0.4.2"}
|
||||
response = http_hypervisor.post('/version', query)
|
||||
response = http_compute.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(http_hypervisor):
|
||||
def test_version_invalid_input_schema(http_compute):
|
||||
query = {'version': "0.4.2", "bla": "blu"}
|
||||
response = http_hypervisor.post('/version', query)
|
||||
response = http_compute.post('/version', query)
|
||||
assert response.status == 400
|
||||
|
||||
|
||||
def test_version_invalid_json(http_hypervisor):
|
||||
def test_version_invalid_json(http_compute):
|
||||
query = "BOUM"
|
||||
response = http_hypervisor.post('/version', query, raw=True)
|
||||
response = http_compute.post('/version', query, raw=True)
|
||||
assert response.status == 400
|
||||
@@ -0,0 +1,133 @@
|
||||
# -*- 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
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
@pytest.yield_fixture(scope="function")
|
||||
def vm(http_compute, project, monkeypatch):
|
||||
|
||||
vboxmanage_path = "/fake/VboxManage"
|
||||
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
|
||||
response = http_compute.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 patch("gns3server.compute.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path):
|
||||
yield response.json
|
||||
|
||||
|
||||
def test_vbox_create(http_compute, project):
|
||||
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
|
||||
response = http_compute.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(http_compute, project, vm):
|
||||
response = http_compute.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(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.check_hw_virtualization", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_stop(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.stop", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_suspend(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.suspend", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_resume(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.resume", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_reload(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.reload", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/virtualbox/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_nio_create_udp(http_compute, vm):
|
||||
|
||||
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
|
||||
response = http_compute.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(http_compute, vm):
|
||||
|
||||
with asyncio_patch('gns3server.compute.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
|
||||
response = http_compute.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(http_compute, vm, free_console_port):
|
||||
response = http_compute.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},
|
||||
example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
+43
-43
@@ -23,22 +23,22 @@ from unittest.mock import patch
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def vm(http_hypervisor, project):
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"})
|
||||
def vm(http_compute, project):
|
||||
response = http_compute.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(http_hypervisor, project):
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1"}, example=True)
|
||||
def test_vpcs_create(http_compute, project):
|
||||
response = http_compute.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(http_hypervisor, project, vm):
|
||||
response = http_hypervisor.get("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_vpcs_get(http_compute, project, vm):
|
||||
response = http_compute.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"
|
||||
@@ -47,8 +47,8 @@ def test_vpcs_get(http_hypervisor, project, vm):
|
||||
assert response.json["status"] == "stopped"
|
||||
|
||||
|
||||
def test_vpcs_create_startup_script(http_hypervisor, project):
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "startup_script": "ip 192.168.1.2\necho TEST"})
|
||||
def test_vpcs_create_startup_script(http_compute, project):
|
||||
response = http_compute.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"
|
||||
@@ -57,8 +57,8 @@ def test_vpcs_create_startup_script(http_hypervisor, project):
|
||||
assert response.json["startup_script_path"] == "startup.vpc"
|
||||
|
||||
|
||||
def test_vpcs_create_port(http_hypervisor, project, free_console_port):
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms".format(project_id=project.id), {"name": "PC TEST 1", "console": free_console_port})
|
||||
def test_vpcs_create_port(http_compute, project, free_console_port):
|
||||
response = http_compute.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"
|
||||
@@ -66,72 +66,72 @@ def test_vpcs_create_port(http_hypervisor, project, free_console_port):
|
||||
assert response.json["console"] == free_console_port
|
||||
|
||||
|
||||
def test_vpcs_nio_create_udp(http_hypervisor, vm):
|
||||
response = http_hypervisor.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)
|
||||
def test_vpcs_nio_create_udp(http_compute, vm):
|
||||
response = http_compute.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"
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
||||
def test_vpcs_nio_create_tap(http_hypervisor, vm, ethernet_device):
|
||||
with patch("gns3server.hypervisor.base_manager.BaseManager.has_privileged_access", return_value=True):
|
||||
response = http_hypervisor.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": ethernet_device})
|
||||
def test_vpcs_nio_create_tap(http_compute, vm, ethernet_device):
|
||||
with patch("gns3server.compute.base_manager.BaseManager.has_privileged_access", return_value=True):
|
||||
response = http_compute.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": ethernet_device})
|
||||
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(http_hypervisor, vm):
|
||||
http_hypervisor.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 = http_hypervisor.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)
|
||||
def test_vpcs_delete_nio(http_compute, vm):
|
||||
http_compute.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 = http_compute.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(http_hypervisor, vm):
|
||||
def test_vpcs_start(http_compute, vm):
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "PC TEST 1"
|
||||
|
||||
|
||||
def test_vpcs_stop(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_vpcs_stop(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.stop", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vpcs_reload(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.vpcs_vm.VPCSVM.reload", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/vpcs/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_vpcs_reload(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.vpcs.vpcs_vm.VPCSVM.reload", return_value=True) as mock:
|
||||
response = http_compute.post("/projects/{project_id}/vpcs/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vpcs_delete(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.vpcs.VPCS.delete_vm", return_value=True) as mock:
|
||||
response = http_hypervisor.delete("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
def test_vpcs_delete(http_compute, vm):
|
||||
with asyncio_patch("gns3server.compute.vpcs.VPCS.delete_vm", return_value=True) as mock:
|
||||
response = http_compute.delete("/projects/{project_id}/vpcs/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vpcs_update(http_hypervisor, vm, tmpdir, free_console_port):
|
||||
response = http_hypervisor.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"},
|
||||
example=True)
|
||||
def test_vpcs_update(http_compute, vm, tmpdir, free_console_port):
|
||||
response = http_compute.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"},
|
||||
example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
+6
-6
@@ -16,21 +16,21 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
def test_hypervisor_create(http_controller, controller):
|
||||
def test_compute_create(http_controller, controller):
|
||||
|
||||
params = {
|
||||
"hypervisor_id": "my_hypervisor_id",
|
||||
"compute_id": "my_compute_id",
|
||||
"protocol": "http",
|
||||
"host": "example.com",
|
||||
"port": 84,
|
||||
"user": "julien",
|
||||
"password": "secure"
|
||||
}
|
||||
response = http_controller.post("/hypervisors", params, example=True)
|
||||
response = http_controller.post("/computes", params, example=True)
|
||||
assert response.status == 201
|
||||
assert response.route == "/hypervisors"
|
||||
assert response.route == "/computes"
|
||||
assert response.json["user"] == "julien"
|
||||
assert "password" not in response.json
|
||||
|
||||
assert len(controller.hypervisors) == 1
|
||||
assert controller.hypervisors["my_hypervisor_id"].host == "example.com"
|
||||
assert len(controller.computes) == 1
|
||||
assert controller.computes["my_compute_id"].host == "example.com"
|
||||
@@ -35,11 +35,11 @@ from gns3server.controller.vm import VM
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hypervisor(http_controller, async_run):
|
||||
hypervisor = MagicMock()
|
||||
hypervisor.id = "example.com"
|
||||
Controller.instance()._hypervisors = {"example.com": hypervisor}
|
||||
return hypervisor
|
||||
def compute(http_controller, async_run):
|
||||
compute = MagicMock()
|
||||
compute.id = "example.com"
|
||||
Controller.instance()._computes = {"example.com": compute}
|
||||
return compute
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -48,21 +48,21 @@ def project(http_controller, async_run):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vm(project, hypervisor, async_run):
|
||||
vm = VM(project, hypervisor, name="test", vm_type="vpcs")
|
||||
def vm(project, compute, async_run):
|
||||
vm = VM(project, compute, name="test", vm_type="vpcs")
|
||||
project._vms[vm.id] = vm
|
||||
return vm
|
||||
|
||||
|
||||
def test_create_vm(http_controller, tmpdir, project, hypervisor):
|
||||
def test_create_vm(http_controller, tmpdir, project, compute):
|
||||
response = MagicMock()
|
||||
response.json = {"console": 2048}
|
||||
hypervisor.post = AsyncioMagicMock(return_value=response)
|
||||
compute.post = AsyncioMagicMock(return_value=response)
|
||||
|
||||
response = http_controller.post("/projects/{}/vms".format(project.id), {
|
||||
"name": "test",
|
||||
"vm_type": "vpcs",
|
||||
"hypervisor_id": "example.com",
|
||||
"compute_id": "example.com",
|
||||
"properties": {
|
||||
"startup_script": "echo test"
|
||||
}
|
||||
@@ -72,27 +72,27 @@ def test_create_vm(http_controller, tmpdir, project, hypervisor):
|
||||
assert "name" not in response.json["properties"]
|
||||
|
||||
|
||||
def test_start_vm(http_controller, tmpdir, project, hypervisor, vm):
|
||||
def test_start_vm(http_controller, tmpdir, project, compute, vm):
|
||||
response = MagicMock()
|
||||
hypervisor.post = AsyncioMagicMock()
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
response = http_controller.post("/projects/{}/vms/{}/start".format(project.id, vm.id), example=True)
|
||||
assert response.status == 201
|
||||
assert response.json["name"] == vm.name
|
||||
|
||||
|
||||
def test_stop_vm(http_controller, tmpdir, project, hypervisor, vm):
|
||||
def test_stop_vm(http_controller, tmpdir, project, compute, vm):
|
||||
response = MagicMock()
|
||||
hypervisor.post = AsyncioMagicMock()
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
response = http_controller.post("/projects/{}/vms/{}/stop".format(project.id, vm.id), example=True)
|
||||
assert response.status == 201
|
||||
assert response.json["name"] == vm.name
|
||||
|
||||
|
||||
def test_suspend_vm(http_controller, tmpdir, project, hypervisor, vm):
|
||||
def test_suspend_vm(http_controller, tmpdir, project, compute, vm):
|
||||
response = MagicMock()
|
||||
hypervisor.post = AsyncioMagicMock()
|
||||
compute.post = AsyncioMagicMock()
|
||||
|
||||
response = http_controller.post("/projects/{}/vms/{}/suspend".format(project.id, vm.id), example=True)
|
||||
assert response.status == 201
|
||||
|
||||
@@ -1,185 +0,0 @@
|
||||
# -*- 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
|
||||
import sys
|
||||
import uuid
|
||||
import aiohttp
|
||||
|
||||
from tests.utils import asyncio_patch
|
||||
from unittest.mock import patch, MagicMock, PropertyMock
|
||||
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", "console_resolution": "1280x1024"}
|
||||
|
||||
|
||||
@pytest.yield_fixture(autouse=True)
|
||||
def mock_connection():
|
||||
docker = Docker.instance()
|
||||
docker._connected = True
|
||||
docker._connector = MagicMock()
|
||||
yield
|
||||
Docker._instance = None
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def vm(http_hypervisor, project, base_params):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
|
||||
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
|
||||
if response.status != 201:
|
||||
print(response.body)
|
||||
assert response.status == 201
|
||||
return response.json
|
||||
|
||||
|
||||
def test_docker_create(http_hypervisor, project, base_params):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.Docker.list_images", return_value=[{"image": "nginx"}]) as mock_list:
|
||||
with asyncio_patch("gns3server.hypervisor.docker.Docker.query", return_value={"Id": "8bd8153ea8f5"}) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms".format(project_id=project.id), base_params)
|
||||
assert response.status == 201
|
||||
assert response.route == "/projects/{project_id}/docker/vms"
|
||||
assert response.json["name"] == "PC TEST 1"
|
||||
assert response.json["project_id"] == project.id
|
||||
assert response.json["container_id"] == "8bd8153ea8f5"
|
||||
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):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.start", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_stop(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.stop", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_reload(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.restart", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_delete(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.delete", return_value=True) as mock:
|
||||
response = http_hypervisor.delete("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_reload(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.pause", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_docker_nio_create_udp(http_hypervisor, vm):
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/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}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
||||
assert response.json["type"] == "nio_udp"
|
||||
|
||||
|
||||
def test_docker_delete_nio(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.adapter_remove_nio_binding") as mock:
|
||||
response = http_hypervisor.delete("/projects/{project_id}/docker/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}/docker/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
||||
|
||||
|
||||
def test_docker_update(http_hypervisor, vm, tmpdir, free_console_port):
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.update") as mock:
|
||||
response = http_hypervisor.put("/projects/{project_id}/docker/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"name": "test",
|
||||
"console": free_console_port,
|
||||
"start_command": "yes",
|
||||
"environment": "GNS3=1\nGNS4=0"},
|
||||
example=True)
|
||||
assert mock.called
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
assert response.json["start_command"] == "yes"
|
||||
assert response.json["environment"] == "GNS3=1\nGNS4=0"
|
||||
|
||||
|
||||
def test_docker_start_capture(http_hypervisor, vm, tmpdir, project):
|
||||
|
||||
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.start_capture") as start_capture:
|
||||
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/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 response.status == 200
|
||||
|
||||
assert start_capture.called
|
||||
assert "test.pcap" in response.json["pcap_file_path"]
|
||||
|
||||
|
||||
def test_docker_start_capture_not_started(http_hypervisor, vm, tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.start_capture") as start_capture:
|
||||
|
||||
params = {"capture_file_name": "test.pcap", "data_link_type": "DLT_EN10MB"}
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/start_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=params)
|
||||
|
||||
assert not start_capture.called
|
||||
assert response.status == 409
|
||||
|
||||
|
||||
def test_docker_stop_capture(http_hypervisor, vm, tmpdir, project):
|
||||
|
||||
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
|
||||
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
|
||||
assert response.status == 204
|
||||
|
||||
assert stop_capture.called
|
||||
|
||||
|
||||
def test_docker_stop_capture_not_started(http_hypervisor, vm, tmpdir):
|
||||
|
||||
with patch("gns3server.hypervisor.docker.docker_vm.DockerVM.is_running", return_value=False) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.docker.docker_vm.DockerVM.stop_capture") as stop_capture:
|
||||
|
||||
response = http_hypervisor.post("/projects/{project_id}/docker/vms/{vm_id}/adapters/0/ports/0/stop_capture".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
|
||||
|
||||
assert not stop_capture.called
|
||||
assert response.status == 409
|
||||
@@ -1,133 +0,0 @@
|
||||
# -*- 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
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
@pytest.yield_fixture(scope="function")
|
||||
def vm(http_hypervisor, project, monkeypatch):
|
||||
|
||||
vboxmanage_path = "/fake/VboxManage"
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True) as mock:
|
||||
response = http_hypervisor.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 patch("gns3server.hypervisor.virtualbox.VirtualBox.find_vboxmanage", return_value=vboxmanage_path):
|
||||
yield response.json
|
||||
|
||||
|
||||
def test_vbox_create(http_hypervisor, project):
|
||||
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.create", return_value=True):
|
||||
response = http_hypervisor.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(http_hypervisor, project, vm):
|
||||
response = http_hypervisor.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(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.check_hw_virtualization", return_value=True) as mock:
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.start", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_stop(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.stop", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/stop".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_suspend(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.suspend", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/suspend".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_resume(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.resume", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/resume".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_reload(http_hypervisor, vm):
|
||||
with asyncio_patch("gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.reload", return_value=True) as mock:
|
||||
response = http_hypervisor.post("/projects/{project_id}/virtualbox/vms/{vm_id}/reload".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
|
||||
assert mock.called
|
||||
assert response.status == 204
|
||||
|
||||
|
||||
def test_vbox_nio_create_udp(http_hypervisor, vm):
|
||||
|
||||
with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_add_nio_binding') as mock:
|
||||
response = http_hypervisor.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(http_hypervisor, vm):
|
||||
|
||||
with asyncio_patch('gns3server.hypervisor.virtualbox.virtualbox_vm.VirtualBoxVM.adapter_remove_nio_binding') as mock:
|
||||
response = http_hypervisor.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(http_hypervisor, vm, free_console_port):
|
||||
response = http_hypervisor.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},
|
||||
example=True)
|
||||
assert response.status == 200
|
||||
assert response.json["name"] == "test"
|
||||
assert response.json["console"] == free_console_port
|
||||
Reference in new issue
Block a user