From 1063b9f3db2b955faf19556c7c2a3cdb4b3bb5a5 Mon Sep 17 00:00:00 2001 From: Alexey Ovchinnikov Date: Tue, 4 Feb 2025 13:59:31 -0600 Subject: [PATCH] cargo: Adding integration tests This commit introduces integration tests for cargo and an e2e test. Signed-off-by: Alexey Ovchinnikov --- tests/integration/test_cargo.py | 116 ++++++++++++++++++ .../cargo_mixed_dep/.build-config.yaml | 13 ++ .../test_data/cargo_mixed_dep/bom.json | 39 ++++++ .../cargo_mixed_dep/container/Containerfile | 8 ++ 4 files changed, 176 insertions(+) create mode 100644 tests/integration/test_cargo.py create mode 100644 tests/integration/test_data/cargo_mixed_dep/.build-config.yaml create mode 100644 tests/integration/test_data/cargo_mixed_dep/bom.json create mode 100644 tests/integration/test_data/cargo_mixed_dep/container/Containerfile diff --git a/tests/integration/test_cargo.py b/tests/integration/test_cargo.py new file mode 100644 index 000000000..13ddeb464 --- /dev/null +++ b/tests/integration/test_cargo.py @@ -0,0 +1,116 @@ +import logging +from pathlib import Path + +import pytest + +from . import utils + +log = logging.getLogger(__name__) + + +@pytest.mark.parametrize( + "test_params", + [ + pytest.param( + utils.TestParameters( + branch="cargo/just-a-crate-dependency", + packages=({"path": ".", "type": "cargo"},), + flags=["--dev-package-managers"], + check_output=False, + check_deps_checksums=False, + check_vendor_checksums=False, + expected_exit_code=0, + expected_output="", + ), + id="just_a_crate_dependency", + ), + pytest.param( + utils.TestParameters( + branch="cargo/just-a-git-dependency", + packages=({"path": ".", "type": "cargo"},), + flags=["--dev-package-managers"], + check_output=False, + check_deps_checksums=False, + check_vendor_checksums=False, + expected_exit_code=0, + expected_output="", + ), + id="just_a_git_dependency", + ), + pytest.param( + utils.TestParameters( + branch="cargo/mixed-git-crate-dependency", + packages=({"path": ".", "type": "cargo"},), + flags=["--dev-package-managers"], + check_output=False, + check_deps_checksums=False, + check_vendor_checksums=False, + expected_exit_code=0, + expected_output="", + ), + id="mixed_git_crate_dependency", + ), + ], +) +def test_cargo_packages( + test_params: utils.TestParameters, + cachi2_image: utils.ContainerImage, + tmp_path: Path, + test_repo_dir: Path, + test_data_dir: Path, + request: pytest.FixtureRequest, +) -> None: + """Integration tests for bundler package manager.""" + test_case = request.node.callspec.id + + utils.fetch_deps_and_check_output( + tmp_path, test_case, test_params, test_repo_dir, test_data_dir, cachi2_image + ) + + +@pytest.mark.parametrize( + "test_params,check_cmd,expected_cmd_output", + [ + pytest.param( + utils.TestParameters( + branch="cargo/mixed-git-crate-dependency", + packages=({"path": ".", "type": "cargo"},), + flags=["--dev-package-managers"], + check_output=True, + check_deps_checksums=False, + check_vendor_checksums=False, + expected_exit_code=0, + expected_output="", + ), + [], # No additional commands are run to verify the build + [], + id="cargo_mixed_dep", + ), + ], +) +def test_e2e_cargo( + test_params: utils.TestParameters, + check_cmd: list[str], + expected_cmd_output: str, + cachi2_image: utils.ContainerImage, + tmp_path: Path, + test_repo_dir: Path, + test_data_dir: Path, + request: pytest.FixtureRequest, +) -> None: + """End to end test for cargo.""" + test_case = request.node.callspec.id + + utils.fetch_deps_and_check_output( + tmp_path, test_case, test_params, test_repo_dir, test_data_dir, cachi2_image + ) + + utils.build_image_and_check_cmd( + tmp_path, + test_repo_dir, + test_data_dir, + test_case, + check_cmd, + expected_cmd_output, + cachi2_image, + ) diff --git a/tests/integration/test_data/cargo_mixed_dep/.build-config.yaml b/tests/integration/test_data/cargo_mixed_dep/.build-config.yaml new file mode 100644 index 000000000..3e48c87d8 --- /dev/null +++ b/tests/integration/test_data/cargo_mixed_dep/.build-config.yaml @@ -0,0 +1,13 @@ +environment_variables: [] +project_files: +- abspath: ${test_case_tmp_path}/.cargo/config.toml + template: | + [source.crates-io] + replace-with = "vendored-sources" + + [source."git+https://github.com/uuid-rs/uuid"] + git = "https://github.com/uuid-rs/uuid" + replace-with = "vendored-sources" + + [source.vendored-sources] + directory = "${output_dir}/deps/cargo" diff --git a/tests/integration/test_data/cargo_mixed_dep/bom.json b/tests/integration/test_data/cargo_mixed_dep/bom.json new file mode 100644 index 000000000..ab4ab0b3e --- /dev/null +++ b/tests/integration/test_data/cargo_mixed_dep/bom.json @@ -0,0 +1,39 @@ +{ + "bomFormat": "CycloneDX", + "components": [ + { + "name": "data-encoding", + "properties": [ + { + "name": "cachi2:found_by", + "value": "cachi2" + } + ], + "purl": "pkg:cargo/data-encoding@2.7.0?source=registry%2Bhttps://github.com/rust-lang/crates.io-index", + "type": "library", + "version": "2.7.0" + }, + { + "name": "uuid", + "properties": [ + { + "name": "cachi2:found_by", + "value": "cachi2" + } + ], + "purl": "pkg:cargo/uuid@1.12.1?source=git%2Bhttps://github.com/uuid-rs/uuid%2370831d21b373a06149885e8aec18c9084ac918c3", + "type": "library", + "version": "1.12.1" + } + ], + "metadata": { + "tools": [ + { + "name": "cachi2", + "vendor": "red hat" + } + ] + }, + "specVersion": "1.4", + "version": 1 +} diff --git a/tests/integration/test_data/cargo_mixed_dep/container/Containerfile b/tests/integration/test_data/cargo_mixed_dep/container/Containerfile new file mode 100644 index 000000000..c9f071e0a --- /dev/null +++ b/tests/integration/test_data/cargo_mixed_dep/container/Containerfile @@ -0,0 +1,8 @@ +FROM docker.io/rust:1-alpine + +# Test disabled network access +RUN if curl -IsS www.google.com; then echo "Has network access!"; exit 1; fi + +WORKDIR /src +RUN . /tmp/cachi2.env && \ + cargo build --offline