forked from pulp/pulpcore
/
test_tasks.py
155 lines (117 loc) · 6.05 KB
/
test_tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""Test that operations can be performed over tasks."""
import json
import subprocess
from urllib.parse import urljoin
from uuid import uuid4
import pytest
from aiohttp import BasicAuth
from pulpcore.client.pulp_file import RepositorySyncURL
from pulpcore.client.pulpcore.exceptions import ApiException
from pulpcore.tests.functional.utils import download_file
@pytest.fixture
def distribution(file_repo, file_distribution_api_client, gen_object_with_cleanup):
distribution = gen_object_with_cleanup(
file_distribution_api_client,
{"name": str(uuid4()), "base_path": str(uuid4()), "repository": file_repo.pulp_href},
)
return distribution
@pytest.mark.parallel
def test_retrieve_task_with_fields_created_resources_only(
bindings_cfg, tasks_api_client, distribution
):
"""Perform filtering over the task's field created_resources."""
task = tasks_api_client.list(created_resources=distribution.pulp_href).results[0]
auth = BasicAuth(login=bindings_cfg.username, password=bindings_cfg.password)
full_href = urljoin(bindings_cfg.host, task.pulp_href)
response_body = download_file(f"{full_href}?fields=created_resources", auth=auth).body
filtered_task = json.loads(response_body)
assert len(filtered_task["created_resources"]) == 1
assert task.created_resources == filtered_task["created_resources"]
@pytest.fixture
def setup_filter_fixture(
file_repo,
file_repository_api_client,
file_remote_ssl_factory,
basic_manifest_path,
tasks_api_client,
monitor_task,
):
remote = file_remote_ssl_factory(manifest_path=basic_manifest_path, policy="on_demand")
body = RepositorySyncURL(remote=remote.pulp_href)
repo_sync_task = monitor_task(file_repository_api_client.sync(file_repo.pulp_href, body).task)
repo_update_action = file_repository_api_client.partial_update(
file_repo.pulp_href, {"description": str(uuid4())}
)
repo_update_task = tasks_api_client.read(repo_update_action.task)
return repo_sync_task, repo_update_task, file_repo, remote
def test_filter_tasks_by_reserved_resources(setup_filter_fixture, tasks_api_client):
"""Filter all tasks by a particular reserved resource."""
repo_sync_task, repo_update_task, _, _ = setup_filter_fixture
reserved_resources_record = repo_update_task.reserved_resources_record[0]
results = tasks_api_client.list(reserved_resources_record=[reserved_resources_record]).results
# Why reserved_resources_record parameter needs to be a list here? ^
assert results[0].pulp_href == repo_update_task.pulp_href
assert len(results) == 2
# Filter all tasks by a non-existing reserved resource.
with pytest.raises(ApiException) as ctx:
tasks_api_client.list(
reserved_resources_record=["a_resource_should_be_never_named_like_this"]
)
assert ctx.value.status == 400
# Filter all tasks by a particular created resource.
created_resources = repo_sync_task.created_resources[0]
results = tasks_api_client.list(created_resources=created_resources).results
assert len(results) == 1
assert results[0].pulp_href == repo_sync_task.pulp_href
# Filter all tasks by a non-existing created resource.
created_resources = "a_resource_should_be_never_named_like_this"
with pytest.raises(ApiException) as ctx:
tasks_api_client.list(created_resources=created_resources)
assert ctx.value.status == 404
def get_prn(uri):
commands = ["from pulpcore.app.util import get_prn;", f"print(get_prn(uri={uri}));"]
process = subprocess.run(
["pulpcore-manager", "shell", "-c", commands], capture_output=True
)
assert process.returncode == 0
prn = process.stdout.decode().strip()
return prn
def test_reserved_resources_filter(setup_filter_fixture, tasks_api_client):
"""Filter tasks using the ReservedResourcesFilter type filters."""
repo_sync_task, repo_update_task, repo, remote = setup_filter_fixture
repo_prn = get_prn(repo.pulp_href)
remote_prn = get_prn(remote.pulp_href)
# Sanity check, TODO: remove pulp_href from filter checks in pulpcore 3.55
assert repo_prn in repo_sync_task.reserved_resources_record
assert f"shared:{remote_prn}" in repo_sync_task.reserved_resources_record
assert repo_prn in repo_update_task.reserved_resources_record
assert remote_prn not in repo_update_task.reserved_resources_record
# reserved_resources filter
href_results = tasks_api_client.filter(reserved_resources=repo.pulp_href)
assert href_results.count == 2
assert set(href_results.results) == {repo_sync_task, repo_update_task}
prn_results = tasks_api_client.filter(reserved_resources=repo_prn)
mixed_results = tasks_api_client.filter(reserved_resources_in=[repo.pulp_href, remote_prn])
assert mixed_results == href_results == prn_results
# shared_resources filter
href_results = tasks_api_client.filter(shared_resources=repo.pulp_href)
assert href_results.count == 0
href_results = tasks_api_client.filter(shared_resources=remote.pulp_href)
assert href_results.count == 1
assert href_results.results[0] == repo_sync_task
prn_results = tasks_api_client.filter(shared_resources=repo_prn)
assert prn_results.count == 0
prn_results = tasks_api_client.filter(shared_resources=remote_prn)
mixed_results = tasks_api_client.filter(shared_resources__in=[repo_prn, remote.pulp_href])
assert prn_results == href_results == mixed_results
# exclusive_resources filter
href_results = tasks_api_client.filter(exclusive_resources=remote.pulp_href)
assert href_results.count == 0
href_results = tasks_api_client.filter(exclusive_resources=repo.pulp_href)
assert href_results.count == 2
assert set(href_results.results) == {repo_sync_task, repo_update_task}
prn_results = tasks_api_client.filter(exclusive_resources=remote_prn)
assert prn_results.count == 0
prn_results = tasks_api_client.filter(exclusive_resources=repo_prn)
mixed_results = tasks_api_client.filter(exclusive_resources__in=[repo_prn, remote_prn])
assert prn_results == href_results == mixed_results