UnisKB/apps/models_provider/test_model_loading_api.py

68 lines
3.6 KiB
Python

import json
from types import SimpleNamespace
from unittest.mock import patch
from django.test import TestCase
from rest_framework.exceptions import ValidationError
from common.constants.permission_constants import RoleConstants
from common.exception.app_exception import AppUnauthorizedFailed
from models_provider.models import Model
from models_provider.views.runtime import ModelLoading
class LoadingApiTests(TestCase):
def setUp(self):
self.model = Model.objects.create(name='loading-test', provider='model_local_provider',
model_type='EMBEDDING', model_name='test-only', credential='test-only', meta={'existing': 'keep'})
self.admin = SimpleNamespace(auth=SimpleNamespace(role_list=[str(RoleConstants.ADMIN.value)], permission_list=[]), data={})
self.view = ModelLoading()
def args(self):
return {'workspace_id': 'default', 'model_id': str(self.model.id)}
def test_policy_is_persistent_and_preserves_existing_metadata(self):
self.admin.data = {'strategy': 'PRELOAD', 'priority': 90}
with patch('models_provider.views.runtime.runtime_request', return_value={'status': 'QUEUED'}) as rpc:
response = self.view.put(self.admin, **self.args())
self.model.refresh_from_db()
self.assertEqual(self.model.meta['existing'], 'keep')
self.assertEqual(self.model.meta['loading_policy'], {'strategy': 'PRELOAD', 'priority': 90, 'idle_seconds': 300})
self.assertEqual(json.loads(response.content)['data']['status'], 'QUEUED')
self.assertEqual(rpc.call_args_list[0].args[0], 'configure')
def test_normal_user_cannot_change_policy_or_load_weights(self):
user = SimpleNamespace(auth=SimpleNamespace(role_list=[], permission_list=[]), data={'action': 'load'})
for method in (self.view.get, self.view.put, self.view.post):
with self.assertRaises(AppUnauthorizedFailed):
method(user, **self.args())
def test_workspace_and_unsupported_provider_are_rejected(self):
with self.assertRaises(ValidationError):
self.view.get(self.admin, workspace_id='other', model_id=str(self.model.id))
self.model.provider = 'aliyun_bai_lian_model_provider'
self.model.model_type = 'LLM'; self.model.save()
response = self.view.get(self.admin, **self.args())
self.assertEqual(json.loads(response.content)['data']['status'], 'EXTERNAL')
with self.assertRaises(ValidationError):
self.view.put(self.admin, **self.args())
def test_arbitrary_actions_and_invalid_policies_do_not_reach_runtime(self):
with patch('models_provider.views.runtime.runtime_request') as rpc:
self.admin.data = {'action': 'shell'}
with self.assertRaises(ValidationError): self.view.post(self.admin, **self.args())
self.admin.data = {'priority': 500}
with self.assertRaises(ValidationError): self.view.put(self.admin, **self.args())
rpc.assert_not_called()
self.model.refresh_from_db()
self.assertNotIn('loading_policy', self.model.meta)
def test_saved_policy_remains_available_when_runtime_is_temporarily_down(self):
self.admin.data = {'strategy': 'PRELOAD', 'priority': 80}
with patch('models_provider.views.runtime.runtime_request', side_effect=ValueError('offline')):
with self.assertRaises(ValidationError): self.view.put(self.admin, **self.args())
response = self.view.get(self.admin, **self.args())
self.model.refresh_from_db()
self.assertEqual(self.model.meta['loading_policy']['priority'], 80)
self.assertEqual(json.loads(response.content)['data']['status'], 'UNAVAILABLE')