forked from VallariAg/teuthology-api
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from kamoltat/wip-ksirivad-add-unit-test
src/services: Init unit-tests for kill & suite service
- Loading branch information
Showing
5 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[pytest] | ||
pythonpath = src | ||
log_cli = 1 | ||
log_cli_level = INFO | ||
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s) | ||
log_cli_date_format=%Y-%m-%d %H:%M:%S |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from fastapi.testclient import TestClient | ||
from fastapi import HTTPException | ||
import pytest | ||
from teuthology_api.main import app | ||
from unittest.mock import patch | ||
from teuthology_api.services.helpers import Request, get_token, get_username | ||
|
||
client = TestClient(app) | ||
|
||
|
||
class MockRequest: | ||
def __init__(self, access_token="testToken123", bad=False): | ||
if bad: | ||
self.session = {} | ||
else: | ||
self.session = { | ||
"user": { | ||
"username": "user1", | ||
"access_token": access_token, | ||
} | ||
} | ||
|
||
|
||
# get_token | ||
@patch("teuthology_api.services.helpers.Request") | ||
def test_get_token_success(m_request): | ||
m_request = MockRequest() | ||
expected = {"access_token": "testToken123", "token_type": "bearer"} | ||
actual = get_token(m_request) | ||
assert expected == actual | ||
|
||
|
||
@patch("teuthology_api.services.helpers.Request") | ||
def test_get_token_fail(m_request): | ||
with pytest.raises(HTTPException) as err: | ||
m_request = MockRequest(bad=True) | ||
get_token(m_request) | ||
assert err.value.status_code == 401 | ||
assert err.value.detail == "You need to be logged in" | ||
|
||
|
||
# get username | ||
@patch("teuthology_api.services.helpers.Request") | ||
def test_get_username_success(m_request): | ||
m_request = MockRequest() | ||
expected = "user1" | ||
actual = get_username(m_request) | ||
assert expected == actual | ||
|
||
|
||
@patch("teuthology_api.services.helpers.Request") | ||
def test_get_username_fail(m_request): | ||
with pytest.raises(HTTPException) as err: | ||
m_request = MockRequest(bad=True) | ||
get_username(m_request) | ||
assert err.value.status_code == 401 | ||
assert err.value.detail == "You need to be logged in" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from fastapi.testclient import TestClient | ||
from teuthology_api.main import app | ||
from unittest.mock import patch | ||
from teuthology_api.services.helpers import get_token | ||
from teuthology_api.services.kill import get_username, get_run_details | ||
import json | ||
from teuthology_api.schemas.kill import KillArgs | ||
|
||
client = TestClient(app) | ||
|
||
|
||
async def override_get_token(): | ||
return {"access_token": "token_123", "token_type": "bearer"} | ||
|
||
|
||
app.dependency_overrides[get_token] = override_get_token | ||
|
||
mock_kill_args = { | ||
"--dry-run": False, | ||
"--non-interactive": False, | ||
"--verbose": 1, | ||
"--help": False, | ||
"--user": "mock_user", | ||
"--owner": "user1", | ||
"--run": "mock_run", | ||
"--preserve-queue": None, | ||
"--job": None, | ||
"--jobspec": None, | ||
"--machine-type": "testnode", | ||
"--archive": None, | ||
} | ||
|
||
|
||
@patch("teuthology_api.services.kill.teuthology.kill.main") | ||
@patch("teuthology_api.services.kill.get_run_details") | ||
@patch("teuthology_api.services.kill.get_username") | ||
def test_kill_run_success(m_get_username, m_get_run_details, m_teuth_kill_main): | ||
m_get_username.return_value = "user1" | ||
m_get_run_details.return_value = {"id": "7451978", "user": "user1"} | ||
m_teuth_kill_main.return_value = None | ||
response = client.post("/kill", data=json.dumps(mock_kill_args)) | ||
assert response.status_code == 200 | ||
assert response.json() == {"kill": "success"} | ||
|
||
|
||
def test_kill_run_fail(): | ||
response = client.post("/kill", data=json.dumps(mock_kill_args)) | ||
assert response.status_code == 401 | ||
assert response.json() == {"detail": "You need to be logged in"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from fastapi.testclient import TestClient | ||
from teuthology_api.main import app | ||
from unittest.mock import patch | ||
from teuthology_api.services.helpers import get_token | ||
from teuthology_api.services.suite import make_run_name, get_run_details | ||
import json | ||
|
||
client = TestClient(app) | ||
|
||
|
||
async def override_get_token(): | ||
return {"access_token": "token_123", "token_type": "bearer"} | ||
|
||
|
||
app.dependency_overrides[get_token] = override_get_token | ||
|
||
mock_suite_args = { | ||
"--dry-run": False, | ||
"--non-interactive": False, | ||
"--verbose": 1, | ||
"--help": False, | ||
"--user": "mock_user", | ||
"--timestamp": "2023-10-21_14:30:00", | ||
"--owner": "user1", | ||
"--suite": "rados", | ||
"--ceph": "ceph1", | ||
"--kernel": "kernel1", | ||
"--flavor": "test-flavor", | ||
"--machine-type": "testnode", | ||
} | ||
|
||
# suite | ||
@patch("teuthology_api.services.suite.teuthology.suite.main") | ||
@patch("teuthology_api.services.suite.get_run_details") | ||
def test_suite_run_success(m_get_run_details, m_teuth_suite_main): | ||
m_get_run_details.return_value = {"id": "7451978", "user": "user1"} | ||
response = client.post("/suite", data=json.dumps(mock_suite_args)) | ||
assert response.status_code == 200 | ||
assert response.json() == {"run": {"id": "7451978", "user": "user1"}, "logs": []} | ||
|
||
|
||
# make_run_name | ||
|
||
|
||
def test_make_run_name(): | ||
m_run_dic = { | ||
"user": "testuser", | ||
"timestamp": "2022-03-21_14:30:00", | ||
"suite": "rados", | ||
"ceph_branch": "ceph1", | ||
"kernel_branch": "kernel1", | ||
"flavor": "test-flavor", | ||
"machine_type": "test-machine", | ||
} | ||
expected = ( | ||
"testuser-2022-03-21_14:30:00-rados-ceph1-kernel1-test-flavor-test-machine" | ||
) | ||
assert make_run_name(m_run_dic) == expected | ||
|
||
|
||
def test_make_run_name_with_single_worker(): | ||
m_run_dic = { | ||
"user": "test_user", | ||
"timestamp": "2022-03-21_14:30:00", | ||
"suite": "rados", | ||
"ceph_branch": "ceph1", | ||
"kernel_branch": "kernel1", | ||
"flavor": "test-flavor", | ||
"machine_type": "worker1", | ||
} | ||
expected = "test_user-2022-03-21_14:30:00-rados-ceph1-kernel1-test-flavor-worker1" | ||
assert make_run_name(m_run_dic) == expected | ||
|
||
|
||
def test_make_run_name_with_multi_worker(): | ||
m_run_dic = { | ||
"user": "test_user", | ||
"timestamp": "2022-03-21_14:30:00", | ||
"suite": "rados", | ||
"ceph_branch": "ceph1", | ||
"kernel_branch": "kernel1", | ||
"flavor": "test-flavor", | ||
"machine_type": "worker1,worker2,worker3", | ||
} | ||
expected = "test_user-2022-03-21_14:30:00-rados-ceph1-kernel1-test-flavor-multi" | ||
assert make_run_name(m_run_dic) == expected | ||
|
||
|
||
def test_make_run_name_with_no_kernel_branch(): | ||
m_run_dic = { | ||
"user": "teuthology", | ||
"timestamp": "2022-03-21_14:30:00", | ||
"suite": "rados", | ||
"ceph_branch": "ceph1", | ||
"kernel_branch": None, | ||
"flavor": "test-flavor", | ||
"machine_type": "test-machine", | ||
} | ||
expected = ( | ||
"teuthology-2022-03-21_14:30:00-rados-ceph1-distro-test-flavor-test-machine" | ||
) | ||
assert make_run_name(m_run_dic) == expected |