From 05b4a44b7febf966a17838fb067b06932b9cbc45 Mon Sep 17 00:00:00 2001 From: thedoubl3j Date: Thu, 19 Dec 2024 16:29:52 -0500 Subject: [PATCH] fix cloudforms test and move credential loading --- awx/main/apps.py | 3 ++ awx/main/models/credential.py | 46 ++++++++++--------- awx/main/tests/conftest.py | 9 ++++ .../test_inventory_source_migration.py | 16 +++++-- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/awx/main/apps.py b/awx/main/apps.py index 3d9896374743..c1c90f83c7fb 100644 --- a/awx/main/apps.py +++ b/awx/main/apps.py @@ -60,6 +60,9 @@ def _load_credential_types_feature(self): @bypass_in_test def load_credential_types_feature(self): + from awx.main.models.credential import load_credentials + + load_credentials() return self._load_credential_types_feature() def load_inventory_plugins(self): diff --git a/awx/main/models/credential.py b/awx/main/models/credential.py index c8cbb96db967..81a598310292 100644 --- a/awx/main/models/credential.py +++ b/awx/main/models/credential.py @@ -638,26 +638,30 @@ def get_absolute_url(self, request=None): return reverse(view_name, kwargs={'pk': self.pk}, request=request) -awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.managed_credentials')} -supported_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.managed_credentials.supported')} -plugin_entry_points = awx_entry_points if detect_server_product_name() == 'AWX' else {**awx_entry_points, **supported_entry_points} - -for ns, ep in plugin_entry_points.items(): - cred_plugin = ep.load() - if not hasattr(cred_plugin, 'inputs'): - setattr(cred_plugin, 'inputs', {}) - if not hasattr(cred_plugin, 'injectors'): - setattr(cred_plugin, 'injectors', {}) - if ns in ManagedCredentialType.registry: - raise ValueError( - 'a ManagedCredentialType with namespace={} is already defined in {}'.format(ns, inspect.getsourcefile(ManagedCredentialType.registry[ns].__class__)) - ) - ManagedCredentialType.registry[ns] = cred_plugin +def load_credentials(): + + awx_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.managed_credentials')} + supported_entry_points = {ep.name: ep for ep in entry_points(group='awx_plugins.managed_credentials.supported')} + plugin_entry_points = awx_entry_points if detect_server_product_name() == 'AWX' else {**awx_entry_points, **supported_entry_points} + + for ns, ep in plugin_entry_points.items(): + cred_plugin = ep.load() + if not hasattr(cred_plugin, 'inputs'): + setattr(cred_plugin, 'inputs', {}) + if not hasattr(cred_plugin, 'injectors'): + setattr(cred_plugin, 'injectors', {}) + if ns in ManagedCredentialType.registry: + raise ValueError( + 'a ManagedCredentialType with namespace={} is already defined in {}'.format( + ns, inspect.getsourcefile(ManagedCredentialType.registry[ns].__class__) + ) + ) + ManagedCredentialType.registry[ns] = cred_plugin -credential_plugins = {ep.name: ep for ep in entry_points(group='awx_plugins.credentials')} -if detect_server_product_name() == 'AWX': - credential_plugins = {} + credential_plugins = {ep.name: ep for ep in entry_points(group='awx_plugins.credentials')} + if detect_server_product_name() == 'AWX': + credential_plugins = {} -for ns, ep in credential_plugins.items(): - plugin = ep.load() - CredentialType.load_plugin(ns, plugin) + for ns, ep in credential_plugins.items(): + plugin = ep.load() + CredentialType.load_plugin(ns, plugin) diff --git a/awx/main/tests/conftest.py b/awx/main/tests/conftest.py index e1a1c05e00e8..fbfb6489f009 100644 --- a/awx/main/tests/conftest.py +++ b/awx/main/tests/conftest.py @@ -229,3 +229,12 @@ def me_inst(): me_mock = mock.MagicMock(return_value=inst) with mock.patch.object(Instance.objects, 'me', me_mock): yield inst + + +@pytest.fixture(scope="session", autouse=True) +def load_all_credentials(): + with mock.patch('awx.main.models.credential.detect_server_product_name', return_value='NOT_AWX'): + from awx.main.models.credential import load_credentials + + load_credentials() + yield diff --git a/awx/main/tests/functional/test_inventory_source_migration.py b/awx/main/tests/functional/test_inventory_source_migration.py index fe0b537426d2..6d17e22936cf 100644 --- a/awx/main/tests/functional/test_inventory_source_migration.py +++ b/awx/main/tests/functional/test_inventory_source_migration.py @@ -37,16 +37,24 @@ def cleanup_cloudforms(): assert 'cloudforms' not in CredentialType.defaults -@pytest.mark.django_db -def test_cloudforms_inventory_removal(request, inventory): - request.addfinalizer(cleanup_cloudforms) - ManagedCredentialType( +@pytest.fixture +def cloudforms_mct(): + ManagedCredentialType.registry['cloudforms'] = ManagedCredentialType( name='Red Hat CloudForms', namespace='cloudforms', kind='cloud', managed=True, inputs={}, + injectors={}, ) + yield + ManagedCredentialType.registry.pop('cloudforms', None) + + +@pytest.mark.django_db +def test_cloudforms_inventory_removal(request, inventory, cloudforms_mct): + request.addfinalizer(cleanup_cloudforms) + CredentialType.defaults['cloudforms']().save() cloudforms = CredentialType.objects.get(namespace='cloudforms') Credential.objects.create(