From 01293f1b4561ef213c54974f09a675c77695f6d5 Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Wed, 11 Feb 2026 11:47:59 -0500 Subject: [PATCH] Restore github app lookup tests * Introduced in PR https://github.com/ansible/awx/pull/16058/changes then a later large merge from AAP back into devel removed the changes * This PR re-introduces the github app lookup migration rename tests with the migration names updated and the kind to namespace correction --- .../migrations/0201_create_managed_creds.py | 4 +- ...202_convert_controller_role_definitions.py | 2 +- .../migrations/0204_squashed_deletions.py | 8 +-- awx/main/tests/functional/test_migrations.py | 51 +++++++++++++++++++ 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/awx/main/migrations/0201_create_managed_creds.py b/awx/main/migrations/0201_create_managed_creds.py index 399a13cbd5..32cb24efd3 100644 --- a/awx/main/migrations/0201_create_managed_creds.py +++ b/awx/main/migrations/0201_create_managed_creds.py @@ -21,6 +21,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(setup_tower_managed_defaults), - migrations.RunPython(setup_rbac_role_system_administrator), + migrations.RunPython(setup_tower_managed_defaults, migrations.RunPython.noop), + migrations.RunPython(setup_rbac_role_system_administrator, migrations.RunPython.noop), ] diff --git a/awx/main/migrations/0202_convert_controller_role_definitions.py b/awx/main/migrations/0202_convert_controller_role_definitions.py index 9a0c0b40fb..ea094c8897 100644 --- a/awx/main/migrations/0202_convert_controller_role_definitions.py +++ b/awx/main/migrations/0202_convert_controller_role_definitions.py @@ -98,5 +98,5 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(convert_controller_role_definitions), + migrations.RunPython(convert_controller_role_definitions, migrations.RunPython.noop), ] diff --git a/awx/main/migrations/0204_squashed_deletions.py b/awx/main/migrations/0204_squashed_deletions.py index c62952f091..3e7bb38749 100644 --- a/awx/main/migrations/0204_squashed_deletions.py +++ b/awx/main/migrations/0204_squashed_deletions.py @@ -3,10 +3,9 @@ from django.db import migrations, models from awx.main.migrations._create_system_jobs import delete_clear_tokens_sjt -# --- START of function merged from 0203_rename_github_app_kind.py --- def update_github_app_kind(apps, schema_editor): """ - Updates the 'kind' field for CredentialType records + Updates the 'namespace' field for CredentialType records from 'github_app' to 'github_app_lookup'. This addresses a change in the entry point key for the GitHub App plugin. """ @@ -15,9 +14,6 @@ def update_github_app_kind(apps, schema_editor): CredentialType.objects.using(db_alias).filter(namespace='github_app').update(namespace='github_app_lookup') -# --- END of function merged from 0203_rename_github_app_kind.py --- - - class Migration(migrations.Migration): dependencies = [ ('main', '0203_remove_team_of_teams'), @@ -118,7 +114,5 @@ class Migration(migrations.Migration): max_length=32, ), ), - # --- START of operations merged from 0203_rename_github_app_kind.py --- migrations.RunPython(update_github_app_kind, migrations.RunPython.noop), - # --- END of operations merged from 0203_rename_github_app_kind.py --- ] diff --git a/awx/main/tests/functional/test_migrations.py b/awx/main/tests/functional/test_migrations.py index 5402dd7778..caa9579f6a 100644 --- a/awx/main/tests/functional/test_migrations.py +++ b/awx/main/tests/functional/test_migrations.py @@ -173,3 +173,54 @@ class TestMigrationSmoke: assert Role.objects.filter( singleton_name='system_administrator', role_field='system_administrator' ).exists(), "expected to find a system_administrator singleton role" + + +@pytest.mark.django_db +class TestGithubAppBug: + """ + Tests that `awx-manage createsuperuser` runs successfully after + the `github_app` CredentialType kind is updated to `github_app_lookup` + via the migration. + """ + + def test_after_github_app_kind_migration(self, migrator): + """ + Verifies that `createsuperuser` does not raise a KeyError + after the 0204_squashed_deletions migration (which includes + the `update_github_app_kind` logic) is applied. + """ + # 1. Apply migrations up to the point *before* the 0204_squashed_deletions migration. + # This simulates the state where the problematic CredentialType might exist. + # We use 0203_remove_team_of_teams as the direct predecessor. + old_state = migrator.apply_tested_migration(('main', '0203_remove_team_of_teams')) + + # Get the CredentialType model from the historical state. + CredentialType = old_state.apps.get_model('main', 'CredentialType') + + # Create a CredentialType with the old, problematic 'namespace' value + CredentialType.objects.create( + name='Legacy GitHub App Credential', + kind='external', + namespace='github_app', # The namespace that causes the KeyError in the registry lookup + managed=True, + created=now(), + modified=now(), + ) + + # Apply the migration that includes the fix (0204_squashed_deletions). + new_state = migrator.apply_tested_migration(('main', '0204_squashed_deletions')) + + # Verify that the CredentialType with the old 'kind' no longer exists + # and the 'kind' has been updated to the new value. + CredentialType = new_state.apps.get_model('main', 'CredentialType') # Get CredentialType model from the new state + + # Assertion 1: The CredentialType with the old 'github_app' kind should no longer exist. + assert not CredentialType.objects.filter( + namespace='github_app' + ).exists(), "CredentialType with old 'github_app' kind should no longer exist after migration." + + # Assertion 2: The CredentialType should now exist with the new 'github_app_lookup' kind + # and retain its original name. + assert CredentialType.objects.filter( + namespace='github_app_lookup', name='Legacy GitHub App Credential' + ).exists(), "CredentialType should be updated to 'github_app_lookup' and retain its name."