Skip to content

Commit

Permalink
src/services: Init unit-tests for kill & suite service
Browse files Browse the repository at this point in the history
Signed-off-by: Kamoltat Sirivadhna <[email protected]>
  • Loading branch information
kamoltat committed Nov 1, 2023
1 parent 9bf2adf commit 7f284bc
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/services/test_kill.py
Original file line number Diff line number Diff line change
@@ -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,
}
71 changes: 71 additions & 0 deletions src/services/test_suite.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7f284bc

Please sign in to comment.