Skip to content

Commit 0bc6c36

Browse files
authored
test: add new integration test utility (#764)
Signed-off-by: Nathan Nguyen <[email protected]>
1 parent 9d37a9c commit 0bc6c36

File tree

112 files changed

+2841
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+2841
-667
lines changed

.github/workflows/_build_docker.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ jobs:
5959
IMAGE_NAME: ghcr.io/oracle/macaron
6060
run: make build-docker
6161

62+
- name: Install dependencies for integration test utility
63+
run: make setup-integration-test-utility-for-docker
64+
6265
# Run the integration tests against the built Docker image.
6366
- name: Test the Docker image
6467
env:

.pre-commit-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ repos:
161161
# - id: pretty-format-toml
162162
# args: [--autofix]
163163

164+
- repo: local
165+
hooks:
166+
- id: integration-test-vet
167+
name: validate integration test cases
168+
entry: python3
169+
args:
170+
- ./tests/integration/run.py
171+
- vet
172+
- ./tests/integration/cases/...
173+
language: system
174+
pass_filenames: false
175+
164176
# On push to the remote, run the unit tests.
165177
- repo: local
166178
hooks:

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ setup-github-actions:
205205
python -m pip install --upgrade wheel
206206
python -m pip install --upgrade --upgrade-strategy eager --editable .[actions]
207207

208+
# Install dependencies for the integration test utility script in workflow to
209+
# test the docker image.
210+
.PHONY: setup-integration-test-utility-for-docker
211+
setup-integration-test-utility-for-docker:
212+
python -m pip install ruamel.yaml cfgv
213+
208214
# Generate a Software Bill of Materials (SBOM).
209215
.PHONY: sbom
210216
sbom: requirements

docs/source/pages/developers_guide/index.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,16 @@ Finally, you need to register your check by adding it to the :mod:`registry modu
216216
Test your check
217217
'''''''''''''''
218218

219-
Finally, you can add tests for you check by adding ``tests/slsa_analyzer/checks/test_repo_check.py`` module. Macaron
220-
uses `pytest <https://docs.pytest.org>`_ and `hypothesis <https://hypothesis.readthedocs.io>`_ for testing. Take a look
219+
Finally, you can add tests for you check. We utilize two types of tests: unit tests, and integration tests.
220+
221+
For unit tests, you can add a ``tests/slsa_analyzer/checks/test_repo_check.py`` module. Macaron
222+
uses `pytest <https://docs.pytest.org>`_ and `hypothesis <https://hypothesis.readthedocs.io>`_ for unit testing. Take a look
221223
at other tests for inspiration!
222224

225+
For integration tests, please refer to the README file under ``tests/integration`` for
226+
further instructions and have a look at our existing integration test cases if you need
227+
some examples.
228+
223229
.. toctree::
224230
:maxdepth: 1
225231

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ minversion = "7.0"
240240
addopts = """-vv -ra --tb native \
241241
--doctest-modules --doctest-continue-on-failure --doctest-glob '*.rst' \
242242
--cov macaron \
243+
--ignore tests/integration \
243244
""" # Consider adding --pdb
244245
# https://docs.python.org/3/library/doctest.html#option-flags
245246
doctest_optionflags = "IGNORE_EXCEPTION_DETAIL"

scripts/dev_scripts/integration_tests.sh

Lines changed: 3 additions & 458 deletions
Large diffs are not rendered by default.

scripts/dev_scripts/integration_tests_docker.sh

Lines changed: 4 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -38,85 +38,6 @@ echo "Run unit tests for the run_macaron.sh script"
3838
python $UNIT_TEST_SCRIPT || log_fail
3939
echo -e "\n----------------------------------------------------------------------------------"
4040

41-
echo -e "\n----------------------------------------------------------------------------------"
42-
echo "timyarkov/multibuild_test: Analyzing Maven artifact with the repo path, the branch name and the commit digest"
43-
echo "with dependency resolution using cyclonedx Maven plugins (defaults)."
44-
echo -e "----------------------------------------------------------------------------------\n"
45-
DEP_EXPECTED=$WORKSPACE/tests/dependency_analyzer/expected_results/cyclonedx_timyarkov_multibuild_test_maven.json
46-
DEP_RESULT=$WORKSPACE/output/reports/maven/org_example/mock_maven_proj/dependencies.json
47-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/maven/org.example/mock_maven_proj/1.0-SNAPSHOT/multibuild_test.dl
48-
run_macaron_clean analyze -purl pkg:maven/org.example/[email protected]?type=jar -rp https://github.com/timyarkov/multibuild_test -b main -d a8b0efe24298bc81f63217aaa84776c3d48976c5 || log_fail
49-
50-
python $COMPARE_DEPS $DEP_RESULT $DEP_EXPECTED || log_fail
51-
52-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
53-
54-
echo -e "\n----------------------------------------------------------------------------------"
55-
echo "timyarkov/multibuild_test: Analyzing Gradle artifact with the repo path, the branch name and the commit digest"
56-
echo "with dependency resolution using cyclonedx Gradle plugins (defaults)."
57-
echo -e "----------------------------------------------------------------------------------\n"
58-
DEP_EXPECTED=$WORKSPACE/tests/dependency_analyzer/expected_results/cyclonedx_timyarkov_multibuild_test_gradle.json
59-
DEP_RESULT=$WORKSPACE/output/reports/maven/org_example/mock_gradle_proj/dependencies.json
60-
$RUN_MACARON_SCRIPT analyze -purl pkg:maven/org.example/[email protected]?type=jar -rp https://github.com/timyarkov/multibuild_test -b main -d a8b0efe24298bc81f63217aaa84776c3d48976c5 || log_fail
61-
62-
python $COMPARE_DEPS $DEP_RESULT $DEP_EXPECTED || log_fail
63-
64-
echo -e "\n----------------------------------------------------------------------------------"
65-
echo "apache/maven: Check the resolved dependency output with config for cyclonedx maven plugin (default)."
66-
echo -e "----------------------------------------------------------------------------------\n"
67-
DEP_RESULT=$WORKSPACE/output/reports/maven/org_apache_maven/maven/dependencies.json
68-
DEP_EXPECTED=$WORKSPACE/tests/dependency_analyzer/expected_results/cyclonedx_apache_maven.json
69-
70-
run_macaron_clean analyze -c $WORKSPACE/tests/dependency_analyzer/configurations/maven_config.yaml || log_fail
71-
python $COMPARE_DEPS $DEP_RESULT $DEP_EXPECTED || log_fail
72-
73-
echo -e "\n----------------------------------------------------------------------------------"
74-
echo "apache/maven: e2e using the local repo path, the branch name and the commit digest without dependency resolution."
75-
echo -e "----------------------------------------------------------------------------------\n"
76-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/maven/maven.dl
77-
78-
run_macaron_clean -lr $WORKSPACE/output/git_repos/github_com analyze -r apache/maven -b master -d 3fc399318edef0d5ba593723a24fff64291d6f9b --skip-deps || log_fail
79-
80-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
81-
82-
echo -e "\n----------------------------------------------------------------------------------"
83-
echo "apache/maven: Check the e2e output JSON file with config and no dependency analyzing."
84-
echo -e "----------------------------------------------------------------------------------\n"
85-
EXPECT_DIR=$WORKSPACE/tests/e2e/expected_results/maven
86-
87-
declare -a COMPARE_FILES=(
88-
"maven.dl"
89-
"guava.dl"
90-
"mockito.dl"
91-
)
92-
93-
run_macaron_clean analyze -c $WORKSPACE/tests/e2e/configurations/maven_config.yaml --skip-deps || log_fail
94-
95-
for i in "${COMPARE_FILES[@]}"
96-
do
97-
$RUN_POLICY -d $DB -f $EXPECT_DIR/$i || log_fail
98-
done
99-
100-
echo -e "\n----------------------------------------------------------------------------------"
101-
echo "apache/maven: Analyzing using a CycloneDx SBOM with target repo path"
102-
echo -e "----------------------------------------------------------------------------------\n"
103-
SBOM_FILE=$WORKSPACE/tests/dependency_analyzer/cyclonedx/resources/apache_maven_root_sbom.json
104-
DEP_EXPECTED=$WORKSPACE/tests/dependency_analyzer/expected_results/apache_maven_with_sbom_provided.json
105-
DEP_RESULT=$WORKSPACE/output/reports/maven/org_apache_maven/maven/dependencies.json
106-
107-
run_macaron_clean analyze -purl pkg:maven/org.apache.maven/[email protected]?type=pom -rp https://github.com/apache/maven -b master -d 3fc399318edef0d5ba593723a24fff64291d6f9b -sbom "$SBOM_FILE" || log_fail
108-
109-
python $COMPARE_DEPS $DEP_RESULT $DEP_EXPECTED || log_fail
110-
111-
echo -e "\n----------------------------------------------------------------------------------"
112-
echo "apache/maven: Analyzing with PURL and repository path without dependency resolution."
113-
echo -e "----------------------------------------------------------------------------------\n"
114-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/purl/maven/maven.dl
115-
116-
run_macaron_clean analyze -purl pkg:maven/apache/maven -rp https://github.com/apache/maven -b master -d 3fc399318edef0d5ba593723a24fff64291d6f9b --skip-deps || log_fail
117-
118-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
119-
12041
echo -e "\n----------------------------------------------------------------------------------"
12142
echo "pkg:pypi/[email protected]: Analyzing the dependencies with virtual env provided as input."
12243
echo -e "----------------------------------------------------------------------------------\n"
@@ -137,130 +58,10 @@ python $COMPARE_POLICIES $POLICY_RESULT $POLICY_EXPECTED || log_fail
13758
# Clean up and remove the virtual environment.
13859
rm -rf "$VIRTUAL_ENV_PATH"
13960

140-
echo -e "\n----------------------------------------------------------------------------------"
141-
echo "urllib3/urllib3: Analyzing the repo path when automatic dependency resolution is skipped."
142-
echo "The CUE expectation file is provided as a single file path."
143-
echo -e "----------------------------------------------------------------------------------\n"
144-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/urllib3/urllib3.dl
145-
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/urllib3_PASS.cue
146-
147-
run_macaron_clean analyze -pe $EXPECTATION_FILE -rp https://github.com/urllib3/urllib3/urllib3 -b main -d 87a0ecee6e691fe5ff93cd000c0158deebef763b --skip-deps || log_fail
148-
149-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
150-
151-
echo -e "\n----------------------------------------------------------------------------------"
152-
echo "urllib3/urllib3: Analyzing the repo path when automatic dependency resolution is skipped."
153-
echo "The CUE expectation file should be found via the directory path."
154-
echo -e "----------------------------------------------------------------------------------\n"
155-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/urllib3/urllib3.dl
156-
EXPECTATION_DIR=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/
157-
158-
run_macaron_clean analyze -pe $EXPECTATION_DIR -rp https://github.com/urllib3/urllib3/urllib3 -b main -d 87a0ecee6e691fe5ff93cd000c0158deebef763b --skip-deps || log_fail
159-
160-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
161-
162-
echo -e "\n----------------------------------------------------------------------------------"
163-
echo "Test verifying CUE provenance expectation for ossf/scorecard"
164-
echo -e "----------------------------------------------------------------------------------\n"
165-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/scorecard/scorecard.dl
166-
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/scorecard.ini
167-
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/scorecard_PASS.cue
168-
169-
run_macaron_clean -dp $DEFAULTS_FILE analyze -pe $EXPECTATION_FILE -purl pkg:github/ossf/[email protected] --skip-deps || log_fail
170-
171-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
172-
173-
echo -e "\n----------------------------------------------------------------------------------"
174-
echo "Run policy CLI with scorecard results."
175-
echo -e "----------------------------------------------------------------------------------\n"
176-
POLICY_FILE=$WORKSPACE/tests/policy_engine/resources/policies/scorecard/scorecard.dl
177-
POLICY_RESULT=$WORKSPACE/output/policy_report.json
178-
POLICY_EXPECTED=$WORKSPACE/tests/policy_engine/expected_results/scorecard/scorecard_policy_report.json
179-
VSA_RESULT=$WORKSPACE/output/vsa.intoto.jsonl
180-
VSA_PAYLOAD_EXPECTED=$WORKSPACE/tests/vsa/integration/github_slsa-framework_scorecard/vsa_payload.json
181-
182-
$RUN_POLICY -f "$POLICY_FILE" -d $DB || log_fail
183-
python $COMPARE_POLICIES $POLICY_RESULT $POLICY_EXPECTED || log_fail
184-
python "$COMPARE_VSA" "$VSA_RESULT" "$VSA_PAYLOAD_EXPECTED" || log_fail
185-
186-
echo -e "\n----------------------------------------------------------------------------------"
187-
echo "slsa-framework/slsa-verifier: Analyzing the repo path when automatic dependency resolution is skipped"
188-
echo "and CUE file is provided as expectation."
189-
echo -e "----------------------------------------------------------------------------------\n"
190-
OUTPUT_POLICY=$WORKSPACE/tests/e2e/expected_results/slsa-verifier/slsa-verifier_cue_PASS.dl
191-
EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/slsa_verifier_PASS.cue
192-
DEFAULTS_FILE=$WORKSPACE/tests/e2e/defaults/slsa_verifier.ini
193-
194-
run_macaron_clean -dp $DEFAULTS_FILE analyze -pe $EXPECTATION_FILE -rp https://github.com/slsa-framework/slsa-verifier -b main -d fc50b662fcfeeeb0e97243554b47d9b20b14efac --skip-deps || log_fail
195-
196-
$RUN_POLICY -d $DB -f $OUTPUT_POLICY || log_fail
197-
198-
echo -e "\n----------------------------------------------------------------------------------"
199-
echo "behnazh-w/example-maven-app as a local and remote repository"
200-
echo "Test the Witness and GitHub provenances as an input, Cue expectation validation, Policy CLI and VSA generation."
201-
echo -e "----------------------------------------------------------------------------------\n"
202-
POLICY_FILE=$WORKSPACE/tests/policy_engine/resources/policies/example-maven-project/policy.dl
203-
POLICY_RESULT=$WORKSPACE/output/policy_report.json
204-
POLICY_EXPECTED=$WORKSPACE/tests/policy_engine/expected_results/example-maven-project/example_maven_project_policy_report.json
205-
VSA_RESULT=$WORKSPACE/output/vsa.intoto.jsonl
206-
VSA_PAYLOAD_EXPECTED=$WORKSPACE/tests/vsa/integration/example-maven-project/vsa_payload.json
207-
208-
# Test the local repo with Witness provenance.
209-
WITNESS_EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/witness-example-maven-project.cue
210-
WITNESS_PROVENANCE_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/resources/valid_provenances/witness-example-maven-project.json
211-
212-
# Cloning the repository locally
213-
git clone https://github.com/behnazh-w/example-maven-app.git $WORKSPACE/output/git_repos/local_repos/example-maven-app || log_fail
214-
215-
# Check the Witness provenance.
216-
run_macaron_clean analyze -pf $WITNESS_PROVENANCE_FILE -pe $WITNESS_EXPECTATION_FILE -purl pkg:maven/io.github.behnazh-w.demo/[email protected]?type=jar --repo-path example-maven-app --skip-deps || log_fail
217-
218-
# Test the remote repo with GitHub provenance.
219-
GITHUB_EXPECTATION_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/expectations/cue/resources/valid_expectations/github-example-maven-project.cue
220-
GITHUB_PROVENANCE_FILE=$WORKSPACE/tests/slsa_analyzer/provenance/resources/valid_provenances/github-example-maven-project.json
221-
222-
# Check the GitHub provenance.
223-
$RUN_MACARON_SCRIPT analyze -pf $GITHUB_PROVENANCE_FILE -pe $GITHUB_EXPECTATION_FILE -purl pkg:maven/io.github.behnazh-w.demo/[email protected]?type=jar --skip-deps || log_fail
224-
225-
# Verify the policy and VSA for all the software components generated from behnazh-w/example-maven-app repo.
226-
$RUN_POLICY -f "$POLICY_FILE" -d $DB || log_fail
227-
228-
python "$COMPARE_POLICIES" "$POLICY_RESULT" "$POLICY_EXPECTED" || log_fail
229-
python "$COMPARE_VSA" "$VSA_RESULT" "$VSA_PAYLOAD_EXPECTED" || log_fail
230-
231-
echo -e "\n----------------------------------------------------------------------------------"
232-
echo "Test running the analysis without setting the GITHUB_TOKEN environment variables."
233-
echo -e "----------------------------------------------------------------------------------\n"
234-
temp="$GITHUB_TOKEN"
235-
GITHUB_TOKEN="" && $RUN_MACARON_SCRIPT analyze -rp https://github.com/slsa-framework/slsa-verifier --skip-deps
236-
if [ $? -eq 0 ];
237-
then
238-
echo -e "Expect non-zero status code but got $?."
239-
log_fail
240-
fi
241-
GITHUB_TOKEN="$temp"
242-
243-
echo -e "\n----------------------------------------------------------------------------------"
244-
echo "apache/maven: test analyzing with invalid PURL"
245-
echo -e "----------------------------------------------------------------------------------\n"
246-
$RUN_MACARON_SCRIPT analyze -purl invalid-purl -rp https://github.com/apache/maven --skip-deps
247-
248-
if [ $? -eq 0 ];
249-
then
250-
echo -e "Expect non-zero status code but got $?."
251-
log_fail
252-
fi
253-
254-
echo -e "\n----------------------------------------------------------------------------------"
255-
echo "apache/maven: test analyzing with both PURL and repository path but no branch and digest are provided."
256-
echo -e "----------------------------------------------------------------------------------\n"
257-
$RUN_MACARON_SCRIPT analyze -purl pkg:maven/apache/maven -rp https://github.com/apache/maven --skip-deps
258-
259-
if [ $? -eq 0 ];
260-
then
261-
echo -e "Expect non-zero status code but got $?."
262-
log_fail
263-
fi
61+
python3 ./tests/integration/run.py run \
62+
--macaron scripts/release_scripts/run_macaron.sh \
63+
--include-tag docker \
64+
./tests/integration/cases/... || log_fail
26465

26566
if [ $RESULT_CODE -ne 0 ];
26667
then

0 commit comments

Comments
 (0)