Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand snapshots part I #1467

Merged
merged 29 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d401067
Extend snapshot testing.
cerrussell Nov 18, 2024
935c099
Remove sdkman step.
cerrussell Nov 18, 2024
6ab2b09
Run java and javascript.
cerrussell Nov 18, 2024
e170fc8
Troubleshooting.
cerrussell Nov 18, 2024
a06fe95
Troubleshooting log.
cerrussell Nov 19, 2024
56e46e7
Enable yarn.
cerrussell Nov 19, 2024
8280788
Bazel troubleshooting.
cerrussell Nov 19, 2024
c9271a5
Correct bazel build, specify java.
cerrussell Nov 19, 2024
03ecbe5
Set java and bazel env variables.
cerrussell Nov 19, 2024
72a493e
Enable skipping specific project.
cerrussell Nov 19, 2024
d23ead7
Skip jazzer.
cerrussell Nov 19, 2024
3a921fa
Add sdkman env.
cerrussell Nov 20, 2024
af9d734
Troubleshooting sdkman path.
cerrussell Nov 20, 2024
0f404c3
Troubleshooting sdkman path.
cerrussell Nov 20, 2024
87f1515
Troubleshooting sdkman path.
cerrussell Nov 20, 2024
097036c
Correct escapes.
cerrussell Nov 20, 2024
5c1ea7d
Revert time logging.
cerrussell Nov 20, 2024
d1fb063
Repo rename.
cerrussell Nov 20, 2024
794b076
Re-enable prebuild.
cerrussell Nov 20, 2024
1002be5
Clean up java prebuilds.
cerrussell Nov 20, 2024
b34efef
Correction.
cerrussell Nov 20, 2024
47029a9
Remove expanded python and dotnet repos.
cerrussell Nov 20, 2024
b2ec328
Run all snapshot tests.
cerrussell Nov 20, 2024
d9a96ca
Syntax corrections.
cerrussell Nov 20, 2024
242b89b
Locate go.
cerrussell Nov 20, 2024
08abc43
Use --migrate-legacy.
cerrussell Nov 20, 2024
cf637e6
Allow bidirectional --migrate-legacy.
cerrussell Nov 20, 2024
3028eb7
Remove testing branch checkout.
cerrussell Nov 20, 2024
84f2f74
Remove self-hosted from matrix.
cerrussell Nov 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/repotests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
fail-fast: true
matrix:
node-version: ['23.x']
os: ['ubuntu-latest', 'windows-latest', 'self-hosted']
os: ['ubuntu-latest', 'windows-latest']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
68 changes: 26 additions & 42 deletions .github/workflows/snapshot-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
pull_request:
branches:
- master
push:
branches:
- master


concurrency:
Expand All @@ -14,74 +17,55 @@ concurrency:

jobs:

generate:
runs-on: ubuntu-latest
test_non_dotnet:
runs-on: ["snapshot-test"]
steps:

- uses: actions/checkout@v4

- name: Setup Node.js environment
uses: actions/[email protected]
with:
node-version: '23.x'
- name: Set up pnpm
uses: pnpm/action-setup@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: 3.12

- uses: pnpm/action-setup@v4

- name: cdxgen, sdkman, custom-json-diff installs
- name: cdxgen, custom-json-diff installs
shell: bash
env:
SHELL: bash
run: |
cdxgen_tarball=$(pnpm pack | tail -1)
npm install -g "$cdxgen_tarball"
python -m pip install --upgrade pip
python -m pip install pytest
git clone https://github.com/appthreat/cdxgen-samples.git /home/runner/work/original_snapshots
python -m pip install -r test/diff/requirements.txt
curl -s "https://get.sdkman.io" | bash
source "/home/runner/.sdkman/bin/sdkman-init.sh"

- name: sdkman install cache
uses: actions/cache@v4
id: sdkman-cache
with:
path: /home/runner/.sdkman/candidates
key: "${{ runner.os }}-sdkman-${{ hashFiles('test/diff/repos.csv') }}"
git clone https://github.com/appthreat/cdxgen-samples.git original_snapshots
python3.12 -m venv .venv
source .venv/bin/activate && pip install -r test/diff/requirements.txt

- name: Generate and run scripts
env:
SDKMAN_DIR: /home/runner/.sdkman
CDXGEN_DEBUG_MODE: debug
CDXGEN_LOG: /home/runner/work/new_snapshots/generate.log
PREFER_MAVEN_DEPS_TREE: false
- name: Generate scripts
run: |
mkdir /home/runner/work/new_snapshots
source .venv/bin/activate
python test/diff/generate.py
bash /home/runner/work/new_snapshots/sdkman_installs.sh
bash /home/runner/work/new_snapshots/cdxgen_commands.sh

- name: Upload shell scripts generated as artifact
uses: actions/upload-artifact@v4
with:
name: scripts
path: /home/runner/work/new_snapshots/*.sh
path: new_snapshots/*.sh

- name: Run scripts
env:
PREFER_MAVEN_DEPS_TREE: false
run: |
bash new_snapshots/cdxgen_commands.sh

- name: Upload cdxgen boms
uses: actions/upload-artifact@v4
with:
name: cdxgen_boms
path: |
/home/runner/work/new_snapshots
new_snapshots

- name: Test BOMs
run: |
source .venv/bin/activate
python test/diff/diff_tests.py --migrate-legacy
if test -f /home/runner/work/new_snapshots/diffs.json; then
if test -f new_snapshots/diffs.json; then
echo "status=FAILED" >> "$GITHUB_ENV"
fi

Expand All @@ -90,9 +74,9 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: diffs
path: |
/home/runner/work/new_snapshots/diffs.json
/home/runner/work/new_snapshots/*.html
path: |
new_snapshots/diffs.json
new_snapshots/*.html

- name: Exit with error
if: ${{ env.status == 'FAILED' }}
Expand Down
53 changes: 40 additions & 13 deletions test/diff/diff_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
import csv
import logging
import os
from typing import Dict, List, Set

from custom_json_diff.lib.custom_diff import compare_dicts, perform_bom_diff, report_results
from custom_json_diff.lib.custom_diff_classes import Options
from custom_json_diff.lib.utils import json_dump, json_load
from custom_json_diff.lib.utils import json_dump,json_load

from generate import filter_repos

logging.disable(logging.INFO)

Expand All @@ -15,7 +18,7 @@ def build_args():
parser.add_argument(
'--directories',
'-d',
default=['/home/runner/work/original_snapshots', '/home/runner/work/new_snapshots'],
default=[f'/{os.getenv("GITHUB_WORKSPACE")}/original_snapshots', f'/{os.getenv("GITHUB_WORKSPACE")}/new_snapshots'],
help='Directories containing the snapshots to compare',
nargs=2
)
Expand All @@ -25,16 +28,36 @@ def build_args():
action="store_true",
help="Migrate legacy snapshots to 1.6 format"
)
parser.add_argument(
'--projects',
'-p',
help='Filter to these projects.',
dest='projects',
)
parser.add_argument(
'--types',
'-t',
help='Filter to these project types.',
dest='project_types',
)
parser.add_argument(
'--skip-projects',
'-s',
help='Skip these projects'
)
return parser.parse_args()


def compare_snapshot(dir1, dir2, options, repo, migrate_legacy):
def compare_snapshot(dir1: str, dir2: str, options: Options, repo: Dict, migrate_legacy: bool):
bom_1 = f"{dir1}/{repo['project']}-bom.json"
bom_2 = f"{dir2}/{repo['project']}-bom.json"
if migrate_legacy:
bom_data = migrate_to_1_6(bom_1)
bom_1 = bom_1.replace("bom.json", "bom.migrated.json")
json_dump(bom_1, bom_data)
bom_data = migrate_to_1_6(bom_1)
bom_2 = bom_2.replace("bom.json", "bom.migrated.json")
json_dump(bom_2, bom_data)
options.file_1 = bom_1
options.file_2 = bom_2
options.output = f'{dir2}/{repo["project"]}-diff.json'
Expand All @@ -45,12 +68,11 @@ def compare_snapshot(dir1, dir2, options, repo, migrate_legacy):
status, result_summary = perform_bom_diff(j1, j2)
report_results(status, result_summary, options, j1, j2)
return status, f"{repo['project']} failed.", result_summary
return status, None, None

return status, f"{repo['project']} succeeded.", {}

def perform_snapshot_tests(dir1, dir2, migrate_legacy):
repo_data = read_csv()

def perform_snapshot_tests(dir1: str, dir2: str, projects: List, project_types: Set, migrate_legacy: bool, skipped_projects):
repo_data = read_csv(projects, project_types, skipped_projects)
options = Options(
allow_new_versions=True,
allow_new_data=True,
Expand All @@ -62,11 +84,9 @@ def perform_snapshot_tests(dir1, dir2, migrate_legacy):
failed_diffs = {}
for repo in repo_data:
status, result, summary = compare_snapshot(dir1, dir2, options, repo, migrate_legacy)
if result:
print(result)
print(result)
if status:
failed_diffs[repo["project"]] = summary

if failed_diffs:
diff_file = os.path.join(dir2, 'diffs.json')
print("Snapshot tests failed.")
Expand Down Expand Up @@ -94,14 +114,21 @@ def migrate_to_1_6(bom_file):
return bom_data


def read_csv():
def read_csv(projects, project_types, skipped_projects):
csv_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "repos.csv")
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
repo_data = list(reader)
return repo_data
return filter_repos(repo_data, projects, project_types, skipped_projects)


if __name__ == '__main__':
args = build_args()
perform_snapshot_tests(args.directories[0], args.directories[1], args.migrate_legacy)
if args.project_types:
if ',' in args.project_types:
project_types = set(args.project_types.split(','))
else:
project_types = {args.project_types}
else:
project_types = set()
perform_snapshot_tests(args.directories[0], args.directories[1], args.projects, project_types, args.migrate_legacy, args.skip_projects)
Loading
Loading