Skip to content

Commit

Permalink
fix validation workflow and linting
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Aug 17, 2023
1 parent f6d31f0 commit 6f032a9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 38 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/validations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
with:
python: false

- name: Run unit tests
run: make unit-go
- name: Run go unit tests
run: make unit

# Note: changing this job name requires making the same update in the .github/workflows/release.yaml pipeline
Unit-Test-Python:
Expand All @@ -56,8 +56,8 @@ jobs:
with:
go: false

- name: Run unit tests
run: make unit-python
- name: Run python unit tests
run: cd manager && make unit

# Note: changing this job name requires making the same update in the .github/workflows/release.yaml pipeline
Build-Snapshot-Artifacts:
Expand Down
1 change: 0 additions & 1 deletion manager/src/grype_db_manager/cli/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import click
import yardstick
from tabulate import tabulate
from yardstick import store
from yardstick.cli import config as ycfg
from yardstick.tool.grype import Grype
from yardstick.tool.syft import Syft
Expand Down
83 changes: 51 additions & 32 deletions manager/src/grype_db_manager/db/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import collections
import logging
from dataclasses import InitVar, dataclass, field
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import yardstick
from yardstick import artifact, capture, comparison, store
Expand Down Expand Up @@ -31,15 +31,17 @@ class GateConfig:
introduced_fns_threshold: int = 0


_default_gate_config = GateConfig()
# note: a class property on a dataclass will not work since it is not mutable (cannot convey
# changes to all future instances)
_default_gate_config = GateConfig() # noqa: PLW0603


def _get_config() -> GateConfig:
return _default_gate_config


def set_default_gate_config(config: GateConfig) -> None:
global _default_gate_config
global _default_gate_config # noqa: PLW0603
_default_gate_config = config


Expand Down Expand Up @@ -338,37 +340,20 @@ def log_validation_results(
verbosity: int = 0,
) -> None:
if verbosity > 2:
image = sorted(stats_by_image_tool_pair.true_positives.keys())[0]
tools = sorted(stats_by_image_tool_pair.true_positives[image].keys())
table = format.stats_table_by_tool(
tools,
image,
stats_by_image_tool_pair,
)

logging.info(table)
_log_stats(stats_by_image_tool_pair)

if verbosity > 1:
# show false negative label entries
fns_by_id = {}
for result in results:
comp = comparisons_by_result_id[result.ID]
fns = comp.false_negative_label_entries
fns_by_id[result.ID] = fns

unique_fns_by_id = collections.defaultdict(list)
for result_id, fns in fns_by_id.items():
for fn in fns:
if _is_unique_fn(fns_by_id, result_id, fn):
unique_fns_by_id[result_id].append(fn)

for result in results:
fns = unique_fns_by_id[result.ID]
ret = f"false negatives found uniquely in result={result.ID}: {len(fns)}\n"
for label in fns:
ret += f"{format.space} {label.summarize()}\n"
logging.info(ret.rstrip())
_log_fns(results, comparisons_by_result_id)

_log_differences(relative_comparison, results, comparisons_by_result_id, verbosity)


def _log_differences(
relative_comparison: comparison.ByPreservedMatch,
results: list[artifact.ScanResult],
comparisons_by_result_id: dict[str, list[comparison.AgainstLabels]],
verbosity: int,
) -> None:
latest_release_tool, current_tool = guess_tool_orientation([r.config.tool for r in results])

table, diffs = format.match_differences_table(
Expand All @@ -386,7 +371,41 @@ def log_validation_results(
logging.info(f"match differences found between tooling: {diffs}")


def _is_unique_fn(fns_by_id, result_id, fn):
def _log_stats(stats_by_image_tool_pair: comparison.ImageToolLabelStats) -> None:
image = sorted(stats_by_image_tool_pair.true_positives.keys())[0]
tools = sorted(stats_by_image_tool_pair.true_positives[image].keys())
table = format.stats_table_by_tool(
tools,
image,
stats_by_image_tool_pair,
)

logging.info(table)


def _log_fns(results: list[artifact.ScanResult], comparisons_by_result_id: dict[str, list[comparison.AgainstLabels]]) -> None:
# show false negative label entries
fns_by_id = {}
for result in results:
comp = comparisons_by_result_id[result.ID]
fns = comp.false_negative_label_entries
fns_by_id[result.ID] = fns

unique_fns_by_id = collections.defaultdict(list)
for result_id, fns in fns_by_id.items():
for fn in fns:
if _is_unique_fn(fns_by_id, result_id, fn):
unique_fns_by_id[result_id].append(fn)

for result in results:
fns = unique_fns_by_id[result.ID]
ret = f"false negatives found uniquely in result={result.ID}: {len(fns)}\n"
for label in fns:
ret += f"{format.space} {label.summarize()}\n"
logging.info(ret.rstrip())


def _is_unique_fn(fns_by_id: dict[str, set[str]], result_id: str, fn: Any) -> bool:
for other_result_id, other_fns in fns_by_id.items():
if other_result_id == result_id:
continue
Expand Down
3 changes: 2 additions & 1 deletion manager/src/grype_db_manager/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def listing_entries_dbs_in_s3( # noqa: PLR0913
suffixes = DB_SUFFIXES

if not download_url_prefix:
raise ValueError("download_url_prefix must be specified")
msg = "download_url_prefix must be specified"
raise ValueError(msg)

# generate metadata from each downloaded archive and add to the listing file
for basename in basenames:
Expand Down

0 comments on commit 6f032a9

Please sign in to comment.