Skip to content

Commit 97f0f83

Browse files
committed
Fix resource sync orphan detection to exclude system user
This change resolves failing resource sync tests by ensuring the system user is properly excluded from orphan detection during resource synchronization. **Problem:** Resource sync tests were failing because the system user (_system) was being incorrectly identified as an "orphaned" resource and deleted during sync operations. The issue was an inconsistency between: 1. The manifest endpoint (views.py) which already excluded system users 2. The orphan detection logic (sync.py) which did not exclude system users This caused the system user to be absent from manifests but present in orphan detection, leading to unintended deletion. **Solution:** - Added system user exclusion to get_orphan_resources() function - Used get_system_user() utility for robust system user identification - Filter by object_id rather than username for more reliable exclusion - Maintains consistency with existing manifest endpoint behavior **Impact:** - Fixes 4 failing tests: test_resource_sync, test_delete_orphans, test_resource_sync_update_conflict, test_resource_sync_create_conflict - Protects system user from being deleted during resource sync operations - No impact on regular user resource synchronization behavior Signed-off-by: Fabricio Aguiar <[email protected]> rh-pre-commit.version: 2.3.2 rh-pre-commit.check-secrets: ENABLED
1 parent 59a1f14 commit 97f0f83

File tree

1 file changed

+11
-1
lines changed
  • ansible_base/resource_registry/tasks

1 file changed

+11
-1
lines changed

ansible_base/resource_registry/tasks/sync.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def get_orphan_resources(
365365
manifest_list: list[ManifestItem],
366366
) -> QuerySet:
367367
"""QuerySet with orphaned managed resources to be deleted."""
368-
return (
368+
queryset = (
369369
Resource.objects.filter(
370370
content_type__resource_type__name=resource_type_name,
371371
)
@@ -376,6 +376,16 @@ def get_orphan_resources(
376376
)
377377
)
378378

379+
# Exclude system user from deletion, consistent with manifest endpoint
380+
if resource_type_name == "shared.user":
381+
from ansible_base.lib.utils.models import get_system_user
382+
383+
system_user = get_system_user()
384+
if system_user:
385+
queryset = queryset.exclude(object_id=system_user.id)
386+
387+
return queryset
388+
379389

380390
def delete_resource(resource: Resource):
381391
"""Wrapper to delete content_object and its related Resource.

0 commit comments

Comments
 (0)