Skip to content

Commit c38c407

Browse files
committed
tests/kill: Add tests for /kill route
1 parent 9835777 commit c38c407

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

src/test_kill.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
from conftest import (KillArgs, kill_test_args, mock_kill_get_run_details,
2+
mock_kill_logs_run, mock_session_cookie,
3+
mock_teuthology_kill_main, test_client)
4+
5+
6+
def test_kill(mock_kill_get_run_details, mock_teuthology_kill_main):
7+
"""
8+
Test killing a run with valid and correct input
9+
"""
10+
request_body = kill_test_args.get_correct_fields()
11+
test_client.cookies = {"session": mock_session_cookie}
12+
response = test_client.post("/kill", json=request_body)
13+
14+
assert response.status_code == 200
15+
assert isinstance(response.json(), dict)
16+
17+
expected_fields = {"kill": str} # {"kill": "success"}
18+
for field, type in expected_fields.items():
19+
assert field in response.json()
20+
assert isinstance(response.json()[field], type)
21+
22+
assert response.json() == {"kill": "success"}
23+
24+
mock_kill_get_run_details.assert_called_once_with(request_body["--run"])
25+
mock_teuthology_kill_main.assert_called_once_with(request_body)
26+
27+
28+
# def test_kill_missing_required_fields(mock_kill_get_run_details):
29+
# """
30+
# Test killing a run without required fields
31+
# """
32+
# request_body = kill_test_args.remove_required_fields()
33+
# test_client.cookies = {"session": mock_session_cookie}
34+
# response = test_client.post("/kill", json=request_body)
35+
36+
# assert response.status_code == 422
37+
# assert "detail" in response.json()
38+
# assert isinstance(response.json()["detail"], list)
39+
40+
# required_fields = KillArgs.schema()["required"]
41+
# for index, field in enumerate(required_fields):
42+
# assert field in response.json()["detail"][index]["loc"]
43+
# assert "body" in response.json()["detail"][index]["loc"]
44+
# assert response.json()["detail"][index]["msg"] == "field required"
45+
# assert response.json()["detail"][index]["type"] == "value_error.missing"
46+
47+
# assert len(response.json()["detail"]) == len(required_fields)
48+
49+
50+
def test_kill_missing_non_required_fields(mock_kill_get_run_details):
51+
"""
52+
Test killing a run without non-required fields
53+
"""
54+
request_body = kill_test_args.remove_non_required_fields()
55+
test_client.cookies = {"session": mock_session_cookie}
56+
response = test_client.post("/kill", json=request_body)
57+
58+
assert response.status_code == 400
59+
assert isinstance(response.json(), dict)
60+
assert response.json() == {"detail": "--run is a required argument"}
61+
62+
63+
# def test_kill_invalid_fields(mock_kill_get_run_details):
64+
# """
65+
# Test killing a run with invalid input
66+
# """
67+
# request_body = {}
68+
# test_client.cookies = {"session": mock_session_cookie}
69+
# response = test_client.post("/kill", json=request_body)
70+
71+
# assert response.status_code == 422
72+
# assert isinstance(response.json(), dict)
73+
74+
# expected_fields = ["detail"]
75+
# for field in expected_fields:
76+
# assert field in response.json()
77+
78+
# assert "value is not a valid string" in response.json()["detail"][0]["msg"]
79+
80+
81+
# def test_kill_incorrect_fields(mock_kill_get_run_details):
82+
# """
83+
# Test killing a run with incorrect input
84+
# """
85+
# request_body = {}
86+
# test_client.cookies = {"session": mock_session_cookie}
87+
# response = test_client.post("/kill", json=request_body)
88+
89+
# assert response.status_code == 500
90+
# assert isinstance(response.json(), dict)
91+
92+
# expected_fields = {}
93+
# for field in expected_fields:
94+
# assert field in response.json()
95+
96+
97+
def test_kill_logs(
98+
mock_kill_get_run_details, mock_kill_logs_run, mock_teuthology_kill_main
99+
):
100+
"""
101+
Test killing a run with logs
102+
"""
103+
request_body = kill_test_args.get_correct_fields()
104+
test_client.cookies = {"session": mock_session_cookie}
105+
response = test_client.post("/kill?logs=True", json=request_body)
106+
107+
assert response.status_code == 200
108+
assert isinstance(response.json(), dict)
109+
110+
expected_fields = {"logs": list} # {"logs": logs}
111+
for field, type in expected_fields.items():
112+
assert field in response.json()
113+
assert isinstance(response.json()[field], type)
114+
115+
assert response.json()["logs"] != []
116+
117+
mock_kill_logs_run.assert_called_once_with(mock_teuthology_kill_main, request_body)
118+
119+
120+
def test_kill_logged_out():
121+
"""
122+
Test killing a run while logged out
123+
"""
124+
request_body = kill_test_args.get_correct_fields()
125+
test_client.cookies = {}
126+
response = test_client.post("/kill", json=request_body)
127+
128+
assert response.status_code == 401
129+
assert isinstance(response.json(), dict)
130+
assert response.json() == {"detail": "You need to be logged in"}
131+
assert "WWW-Authenticate" in response.headers
132+
assert response.headers["WWW-Authenticate"] == "Bearer"

0 commit comments

Comments
 (0)