Skip to content

Commit

Permalink
improve assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Dec 19, 2024
1 parent a607d13 commit 42c6391
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pants.backend.docker.rules import rules as docker_rules
from pants.backend.docker.target_types import DockerImageTarget
from pants.backend.tools.trivy.rules import rules as trivy_rules
from pants.backend.tools.trivy.testutil import assert_trivy_output, trivy_config
from pants.backend.tools.trivy.testutil import assert_trivy_output, trivy_config, assert_trivy_success
from pants.core.goals import package
from pants.core.goals.lint import LintResult
from pants.core.util_rules import source_files
Expand Down Expand Up @@ -80,8 +80,7 @@ def test_trivy_good(rule_runner: RuleRunner) -> None:
],
)

assert result.exit_code == 0
assert json.loads(result.stdout), "got nothing as output from trivy"
assert_trivy_success(result)


def test_trivy_bad(rule_runner: RuleRunner) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ def rule_runner(standard_deployment: StandardDeployment) -> RuleRunner:
}
)
rule_runner.write_files(trivy_deployment.files)
# # test changing severity
# rule_runner.set_options([
# "--trivy-severity=CRITICAL"
# ])

return rule_runner

Expand Down
29 changes: 26 additions & 3 deletions src/python/pants/backend/tools/trivy/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import json

from pants.core.goals.lint import LintResult

trivy_config = """
format: json
"""


def assert_trivy_output(
result, expected_exit_code: int, target: str, scanner_type: str, expected_error_count: int
result: LintResult, expected_exit_code: int, target: str, scanner_type: str, expected_error_count: int
):
assert result.exit_code == expected_exit_code
report = json.loads(result.stdout)
if result.exit_code != expected_exit_code:
raise AssertionError(f"Trivy process had incorrect exit code, expected={expected_exit_code}, actual={result.exit_code}, stdout={result.stdout}, stderr={result.stderr}")

try:
report = json.loads(result.stdout)
except json.decoder.JSONDecodeError as e:
raise AssertionError(f"Trivy output could not be parsed as JSON, stdout={result.stdout=}, stderr={result.stderr}") from e

findings_by_target = {res["Target"]: res for res in report["Results"]}
assert (
target in findings_by_target
), f"Did not find expected file in results, target={target} files={list(findings_by_target.keys())}"

if scanner_type == "config":
found_count = findings_by_target[target]["MisconfSummary"]["Failures"]
assert (
Expand All @@ -26,3 +35,17 @@ def assert_trivy_output(
assert (
found_count == expected_error_count
), f"Did not find expected vulnerabilities found={found_count} expected={expected_error_count}"




def assert_trivy_success(result: LintResult):
if result.exit_code != 0:
raise AssertionError(
f"Trivy process was not successful, stdout={result.stdout}"
)

try:
json.loads(result.stdout)
except json.decoder.JSONDecodeError as e:
raise AssertionError(f"Trivy output could not be parsed as JSON, stdout={result.stdout}, stderr={result.stderr}") from e

0 comments on commit 42c6391

Please sign in to comment.