From 94d7f29de8a97c31ba279ebde251dbbdc1b9e0fb Mon Sep 17 00:00:00 2001 From: majorchork Date: Mon, 20 Mar 2023 10:06:55 +0100 Subject: [PATCH] feat: add unit tests for routes Signed-off-by: majorchork --- src/tests/__init__.py | 0 src/tests/conftest.py | 111 ++++++++++++++++++++++++ src/tests/test_routes/__init__.py | 0 src/tests/test_routes/test_kill | 10 +++ src/tests/test_routes/test_login.py | 3 + src/tests/test_routes/test_main.py | 7 ++ src/tests/test_routes/test_suite.py | 22 +++++ src/tests/test_schemas/__init__.py | 0 src/tests/test_schemas/test_base.py | 15 ++++ src/tests/test_schemas/test_kill.py | 22 +++++ src/tests/test_schemas/test_schedule.py | 29 +++++++ src/tests/test_schemas/test_suite.py | 20 +++++ 12 files changed, 239 insertions(+) create mode 100644 src/tests/__init__.py create mode 100644 src/tests/conftest.py create mode 100644 src/tests/test_routes/__init__.py create mode 100644 src/tests/test_routes/test_kill create mode 100644 src/tests/test_routes/test_login.py create mode 100644 src/tests/test_routes/test_main.py create mode 100644 src/tests/test_routes/test_suite.py create mode 100644 src/tests/test_schemas/__init__.py create mode 100644 src/tests/test_schemas/test_base.py create mode 100644 src/tests/test_schemas/test_kill.py create mode 100644 src/tests/test_schemas/test_schedule.py create mode 100644 src/tests/test_schemas/test_suite.py diff --git a/src/tests/__init__.py b/src/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/conftest.py b/src/tests/conftest.py new file mode 100644 index 0000000..3c40c2a --- /dev/null +++ b/src/tests/conftest.py @@ -0,0 +1,111 @@ +import pytest +from fastapi.testclient import TestClient +from schemas.base import BaseArgs +from schemas.schedule import SchedulerArgs +from schemas.suite import SuiteArgs +from schemas.kill import KillArgs + + +from main import app + +client = TestClient(app) + + +@pytest.fixture(scope="module") +def test_client() -> TestClient: + """ + create test client instance + """ + client = TestClient(app) + yield client + + +@pytest.fixture(scope="module") +def suite_payload() -> dict: + """ + create test client instance + """ + payload = { + "--ceph": "wip-dis-testing-2", + "--ceph-repo": "https://github.com/ceph/ceph-ci.git", + "--kernel": "distro", + "--limit": "2", + "--newest": "0", + "--machine-type": "testnode", + "--num": "1", + "--priority": "70", + "--suite": "teuthology:no-ceph", + "--suite-branch": "wip-dis-testing-2", + "--suite-repo": "https://github.com/ceph/ceph-ci.git", + "--teuthology-branch": "main", + "--verbose": "1", + "--user": "vallariag" + } + + return payload + + +@pytest.fixture(scope="module") +def base_schema_payload() -> tuple: + payload: dict = { + "--dry-run":True, + "--non-interactive":True, + "--verbose":1, + "--help":True, + "--user":"vallariag" + } + + return (payload, BaseArgs ) + + +@pytest.fixture(scope="module") +def schedule_schema_payload() -> tuple: + payload: dict = { + "--owner":"vallariag", + "--seed":"-4", + "--force-priority":True, + "--no-nested-subset":True, + "--job-threshold":"627", + } + + return (payload, SchedulerArgs) + + + + +@pytest.fixture(scope="module") +def suite_schema_payload() -> tuple: + payload: dict = { + "--ceph": "wip-dis-testing-2", + "--ceph-repo": "https://github.com/ceph/ceph-ci.git", + "--kernel": "distro", + "--limit": "2", + "--newest": "0", + "--machine-type": "testnode", + "--num": "1", + "--priority": "70", + "--suite": "teuthology:no-ceph", + "--suite-branch": "wip-dis-testing-2", + "--suite-repo": "https://github.com/ceph/ceph-ci.git", + "--teuthology-branch": "main", + "--verbose": "1", + "--user": "vallariag" + } + return (payload, SuiteArgs) + + +@pytest.fixture(scope="module") +def kill_schema_payload() -> tuple: + payload: dict = { + + "--run": "run job", + "--preserve-queue": True, + "--job": ["job1", "job2"], + "--jobspec": "do job", + "--machine-type": "debian", + "--archive": "", + "--user": "vallariag", + "--dry-run": False, + } + + return (payload, KillArgs) diff --git a/src/tests/test_routes/__init__.py b/src/tests/test_routes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/test_routes/test_kill b/src/tests/test_routes/test_kill new file mode 100644 index 0000000..e72a288 --- /dev/null +++ b/src/tests/test_routes/test_kill @@ -0,0 +1,10 @@ + + +def test_create_kill(test_client, payload): + response = test_client.post( + "/kill", + headers={"Content-Type": "application/json"}, + params={"dry_run": False, "logs":True}, + json=payload, + ) + assert response.status_code == 200 \ No newline at end of file diff --git a/src/tests/test_routes/test_login.py b/src/tests/test_routes/test_login.py new file mode 100644 index 0000000..636923c --- /dev/null +++ b/src/tests/test_routes/test_login.py @@ -0,0 +1,3 @@ +def test_git_login(test_client): + response = test_client.get("/login") + assert response.status_code == 302 diff --git a/src/tests/test_routes/test_main.py b/src/tests/test_routes/test_main.py new file mode 100644 index 0000000..d1face1 --- /dev/null +++ b/src/tests/test_routes/test_main.py @@ -0,0 +1,7 @@ + + +def test_root(test_client): + response = test_client.get("/") + assert response.status_code == 200 + assert response.json().get("root") == "success" + assert len(response.json()) == 2 \ No newline at end of file diff --git a/src/tests/test_routes/test_suite.py b/src/tests/test_routes/test_suite.py new file mode 100644 index 0000000..3d9bb1e --- /dev/null +++ b/src/tests/test_routes/test_suite.py @@ -0,0 +1,22 @@ +def test_create_suite(test_client, suite_payload): + response = test_client.post( + "/suite", + headers={"Content-Type": "application/json"}, + params={"dry_run": False, "logs":True}, + json=suite_payload, + ) + assert response.status_code == 200 + assert response.get("run") == {} + assert len(response.get("logs")) > 0 + + +def test_failed_create_suite(test_client, suite_payload): + response = test_client.post( + "/suite", + headers={"Content-Type": "application/json"}, + params={"dry_run": False, "logs":True}, + json=suite_payload, + ) + assert response.status_code == 401 + + diff --git a/src/tests/test_schemas/__init__.py b/src/tests/test_schemas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/test_schemas/test_base.py b/src/tests/test_schemas/test_base.py new file mode 100644 index 0000000..ec86017 --- /dev/null +++ b/src/tests/test_schemas/test_base.py @@ -0,0 +1,15 @@ + + + +from schemas.base import BaseArgs + +def test_base_schema(base_schema_payload): + payload, BaseArgs = base_schema_payload + base_args = BaseArgs(**payload) + + assert base_args.dry_run == payload.get("--dry-run") + assert base_args.non_interactive == payload.get("--non-interactive") + assert base_args.help == payload.get("--help") + assert base_args.user == payload.get("--user") + assert base_args.verbose == payload.get("--verbose") + \ No newline at end of file diff --git a/src/tests/test_schemas/test_kill.py b/src/tests/test_schemas/test_kill.py new file mode 100644 index 0000000..994e1ad --- /dev/null +++ b/src/tests/test_schemas/test_kill.py @@ -0,0 +1,22 @@ + +def test_kill_schema(base_schema_payload, kill_schema_payload): + """ + test ScheduleArgs schema which inherits from BaseArgs + use the base_schema fixtures + """ + bs_payload, _ = base_schema_payload + ks_payload, KilArgs = kill_schema_payload + + ks_payload.update(bs_payload) + + kill_args = KilArgs(**ks_payload) + + assert kill_args.owner == ks_payload.get("--owner") + assert kill_args.run == ks_payload.get("--run") + assert kill_args.preserve_queue == ks_payload.get("--preserve-queue") + assert kill_args.job == ks_payload.get("--job") + assert kill_args.machine_type == ks_payload.get("--machine-type") + assert kill_args.archive == ks_payload.get("--archive") + + + \ No newline at end of file diff --git a/src/tests/test_schemas/test_schedule.py b/src/tests/test_schemas/test_schedule.py new file mode 100644 index 0000000..9fb1fb2 --- /dev/null +++ b/src/tests/test_schemas/test_schedule.py @@ -0,0 +1,29 @@ + +def test_schedule_schema(base_schema_payload, schedule_schema_payload): + """ + test ScheduleArgs schema which inherits from BaseArgs + use the base_schema fixtures + """ + bs_payload, _ = base_schema_payload + + ss_payload, SchedulerArgs = schedule_schema_payload + + ss_payload.update(bs_payload) + + schedule_args = SchedulerArgs(**ss_payload) + + assert schedule_args.dry_run == ss_payload.get("--dry-run") + assert schedule_args.non_interactive == ss_payload.get("--non-interactive") + assert schedule_args.help == ss_payload.get("--help") + assert schedule_args.user == ss_payload.get("--user") + assert schedule_args.filter_all == None + assert schedule_args.subset == None + assert schedule_args.owner == ss_payload.get("--owner") + assert schedule_args.force_priority == ss_payload.get("--force-priority") + assert schedule_args.no_nested_subset == ss_payload.get("--no-nested-subset") + assert schedule_args.job_threshold == ss_payload.get("--job-threshold") + + + + + diff --git a/src/tests/test_schemas/test_suite.py b/src/tests/test_schemas/test_suite.py new file mode 100644 index 0000000..98c4be5 --- /dev/null +++ b/src/tests/test_schemas/test_suite.py @@ -0,0 +1,20 @@ + +def test_suite_schema(base_schema_payload, suite_schema_payload): + """ + test ScheduleArgs schema which inherits from BaseArgs + use the base_schema fixtures + """ + bs_payload, _ = base_schema_payload + ss_payload, SuiteArgs = suite_schema_payload + + ss_payload.update(bs_payload) + + suite_args = SuiteArgs(**ss_payload) + + assert suite_args.dry_run == ss_payload.get("--dry-run") + assert suite_args.suite == ss_payload.get("--suite") + assert suite_args.suite_branch == ss_payload.get("--suite-branch") + assert suite_args.suite_repo == ss_payload.get("--suite-repo") + + +