From 7f284bc949b5a55e204b9ab248e6c381ba42863b Mon Sep 17 00:00:00 2001 From: Kamoltat Sirivadhna Date: Wed, 1 Nov 2023 11:53:40 -0400 Subject: [PATCH] src/services: Init unit-tests for kill & suite service Signed-off-by: Kamoltat Sirivadhna --- src/services/test_kill.py | 52 ++++++++++++++++++++++++++++ src/services/test_suite.py | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/services/test_kill.py create mode 100644 src/services/test_suite.py diff --git a/src/services/test_kill.py b/src/services/test_kill.py new file mode 100644 index 0000000..4356d0d --- /dev/null +++ b/src/services/test_kill.py @@ -0,0 +1,52 @@ +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, + } \ No newline at end of file diff --git a/src/services/test_suite.py b/src/services/test_suite.py new file mode 100644 index 0000000..00066de --- /dev/null +++ b/src/services/test_suite.py @@ -0,0 +1,71 @@ +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 \ No newline at end of file