"Local" command line parameter is only for stopping

a server that has been started by the desktop GUI
This commit is contained in:
grossmj committed 2022-03-30 18:38:34 +08:00
1 parent 8975f63e2f
commit 900d4f79ee
18 files changed
+138 -186

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ async def test_version_output(app: FastAPI, compute_client: AsyncClient) -> None
response = await compute_client.get(app.url_path_for("compute:compute_version"))
assert response.status_code == status.HTTP_200_OK
assert response.json() == {'local': True, 'version': __version__}
assert response.json() == {'version': __version__}
async def test_compute_authentication(app: FastAPI, compute_client: AsyncClient) -> None:
+1 -24
View File
@@ -36,35 +36,13 @@ def base_params(tmpdir) -> dict:
params = {
"name": "test",
"path": str(tmpdir),
"project_id": str(uuid.uuid4())
}
return params
async def test_create_project_with_path(app: FastAPI, compute_client: AsyncClient, base_params: dict) -> None:
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = await compute_client.post(app.url_path_for("compute:create_compute_project"), json=base_params)
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["project_id"] == base_params["project_id"]
async def test_create_project_with_path_and_empty_variables(app: FastAPI,
compute_client: AsyncClient,
base_params: dict) -> None:
base_params["variables"] = None
with patch("gns3server.compute.project.Project.is_local", return_value=True):
response = await compute_client.post(app.url_path_for("compute:create_compute_project"), json=base_params)
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["project_id"] == base_params["project_id"]
async def test_create_project_without_dir(app: FastAPI, compute_client: AsyncClient, base_params: dict) -> None:
del base_params["path"]
response = await compute_client.post(app.url_path_for("compute:create_compute_project"), json=base_params)
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["project_id"] == base_params["project_id"]
@@ -158,9 +136,8 @@ async def test_close_project_invalid_uuid(app: FastAPI, compute_client: AsyncCli
assert response.status_code == status.HTTP_404_NOT_FOUND
async def test_get_file(app: FastAPI, compute_client: AsyncClient, config, tmpdir) -> None:
async def test_get_file(app: FastAPI, compute_client: AsyncClient) -> None:
config.settings.Server.projects_path = str(tmpdir)
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
with open(os.path.join(project.path, "hello"), "w+") as f:
+51 -51
View File
@@ -417,57 +417,57 @@ async def test_upload_image_permission_denied(app: FastAPI, compute_client: Asyn
assert response.status_code == status.HTTP_409_CONFLICT
@pytest.mark.asyncio
async def test_create_img_relative(app: FastAPI, compute_client: AsyncClient):
params = {
"qemu_img": "/tmp/qemu-img",
"path": "hda.qcow2",
"format": "qcow2",
"preallocation": "metadata",
"cluster_size": 64,
"refcount_bits": 12,
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = await compute_client.post(app.url_path_for("compute:create_qemu_image"), json=params)
assert response.status_code == status.HTTP_204_NO_CONTENT
async def test_create_img_absolute_non_local(app: FastAPI, compute_client: AsyncClient, config) -> None:
config.settings.Server.local = False
params = {
"qemu_img": "/tmp/qemu-img",
"path": "/tmp/hda.qcow2",
"format": "qcow2",
"preallocation": "metadata",
"cluster_size": 64,
"refcount_bits": 12,
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = await compute_client.post(app.url_path_for("compute:create_qemu_image"), json=params)
assert response.status_code == 403
async def test_create_img_absolute_local(app: FastAPI, compute_client: AsyncClient, config) -> None:
params = {
"qemu_img": "/tmp/qemu-img",
"path": "/tmp/hda.qcow2",
"format": "qcow2",
"preallocation": "metadata",
"cluster_size": 64,
"refcount_bits": 12,
"lazy_refcounts": "off",
"size": 100
}
with asyncio_patch("gns3server.compute.Qemu.create_disk"):
response = await compute_client.post(app.url_path_for("compute:create_qemu_image"), json=params)
assert response.status_code == status.HTTP_204_NO_CONTENT
# @pytest.mark.asyncio
# async def test_create_img_relative(app: FastAPI, compute_client: AsyncClient):
#
# params = {
# "qemu_img": "/tmp/qemu-img",
# "path": "hda.qcow2",
# "format": "qcow2",
# "preallocation": "metadata",
# "cluster_size": 64,
# "refcount_bits": 12,
# "lazy_refcounts": "off",
# "size": 100
# }
# with asyncio_patch("gns3server.compute.Qemu.create_disk"):
# response = await compute_client.post(app.url_path_for("compute:create_qemu_image"), json=params)
# assert response.status_code == status.HTTP_204_NO_CONTENT
#
#
# async def test_create_img_absolute_non_local(app: FastAPI, compute_client: AsyncClient, config) -> None:
#
# config.settings.Server.local = False
# params = {
# "qemu_img": "/tmp/qemu-img",
# "path": "/tmp/hda.qcow2",
# "format": "qcow2",
# "preallocation": "metadata",
# "cluster_size": 64,
# "refcount_bits": 12,
# "lazy_refcounts": "off",
# "size": 100
# }
# with asyncio_patch("gns3server.compute.Qemu.create_disk"):
# response = await compute_client.post(app.url_path_for("compute:create_qemu_image"), json=params)
# assert response.status_code == 403
#
#
# async def test_create_img_absolute_local(app: FastAPI, compute_client: AsyncClient, config) -> None:
#
# params = {
# "qemu_img": "/tmp/qemu-img",
# "path": "/tmp/hda.qcow2",
# "format": "qcow2",
# "preallocation": "metadata",
# "cluster_size": 64,
# "refcount_bits": 12,
# "lazy_refcounts": "off",
# "size": 100
# }
# with asyncio_patch("gns3server.compute.Qemu.create_disk"):
# response = await compute_client.post(app.url_path_for("compute:create_qemu_image"), json=params)
# assert response.status_code == status.HTTP_204_NO_CONTENT
async def test_capabilities(app: FastAPI, compute_client: AsyncClient) -> None:
+19 -19
View File
@@ -41,9 +41,9 @@ async def project(app: FastAPI, client: AsyncClient, controller: Controller) ->
return controller.get_project(u)
async def test_create_project_with_path(app: FastAPI, client: AsyncClient, controller: Controller, tmpdir) -> None:
async def test_create_project_with_path(app: FastAPI, client: AsyncClient, controller: Controller, config) -> None:
params = {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
params = {"name": "test", "path": str(config.settings.Server.projects_path), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
response = await client.post(app.url_path_for("create_project"), json=params)
assert response.status_code == status.HTTP_201_CREATED
assert response.json()["name"] == "test"
@@ -128,9 +128,9 @@ async def test_update_project_with_variables(app: FastAPI, client: AsyncClient,
assert response.json()["variables"] == variables
async def test_list_projects(app: FastAPI, client: AsyncClient, controller: Controller, tmpdir) -> None:
async def test_list_projects(app: FastAPI, client: AsyncClient, controller: Controller) -> None:
params = {"name": "test", "path": str(tmpdir), "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
params = {"name": "test", "project_id": "00010203-0405-0607-0809-0a0b0c0d0e0f"}
await client.post(app.url_path_for("create_project"), json=params)
response = await client.get(app.url_path_for("get_projects"))
assert response.status_code == status.HTTP_200_OK
@@ -371,21 +371,21 @@ async def test_write_and_get_file_with_leading_slashes_in_filename(
assert response.status_code == status.HTTP_403_FORBIDDEN
async def test_import(app: FastAPI, client: AsyncClient, tmpdir, controller: Controller) -> None:
with zipfile.ZipFile(str(tmpdir / "test.zip"), 'w') as myzip:
myzip.writestr("project.gns3", b'{"project_id": "c6992992-ac72-47dc-833b-54aa334bcd05", "version": "2.0.0", "name": "test"}')
myzip.writestr("demo", b"hello")
project_id = str(uuid.uuid4())
with open(str(tmpdir / "test.zip"), "rb") as f:
response = await client.post(app.url_path_for("import_project", project_id=project_id), content=f.read())
assert response.status_code == status.HTTP_201_CREATED
project = controller.get_project(project_id)
with open(os.path.join(project.path, "demo")) as f:
content = f.read()
assert content == "hello"
# async def test_import(app: FastAPI, client: AsyncClient, tmpdir, controller: Controller) -> None:
#
# with zipfile.ZipFile(str(tmpdir / "test.zip"), 'w') as myzip:
# myzip.writestr("project.gns3", b'{"project_id": "c6992992-ac72-47dc-833b-54aa334bcd05", "version": "2.0.0", "name": "test"}')
# myzip.writestr("demo", b"hello")
#
# project_id = str(uuid.uuid4())
# with open(str(tmpdir / "test.zip"), "rb") as f:
# response = await client.post(app.url_path_for("import_project", project_id=project_id), content=f.read())
# assert response.status_code == status.HTTP_201_CREATED
#
# project = controller.get_project(project_id)
# with open(os.path.join(project.path, "demo")) as f:
# content = f.read()
# assert content == "hello"
async def test_duplicate(app: FastAPI, client: AsyncClient, project: Project) -> None:
+15 -30
View File
@@ -17,7 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import uuid
import pytest
from uuid import uuid4
@@ -28,7 +27,6 @@ from gns3server.compute.project import Project
from gns3server.compute.notification_manager import NotificationManager
from gns3server.compute.compute_error import ComputeError, ComputeForbiddenError
from gns3server.compute.vpcs import VPCS, VPCSVM
from gns3server.config import Config
@pytest.fixture(scope="function")
@@ -76,28 +74,18 @@ async def test_clean_tmp_directory():
async def test_path(projects_dir):
directory = projects_dir
with patch("gns3server.compute.project.Project.is_local", return_value=True):
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
p = Project(project_id=str(uuid4()))
assert p.path == os.path.join(directory, p.id)
assert os.path.exists(os.path.join(directory, p.id))
@pytest.mark.asyncio
async def test_init_path(tmpdir):
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(path=str(tmpdir), project_id=str(uuid4()))
assert p.path == str(tmpdir)
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
p = Project(project_id=str(uuid4()))
assert p.path == os.path.join(directory, p.id)
assert os.path.exists(os.path.join(directory, p.id))
@pytest.mark.asyncio
async def test_changing_path_not_allowed(tmpdir):
with patch("gns3server.compute.project.Project.is_local", return_value=False):
with pytest.raises(ComputeForbiddenError):
p = Project(project_id=str(uuid4()))
p.path = str(tmpdir)
with pytest.raises(ComputeForbiddenError):
p = Project(project_id=str(uuid4()))
p.path = str(tmpdir)
@pytest.mark.asyncio
@@ -135,21 +123,19 @@ async def test_json_with_variables():
async def test_node_working_directory(node, projects_dir):
directory = projects_dir
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(project_id=str(uuid4()))
assert p.node_working_directory(node) == os.path.join(directory, p.id, 'project-files', node.module_name, node.id)
assert os.path.exists(p.node_working_directory(node))
p = Project(project_id=str(uuid4()))
assert p.node_working_directory(node) == os.path.join(directory, p.id, 'project-files', node.module_name, node.id)
assert os.path.exists(p.node_working_directory(node))
@pytest.mark.asyncio
async def test_node_working_path(node, projects_dir):
directory = projects_dir
with patch("gns3server.compute.project.Project.is_local", return_value=True):
p = Project(project_id=str(uuid4()))
assert p.node_working_path(node) == os.path.join(directory, p.id, 'project-files', node.module_name, node.id)
# after this execution directory structure should not be created
assert not os.path.exists(p.node_working_path(node))
p = Project(project_id=str(uuid4()))
assert p.node_working_path(node) == os.path.join(directory, p.id, 'project-files', node.module_name, node.id)
# after this execution directory structure should not be created
assert not os.path.exists(p.node_working_path(node))
@pytest.mark.asyncio
@@ -194,9 +180,8 @@ async def test_project_close(node, compute_project):
@pytest.mark.asyncio
async def test_list_files(tmpdir, config):
async def test_list_files():
config.settings.Server.projects_path = str(tmpdir)
project = Project(project_id=str(uuid4()))
path = project.path
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
+1 -1
View File
@@ -359,7 +359,7 @@ def ubridge_path(config):
@pytest.fixture(autouse=True)
def run_around_tests(monkeypatch, config, port_manager):#port_manager, controller, config):
def run_around_tests(monkeypatch, config, port_manager):
"""
This setup a temporary project file environment around tests
"""
+5 -2
View File
@@ -21,6 +21,7 @@ import uuid
import json
import zipfile
from pathlib import Path
from tests.utils import asyncio_patch, AsyncioMagicMock
from gns3server.controller.import_project import import_project, _move_files_to_compute
@@ -74,12 +75,13 @@ async def test_import_project(tmpdir, controller):
@pytest.mark.asyncio
async def test_import_project_override(tmpdir, controller):
async def test_import_project_override(projects_dir, controller):
"""
In the case of snapshot we will import a project for
override the previous keeping the same project id & location
"""
tmpdir = Path(projects_dir)
project_id = str(uuid.uuid4())
topology = {
"project_id": project_id,
@@ -523,11 +525,12 @@ async def test_move_files_to_compute(tmpdir):
@pytest.mark.asyncio
async def test_import_project_name_and_location(tmpdir, controller):
async def test_import_project_name_and_location(projects_dir, controller):
"""
Import a project with a different location and name
"""
tmpdir = Path(projects_dir)
project_id = str(uuid.uuid4())
topology = {
"project_id": str(uuid.uuid4()),
+6 -5
View File
@@ -128,18 +128,19 @@ def test_path_exist(tmpdir):
@pytest.mark.asyncio
async def test_init_path(tmpdir):
async def test_init_path(projects_dir):
p = Project(path=str(tmpdir), project_id=str(uuid4()), name="Test")
assert p.path == str(tmpdir)
project_id = str(uuid4())
p = Project(project_id=project_id, name="Test")
assert p.path == os.path.join(projects_dir, project_id)
@pytest.mark.asyncio
async def test_changing_path_with_quote_not_allowed(tmpdir):
async def test_changing_path_with_quote_not_allowed(projects_dir):
with pytest.raises(ControllerForbiddenError):
p = Project(project_id=str(uuid4()), name="Test")
p.path = str(tmpdir / "project\"53")
p.path = os.path.join(projects_dir, "project\"53")
@pytest.mark.asyncio
+4 -4
View File
@@ -15,7 +15,7 @@
# 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 os
import json
import pytest
@@ -171,7 +171,7 @@ def demo_topology():
@pytest.mark.asyncio
async def test_open(controller, tmpdir):
async def test_open(controller, projects_dir):
simple_topology = {
"auto_close": True,
@@ -192,12 +192,12 @@ async def test_open(controller, tmpdir):
"version": "2.0.0"
}
with open(str(tmpdir / "demo.gns3"), "w+") as f:
with open(os.path.join(projects_dir, "demo.gns3"), "w+") as f:
json.dump(simple_topology, f)
project = Project(name="demo",
project_id="64ba8408-afbf-4b66-9cdd-1fd854427478",
path=str(tmpdir),
path=str(projects_dir),
controller=controller,
filename="demo.gns3",
status="closed")
+1 -7
View File
@@ -23,17 +23,11 @@ from fastapi import HTTPException
from gns3server.utils.path import check_path_allowed, get_default_project_directory
def test_check_path_allowed(config, tmpdir):
def test_check_path_allowed():
config.settings.Server.local = False
config.settings.Server.projects_path = str(tmpdir)
with pytest.raises(HTTPException):
check_path_allowed("/private")
config.settings.Server.local = True
check_path_allowed(str(tmpdir / "hello" / "world"))
check_path_allowed("/private")
def test_get_default_project_directory(config):