Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hyagi committed Jan 15, 2025
1 parent 727cfbc commit fde9072
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 37 deletions.
21 changes: 13 additions & 8 deletions pulp_file/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,7 @@ def _file_distribution_factory(pulp_domain=None, **body):
kwargs = {}
if pulp_domain:
kwargs["pulp_domain"] = pulp_domain
file_distribution = gen_object_with_cleanup(file_bindings.DistributionsFileApi, data, **kwargs)

# if the base_url starts with / it means content_origin is not defined
if file_distribution.base_url.startswith("/"):
file_distribution.base_url.replace("/", bindings_cfg.host, 1)
return file_distribution


return gen_object_with_cleanup(file_bindings.DistributionsFileApi, data, **kwargs)

return _file_distribution_factory

Expand Down Expand Up @@ -387,3 +380,15 @@ def _generate_server_and_remote(*, manifest_path, policy):
return server, remote

yield _generate_server_and_remote


# if content_origin == null, base_url will return the relative path and
# we need to add the hostname to run the tests
@pytest.fixture
def file_distribution_base_url(bindings_cfg):
def _file_distribution_base_url(base_url):
if base_url.startswith("http"):
return base_url
return bindings_cfg.host+base_url

return _file_distribution_base_url
4 changes: 2 additions & 2 deletions pulpcore/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,10 @@ def pulp_api_v3_url(bindings_cfg, pulp_api_v3_path):


@pytest.fixture(scope="session")
def pulp_content_url(pulp_settings, pulp_domain_enabled):
def pulp_content_url(bindings_cfg, pulp_settings, pulp_domain_enabled):
url = f"{pulp_settings.CONTENT_ORIGIN}{pulp_settings.CONTENT_PATH_PREFIX}"
if not pulp_settings.CONTENT_ORIGIN:
url = f"https://pulp{pulp_settings.CONTENT_PATH_PREFIX}"
url = f"{bindings_cfg.host}{pulp_settings.CONTENT_PATH_PREFIX}"

if pulp_domain_enabled:
url += "default/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@pytest.mark.parallel
def test_file_remote_on_demand(
basic_manifest_path,
file_distribution_base_url,
file_distribution_factory,
file_fixtures_root,
file_repo_with_auto_publish,
Expand All @@ -39,7 +40,7 @@ def test_file_remote_on_demand(
# Create a distribution from the publication
distribution = file_distribution_factory(repository=repo.pulp_href)
# attempt to download_file() a file
download_file(f"{distribution.base_url}/1.iso")
download_file(f"{file_distribution_base_url(distribution.base_url)}/1.iso")


@pytest.mark.parallel
Expand Down
22 changes: 12 additions & 10 deletions pulpcore/tests/functional/api/using_plugin/test_content_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

@pytest.mark.parallel
def test_delete_remote_on_demand(
file_distribution_base_url,
file_repo_with_auto_publish,
file_remote_ssl_factory,
file_bindings,
Expand All @@ -33,14 +34,15 @@ def test_delete_remote_on_demand(

# Create a distribution pointing to the repository
distribution = file_distribution_factory(repository=repo.pulp_href)
distribution_base_url = file_distribution_base_url(distribution.base_url)

# Download the manifest from the remote
expected_file_list = list(get_files_in_manifest(remote.url))

# Delete the remote and assert that downloading content returns a 404
monitor_task(file_bindings.RemotesFileApi.delete(remote.pulp_href).task)
with pytest.raises(ClientResponseError) as exc:
url = urljoin(distribution.base_url, expected_file_list[0][0])
url = urljoin(distribution_base_url, expected_file_list[0][0])
download_file(url)
assert exc.value.status == 404

Expand All @@ -50,7 +52,7 @@ def test_delete_remote_on_demand(
monitor_task(file_bindings.RepositoriesFileApi.sync(repo.pulp_href, body).task)

# Assert that files can now be downloaded from the distribution
content_unit_url = urljoin(distribution.base_url, expected_file_list[0][0])
content_unit_url = urljoin(distribution_base_url, expected_file_list[0][0])
downloaded_file = download_file(content_unit_url)
actual_checksum = hashlib.sha256(downloaded_file.body).hexdigest()
expected_checksum = expected_file_list[0][1]
Expand All @@ -59,6 +61,7 @@ def test_delete_remote_on_demand(

@pytest.mark.parallel
def test_remote_artifact_url_update(
file_distribution_base_url,
file_repo_with_auto_publish,
file_remote_ssl_factory,
file_bindings,
Expand All @@ -79,13 +82,14 @@ def test_remote_artifact_url_update(

# Create a distribution from the publication
distribution = file_distribution_factory(repository=repo.pulp_href)
distribution_base_url = file_distribution_base_url(distribution.base_url)

# Download the manifest from the remote
expected_file_list = list(get_files_in_manifest(remote.url))

# Assert that trying to download content raises a 404
with pytest.raises(ClientResponseError) as exc:
url = urljoin(distribution.base_url, expected_file_list[0][0])
url = urljoin(distribution_base_url, expected_file_list[0][0])
download_file(url)
assert exc.value.status == 404

Expand All @@ -97,7 +101,7 @@ def test_remote_artifact_url_update(
monitor_task(
file_bindings.RepositoriesFileApi.sync(file_repo_with_auto_publish.pulp_href, body).task
)
content_unit_url = urljoin(distribution.base_url, expected_file_list[0][0])
content_unit_url = urljoin(distribution_base_url, expected_file_list[0][0])
downloaded_file = download_file(content_unit_url)
actual_checksum = hashlib.sha256(downloaded_file.body).hexdigest()
expected_checksum = expected_file_list[0][1]
Expand All @@ -106,8 +110,8 @@ def test_remote_artifact_url_update(

@pytest.mark.parallel
def test_remote_content_changed_with_on_demand(
bindings_cfg,
write_3_iso_file_fixture_data_factory,
file_distribution_base_url,
file_repo_with_auto_publish,
file_remote_ssl_factory,
file_bindings,
Expand Down Expand Up @@ -136,10 +140,7 @@ def test_remote_content_changed_with_on_demand(
expected_file_list = list(get_files_in_manifest(remote.url))
write_3_iso_file_fixture_data_factory("basic", overwrite=True)

if distribution.base_url.startswith("http"):
get_url = urljoin(distribution.base_url, expected_file_list[0][0])
else:
get_url = urljoin(bindings_cfg.host+distribution.base_url, expected_file_list[0][0])
get_url = urljoin(file_distribution_base_url(distribution.base_url), expected_file_list[0][0])

# WHEN (first request)
result = subprocess.run(["curl", "-v", get_url], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Expand All @@ -160,6 +161,7 @@ def test_remote_content_changed_with_on_demand(
@pytest.mark.parallel
def test_handling_remote_artifact_on_demand_streaming_failure(
write_3_iso_file_fixture_data_factory,
file_distribution_base_url,
file_repo_with_auto_publish,
file_remote_factory,
file_bindings,
Expand Down Expand Up @@ -219,7 +221,7 @@ def get_original_content_info(remote):
return content_unit[0], content_unit[1]

def download_from_distribution(content, distribution):
content_unit_url = urljoin(distribution.base_url, content_name)
content_unit_url = urljoin(file_distribution_base_url(distribution.base_url), content_name)
downloaded_file = download_file(content_unit_url)
actual_checksum = hashlib.sha256(downloaded_file.body).hexdigest()
return actual_checksum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_hidden_distros(file_distribution_factory, pulp_content_url, http_get):
def test_zero_byte_file_listing(
file_bindings,
file_distribution_factory,
file_distribution_base_url,
file_repo_with_auto_publish,
random_artifact_factory,
http_get,
Expand All @@ -37,8 +38,7 @@ def test_zero_byte_file_listing(
).task
monitor_task(task)
distribution = file_distribution_factory(repository=file_repo_with_auto_publish.pulp_href)

response = http_get(distribution.base_url)
response = http_get(file_distribution_base_url(distribution.base_url))
z_line = [i for i in response.decode("utf-8").split("\n") if i.startswith('<a href="zero">')]
assert len(z_line) == 1
assert z_line[0].endswith("0 Bytes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@pytest.mark.parallel
def test_content_directory_listing(
bindings_cfg,
pulpcore_bindings,
file_distribution_factory,
gen_object_with_cleanup,
Expand All @@ -37,7 +38,7 @@ def test_content_directory_listing(

content_origin = pulp_status.content_settings.content_origin
if not content_origin:
content_origin = "https://pulp"
content_origin = bindings_cfg.host
base_url = urljoin(
content_origin,
pulp_status.content_settings.content_path_prefix,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@pytest.mark.parallel
def test_content_promotion(
file_bindings,
file_distribution_base_url,
file_repo_with_auto_publish,
file_remote_ssl_factory,
file_distribution_factory,
Expand Down Expand Up @@ -43,12 +44,13 @@ def test_content_promotion(

for distro in [distribution1, distribution2, distribution3]:
# Assert that all 3 distributions can be accessed
r = get_from_url(distro.base_url)
distro_base_url = file_distribution_base_url(distro.base_url)
r = get_from_url(distro_base_url)
assert r.status == 200
# Download one of the files from the distribution and assert it has the correct checksum
expected_files_list = list(expected_files)
content_unit = expected_files_list[0]
content_unit_url = urljoin(distro.base_url, content_unit[0])
content_unit_url = urljoin(distro_base_url, content_unit[0])
downloaded_file = download_file(content_unit_url)
actual_checksum = hashlib.sha256(downloaded_file.body).hexdigest()
expected_checksum = content_unit[1]
Expand Down
11 changes: 7 additions & 4 deletions pulpcore/tests/functional/api/using_plugin/test_contentguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
def test_rbac_content_guard_full_workflow(
pulpcore_bindings,
file_bindings,
file_distribution_base_url,
pulp_admin_user,
anonymous_user,
gen_user,
Expand Down Expand Up @@ -44,7 +45,7 @@ def _assert_access(authorized_users):
auth = BasicAuth(login=user.username, password=user.password)
else:
auth = None
response = get_from_url(distro.base_url, auth=auth)
response = get_from_url(file_distribution_base_url(distro.base_url), auth=auth)
expected_status = 404 if user in authorized_users else 403
assert response.status == expected_status, f"Failed on {user.username=}"

Expand Down Expand Up @@ -94,6 +95,7 @@ def _assert_access(authorized_users):
def test_header_contentguard_workflow(
pulpcore_bindings,
file_bindings,
file_distribution_base_url,
gen_user,
file_distribution_factory,
gen_object_with_cleanup,
Expand All @@ -118,15 +120,16 @@ def test_header_contentguard_workflow(
assert guard.pulp_href == distro.content_guard

# Expect to receive a 403 Forbiden
response = get_from_url(distro.base_url, headers=None)
distro_base_url = file_distribution_base_url(distro.base_url)
response = get_from_url(distro_base_url, headers=None)
assert response.status == 403

# Expect the status to be 404 given the distribution is accessible
# but not pointing to any publication, or repository version.
header_value = b64encode(b"123456").decode("ascii")
headers = {"x-header": header_value}

response = get_from_url(distro.base_url, headers=headers)
response = get_from_url(distro_base_url, headers=headers)
assert response.status == 404

# Check the access using an jq_filter
Expand Down Expand Up @@ -160,7 +163,7 @@ def test_header_contentguard_workflow(
header_value = b64encode(byte_header_content).decode("utf8")
headers = {header_name: header_value}

response = get_from_url(distro.base_url, headers=headers)
response = get_from_url(file_distribution_base_url(distro.base_url), headers=headers)
assert response.status == 404


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def _sync_repository_distribution(policy="immediate"):

@pytest.mark.parallel
def test_reclaim_on_demand_content(
file_distribution_base_url,
pulpcore_bindings,
sync_repository_distribution,
monitor_task,
Expand All @@ -93,8 +94,9 @@ def test_reclaim_on_demand_content(
"""
repo, remote, distribution = sync_repository_distribution(policy="on_demand")

content = get_files_in_manifest(urljoin(distribution.base_url, "PULP_MANIFEST")).pop()
download_file(urljoin(distribution.base_url, content[0]))
distribution_base_url = file_distribution_base_url(distribution.base_url)
content = get_files_in_manifest(urljoin(distribution_base_url, "PULP_MANIFEST")).pop()
download_file(urljoin(distribution_base_url, content[0]))

expected_files = get_files_in_manifest(remote.url)
artifact_sha256 = get_file_by_path(content[0], expected_files)[1]
Expand All @@ -108,13 +110,14 @@ def test_reclaim_on_demand_content(

assert 0 == pulpcore_bindings.ArtifactsApi.list(sha256=artifact_sha256).count

download_file(urljoin(distribution.base_url, content[0]))
download_file(urljoin(distribution_base_url, content[0]))

assert 1 == pulpcore_bindings.ArtifactsApi.list(sha256=artifact_sha256).count


@pytest.mark.parallel
def test_immediate_reclaim_becomes_on_demand(
file_distribution_base_url,
pulpcore_bindings,
sync_repository_distribution,
monitor_task,
Expand All @@ -125,9 +128,10 @@ def test_immediate_reclaim_becomes_on_demand(
artifacts_before_reclaim = pulpcore_bindings.ArtifactsApi.list().count
assert artifacts_before_reclaim > 0

content = get_files_in_manifest(urljoin(distribution.base_url, "PULP_MANIFEST")).pop()
distribution_base_url = file_distribution_base_url(distribution.base_url)
content = get_files_in_manifest(urljoin(distribution_base_url, "PULP_MANIFEST")).pop()
# Populate cache
download_file(urljoin(distribution.base_url, content[0]))
download_file(urljoin(distribution_base_url, content[0]))

reclaim_response = pulpcore_bindings.RepositoriesReclaimSpaceApi.reclaim(
{"repo_hrefs": [repo.pulp_href]}
Expand All @@ -138,7 +142,7 @@ def test_immediate_reclaim_becomes_on_demand(
artifact_sha256 = get_file_by_path(content[0], expected_files)[1]
assert 0 == pulpcore_bindings.ArtifactsApi.list(sha256=artifact_sha256).count

download_file(urljoin(distribution.base_url, content[0]))
download_file(urljoin(distribution_base_url, content[0]))

assert 1 == pulpcore_bindings.ArtifactsApi.list(sha256=artifact_sha256).count

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def test_clear_all_units_repo_version(
@pytest.mark.parallel
def test_repo_version_retention(
file_bindings,
file_distribution_base_url,
file_repository_content,
file_repository_factory,
file_remote_ssl_factory,
Expand Down Expand Up @@ -873,7 +874,8 @@ def test_repo_version_retention(

# check that the last publication is distributed
distro = file_distribution_factory(repository=repo.pulp_href)
manifest_files = get_files_in_manifest(f"{distro.base_url}PULP_MANIFEST")
distro_base_url = file_distribution_base_url(distro.base_url)
manifest_files = get_files_in_manifest(f"{distro_base_url}PULP_MANIFEST")
assert len(manifest_files) == contents.count


Expand Down

0 comments on commit fde9072

Please sign in to comment.