Skip to content

Commit

Permalink
Fix tests for new bindings version
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Jan 2, 2025
1 parent ca4b8c0 commit 473c421
Show file tree
Hide file tree
Showing 34 changed files with 296 additions and 427 deletions.
2 changes: 2 additions & 0 deletions CHANGES/+python_bindings.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Updated the OpenAPI generator version used to generate python bindings to 7.10.0.
This involves stricter client side validation of api calls when using the bindings.
2 changes: 1 addition & 1 deletion pulp_certguard/tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def repository_test_file(
filename.write_bytes(b"test content")
repository = file_repository_factory(autopublish=True)
upload_task = file_bindings.ContentFilesApi.create(
relative_path="test_file", file=filename, repository=repository.pulp_href
relative_path="test_file", file=str(filename), repository=repository.pulp_href
).task
monitor_task(upload_task)
return repository
Expand Down
38 changes: 18 additions & 20 deletions pulp_file/tests/functional/api/test_crud_content_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

from pulpcore.tests.functional.utils import PulpTaskError

from pulpcore.client.pulpcore.exceptions import ApiException as coreApiException
from pulpcore.client.pulp_file.exceptions import ApiException


@pytest.mark.parallel
def test_crud_content_unit(file_bindings, random_artifact, gen_object_with_cleanup):
Expand Down Expand Up @@ -184,17 +181,18 @@ def test_cannot_create_repo_version_with_two_relative_paths_the_same(


@pytest.mark.parallel
def test_bad_inputs_to_modify_endpoint(file_bindings, file_repo, needs_pulp_plugin):
needs_pulp_plugin("core", min="3.23.0.dev")

with pytest.raises(ApiException):
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, [{}])

with pytest.raises(ApiException):
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, {"a": "b"})

with pytest.raises(ApiException):
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, ["/content/"])
@pytest.mark.parametrize(
"bad_input",
[
pytest.param([()], id="list_with_empty_dict"),
# Pydantic ignores the superfluous parameters for us.
pytest.param({"a": "b"}, id="dict", marks=pytest.mark.xfail),
pytest.param(["/content/"], id="list"),
],
)
def test_modify_rejects_bad_input(file_bindings, file_repo, bad_input):
with pytest.raises((file_bindings.module.ApiException, ValueError)):
file_bindings.RepositoriesFileApi.modify(file_repo.pulp_href, bad_input)


@pytest.mark.parallel
Expand All @@ -220,10 +218,10 @@ def test_create_file_content_from_chunked_upload(
# Upload the file and generate content
upload = gen_object_with_cleanup(pulpcore_bindings.UploadsApi, {"size": 256})
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_1, content_range="bytes 0-127/256"
upload_href=upload.pulp_href, file=str(file_1), content_range="bytes 0-127/256"
)
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_2, content_range="bytes 128-255/256"
upload_href=upload.pulp_href, file=str(file_2), content_range="bytes 128-255/256"
)
most_recent_path = str(uuid.uuid4())
response = file_bindings.ContentFilesApi.create(
Expand All @@ -233,16 +231,16 @@ def test_create_file_content_from_chunked_upload(
content = file_bindings.ContentFilesApi.read(task.created_resources[0])
assert content.sha256 == expected_digest
# Upload gets deleted if the content gets created
with pytest.raises(coreApiException):
with pytest.raises(pulpcore_bindings.module.ApiException):
pulpcore_bindings.UploadsApi.read(upload.pulp_href)

# Attempt to create a duplicate content by re-using the most recent relative path
upload = gen_object_with_cleanup(pulpcore_bindings.UploadsApi, {"size": 256})
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_1, content_range="bytes 0-127/256"
upload_href=upload.pulp_href, file=str(file_1), content_range="bytes 0-127/256"
)
pulpcore_bindings.UploadsApi.update(
upload_href=upload.pulp_href, file=file_2, content_range="bytes 128-255/256"
upload_href=upload.pulp_href, file=str(file_2), content_range="bytes 128-255/256"
)
response = file_bindings.ContentFilesApi.create(
upload=upload.pulp_href, relative_path=most_recent_path
Expand All @@ -251,7 +249,7 @@ def test_create_file_content_from_chunked_upload(
content = file_bindings.ContentFilesApi.read(task.created_resources[0])
assert content.sha256 == expected_digest
# Upload gets deleted even though no new content got created
with pytest.raises(coreApiException):
with pytest.raises(pulpcore_bindings.module.ApiException):
pulpcore_bindings.UploadsApi.read(upload.pulp_href)


Expand Down
25 changes: 5 additions & 20 deletions pulp_file/tests/functional/api/test_crud_remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import pytest

from pulpcore.client.pulp_file.exceptions import ApiException


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

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


@pytest.mark.parallel
def test_create_file_remote_with_invalid_parameter(file_bindings, gen_object_with_cleanup):
unexpected_field_remote_data = {
"name": str(uuid.uuid4()),
"url": "http://example.com",
"foo": "bar",
}

with pytest.raises(ApiException) as exc:
gen_object_with_cleanup(file_bindings.RemotesFileApi, unexpected_field_remote_data)
assert exc.value.status == 400
assert json.loads(exc.value.body) == {"foo": ["Unexpected field"]}


@pytest.mark.parallel
def test_create_file_remote_without_url(file_bindings, gen_object_with_cleanup):
with pytest.raises(ApiException) as exc:
with pytest.raises((file_bindings.module.ApiException, ValueError)) as exc:
gen_object_with_cleanup(file_bindings.RemotesFileApi, {"name": str(uuid.uuid4())})
assert exc.value.status == 400
assert json.loads(exc.value.body) == {"url": ["This field is required."]}
if isinstance(exc.value, file_bindings.module.ApiException):
assert exc.value.status == 400
assert json.loads(exc.value.body) == {"url": ["This field is required."]}


@pytest.mark.parallel
Expand Down
23 changes: 10 additions & 13 deletions pulp_file/tests/functional/api/test_domains.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import json

from pulpcore.app import settings
from pulpcore.client.pulp_file import ApiException
from pulpcore.client.pulpcore import ApiException as CoreApiException
from pulpcore.client.pulpcore import Repair
from pulpcore.tests.functional.utils import generate_iso, download_file


Expand Down Expand Up @@ -45,7 +42,7 @@ def test_object_creation(

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

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

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

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

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

task = file_bindings.ContentFilesApi.create(relative_path, file=filename).task
task = file_bindings.ContentFilesApi.create(relative_path, file=str(filename)).task
task2 = file_bindings.ContentFilesApi.create(
relative_path, file=filename, pulp_domain=domain.name
relative_path, file=str(filename), pulp_domain=domain.name
).task
response = monitor_task(task)
default_content = file_bindings.ContentFilesApi.read(response.created_resources[0])
Expand Down Expand Up @@ -230,7 +227,7 @@ def test_content_promotion(

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

Expand All @@ -284,7 +281,7 @@ def test_domain_rbac(pulpcore_bindings, file_bindings, gen_user, gen_object_with
repos = file_bindings.RepositoriesFileApi.list()
assert repos.count == 0
# Try to create a repo
with pytest.raises(ApiException) as e:
with pytest.raises(file_bindings.ApiException) as e:
file_bindings.RepositoriesFileApi.create(
{"name": str(uuid.uuid4())}, pulp_domain=domain.name
)
Expand Down
10 changes: 0 additions & 10 deletions pulp_file/tests/functional/api/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ def test_model_partial_update(file_repository_factory, file_bindings, monitor_ta
assert file_repo.pulp_labels == labels


@pytest.mark.parallel
def test_invalid_label_type(file_repository_factory):
"""Test that label doesn't accept non-dicts"""

with pytest.raises(ApiException) as e_info:
labels = "key_a" # str instead of dict
file_repository_factory(name=str(uuid4()), pulp_labels=labels)
assert e_info.value.status == 400


@pytest.mark.parallel
def test_invalid_labels(file_repository_factory):
"""Test that label keys and values are validated."""
Expand Down
26 changes: 16 additions & 10 deletions pulp_file/tests/functional/api/test_rbac.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import pytest
import uuid

from pulpcore.client.pulp_file import ApiException
from pulpcore.client.pulp_file import AsyncOperationResponse


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


@pytest.fixture
def try_action(monitor_task):
def try_action(file_bindings, monitor_task):
def _try_action(user, client, action, outcome, *args, **kwargs):
action_api = getattr(client, f"{action}_with_http_info")
try:
with user:
response, status, _ = action_api(*args, **kwargs, _return_http_data_only=False)
if isinstance(response, AsyncOperationResponse):
response = monitor_task(response.task)
except ApiException as e:
response = action_api(*args, **kwargs)
if isinstance(response, tuple):
# old bindings
data, status, _ = response
else:
# new bindings
data = response.data
status_code = response.status_code
if isinstance(data, file_bindings.module.AsyncOperationResponse):
data = monitor_task(data.task)
except file_bindings.module.ApiException as e:
assert e.status == outcome, f"{e}"
else:
assert status == outcome, f"User performed {action} when they shouldn't been able to"
return response
assert (
status_code == outcome
), f"User performed {action} when they shouldn't been able to"
return data

return _try_action

Expand Down
4 changes: 2 additions & 2 deletions pulp_file/tests/functional/api/test_remote_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_http_sync_ssl_tls_validation_off(
Test file on_demand sync with https:// serving from an untrusted certificate.
"""
remote_on_demand = file_remote_ssl_factory(
manifest_path=basic_manifest_path, policy="on_demand", tls_validation="false"
manifest_path=basic_manifest_path, policy="on_demand", tls_validation=False
)

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

_run_basic_sync_and_assert(
Expand Down
9 changes: 5 additions & 4 deletions pulp_file/tests/functional/api/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@ def test_sync_file_protocol_handler(
gen_object_with_cleanup,
monitor_task,
fixtures_cfg,
tmp_path,
wget_recursive_download_on_host,
):
"""Test syncing from a file repository with the file:// protocol handler"""
wget_recursive_download_on_host(urljoin(fixtures_cfg.remote_fixtures_origin, "file/"), "/tmp")
wget_recursive_download_on_host(urljoin(fixtures_cfg.remote_fixtures_origin, "file/"), tmp_path)
remote_kwargs = {
"url": "file:///tmp/file/PULP_MANIFEST",
"url": f"file:///{tmp_path}/file/PULP_MANIFEST",
"policy": "immediate",
"name": str(uuid.uuid4()),
}
remote = gen_object_with_cleanup(file_bindings.RemotesFileApi, remote_kwargs)
files = set(os.listdir("/tmp/file/"))
files = set(os.listdir(tmp_path / "file"))

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

# test that all the files are still present
assert set(os.listdir("/tmp/file/")) == files
assert set(os.listdir(tmp_path / "file")) == files

file_repo = file_bindings.RepositoriesFileApi.read(file_repo.pulp_href)
assert file_repo.latest_version_href.endswith("/versions/1/")
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def _random_artifact_factory(size=32, pulp_domain=None):
kwargs["pulp_domain"] = pulp_domain
temp_file = tmp_path / str(uuid.uuid4())
temp_file.write_bytes(os.urandom(size))
return pulpcore_bindings.ArtifactsApi.create(temp_file, **kwargs)
return pulpcore_bindings.ArtifactsApi.create(str(temp_file), **kwargs)

return _random_artifact_factory

Expand Down
10 changes: 4 additions & 6 deletions pulpcore/tests/functional/api/test_api_docs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Tests related to the api docs page."""

import pytest
from pulpcore.client.pulpcore import ApiException


@pytest.fixture(scope="session")
Expand All @@ -15,7 +14,7 @@ def test_valid_credentials(pulpcore_bindings, pulp_docs_url):
Assert the API documentation is returned.
"""
response = pulpcore_bindings.client.request("GET", pulp_docs_url)
response = pulpcore_bindings.client.rest_client.request("GET", pulp_docs_url)
assert response.status == 200


Expand All @@ -26,7 +25,7 @@ def test_no_credentials(pulpcore_bindings, pulp_docs_url, anonymous_user):
Assert the API documentation is returned.
"""
with anonymous_user:
response = pulpcore_bindings.client.request("GET", pulp_docs_url)
response = pulpcore_bindings.client.rest_client.request("GET", pulp_docs_url)
assert response.status == 200


Expand All @@ -36,7 +35,6 @@ def test_http_method(pulpcore_bindings, pulp_docs_url):
Assert an error is returned.
"""
with pytest.raises(ApiException) as e:
pulpcore_bindings.client.request("POST", pulp_docs_url)
response = pulpcore_bindings.client.rest_client.request("POST", pulp_docs_url)

assert e.value.status == 405
assert response.status == 405
Loading

0 comments on commit 473c421

Please sign in to comment.