Skip to content

Commit 409a46d

Browse files
committed
src/services: Init unit-tests for kill & suite service
Signed-off-by: Kamoltat Sirivadhna <[email protected]>
1 parent 9bf2adf commit 409a46d

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed

Diff for: pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
pythonpath = src

Diff for: src/services/suite.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def make_run_name(run_dic):
6666
str(run_dic["timestamp"]),
6767
run_dic["suite"],
6868
run_dic["ceph_branch"],
69-
run_dic["kernel_branch"] or "-",
69+
run_dic["kernel_branch"] or "distro",
7070
run_dic["flavor"],
7171
worker,
7272
]

Diff for: src/services/test_kill.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from fastapi.testclient import TestClient
2+
from unittest.mock import patch
3+
4+
from ..main import app
5+
6+
client = TestClient(app)
7+
8+
9+
@patch("app.main.teuthology.kill.main")
10+
def test_kill_job_success(mock_teuthology_kill_main):
11+
mock_teuthology_kill_main.return_value = None
12+
response = client.post(
13+
"/kill",
14+
json={"run": "run1"},
15+
headers={"Authorization": "Bearer access_token"},
16+
)
17+
assert response.status_code == 200
18+
assert response.json() == {"kill": "success"}
19+
20+
21+
def test_missing_access_token():
22+
response = client.post("/kill", json={"run": "run1"})
23+
assert response.status_code == 401
24+
assert response.json() == {
25+
"detail": "You need to be logged in",
26+
"headers": {"WWW-Authenticate": "Bearer"},
27+
"status_code": 401,
28+
}
29+
30+
31+
def test_missing_run_argument():
32+
response = client.post(
33+
"/kill",
34+
headers={"Authorization": "Bearer access_token"},
35+
)
36+
assert response.status_code == 400
37+
assert response.json() == {"detail": "--run is a required argument", "status_code": 400}
38+
39+
40+
@patch("app.main.get_username")
41+
def test_insufficient_permission(mock_get_username):
42+
mock_get_username.return_value = "user1"
43+
response = client.post(
44+
"/kill",
45+
json={"run": "run1"},
46+
headers={"Authorization": "Bearer access_token"},
47+
)
48+
assert response.status_code == 401
49+
assert response.json() == {
50+
"detail": "You don't have permission to kill this run/job",
51+
"status_code": 401,
52+
}

Diff for: src/services/test_suite.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from fastapi.testclient import TestClient
2+
from main import app
3+
from suite import *
4+
5+
"""
6+
Created a TestClient instance and define a dictionary run_dic with test data for the make_run_name function,
7+
then called the function with the test data and assert that the result matches the expected output
8+
"""
9+
10+
def test_make_run_name():
11+
client = TestClient(app)
12+
run_dic = {
13+
"user": "testuser",
14+
"timestamp": "2022-03-21_14:30:00",
15+
"suite": "rados",
16+
"ceph_branch": "ceph1",
17+
"kernel_branch": "kernel1",
18+
"flavor": "test-flavor",
19+
"machine_type": "test-machine"
20+
}
21+
expected_result = "testuser-2022-03-21_14:30:00-rados-ceph1-kernel1-test-flavor-test-machine"
22+
assert make_run_name(run_dic) == expected_result
23+
24+
"""
25+
Test the `make_run_name` function with an input dictionary containing a single worker machine type.
26+
"""
27+
def test_make_run_name_with_single_worker():
28+
run_dic = {
29+
"user": "test_user",
30+
"timestamp": "2022-03-21_14:30:00",
31+
"suite": "rados",
32+
"ceph_branch": "ceph1",
33+
"kernel_branch": "kernel1",
34+
"flavor": "test-flavor",
35+
"machine_type": "worker1"
36+
}
37+
expected_run_name = "test_user-2022-03-21_14:30:00-rados-ceph1-kernel1-test-flavor-worker1"
38+
assert make_run_name(run_dic) == expected_run_name
39+
40+
"""
41+
Test the `make_run_name` function with a multi-machine type input dictionary.
42+
"""
43+
44+
def test_make_run_name_with_multi_worker():
45+
run_dic = {
46+
"user": "test_user",
47+
"timestamp": "2022-03-21_14:30:00",
48+
"suite": "rados",
49+
"ceph_branch": "ceph1",
50+
"kernel_branch": "kernel1",
51+
"flavor": "test-flavor",
52+
"machine_type": "worker1,worker2,worker3"
53+
}
54+
expected_run_name = "test_user-2022-03-21_14:30:00-rados-ceph1-kernel1-test-flavor-multi"
55+
assert make_run_name(run_dic) == expected_run_name
56+
57+
"""
58+
Test the function for no kernel branch
59+
"""
60+
def test_make_run_name_with_no_kernel_branch():
61+
run_dic = {
62+
"user": "teuthology",
63+
"timestamp": "2022-03-21_14:30:00",
64+
"suite": "rados",
65+
"ceph_branch": "ceph1",
66+
"kernel_branch": None,
67+
"flavor": "test-flavor",
68+
"machine_type": "test-machine"
69+
}
70+
expected_run_name = "teuthology-2022-03-21_14:30:00-rados-ceph1-distro-test-flavor-test-machine"
71+
assert make_run_name(run_dic) == expected_run_name

0 commit comments

Comments
 (0)