Skip to content

Commit

Permalink
Add integration test to cover generated .repo files
Browse files Browse the repository at this point in the history
This test prefetches RPM content and proceeds to generate the .repo file
by using the `inject-files` command.

Since the steps for this test differ from the usual, it was necessary to
customize the test boilerplate to call `inject-files` without actually
fully building the image, since only the contents of the generated .repo
file need to be checked.

Signed-off-by: Bruno Pimentel <bpimente@redhat.com>
  • Loading branch information
brunoapimentel committed May 14, 2024
1 parent d97feb6 commit 73c5fa4
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/integration/test_data/rpm_test_repo_file/cachi2.repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[another-repoid]
baseurl=file:///tmp/rpm_test_repo_file-output/deps/rpm/x86_64/another-repoid
gpgcheck=1
[cachi2-aaa000]
baseurl=file:///tmp/rpm_test_repo_file-output/deps/rpm/x86_64/cachi2-aaa000
gpgcheck=1
name=Generated repository containing all packages unaffiliated with any official repository
[releases]
baseurl=file:///tmp/rpm_test_repo_file-output/deps/rpm/x86_64/releases
gpgcheck=1
73 changes: 73 additions & 0 deletions tests/integration/test_rpm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import re
from pathlib import Path
from typing import List

Expand Down Expand Up @@ -105,6 +107,77 @@ def test_rpm_packages(
)


@pytest.mark.parametrize(
"test_params",
[
pytest.param(
utils.TestParameters(
repo="https://github.com/cachito-testing/cachi2-rpm",
ref="ce7fed744fc8fc2fd5d8981027e519ecd50b8805",
packages=({"path": ".", "type": "rpm"},),
flags=["--dev-package-managers"],
check_output=False,
check_deps_checksums=False,
check_vendor_checksums=False,
expected_exit_code=0,
),
id="rpm_test_repo_file",
),
],
)
def test_repo_files(
test_params: utils.TestParameters,
cachi2_image: utils.ContainerImage,
tmp_path: Path,
test_data_dir: Path,
request: pytest.FixtureRequest,
) -> None:
"""Test if the contents of the generated .repo file are correct."""
test_case = request.node.callspec.id
output_folder = tmp_path.joinpath(f"{test_case}-output")

source_folder = utils.clone_repository(
test_params.repo, test_params.ref, f"{test_case}-source", tmp_path
)

utils.fetch_deps_and_check_output(
tmp_path, test_case, test_params, source_folder, test_data_dir, cachi2_image
)

# call inject-files to create the .repo file
cmd = [
"inject-files",
output_folder,
"--for-output-dir",
Path("/tmp", f"{test_case}-output"),
]
(output, exit_code) = cachi2_image.run_cmd_on_image(cmd, tmp_path)
assert exit_code == 0, f"Injecting project files failed. output-cmd: {output}"

# load .repo file contents
def read_and_normalize_repofile(path: Path) -> str:
with open(path) as file:
# whenever an RPM lacks a repoid in the lockfile, Cachi2 will resort to a randomly
# generated internal repoid, which needs to be replaced by a constant string so it can
# be tested consistently.
return re.sub(r"cachi2-[a-f0-9]{6}", "cachi2-aaa000", file.read())

repo_file_content = read_and_normalize_repofile(
output_folder.joinpath("deps/rpm/x86_64/repos.d/cachi2.repo")
)

# update test data if needed
expected_repo_file_path = test_data_dir.joinpath(test_case, "cachi2.repo")

if os.getenv("CACHI2_GENERATE_TEST_DATA") == "true":
expected_repo_file_path.parent.mkdir(parents=True, exist_ok=True)
with open(expected_repo_file_path, "w") as file:
file.write(repo_file_content)

# check if .repo file content matches the expected test data
assert repo_file_content == read_and_normalize_repofile(expected_repo_file_path)


@pytest.mark.parametrize(
"test_params, check_cmd, expected_cmd_output",
[
Expand Down

0 comments on commit 73c5fa4

Please sign in to comment.