forked from pulp/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
185 lines (138 loc) · 6.48 KB
/
Copy pathconftest.py
File metadata and controls
185 lines (138 loc) · 6.48 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import pytest
import uuid
from pulp_smash.pulp3.utils import gen_distribution, gen_repo
from pulp_python.tests.functional.constants import PYTHON_EGG_FILENAME, PYTHON_URL
from pulp_python.tests.functional.utils import gen_python_remote
from pulpcore.client.pulp_python import (
ApiClient,
ContentPackagesApi,
DistributionsPypiApi,
PublicationsPypiApi,
RepositoriesPythonApi,
RepositoriesPythonVersionsApi,
RemotesPythonApi,
)
# Bindings API Fixtures
@pytest.fixture
def python_bindings_client(cid, bindings_cfg):
"""Provides the python bindings client object."""
api_client = ApiClient(bindings_cfg)
api_client.default_headers["Correlation-ID"] = cid
return api_client
@pytest.fixture
def python_repo_api_client(python_bindings_client):
"""Provides the Python Repository API client object."""
return RepositoriesPythonApi(python_bindings_client)
@pytest.fixture
def python_repo_version_api_client(python_bindings_client):
"""Provides the Python Repository Version API client object."""
return RepositoriesPythonVersionsApi(python_bindings_client)
@pytest.fixture
def python_distro_api_client(python_bindings_client):
"""Provides the Python Distribution API client object."""
return DistributionsPypiApi(python_bindings_client)
@pytest.fixture
def python_content_api_client(python_bindings_client):
"""Provides the Python Package Content API client object."""
return ContentPackagesApi(python_bindings_client)
@pytest.fixture
def python_remote_api_client(python_bindings_client):
"""Provides the Python Remotes API client object."""
return RemotesPythonApi(python_bindings_client)
@pytest.fixture
def python_publication_api_client(python_bindings_client):
"""Proves the Python Publication API client object."""
return PublicationsPypiApi(python_bindings_client)
# Object Generation Fixtures
@pytest.fixture
def python_repo_factory(python_repo_api_client, gen_object_with_cleanup):
"""A factory to generate a Python Repository with auto-cleanup."""
def _gen_python_repo(**kwargs):
kwargs.setdefault("name", str(uuid.uuid4()))
return gen_object_with_cleanup(python_repo_api_client, kwargs)
return _gen_python_repo
@pytest.fixture
def python_repo(python_repo_api_client, gen_object_with_cleanup):
"""Creates a Python Repository and deletes it at test cleanup time."""
return gen_object_with_cleanup(python_repo_api_client, gen_repo())
@pytest.fixture
def python_distribution_factory(python_distro_api_client, gen_object_with_cleanup):
"""A factory to generate a Python Distribution with auto-cleanup."""
def _gen_python_distribution(**kwargs):
distro_data = gen_distribution(**kwargs)
return gen_object_with_cleanup(python_distro_api_client, distro_data)
yield _gen_python_distribution
@pytest.fixture
def python_publication_factory(python_publication_api_client, gen_object_with_cleanup):
"""A factory to generate a Python Publication with auto-cleanup."""
def _gen_python_publication(repository, version=None):
if version:
body = {"repository_version": f"{repository.versions_href}{version}/"}
else:
body = {"repository": repository.pulp_href}
return gen_object_with_cleanup(python_publication_api_client, body)
yield _gen_python_publication
@pytest.fixture
def python_remote_factory(python_remote_api_client, gen_object_with_cleanup):
"""A factory to generate a Python Remote with auto-cleanup."""
def _gen_python_remote(**kwargs):
body = gen_python_remote(**kwargs)
return gen_object_with_cleanup(python_remote_api_client, body)
yield _gen_python_remote
@pytest.fixture
def python_repo_with_sync(
python_repo_api_client, python_repo_factory, python_remote_factory, monitor_task
):
"""A factory to generate a Python Repository synced with the passed in Remote."""
def _gen_python_repo_sync(remote=None, mirror=False, repository=None, **body):
kwargs = {}
if pulp_domain := body.get("pulp_domain"):
kwargs["pulp_domain"] = pulp_domain
remote = remote or python_remote_factory(**kwargs)
repo = repository or python_repo_factory(**body)
sync_body = {"mirror": mirror, "remote": remote.pulp_href}
monitor_task(python_repo_api_client.sync(repo.pulp_href, sync_body).task)
return python_repo_api_client.read(repo.pulp_href)
yield _gen_python_repo_sync
@pytest.fixture
def download_python_file(tmp_path, http_get):
"""Download a Python file and return its path."""
def _download_python_file(relative_path, url):
file_path = tmp_path / relative_path
with open(file_path, mode="wb") as f:
f.write(http_get(url))
return file_path
yield _download_python_file
@pytest.fixture
def python_content_factory(python_content_api_client, download_python_file, monitor_task):
"""A factory to create a Python Package Content."""
def _gen_python_content(relative_path=PYTHON_EGG_FILENAME, url=None, **body):
body["relative_path"] = relative_path
if url:
body["file"] = download_python_file(relative_path, url)
elif not any(x in body for x in ("artifact", "file", "upload")):
body["file"] = download_python_file(PYTHON_EGG_FILENAME, PYTHON_URL)
if repo := body.get("repository"):
repo_href = repo if isinstance(repo, str) else repo.pulp_href
body["repository"] = repo_href
task = python_content_api_client.create(**body).task
response = monitor_task(task)
return python_content_api_client.read(response.created_resources[0])
yield _gen_python_content
@pytest.fixture
def python_content_summary(python_repo_api_client, python_repo_version_api_client):
"""Get a summary of the repository version's content."""
def _gen_summary(repository_version=None, repository=None, version=None):
if repository_version is None:
repo_href = get_href(repository)
if version:
repo_ver_href = f"{repo_href}versions/{version}/"
else:
repo_ver_href = python_repo_api_client.read(repo_href).latest_version_href
else:
repo_ver_href = get_href(repository_version)
return python_repo_version_api_client.read(repo_ver_href).content_summary
yield _gen_summary
def get_href(item):
"""Tries to get the href from the given item, whether it is a string or object."""
return item if isinstance(item, str) else item.pulp_href