From 27da8458e825301a282627916abf2336d69a8ab4 Mon Sep 17 00:00:00 2001 From: grossmj Date: Mon, 29 Jun 2020 18:43:35 +0930 Subject: [PATCH] Allow tests to be run by root. Fixes #1784 --- tests/compute/test_project.py | 2 ++ tests/handlers/api/compute/test_dynamips.py | 9 +++++---- tests/handlers/api/compute/test_qemu.py | 12 ++++++------ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tests/compute/test_project.py b/tests/compute/test_project.py index f04b16a0..fc6db970 100644 --- a/tests/compute/test_project.py +++ b/tests/compute/test_project.py @@ -17,6 +17,7 @@ # along with this program. If not, see . import os +import sys import uuid import asyncio import pytest @@ -149,6 +150,7 @@ async def test_project_delete(): assert os.path.exists(directory) is False +@pytest.mark.skipif(not sys.platform.startswith("win") and os.getuid() == 0, reason="Root can delete any project") async def test_project_delete_permission_issue(): project = Project(project_id=str(uuid4())) diff --git a/tests/handlers/api/compute/test_dynamips.py b/tests/handlers/api/compute/test_dynamips.py index 19e6504d..2831b6e7 100644 --- a/tests/handlers/api/compute/test_dynamips.py +++ b/tests/handlers/api/compute/test_dynamips.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import pytest +import sys import os import stat from unittest.mock import patch @@ -184,13 +185,13 @@ async def test_upload_image(compute_api, images_dir): assert checksum == "033bd94b1168d7e4f0d644c3c95e35bf" -async def test_upload_image_permission_denied(compute_api, tmpdir, images_dir): +@pytest.mark.skipif(not sys.platform.startswith("win") and os.getuid() == 0, reason="Root can delete any image") +async def test_upload_image_permission_denied(compute_api, images_dir): os.makedirs(os.path.join(images_dir, "IOS"), exist_ok=True) with open(os.path.join(images_dir, "IOS", "test2.tmp"), "w+") as f: f.write("") os.chmod(os.path.join(images_dir, "IOS", "test2.tmp"), 0) - with patch("gns3server.utils.images.default_images_directory", return_value=str(tmpdir)): - response = await compute_api.post("/dynamips/images/test2", body="TEST", raw=True) - assert response.status == 409 + response = await compute_api.post("/dynamips/images/test2", body="TEST", raw=True) + assert response.status == 409 diff --git a/tests/handlers/api/compute/test_qemu.py b/tests/handlers/api/compute/test_qemu.py index 9cb9e8b3..3c349797 100644 --- a/tests/handlers/api/compute/test_qemu.py +++ b/tests/handlers/api/compute/test_qemu.py @@ -315,15 +315,15 @@ async def test_upload_image_forbiden_location(compute_api, tmpdir): assert response.status == 404 -async def test_upload_image_permission_denied(compute_api, tmpdir): +@pytest.mark.skipif(not sys.platform.startswith("win") and os.getuid() == 0, reason="Root can delete any image") +async def test_upload_image_permission_denied(compute_api, images_dir): - with open(str(tmpdir / "test2.tmp"), "w+") as f: + with open(os.path.join(images_dir, "QEMU", "test2.tmp"), "w+") as f: f.write("") - os.chmod(str(tmpdir / "test2.tmp"), 0) + os.chmod(os.path.join(images_dir, "QEMU", "test2.tmp"), 0) - with patch("gns3server.compute.Qemu.get_images_directory", return_value=str(tmpdir)): - response = await compute_api.post("/qemu/images/test2", body="TEST", raw=True) - assert response.status == 409 + response = await compute_api.post("/qemu/images/test2", body="TEST", raw=True) + assert response.status == 409 async def test_create_img_relative(compute_api):