Skip to content

Commit 82ff903

Browse files
committed
wip: assert deprecation msg is logged
1 parent bc91803 commit 82ff903

2 files changed

Lines changed: 46 additions & 30 deletions

File tree

pulpcore/pytest_plugin.py

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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")
9971016
def 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")
10261048
def dispatch_task_group(dispatch_task):
10271049
def _dispatch_task_group(task_name, *args, **kwargs):

pulpcore/tests/functional/api/test_tasking.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import json
44
import pytest
55
import time
6-
import logging
76

87
from aiohttp import BasicAuth
98
from datetime import datetime
@@ -475,23 +474,22 @@ def test_succeeds_on_api_worker(self, pulpcore_bindings, dispatch_task):
475474
def test_executes_on_api_worker_when_no_async(
476475
self,
477476
pulpcore_bindings,
478-
dispatch_task,
479-
caplog,
477+
dispatch_task_with_stderr,
480478
):
481479
"""
482480
GIVEN a task with no resource requirements
483481
AND the task IS NOT an async function
484482
WHEN dispatching a task as immediate
485483
THEN the task completes with no associated worker
486484
"""
487-
caplog.set_level(logging.WARNING)
488485
# TODO: on 3.85 this should throw an error
489-
task_href = dispatch_task(
486+
task_href, stderr = dispatch_task_with_stderr(
490487
"pulpcore.app.tasks.test.sleep", args=(LT_TIMEOUT,), immediate=True
491488
)
492489
task = pulpcore_bindings.TasksApi.read(task_href)
493490
assert task.state == "completed"
494491
assert task.worker is None
492+
assert "Support for non-coroutine immediate tasks will be dropped" in stderr
495493

496494
@pytest.mark.parallel
497495
def test_timeouts_on_api_worker(self, pulpcore_bindings, dispatch_task):
@@ -546,16 +544,14 @@ def test_executes_in_task_worker(
546544
WHEN dispatching a task as immediate
547545
THEN the task completes with a worker
548546
"""
549-
COMMON_RESOURCE = "MMM"
547+
COMMON_RESOURCE = str(uuid4())
550548
with dispatch_long_task(required_resources=[COMMON_RESOURCE]):
551549
task_href = dispatch_task(
552550
"pulpcore.app.tasks.test.asleep",
553551
args=(LT_TIMEOUT,),
554552
immediate=True,
555553
exclusive_resources=[COMMON_RESOURCE],
556554
)
557-
task = pulpcore_bindings.TasksApi.read(task_href)
558-
assert task.state == "waiting"
559555
task = monitor_task(task_href)
560556
assert task.state == "completed"
561557
assert task.worker is not None
@@ -569,7 +565,7 @@ def test_throws_when_non_deferrable(
569565
WHEN dispatching as immediate and not deferrable
570566
THEN an error is raised
571567
"""
572-
COMMON_RESOURCE = "NNN"
568+
COMMON_RESOURCE = str(uuid4())
573569
with dispatch_long_task(required_resources=[COMMON_RESOURCE]):
574570
task_href = dispatch_task(
575571
"pulpcore.app.tasks.test.asleep",
@@ -593,7 +589,7 @@ def test_timeouts_on_task_worker(
593589
AND it takes longer than timeout
594590
THEN an error is raised
595591
"""
596-
COMMON_RESOURCE = "PPP"
592+
COMMON_RESOURCE = str(uuid4())
597593
with pytest.raises(PulpTaskError) as ctx:
598594
with dispatch_long_task(required_resources=[COMMON_RESOURCE]):
599595
task_href = dispatch_task(
@@ -602,7 +598,5 @@ def test_timeouts_on_task_worker(
602598
immediate=True,
603599
exclusive_resources=[COMMON_RESOURCE],
604600
)
605-
task = pulpcore_bindings.TasksApi.read(task_href)
606-
assert task.state == "waiting"
607601
monitor_task(task_href)
608602
assert "task timed out after" in ctx.value.task.error["description"]

0 commit comments

Comments
 (0)