Skip to content

Commit

Permalink
refactor test cache workflow
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Sep 5, 2024
1 parent 8496a0a commit aaf6df7
Show file tree
Hide file tree
Showing 33 changed files with 677 additions and 260 deletions.
16 changes: 16 additions & 0 deletions .binny.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,19 @@ tools:
method: github-release
with:
repo: cli/cli

# used to upload test fixture cache
- name: oras
version:
want: v1.2.0
method: github-release
with:
repo: oras-project/oras

# used to upload test fixture cache
- name: yq
version:
want: v4.44.3
method: github-release
with:
repo: mikefarah/yq
29 changes: 22 additions & 7 deletions .github/actions/bootstrap/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ inputs:
cache-key-prefix:
description: "Prefix all cache keys with this value"
required: true
default: "1ac8281053"
compute-fingerprints:
description: "Compute test fixture fingerprints"
default: "181053ac82"
download-test-fixture-cache:
description: "Download test fixture cache from OCI and github actions"
required: true
default: "true"
default: "false"
bootstrap-apt-packages:
description: "Space delimited list of tools to install via apt"
default: "libxml2-utils"


runs:
using: "composite"
steps:
Expand Down Expand Up @@ -55,7 +54,23 @@ runs:
DEBIAN_FRONTEND=noninteractive sudo apt update && sudo -E apt install -y ${{ inputs.bootstrap-apt-packages }}
- name: Create all cache fingerprints
if: inputs.compute-fingerprints == 'true'
id: fingerprint
if: inputs.download-test-fixture-cache == 'true'
shell: bash
run: make fingerprints
run: |
make fingerprints
echo "fingerprint=$(make fingerprint)" | tee -a $GITHUB_OUTPUT
- name: Restore ORAS cache from github actions
if: inputs.download-test-fixture-cache == 'true'
uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2
with:
path: ${{ github.workspace }}/.tool
key: |
${{ inputs.cache-key-prefix }}-${{ runner.os }}-oras-cache-${{ steps.fingerprint.outputs.fingerprint }}
${{ inputs.cache-key-prefix }}-${{ runner.os }}-oras-cache-
- name: Download test fixture cache
if: inputs.download-test-fixture-cache == 'true'
shell: bash
run: make download-test-fixture-cache
11 changes: 0 additions & 11 deletions .github/scripts/ci-check.sh

This file was deleted.

126 changes: 126 additions & 0 deletions .github/scripts/find_cache_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
import os
import glob
import sys
import json
import hashlib


IGNORED_PREFIXES = []


def find_fingerprints_and_check_dirs(base_dir):
all_fingerprints = set(glob.glob(os.path.join(base_dir, '**', 'test*', '**', '*.fingerprint'), recursive=True))
cache_dirs = set(glob.glob(os.path.join(base_dir, '**', 'test-fixtures', 'cache'), recursive=True))

all_fingerprints = {os.path.relpath(fp) for fp in all_fingerprints
if not any(fp.startswith(prefix) for prefix in IGNORED_PREFIXES)}

if not all_fingerprints and not cache_dirs:
show("No .fingerprint files or cache directories found.")
exit(1)

missing_content = []
valid_paths = set()
fingerprint_contents = []

for fingerprint in all_fingerprints:
path = fingerprint.replace('.fingerprint', '')

if not os.path.exists(path):
missing_content.append(path)
continue

if not os.path.isdir(path):
valid_paths.add(path)
continue

if os.listdir(path):
valid_paths.add(path)
else:
missing_content.append(path)

with open(fingerprint, 'r') as f:
content = f.read().strip()
fingerprint_contents.append((fingerprint, content))

# add cache dirs to valid paths
for cache_dir in cache_dirs:
if os.path.isdir(cache_dir) and os.listdir(cache_dir):
valid_paths.add(os.path.relpath(cache_dir))
else:
missing_content.append(os.path.relpath(cache_dir))

return sorted(valid_paths), missing_content, fingerprint_contents


def calculate_sha256(fingerprint_contents):
sorted_fingerprint_contents = sorted(fingerprint_contents, key=lambda x: x[0])

concatenated_contents = ''.join(content for _, content in sorted_fingerprint_contents)

sha256_hash = hashlib.sha256(concatenated_contents.encode()).hexdigest()

return sha256_hash


def show(*s: str):
print(*s, file=sys.stderr)


# outputs a input hash and set of paths that should be cached, for example:
# {
# "input": "85ae27de8da54826dbb374e8a302b19c9ecc6361f1714dce88f2e45a60b58870",
# "paths": [
# "cmd/syft/internal/test/integration/test-fixtures/cache",
# "syft/file/cataloger/executable/test-fixtures/elf/bin",
# "syft/file/cataloger/executable/test-fixtures/shared-info/bin",
# "syft/file/cataloger/filedigest/test-fixtures/cache",
# "syft/file/cataloger/filemetadata/test-fixtures/cache",
# "syft/file/cataloger/internal/test-fixtures/cache",
# "syft/format/internal/testutil/test-fixtures/cache",
# "syft/format/syftjson/test-fixtures/cache",
# "syft/internal/fileresolver/test-fixtures/cache",
# "syft/pkg/cataloger/binary/test-fixtures/cache",
# "syft/pkg/cataloger/binary/test-fixtures/classifiers/bin",
# "syft/pkg/cataloger/binary/test-fixtures/classifiers/dynamic",
# "syft/pkg/cataloger/debian/test-fixtures/cache",
# "syft/pkg/cataloger/golang/test-fixtures/archs/binaries",
# "syft/pkg/cataloger/golang/test-fixtures/cache",
# "syft/pkg/cataloger/java/test-fixtures/jar-metadata/cache",
# "syft/pkg/cataloger/java/test-fixtures/java-builds/packages",
# "syft/pkg/cataloger/kernel/test-fixtures/cache",
# "syft/pkg/cataloger/python/test-fixtures/cache",
# "syft/pkg/cataloger/redhat/test-fixtures/cache",
# "syft/pkg/cataloger/redhat/test-fixtures/rpms",
# "syft/pkg/cataloger/rust/test-fixtures/cache",
# "syft/source/test-fixtures/cache",
# "test/cli/test-fixtures/cache",
# "test/install/cache"
# ]
# }
#
# ...if the input is inconsistent then this script will exit with a non-zero status code instead.
def main():
base_dir = '.'
valid_paths, missing_content, fingerprint_contents = find_fingerprints_and_check_dirs(base_dir)

if missing_content:
show("The following paths are missing or have no content, but have corresponding .fingerprint files:")
for path in sorted(missing_content):
show(f"- {path}")
show("Please ensure these paths exist and have content if they are directories.")
exit(1)

sha256_hash = calculate_sha256(fingerprint_contents)

output = {
"input": sha256_hash,
"paths": sorted(valid_paths)
}

print(json.dumps(output, indent=2))


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion .github/workflows/update-bootstrap-tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ jobs:
uses: ./.github/actions/bootstrap
with:
bootstrap-apt-packages: ""
compute-fingerprints: "false"
go-dependencies: false

- name: "Update tool versions"
Expand Down
74 changes: 7 additions & 67 deletions .github/workflows/validations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,8 @@ jobs:

- name: Bootstrap environment
uses: ./.github/actions/bootstrap

- name: Restore file executable test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/file/cataloger/executable/test-fixtures/elf/bin
key: ${{ runner.os }}-unit-file-executable-elf-cache-${{ hashFiles( 'syft/file/cataloger/executable/test-fixtures/elf/cache.fingerprint' ) }}

- name: Restore file executable shared-info test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/file/cataloger/executable/test-fixtures/shared-info/bin
key: ${{ runner.os }}-unit-file-executable-shared-info-cache-${{ hashFiles( 'syft/file/cataloger/executable/test-fixtures/shared-info/cache.fingerprint' ) }}

- name: Restore Java test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/pkg/cataloger/java/test-fixtures/java-builds/packages
key: ${{ runner.os }}-unit-java-cache-${{ hashFiles( 'syft/pkg/cataloger/java/test-fixtures/java-builds/cache.fingerprint' ) }}

- name: Restore RPM test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/pkg/cataloger/redhat/test-fixtures/rpms
key: ${{ runner.os }}-unit-rpm-cache-${{ hashFiles( 'syft/pkg/cataloger/redhat/test-fixtures/rpms.fingerprint' ) }}

- name: Restore go binary test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/pkg/cataloger/golang/test-fixtures/archs/binaries
key: ${{ runner.os }}-unit-go-binaries-cache-${{ hashFiles( 'syft/pkg/cataloger/golang/test-fixtures/archs/binaries.fingerprint' ) }}

- name: Restore binary cataloger test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/pkg/cataloger/binary/test-fixtures/classifiers/bin
key: ${{ runner.os }}-unit-binary-cataloger-cache-${{ hashFiles( 'syft/pkg/cataloger/binary/test-fixtures/cache.fingerprint' ) }}

- name: Restore Kernel test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: syft/pkg/cataloger/kernel/test-fixtures/cache
key: ${{ runner.os }}-unit-kernel-cache-${{ hashFiles( 'syft/pkg/cataloger/kernel/test-fixtures/cache.fingerprint' ) }}
download-test-fixture-cache: true

- name: Run unit tests
run: make unit
Expand All @@ -91,16 +51,12 @@ jobs:

- name: Bootstrap environment
uses: ./.github/actions/bootstrap
with:
download-test-fixture-cache: true

- name: Validate syft output against the CycloneDX schema
run: make validate-cyclonedx-schema

- name: Restore integration test cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: ${{ github.workspace }}/cmd/syft/internal/test/integration/test-fixtures/cache
key: ${{ runner.os }}-integration-test-cache-${{ hashFiles('/cmd/syft/internal/test/integration/test-fixtures/cache.fingerprint') }}

- name: Run integration tests
run: make integration

Expand Down Expand Up @@ -143,6 +99,8 @@ jobs:

- name: Bootstrap environment
uses: ./.github/actions/bootstrap
with:
download-test-fixture-cache: true

- name: Download snapshot build
id: snapshot-cache
Expand All @@ -162,13 +120,6 @@ jobs:
- name: Run comparison tests (Linux)
run: make compare-linux

- name: Restore install.sh test image cache
id: install-test-image-cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: ${{ github.workspace }}/test/install/cache
key: ${{ runner.os }}-install-test-image-cache-${{ hashFiles('test/install/cache.fingerprint') }}

- name: Load test image cache
if: steps.install-test-image-cache.outputs.cache-hit == 'true'
run: make install-test-cache-load
Expand Down Expand Up @@ -196,8 +147,8 @@ jobs:
uses: ./.github/actions/bootstrap
with:
bootstrap-apt-packages: ""
compute-fingerprints: "false"
go-dependencies: false
download-test-fixture-cache: true

- name: Download snapshot build
id: snapshot-cache
Expand All @@ -214,13 +165,6 @@ jobs:
if: steps.snapshot-cache.outputs.cache-hit != 'true'
run: echo "unable to download snapshots from previous job" && false

- name: Restore docker image cache for compare testing
id: mac-compare-testing-cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: image.tar
key: ${{ runner.os }}-${{ hashFiles('test/compare/mac.sh') }}

- name: Run comparison tests (Mac)
run: make compare-mac

Expand All @@ -238,12 +182,8 @@ jobs:

- name: Bootstrap environment
uses: ./.github/actions/bootstrap

- name: Restore CLI test-fixture cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 #v4.0.2
with:
path: ${{ github.workspace }}/test/cli/test-fixtures/cache
key: ${{ runner.os }}-cli-test-cache-${{ hashFiles('test/cli/test-fixtures/cache.fingerprint') }}
download-test-fixture-cache: true

- name: Download snapshot build
id: snapshot-cache
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ ci-bootstrap-go:

# this is a bootstrapping catch-all, where if the target doesn't exist, we'll ensure the tools are installed and then try again
%:
make $(TASK)
$(TASK) $@
@make --silent $(TASK)
@$(TASK) $@

## Shim targets #################################

Expand Down
Loading

0 comments on commit aaf6df7

Please sign in to comment.