diff --git a/src/services/test_kill.py b/src/services/test_kill.py new file mode 100644 index 0000000..40e8e9b --- /dev/null +++ b/src/services/test_kill.py @@ -0,0 +1,106 @@ +from fastapi.testclient import TestClient +from unittest.mock import patch + +from main import app + + +client = TestClient(app) + + +@patch("app.main.teuthology.kill.main") +def test_kill_job_success(mock_teuthology_kill_main): + mock_teuthology_kill_main.return_value = None + response = client.post( + "/kill", + json={"run": "run1"}, + headers={"Authorization": "Bearer access_token"}, + ) + assert response.status_code == 200 + assert response.json() == {"kill": "success"} + + +def test_missing_access_token(): + response = client.post("/kill", json={"run": "run1"}) + assert response.status_code == 401 + assert response.json() == { + "detail": "You need to be logged in", + "headers": {"WWW-Authenticate": "Bearer"}, + "status_code": 401, + } + + +def test_missing_run_argument(): + response = client.post( + "/kill", + headers={"Authorization": "Bearer access_token"}, + ) + assert response.status_code == 400 + assert response.json() == {"detail": "--run is a required argument", "status_code": 400} + + +@patch("app.main.get_username") +def test_insufficient_permission(mock_get_username): + mock_get_username.return_value = "user1" + response = client.post( + "/kill", + json={"run": "run1"}, + headers={"Authorization": "Bearer access_token"}, + ) + assert response.status_code == 401 + assert response.json() == { + "detail": "You don't have permission to kill this run/job", + "status_code": 401, + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/services/test_suite.py b/src/services/test_suite.py new file mode 100644 index 0000000..caac378 --- /dev/null +++ b/src/services/test_suite.py @@ -0,0 +1,73 @@ +from fastapi.testclient import TestClient +from main import app +from suite import * + +""" +Created a TestClient instance and define a dictionary run_dic with test data for the make_run_name function, +then called the function with the test data and assert that the result matches the expected output + +""" + +def test_make_run_name(): + client = TestClient(app) + run_dic = { + "user": "testuser", + "timestamp": "2022-03-21_14:30:00", + "suite": "test-suite", + "ceph_branch": "ceph1", + "kernel_branch": "kernel1", + "flavor": "test-flavor", + "machine_type": "test-machine" + } + expected_result = "testuser-2022-03-21_14:30:00-test-suite-ceph1-kernel1-test-flavor-test-machine" + assert make_run_name(run_dic) == expected_result + +""" + Test the `make_run_name` function with an input dictionary containing a single worker machine type. +""" +def test_make_run_name_with_single_worker(): + run_dic = { + "user": "test_user", + "timestamp": "2022-03-21_14:30:00", + "suite": "testing_suite", + "ceph_branch": "ceph1", + "kernel_branch": "kernel1", + "flavor": "test-flavor", + "machine_type": "worker1" + } + expected_run_name = "test_user-2022-03-21_14:30:00-testing_suite-ceph1-kernel1-test-flavor-worker1" + assert make_run_name(run_dic) == expected_run_name + +""" +Test the `make_run_name` function with a multi-machine type input dictionary. +""" + +def test_make_run_name_with_multi_worker(): + run_dic = { + "user": "test_user", + "timestamp": "2022-03-21_14:30:00", + "suite": "test-suite", + "ceph_branch": "ceph1", + "kernel_branch": "kernel1", + "flavor": "test-flavor", + "machine_type": "worker1,worker2,worker3" + } + expected_run_name = "test_user-2022-03-21_14:30:00-test_suite-ceph1-kernel1-test-flavor-multi" + assert make_run_name(run_dic) == expected_run_name + +""" +Test the function for no kernel branch +""" +def test_make_run_name_with_no_kernel_branch(): + run_dic = { + "user": "teuthology", + "timestamp": "2022-03-21_14:30:00", + "suite": "test-suite", + "ceph_branch": "ceph1", + "kernel_branch": None, + "flavor": "test-flavor", + "machine_type": "test-machine" + } + expected_run_name = "teuthology-2022-03-21_14:30:00-test-suite-ceph1--test-flavor-test-machine" + assert make_run_name(run_dic) == expected_run_name + diff --git a/src/test_error_handling.py b/src/test_error_handling.py new file mode 100644 index 0000000..38c112d --- /dev/null +++ b/src/test_error_handling.py @@ -0,0 +1,24 @@ +from fastapi.testclient import TestClient +from app import app +import pytest + + +client = TestClient(app) + +def test_invalid_job_id(): + response = client.get("/jobs/1234") + assert response.status_code == 404 + assert response.json() == {"detail": "Job not found"} + +def test_missing_parameter(): + response = client.post("/jobs/run", json={}) + assert response.status_code == 422 + assert response.json() == { + "detail": [ + { + "loc": ["body", "config"], + "msg": "field required", + "type": "value_error.missing" + } + ] + }