@@ -993,35 +993,57 @@ def _wget_recursive_download_on_host(url, destination):
993993# Tasking related fixtures
994994
995995
996+ def _build_dispatch_cmd (pulpcore_bindings , task_group_id , args , kwargs ):
997+ cid = pulpcore_bindings .client .default_headers .get ("Correlation-ID" ) or str (uuid .uuid4 ())
998+ username = pulpcore_bindings .client .configuration .username
999+ return (
1000+ "from django_guid import set_guid; "
1001+ "from pulpcore.tasking.tasks import dispatch; "
1002+ "from pulpcore.app.models import TaskGroup; "
1003+ "from pulpcore.app.util import get_url, set_current_user; "
1004+ "from django.contrib.auth import get_user_model; "
1005+ "User = get_user_model(); "
1006+ f"user = User.objects.filter(username='{ username } ').first(); "
1007+ "set_current_user(user); "
1008+ f"set_guid({ cid !r} ); "
1009+ f"tg = { task_group_id !r} and TaskGroup.objects.filter(pk={ task_group_id !r} ).first(); "
1010+ f"task = dispatch(*{ args !r} , task_group=tg, **{ kwargs !r} ); "
1011+ "print(get_url(task))"
1012+ )
1013+
1014+
9961015@pytest .fixture (scope = "session" )
9971016def dispatch_task (pulpcore_bindings ):
9981017 def _dispatch_task (* args , task_group_id = None , ** kwargs ):
999- cid = pulpcore_bindings .client .default_headers .get ("Correlation-ID" ) or str (uuid .uuid4 ())
1000- username = pulpcore_bindings .client .configuration .username
1001- commands = (
1002- "from django_guid import set_guid; "
1003- "from pulpcore.tasking.tasks import dispatch; "
1004- "from pulpcore.app.models import TaskGroup; "
1005- "from pulpcore.app.util import get_url, set_current_user; "
1006- "from django.contrib.auth import get_user_model; "
1007- "User = get_user_model(); "
1008- f"user = User.objects.filter(username='{ username } ').first(); "
1009- "set_current_user(user); "
1010- f"set_guid({ cid !r} ); "
1011- f"tg = { task_group_id !r} and TaskGroup.objects.filter(pk={ task_group_id !r} ).first(); "
1012- f"task = dispatch(*{ args !r} , task_group=tg, **{ kwargs !r} ); "
1013- "print(get_url(task))"
1014- )
1015-
1018+ commands = _build_dispatch_cmd (pulpcore_bindings , task_group_id , args , kwargs )
10161019 process = subprocess .run (["pulpcore-manager" , "shell" , "-c" , commands ], capture_output = True )
1017-
10181020 assert process .returncode == 0
10191021 task_href = process .stdout .decode ().strip ()
10201022 return task_href
10211023
10221024 return _dispatch_task
10231025
10241026
1027+ @pytest .fixture (scope = "session" )
1028+ def dispatch_task_with_stderr (pulpcore_bindings ):
1029+ """Variation of dispatch_task that returns (task_href, stderr_content).
1030+
1031+ The stderr can be useful write asserts over log messages.
1032+ This is required because the pytest caplog fixture can't capture log messages
1033+ streamed from the pulpcore-manager call.
1034+ """
1035+
1036+ def _dispatch_task (* args , task_group_id = None , ** kwargs ):
1037+ commands = _build_dispatch_cmd (pulpcore_bindings , task_group_id , args , kwargs )
1038+ process = subprocess .run (["pulpcore-manager" , "shell" , "-c" , commands ], capture_output = True )
1039+ assert process .returncode == 0
1040+ task_href = process .stdout .decode ().strip ()
1041+ stderr = process .stderr .decode ()
1042+ return task_href , stderr
1043+
1044+ return _dispatch_task
1045+
1046+
10251047@pytest .fixture (scope = "session" )
10261048def dispatch_task_group (dispatch_task ):
10271049 def _dispatch_task_group (task_name , * args , ** kwargs ):
0 commit comments