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 22, 2023
1 parent 573fa93 commit ba83028
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pytest.ini
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
2 changes: 1 addition & 1 deletion src/teuthology_api/services/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def make_run_name(run_dic):
str(run_dic["timestamp"]),
run_dic["suite"],
run_dic["ceph_branch"],
run_dic["kernel_branch"] or "-",
run_dic["kernel_branch"] or "distro",
run_dic["flavor"],
worker,
]
Expand Down
56 changes: 56 additions & 0 deletions src/teuthology_api/services/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from fastapi.testclient import TestClient
from fastapi import HTTPException
import pytest
from main import app
from mock import patch
from 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('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('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('services.helpers.Request')
def test_get_username_success(m_request):
m_request = MockRequest()
expected = "user1"
actual = get_username(m_request)
assert expected == actual

@patch('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'

45 changes: 45 additions & 0 deletions src/teuthology_api/services/test_kill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from fastapi.testclient import TestClient
from main import app
from mock import patch
from services.helpers import get_token
from services.kill import get_username, get_run_details
import json
from 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('services.kill.teuthology.kill.main')
@patch('services.kill.get_run_details')
@patch('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"}
91 changes: 91 additions & 0 deletions src/teuthology_api/services/test_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from fastapi.testclient import TestClient
from main import app
from mock import patch
from services.helpers import get_token
from 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('services.suite.teuthology.suite.main')
@patch('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

0 comments on commit ba83028

Please sign in to comment.