Skip to content

Commit

Permalink
[reduce] Fix loading assets and env vars from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Nov 29, 2023
1 parent d009df3 commit f8cfb95
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
12 changes: 7 additions & 5 deletions grizzly/reduce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,12 +843,14 @@ def main(cls, args):
rr=args.rr,
valgrind=args.valgrind,
)
# local environ takes priority over environ loaded from test case
if env_vars is not None:
env_vars.update(target.environ)
target.environ = env_vars
env_vars = None
# TODO: support overriding existing assets
LOG.debug("adding environment loaded from test case")
target.merge_environment(env_vars)
# use asset manager created from test case content if available
if asset_mgr:
target.asset_mgr = asset_mgr
# target is now responsible for `asset_mgr`
asset_mgr = None
# prioritize specified assets over included
target.asset_mgr.add_batch(args.asset)
target.process_assets()
Expand Down
39 changes: 38 additions & 1 deletion grizzly/reduce/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ..common.storage import TestCase, TestCaseLoadFailure
from ..common.utils import ConfigError, Exit
from ..target import Target, TargetLaunchError, TargetLaunchTimeout
from ..target import AssetManager, Target, TargetLaunchError, TargetLaunchTimeout
from . import ReduceManager
from .args import ReduceArgs, ReduceFuzzManagerIDArgs, ReduceFuzzManagerIDQualityArgs
from .exceptions import GrizzlyReduceBaseException
Expand Down Expand Up @@ -255,3 +255,40 @@ def test_main_https_support(mocker, tmp_path, https_supported):
mocker.patch("grizzly.reduce.core.load_plugin", return_value=target_cls)
assert ReduceManager.main(args) == 0
assert target.https.call_count == 1


def test_main_load_assets_and_env(mocker, tmp_path):
"""test ReduceManager.main() - Use assets and env vars from loaded TestCase"""
asset_mgr = mocker.Mock(spec_set=AssetManager)
mocker.patch(
"grizzly.reduce.core.ReplayManager.load_testcases",
autospec=True,
return_value=([], asset_mgr, {"FOO_ENV": "123"}),
)
mocker.patch("grizzly.reduce.core.ReduceManager.run", autospec=True, return_value=0)
mocker.patch("grizzly.reduce.core.ReductionStatus", autospec=True)
mocker.patch("grizzly.reduce.core.Sapphire", autospec=True)
(tmp_path / "test.html").touch()
# setup args
args = mocker.Mock(
ignore=["fake"],
input=[tmp_path / "test.html"],
min_crashes=1,
relaunch=1,
repeat=1,
sig=None,
test_index=None,
time_limit=1,
timeout=1,
)

target = mocker.Mock(spec_set=Target)
target_cls = mocker.MagicMock(spec_set=Target, return_value=target)
mocker.patch("grizzly.reduce.core.load_plugin", return_value=target_cls)
assert ReduceManager.main(args) == 0
assert target.merge_environment.call_count == 1
assert target.merge_environment.call_args.args == ({"FOO_ENV": "123"},)
assert asset_mgr.add_batch.call_count == 1
# this should not be called since the AssetManager was given to the target
# and the target is a mock
assert asset_mgr.cleanup.call_count == 0

0 comments on commit f8cfb95

Please sign in to comment.