From 73c5fa4797e50ada20191637ce512f5707282863 Mon Sep 17 00:00:00 2001 From: Bruno Pimentel Date: Thu, 2 May 2024 18:44:36 -0300 Subject: [PATCH] Add integration test to cover generated .repo files 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 --- .../test_data/rpm_test_repo_file/cachi2.repo | 10 +++ tests/integration/test_rpm.py | 73 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/integration/test_data/rpm_test_repo_file/cachi2.repo diff --git a/tests/integration/test_data/rpm_test_repo_file/cachi2.repo b/tests/integration/test_data/rpm_test_repo_file/cachi2.repo new file mode 100644 index 000000000..d69d126a3 --- /dev/null +++ b/tests/integration/test_data/rpm_test_repo_file/cachi2.repo @@ -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 diff --git a/tests/integration/test_rpm.py b/tests/integration/test_rpm.py index f634fae10..c57065eb1 100644 --- a/tests/integration/test_rpm.py +++ b/tests/integration/test_rpm.py @@ -1,3 +1,5 @@ +import os +import re from pathlib import Path from typing import List @@ -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", [