Skip to content

Commit

Permalink
fix cloudforms test and move credential loading
Browse files Browse the repository at this point in the history
  • Loading branch information
thedoubl3j committed Jan 8, 2025
1 parent c677803 commit 05b4a44
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
3 changes: 3 additions & 0 deletions awx/main/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
46 changes: 25 additions & 21 deletions awx/main/models/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions awx/main/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 12 additions & 4 deletions awx/main/tests/functional/test_inventory_source_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 05b4a44

Please sign in to comment.