Skip to content

Commit b1a5128

Browse files
committed
Fix tests for new bindings version
1 parent 75f4025 commit b1a5128

34 files changed

+298
-428
lines changed

CHANGES/+python_bindings.removal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updated the OpenAPI generator version used to generate python bindings to 7.10.0.
2+
This involves stricter client side validation of api calls when using the bindings.

pulp_certguard/tests/functional/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def repository_test_file(
1313
filename.write_bytes(b"test content")
1414
repository = file_repository_factory(autopublish=True)
1515
upload_task = file_bindings.ContentFilesApi.create(
16-
relative_path="test_file", file=filename, repository=repository.pulp_href
16+
relative_path="test_file", file=str(filename), repository=repository.pulp_href
1717
).task
1818
monitor_task(upload_task)
1919
return repository

pulp_file/tests/functional/api/test_crud_content_unit.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
from pulpcore.tests.functional.utils import PulpTaskError
99

10-
from pulpcore.client.pulpcore.exceptions import ApiException as coreApiException
11-
from pulpcore.client.pulp_file.exceptions import ApiException
12-
1310

1411
@pytest.mark.parallel
1512
def test_crud_content_unit(file_bindings, random_artifact, gen_object_with_cleanup):
@@ -184,17 +181,18 @@ def test_cannot_create_repo_version_with_two_relative_paths_the_same(
184181

185182

186183
@pytest.mark.parallel
187-
def test_bad_inputs_to_modify_endpoint(file_bindings, file_repo, needs_pulp_plugin):
188-
needs_pulp_plugin("core", min="3.23.0.dev")
189-
190-
with pytest.raises(ApiException):
191-
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, [{}])
192-
193-
with pytest.raises(ApiException):
194-
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, {"a": "b"})
195-
196-
with pytest.raises(ApiException):
197-
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, ["/content/"])
184+
@pytest.mark.parametrize(
185+
"bad_input",
186+
[
187+
pytest.param([()], id="list_with_empty_dict"),
188+
# Pydantic ignores the superfluous parameters for us.
189+
pytest.param({"a": "b"}, id="dict", marks=pytest.mark.xfail),
190+
pytest.param(["/content/"], id="list"),
191+
],
192+
)
193+
def test_modify_rejects_bad_input(file_bindings, file_repo, bad_input):
194+
with pytest.raises((file_bindings.module.ApiException, ValueError)):
195+
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, bad_input)
198196

199197

200198
@pytest.mark.parallel
@@ -220,10 +218,10 @@ def test_create_file_content_from_chunked_upload(
220218
# Upload the file and generate content
221219
upload = gen_object_with_cleanup(pulpcore_bindings.UploadsApi, {"size": 256})
222220
pulpcore_bindings.UploadsApi.update(
223-
upload_href=upload.pulp_href, file=file_1, content_range="bytes 0-127/256"
221+
upload_href=upload.pulp_href, file=str(file_1), content_range="bytes 0-127/256"
224222
)
225223
pulpcore_bindings.UploadsApi.update(
226-
upload_href=upload.pulp_href, file=file_2, content_range="bytes 128-255/256"
224+
upload_href=upload.pulp_href, file=str(file_2), content_range="bytes 128-255/256"
227225
)
228226
most_recent_path = str(uuid.uuid4())
229227
response = file_bindings.ContentFilesApi.create(
@@ -233,16 +231,16 @@ def test_create_file_content_from_chunked_upload(
233231
content = file_bindings.ContentFilesApi.read(task.created_resources[0])
234232
assert content.sha256 == expected_digest
235233
# Upload gets deleted if the content gets created
236-
with pytest.raises(coreApiException):
234+
with pytest.raises(pulpcore_bindings.module.ApiException):
237235
pulpcore_bindings.UploadsApi.read(upload.pulp_href)
238236

239237
# Attempt to create a duplicate content by re-using the most recent relative path
240238
upload = gen_object_with_cleanup(pulpcore_bindings.UploadsApi, {"size": 256})
241239
pulpcore_bindings.UploadsApi.update(
242-
upload_href=upload.pulp_href, file=file_1, content_range="bytes 0-127/256"
240+
upload_href=upload.pulp_href, file=str(file_1), content_range="bytes 0-127/256"
243241
)
244242
pulpcore_bindings.UploadsApi.update(
245-
upload_href=upload.pulp_href, file=file_2, content_range="bytes 128-255/256"
243+
upload_href=upload.pulp_href, file=str(file_2), content_range="bytes 128-255/256"
246244
)
247245
response = file_bindings.ContentFilesApi.create(
248246
upload=upload.pulp_href, relative_path=most_recent_path
@@ -251,7 +249,7 @@ def test_create_file_content_from_chunked_upload(
251249
content = file_bindings.ContentFilesApi.read(task.created_resources[0])
252250
assert content.sha256 == expected_digest
253251
# Upload gets deleted even though no new content got created
254-
with pytest.raises(coreApiException):
252+
with pytest.raises(pulpcore_bindings.module.ApiException):
255253
pulpcore_bindings.UploadsApi.read(upload.pulp_href)
256254

257255

pulp_file/tests/functional/api/test_crud_remotes.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
import pytest
77

8-
from pulpcore.client.pulp_file.exceptions import ApiException
9-
108

119
@pytest.mark.parallel
1210
def test_remote_crud_workflow(file_bindings, gen_object_with_cleanup, monitor_task):
@@ -15,7 +13,7 @@ def test_remote_crud_workflow(file_bindings, gen_object_with_cleanup, monitor_ta
1513
assert remote.url == remote_data["url"]
1614
assert remote.name == remote_data["name"]
1715

18-
with pytest.raises(ApiException) as exc:
16+
with pytest.raises(file_bindings.module.ApiException) as exc:
1917
gen_object_with_cleanup(file_bindings.RemotesFileApi, remote_data)
2018
assert exc.value.status == 400
2119
assert json.loads(exc.value.body) == {"name": ["This field must be unique."]}
@@ -39,26 +37,13 @@ def test_remote_crud_workflow(file_bindings, gen_object_with_cleanup, monitor_ta
3937
assert remote.url == all_new_remote_data["url"]
4038

4139

42-
@pytest.mark.parallel
43-
def test_create_file_remote_with_invalid_parameter(file_bindings, gen_object_with_cleanup):
44-
unexpected_field_remote_data = {
45-
"name": str(uuid.uuid4()),
46-
"url": "http://example.com",
47-
"foo": "bar",
48-
}
49-
50-
with pytest.raises(ApiException) as exc:
51-
gen_object_with_cleanup(file_bindings.RemotesFileApi, unexpected_field_remote_data)
52-
assert exc.value.status == 400
53-
assert json.loads(exc.value.body) == {"foo": ["Unexpected field"]}
54-
55-
5640
@pytest.mark.parallel
5741
def test_create_file_remote_without_url(file_bindings, gen_object_with_cleanup):
58-
with pytest.raises(ApiException) as exc:
42+
with pytest.raises((file_bindings.module.ApiException, ValueError)) as exc:
5943
gen_object_with_cleanup(file_bindings.RemotesFileApi, {"name": str(uuid.uuid4())})
60-
assert exc.value.status == 400
61-
assert json.loads(exc.value.body) == {"url": ["This field is required."]}
44+
if isinstance(exc.value, file_bindings.module.ApiException):
45+
assert exc.value.status == 400
46+
assert json.loads(exc.value.body) == {"url": ["This field is required."]}
6247

6348

6449
@pytest.mark.parallel

pulp_file/tests/functional/api/test_domains.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import json
44

55
from pulpcore.app import settings
6-
from pulpcore.client.pulp_file import ApiException
7-
from pulpcore.client.pulpcore import ApiException as CoreApiException
8-
from pulpcore.client.pulpcore import Repair
96
from pulpcore.tests.functional.utils import generate_iso, download_file
107

118

@@ -45,7 +42,7 @@ def test_object_creation(
4542

4643
# Try to create an object w/ cross domain relations
4744
default_remote = file_remote_factory(manifest_path=basic_manifest_path, policy="immediate")
48-
with pytest.raises(ApiException) as e:
45+
with pytest.raises(file_bindings.ApiException) as e:
4946
repo_body = {"name": str(uuid.uuid4()), "remote": default_remote.pulp_href}
5047
file_bindings.RepositoriesFileApi.create(repo_body, pulp_domain=domain.name)
5148
assert e.value.status == 400
@@ -54,7 +51,7 @@ def test_object_creation(
5451
"non_field_errors": [f"Objects must all be a part of the {domain_name} domain."]
5552
}
5653

57-
with pytest.raises(ApiException) as e:
54+
with pytest.raises(file_bindings.ApiException) as e:
5855
sync_body = {"remote": default_remote.pulp_href}
5956
file_bindings.RepositoriesFileApi.sync(repo.pulp_href, sync_body)
6057
assert e.value.status == 400
@@ -115,15 +112,15 @@ def test_artifact_upload(
115112
assert second_artifact.sha256 == file["digest"]
116113

117114
# Test that duplicate artifact can not be uploaded in same domain
118-
with pytest.raises(CoreApiException) as e:
119-
pulpcore_bindings.ArtifactsApi.create(filename, pulp_domain=domain.name)
115+
with pytest.raises(pulpcore_bindings.ApiException) as e:
116+
pulpcore_bindings.ArtifactsApi.create(str(filename), pulp_domain=domain.name)
120117
assert e.value.status == 400
121118
assert json.loads(e.value.body) == {
122119
"non_field_errors": [f"Artifact with sha256 checksum of '{file['digest']}' already exists."]
123120
}
124121

125122
# Show that duplicate artifacts can be uploaded into different domains
126-
dup_artifact = pulpcore_bindings.ArtifactsApi.create(filename, pulp_domain="default")
123+
dup_artifact = pulpcore_bindings.ArtifactsApi.create(str(filename), pulp_domain="default")
127124
assert "default/api/v3/" in dup_artifact.pulp_href
128125
assert dup_artifact.sha256 == second_artifact.sha256
129126

@@ -148,9 +145,9 @@ def test_content_upload(
148145
file = generate_iso(filename)
149146
relative_path = "1.iso"
150147

151-
task = file_bindings.ContentFilesApi.create(relative_path, file=filename).task
148+
task = file_bindings.ContentFilesApi.create(relative_path, file=str(filename)).task
152149
task2 = file_bindings.ContentFilesApi.create(
153-
relative_path, file=filename, pulp_domain=domain.name
150+
relative_path, file=str(filename), pulp_domain=domain.name
154151
).task
155152
response = monitor_task(task)
156153
default_content = file_bindings.ContentFilesApi.read(response.created_resources[0])
@@ -230,7 +227,7 @@ def test_content_promotion(
230227

231228
# Test that a repository version repair operation can be run without error
232229
response = file_bindings.RepositoriesFileVersionsApi.repair(
233-
repo.latest_version_href, Repair(verify_checksums=True)
230+
repo.latest_version_href, file_bindings.Repair(verify_checksums=True)
234231
)
235232
results = monitor_task(response.task)
236233
assert results.state == "completed"
@@ -273,7 +270,7 @@ def test_domain_rbac(pulpcore_bindings, file_bindings, gen_user, gen_object_with
273270
assert repos.count == 1
274271
assert repos.results[0].pulp_href == repo.pulp_href
275272
# Try to create a repository in default domain
276-
with pytest.raises(ApiException) as e:
273+
with pytest.raises(file_bindings.ApiException) as e:
277274
file_bindings.RepositoriesFileApi.create({"name": str(uuid.uuid4())})
278275
assert e.value.status == 403
279276

@@ -284,7 +281,7 @@ def test_domain_rbac(pulpcore_bindings, file_bindings, gen_user, gen_object_with
284281
repos = file_bindings.RepositoriesFileApi.list()
285282
assert repos.count == 0
286283
# Try to create a repo
287-
with pytest.raises(ApiException) as e:
284+
with pytest.raises(file_bindings.ApiException) as e:
288285
file_bindings.RepositoriesFileApi.create(
289286
{"name": str(uuid.uuid4())}, pulp_domain=domain.name
290287
)

pulp_file/tests/functional/api/test_labels.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,6 @@ def test_model_partial_update(file_repository_factory, file_bindings, monitor_ta
110110
assert file_repo.pulp_labels == labels
111111

112112

113-
@pytest.mark.parallel
114-
def test_invalid_label_type(file_repository_factory):
115-
"""Test that label doesn't accept non-dicts"""
116-
117-
with pytest.raises(ApiException) as e_info:
118-
labels = "key_a" # str instead of dict
119-
file_repository_factory(name=str(uuid4()), pulp_labels=labels)
120-
assert e_info.value.status == 400
121-
122-
123113
@pytest.mark.parallel
124114
def test_invalid_labels(file_repository_factory):
125115
"""Test that label keys and values are validated."""

pulp_file/tests/functional/api/test_rbac.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import pytest
22
import uuid
33

4-
from pulpcore.client.pulp_file import ApiException
5-
from pulpcore.client.pulp_file import AsyncOperationResponse
6-
74

85
@pytest.fixture()
96
def gen_users(gen_user):
@@ -23,19 +20,28 @@ def _gen_users(role_names=list()):
2320

2421

2522
@pytest.fixture
26-
def try_action(monitor_task):
23+
def try_action(file_bindings, monitor_task):
2724
def _try_action(user, client, action, outcome, *args, **kwargs):
2825
action_api = getattr(client, f"{action}_with_http_info")
2926
try:
3027
with user:
31-
response, status, _ = action_api(*args, **kwargs, _return_http_data_only=False)
32-
if isinstance(response, AsyncOperationResponse):
33-
response = monitor_task(response.task)
34-
except ApiException as e:
28+
response = action_api(*args, **kwargs)
29+
if isinstance(response, tuple):
30+
# old bindings
31+
data, status, _ = response
32+
else:
33+
# new bindings
34+
data = response.data
35+
status_code = response.status_code
36+
if isinstance(data, file_bindings.module.AsyncOperationResponse):
37+
data = monitor_task(data.task)
38+
except file_bindings.module.ApiException as e:
3539
assert e.status == outcome, f"{e}"
3640
else:
37-
assert status == outcome, f"User performed {action} when they shouldn't been able to"
38-
return response
41+
assert (
42+
status_code == outcome
43+
), f"User performed {action} when they shouldn't been able to"
44+
return data
3945

4046
return _try_action
4147

pulp_file/tests/functional/api/test_remote_settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_http_sync_ssl_tls_validation_off(
5656
Test file on_demand sync with https:// serving from an untrusted certificate.
5757
"""
5858
remote_on_demand = file_remote_ssl_factory(
59-
manifest_path=basic_manifest_path, policy="on_demand", tls_validation="false"
59+
manifest_path=basic_manifest_path, policy="on_demand", tls_validation=False
6060
)
6161

6262
_run_basic_sync_and_assert(
@@ -79,7 +79,7 @@ def test_http_sync_ssl_tls_validation_on(
7979
Test file on_demand sync with https:// and a client connection configured to trust it.
8080
"""
8181
remote_on_demand = file_remote_ssl_factory(
82-
manifest_path=basic_manifest_path, policy="on_demand", tls_validation="true"
82+
manifest_path=basic_manifest_path, policy="on_demand", tls_validation=True
8383
)
8484

8585
_run_basic_sync_and_assert(

pulp_file/tests/functional/api/test_sync.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ def test_sync_file_protocol_handler(
1818
gen_object_with_cleanup,
1919
monitor_task,
2020
fixtures_cfg,
21+
tmp_path,
2122
wget_recursive_download_on_host,
2223
):
2324
"""Test syncing from a file repository with the file:// protocol handler"""
24-
wget_recursive_download_on_host(urljoin(fixtures_cfg.remote_fixtures_origin, "file/"), "/tmp")
25+
wget_recursive_download_on_host(urljoin(fixtures_cfg.remote_fixtures_origin, "file/"), tmp_path)
2526
remote_kwargs = {
26-
"url": "file:///tmp/file/PULP_MANIFEST",
27+
"url": f"file:///{tmp_path}/file/PULP_MANIFEST",
2728
"policy": "immediate",
2829
"name": str(uuid.uuid4()),
2930
}
3031
remote = gen_object_with_cleanup(file_bindings.RemotesFileApi, remote_kwargs)
31-
files = set(os.listdir("/tmp/file/"))
32+
files = set(os.listdir(tmp_path / "file"))
3233

3334
body = RepositorySyncURL(remote=remote.pulp_href)
3435
monitor_task(file_bindings.RepositoriesFileApi.sync(file_repo.pulp_href, body).task)
3536

3637
# test that all the files are still present
37-
assert set(os.listdir("/tmp/file/")) == files
38+
assert set(os.listdir(tmp_path / "file")) == files
3839

3940
file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
4041
assert file_repo.latest_version_href.endswith("/versions/1/")

pulpcore/pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def _random_artifact_factory(size=32, pulp_domain=None):
585585
kwargs["pulp_domain"] = pulp_domain
586586
temp_file = tmp_path / str(uuid.uuid4())
587587
temp_file.write_bytes(os.urandom(size))
588-
return pulpcore_bindings.ArtifactsApi.create(temp_file, **kwargs)
588+
return pulpcore_bindings.ArtifactsApi.create(str(temp_file), **kwargs)
589589

590590
return _random_artifact_factory
591591

0 commit comments

Comments
 (0)