mirror of
https://github.com/ansible/awx.git
synced 2026-06-24 16:17:51 -02:30
Move plugin loading to lazy-on-first-access, DB sync to dispatcher Remove credential type and inventory plugin loading from Django's app.ready() path. In-memory registries (ManagedCredentialType.registry and InventorySourceOptions.injectors) are now populated lazily on first access via LazyLoadDict, a dict subclass that calls a loader function on the first read operation. This ensures web workers, dispatcher workers, and management commands all get the registries populated exactly when needed, without eager loading at startup. The DB sync (CredentialType.setup_tower_managed_defaults) is moved to the dispatcher's startup task, where it only needs to run once per deployment rather than in every Django process. Co-Authored-By: Alan Rominger <arominge@redhat.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import pytest
|
|
|
|
from django.apps import apps
|
|
from django.core.management.base import CommandError
|
|
|
|
from awx.main.tasks.system import _sync_credential_types_to_db
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_setup_tower_managed_defaults(mocker):
|
|
return mocker.patch('awx.main.models.credential.CredentialType.setup_tower_managed_defaults')
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sync_credential_types_migrations_ran(mocker, mock_setup_tower_managed_defaults):
|
|
mocker.patch('awx.main.tasks.system.is_database_synchronized', return_value=True)
|
|
|
|
_sync_credential_types_to_db()
|
|
|
|
mock_setup_tower_managed_defaults.assert_called_once()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_sync_credential_types_migrations_not_ran(mocker, mock_setup_tower_managed_defaults):
|
|
mocker.patch('awx.main.tasks.system.is_database_synchronized', return_value=False)
|
|
|
|
_sync_credential_types_to_db()
|
|
|
|
mock_setup_tower_managed_defaults.assert_not_called()
|
|
|
|
|
|
def test_check_db_requirement_no_violations(mocker):
|
|
mocker.patch('awx.main.apps.db_requirement_violations', return_value=None)
|
|
main_config = apps.get_app_config('main')
|
|
|
|
result = main_config.check_db_requirement()
|
|
|
|
assert result is None
|
|
|
|
|
|
def test_check_db_requirement_with_violations(mocker):
|
|
violation_msg = "Database version check failed"
|
|
mocker.patch('awx.main.apps.db_requirement_violations', return_value=violation_msg)
|
|
main_config = apps.get_app_config('main')
|
|
|
|
with pytest.raises(CommandError) as exc_info:
|
|
main_config.check_db_requirement()
|
|
|
|
assert str(exc_info.value) == violation_msg
|