Skip to content

Commit

Permalink
Output package versions in debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Jun 20, 2024
1 parent 6e477fb commit 329696f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 12 deletions.
4 changes: 2 additions & 2 deletions grizzly/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .common.fuzzmanager import FM_CONFIG
from .common.plugins import scan as scan_plugins
from .common.plugins import scan_target_assets
from .common.utils import DEFAULT_TIME_LIMIT, TIMEOUT_DELAY, __version__
from .common.utils import DEFAULT_TIME_LIMIT, TIMEOUT_DELAY, package_version


# ref: https://stackoverflow.com/questions/12268602/sort-argparse-help-alphabetically
Expand Down Expand Up @@ -205,7 +205,7 @@ def __init__(self) -> None:
"--version",
"-V",
action="version",
version=__version__,
version=package_version("grizzly-framework"),
help="Show version number",
)
if system().startswith("Linux"):
Expand Down
5 changes: 3 additions & 2 deletions grizzly/common/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
from time import time
from typing import Any, Dict, Generator, Optional, Tuple, Union, cast

from .utils import __version__, grz_tmp
from .utils import grz_tmp, package_version

__all__ = ("TestCase", "TestCaseLoadFailure", "TestFileExists")
__author__ = "Tyson Smith"
__credits__ = ["Tyson Smith"]


LOG = getLogger(__name__)
GRZ_VERSION = package_version("grizzly-framework")
TEST_INFO = "test_info.json"


Expand Down Expand Up @@ -76,7 +77,7 @@ def __init__(
self.input_fname = input_fname # file that was used to create the test case
self.entry_point = self.sanitize_path(entry_point)
self.timestamp = time() if timestamp is None else timestamp
self.version = __version__
self.version = GRZ_VERSION
self._files = TestFileMap()
if data_path is not None:
self._root = data_path
Expand Down
17 changes: 17 additions & 0 deletions grizzly/common/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from importlib.metadata import PackageNotFoundError
from logging import DEBUG, INFO

from pytest import mark
Expand All @@ -12,6 +13,7 @@
configure_logging,
display_time_limits,
grz_tmp,
package_version,
time_limits,
)

Expand Down Expand Up @@ -108,3 +110,18 @@ def test_display_time_limits_01(caplog, time_limit, timeout, no_harness, msg):
"""test display_time_limits()"""
display_time_limits(time_limit, timeout, no_harness)
assert msg in caplog.text


@mark.parametrize(
"version, expected",
[
# missing package
(PackageNotFoundError(), "unknown"),
# success
(("1.2.3",), "1.2.3"),
],
)
def test_package_version_01(mocker, version, expected):
"""test package_version()"""
mocker.patch("grizzly.common.utils.version", autospec=True, side_effect=version)
assert package_version("foo", default="unknown") == expected
23 changes: 18 additions & 5 deletions grizzly/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@
"Exit",
"grz_tmp",
"HARNESS_FILE",
"package_version",
"time_limits",
"TIMEOUT_DELAY",
)
__author__ = "Tyson Smith"
__credits__ = ["Tyson Smith"]

try:
__version__ = version("grizzly-framework")
except PackageNotFoundError: # pragma: no cover
# package is not installed
__version__ = "unknown"

DEFAULT_TIME_LIMIT = 30
GRZ_TMP = Path(getenv("GRZ_TMP", gettempdir()), "grizzly")
Expand Down Expand Up @@ -109,6 +105,23 @@ def display_time_limits(time_limit: int, timeout: int, no_harness: bool) -> None
LOG.warning("TIMEOUT DISABLED, not recommended for automation")


def package_version(name: str, default: str = "unknown") -> str:
"""Get version of an installed package.
Args:
name: Package name.
default: String to use if package is not found.
Returns:
Version string.
"""
try:
return version(name)
except PackageNotFoundError:
# package is not installed
return default


def grz_tmp(*subdir: Union[str, Path]) -> Path:
"""Create (if needed) a temporary working directory in a known location.
Expand Down
9 changes: 8 additions & 1 deletion grizzly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
FuzzManagerReporter,
Reporter,
)
from .common.utils import Exit, configure_logging, display_time_limits, time_limits
from .common.utils import (
Exit,
configure_logging,
display_time_limits,
package_version,
time_limits,
)
from .session import LogRate, Session
from .target import Target, TargetLaunchError, TargetLaunchTimeout

Expand All @@ -30,6 +36,7 @@
def main(args: Namespace) -> int:
configure_logging(args.log_level)
LOG.info("Starting Grizzly (%d)", getpid())
LOG.debug("grizzly-framework version: %s", package_version("grizzly-framework"))

if args.headless:
LOG.info("Running browser headless (%s)", args.headless)
Expand Down
9 changes: 8 additions & 1 deletion grizzly/reduce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
from ..common.status import STATUS_DB_REDUCE, ReductionStatus
from ..common.status_reporter import ReductionStatusReporter
from ..common.storage import TestCase, TestCaseLoadFailure
from ..common.utils import ConfigError, Exit, configure_logging, time_limits
from ..common.utils import (
ConfigError,
Exit,
configure_logging,
package_version,
time_limits,
)
from ..replay import ReplayManager, ReplayResult
from ..target import AssetManager, Target, TargetLaunchError, TargetLaunchTimeout
from .exceptions import GrizzlyReduceBaseException, NotReproducible
Expand Down Expand Up @@ -756,6 +762,7 @@ def main(cls, args: Namespace) -> int:
setlocale(LC_ALL, "")

LOG.info("Starting Grizzly Reduce")
LOG.debug("grizzly-framework version: %s", package_version("grizzly-framework"))

if args.headless:
LOG.info("Running browser headless (%s)", args.headless)
Expand Down
2 changes: 2 additions & 0 deletions grizzly/replay/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
configure_logging,
display_time_limits,
grz_tmp,
package_version,
time_limits,
)
from ..target import (
Expand Down Expand Up @@ -599,6 +600,7 @@ def harness_fn(_: str) -> bytes: # pragma: no cover
def main(cls, args: Namespace) -> int:
configure_logging(args.log_level)
LOG.info("Starting Grizzly Replay")
LOG.debug("grizzly-framework version: %s", package_version("grizzly-framework"))

if args.headless:
LOG.info("Running browser headless (%s)", args.headless)
Expand Down
3 changes: 2 additions & 1 deletion grizzly/target/puppet_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sapphire import CertificateBundle

from ..common.report import Report
from ..common.utils import grz_tmp
from ..common.utils import grz_tmp, package_version
from .target import Result, Target, TargetLaunchError, TargetLaunchTimeout
from .target_monitor import TargetMonitor

Expand Down Expand Up @@ -95,6 +95,7 @@ def __init__(
memory_limit: int,
**kwds: Dict[str, Any],
) -> None:
LOG.debug("ffpuppet version: %s", package_version("ffpuppet"))
certs = cast(Optional[CertificateBundle], kwds.pop("certs", None))
# only pass certs to FFPuppet if certutil is available
# otherwise certs can't be used
Expand Down

0 comments on commit 329696f

Please sign in to comment.