Skip to content

Commit

Permalink
WIP Fix tests for new bindings version
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Dec 18, 2024
1 parent 670263c commit 5fd49b4
Show file tree
Hide file tree
Showing 30 changed files with 240 additions and 370 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
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
34 changes: 23 additions & 11 deletions pulpcore/tests/functional/api/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
<https://docs.pulpproject.org/restapi.html#section/Authentication>`_.
"""

import pytest
import json

from base64 import b64encode
from pulpcore.client.pulpcore import ApiException

import pytest

from pulpcore.app import settings

Expand All @@ -20,8 +19,15 @@ def test_base_auth_success(pulpcore_bindings, pulp_admin_user):
Assert that a response indicating success is returned.
"""
with pulp_admin_user:
response, status, headers = pulpcore_bindings.ArtifactsApi.list_with_http_info()
assert status == 200
response = pulpcore_bindings.ArtifactsApi.list_with_http_info()
if isinstance(response, tuple):
# old bindings
_, status_code, headers = response
else:
# new bindings
status_code = response.status_code
headers = response.headers
assert status_code == 200
assert headers["Content-Type"] == "application/json"
# Maybe test correlation ID as well?

Expand All @@ -33,7 +39,7 @@ def test_base_auth_failure(pulpcore_bindings, invalid_user):
Assert that a response indicating failure is returned.
"""
with invalid_user:
with pytest.raises(ApiException) as e:
with pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.list()

assert e.value.status == 401
Expand All @@ -50,7 +56,7 @@ def test_base_auth_required(pulpcore_bindings, anonymous_user):
Assert that a response indicating failure is returned.
"""
with anonymous_user:
with pytest.raises(ApiException) as e:
with pytest.raises(pulpcore_bindings.ApiException) as e:
pulpcore_bindings.ArtifactsApi.list()

assert e.value.status == 401
Expand Down Expand Up @@ -79,8 +85,14 @@ def test_jq_header_remote_auth(pulpcore_bindings, anonymous_user):
encoded_header = b64encode(bytes(header_content, "ascii"))

pulpcore_bindings.ArtifactsApi.api_client.default_headers["x-rh-identity"] = encoded_header
_, status, _ = pulpcore_bindings.ArtifactsApi.list_with_http_info()
assert status == 200
response = pulpcore_bindings.ArtifactsApi.list_with_http_info()
if isinstance(response, tuple):
# old bindings
_, status_code, _ = response
else:
# new bindings
status_code = response.status_code
assert status_code == 200


@pytest.mark.parallel
Expand All @@ -106,7 +118,7 @@ def test_jq_header_remote_auth_denied_by_wrong_header(pulpcore_bindings, anonymo
encoded_header
)

with pytest.raises(ApiException) as exception:
with pytest.raises(pulpcore_bindings.ApiException) as exception:
pulpcore_bindings.ArtifactsApi.list()

assert exception.value.status == 401
Expand All @@ -127,7 +139,7 @@ def test_jq_header_remote_auth_denied_by_wrong_content(pulpcore_bindings, anonym

pulpcore_bindings.ArtifactsApi.api_client.default_headers["x-rh-identity"] = encoded_header

with pytest.raises(ApiException) as exception:
with pytest.raises(pulpcore_bindings.ApiException) as exception:
pulpcore_bindings.ArtifactsApi.list()

assert exception.value.status == 401
12 changes: 9 additions & 3 deletions pulpcore/tests/functional/api/test_correlation_id.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
def test_correlation_id(cid, pulpcore_bindings, monitor_task):
"""Test that a correlation can be passed as a header and logged."""
response, status, headers = pulpcore_bindings.OrphansCleanupApi.cleanup_with_http_info({})
monitor_task(response.task)
task = pulpcore_bindings.TasksApi.read(response.task)
response = pulpcore_bindings.OrphansCleanupApi.cleanup_with_http_info({})
if isinstance(response, tuple):
# old bindings
data, _, headers = response
else:
# new bindings
data = response.data
headers = response.headers
task = monitor_task(data.task)
assert headers["Correlation-ID"] == cid
assert task.logging_cid == cid
Loading

0 comments on commit 5fd49b4

Please sign in to comment.