-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Model reload changes. * Model reload changes. * Model reload changes. --------- Co-authored-by: Nupur Khare <[email protected]> Co-authored-by: udit-pandey <[email protected]>
- Loading branch information
1 parent
8ffc87b
commit 91775c1
Showing
6 changed files
with
136 additions
and
13 deletions.
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
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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import json | ||
import os | ||
import time | ||
from datetime import datetime, timedelta | ||
from unittest import mock | ||
from urllib.parse import urlencode, quote_plus | ||
|
||
|
@@ -828,6 +829,63 @@ def test_reload(mock_store): | |
assert header in actual_headers | ||
|
||
|
||
@patch('kairon.chat.utils.ChatUtils.reload') | ||
def test_reload_logging(mock_reload): | ||
processor = MongoProcessor() | ||
start_time = datetime.utcnow() - timedelta(days=1) | ||
end_time = datetime.utcnow() + timedelta(days=1) | ||
mock_reload.return_value = None | ||
response = client.get( | ||
f"/api/bot/{bot}/reload", | ||
headers={ | ||
"Authorization": token_type + " " + token | ||
}, | ||
) | ||
actual = response.json() | ||
assert actual["success"] | ||
assert actual["error_code"] == 0 | ||
assert actual["data"] is None | ||
assert actual["message"] == "Reloading Model!" | ||
logs = processor.get_logs(bot, "audit_logs", start_time, end_time) | ||
logs[0].pop('timestamp') | ||
logs[0].pop('_id') | ||
logs[0]['data'].pop('process_id') | ||
assert logs[0] == {'attributes': [{'key': 'bot', 'value': bot}], 'user': '[email protected]', 'action': 'activity', | ||
'entity': 'model_reload', | ||
'data': {'message': None, 'username': '[email protected]', 'exception': None, 'status': 'Success'}} | ||
|
||
|
||
@mock.patch('kairon.chat.agent_processor.AgentProcessor.reload', autospec=True) | ||
def test_reload_event_exception(mock_reload): | ||
processor = MongoProcessor() | ||
start_time = datetime.utcnow() - timedelta(days=1) | ||
end_time = datetime.utcnow() + timedelta(days=1) | ||
def _reload(*args): | ||
raise Exception('Simulated exception during model reload') | ||
|
||
mock_reload.side_effect = _reload | ||
response = client.get( | ||
f"/api/bot/{bot}/reload", | ||
headers={ | ||
"Authorization": token_type + " " + token | ||
}, | ||
) | ||
actual = response.json() | ||
assert actual["success"] | ||
assert actual["error_code"] == 0 | ||
assert actual["data"] is None | ||
assert actual["message"] == "Reloading Model!" | ||
logs = processor.get_logs(bot, "audit_logs", start_time, end_time) | ||
logs[0].pop('timestamp') | ||
logs[0].pop('_id') | ||
logs[0]['data'].pop('process_id') | ||
assert logs[0] == {'attributes': [{'key': 'bot', 'value': bot}], 'user': '[email protected]', | ||
'action': 'activity', 'entity': 'model_reload', | ||
'data': {'message': None, 'username': '[email protected]', | ||
'exception': 'Simulated exception during model reload', 'status': 'Failed'}, | ||
} | ||
|
||
|
||
def test_reload_exception(): | ||
response = client.get( | ||
f"/api/bot/{bot}/reload", | ||
|
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 |
---|---|---|
|
@@ -5835,6 +5835,9 @@ def test_set_templates_insecure(): | |
|
||
@responses.activate | ||
def test_reload_model(monkeypatch): | ||
processor = MongoProcessor() | ||
start_time = datetime.utcnow() - timedelta(days=1) | ||
end_time = datetime.utcnow() + timedelta(days=1) | ||
def mongo_store(*arge, **kwargs): | ||
return None | ||
|
||
|
@@ -5859,6 +5862,39 @@ def mongo_store(*arge, **kwargs): | |
assert actual['error_code'] == 0 | ||
assert actual['message'] == "Reloading Model!" | ||
assert actual['success'] | ||
logs = processor.get_logs(pytest.bot, "audit_logs", start_time, end_time) | ||
logs[0].pop('timestamp') | ||
logs[0].pop('_id') | ||
assert logs[0] == {'attributes': [{'key': 'bot', 'value': pytest.bot}], 'user': '[email protected]', 'action': 'activity', 'entity': 'model_reload', 'data': {'message': None, 'username': '[email protected]', 'exception': None, 'status': 'Initiated'}} | ||
|
||
|
||
@responses.activate | ||
def test_reload_model_exception(): | ||
processor = MongoProcessor() | ||
start_time = datetime.utcnow() - timedelta(days=1) | ||
end_time = datetime.utcnow() + timedelta(days=1) | ||
# def mongo_store(*arge, **kwargs): | ||
# return None | ||
# | ||
# monkeypatch.setattr(Utility, "get_local_mongo_store", mongo_store) | ||
# monkeypatch.setitem(Utility.environment['action'], "url", None) | ||
# monkeypatch.setitem(Utility.environment['model']['agent'], "url", "http://localhost/") | ||
response = client.get( | ||
f"/api/bot/{pytest.bot}/model/reload", | ||
headers={"Authorization": pytest.token_type + " " + pytest.access_token} | ||
) | ||
actual = response.json() | ||
assert actual['data'] is None | ||
assert not actual["success"] | ||
assert actual["error_code"] == 422 | ||
assert actual['message'] == 'Agent config not found!' | ||
logs = processor.get_logs(pytest.bot, "audit_logs", start_time, end_time) | ||
logs[0].pop('timestamp') | ||
logs[0].pop('_id') | ||
assert logs[0] == {'attributes': [{'key': 'bot', 'value': pytest.bot}], | ||
'user': '[email protected]', 'action': 'activity', 'entity': 'model_reload', | ||
'data': {'message': None, 'username': '[email protected]', | ||
'exception': 'Agent config not found!', 'status': 'Failed'}} | ||
|
||
|
||
def test_get_config_templates(): | ||
|