Skip to content

Commit

Permalink
Always create an AssetManager when creating Target
Browse files Browse the repository at this point in the history
Also make references to AssetManager less ambiguous.
  • Loading branch information
tysmith committed Nov 13, 2023
1 parent 766c310 commit 72b2bd1
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 215 deletions.
16 changes: 8 additions & 8 deletions grizzly/common/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ def test_testcase_08(tmp_path):
(tmp_path / "src" / "nested" / "empty").mkdir()
dst_dir = tmp_path / "dst"
# build test case
with AssetManager(base_path=tmp_path) as assets:
assets.add("example", asset_file)
with AssetManager(base_path=tmp_path) as asset_mgr:
asset_mgr.add("example", asset_file)
with TestCase("target.bin", "test-adapter") as src:
src.env_vars["TEST_ENV_VAR"] = "100"
src.add_from_file(entry_point)
Expand All @@ -230,8 +230,8 @@ def test_testcase_08(tmp_path):
file_name=str((nested / "x.bin").relative_to(src_dir)),
required=False,
)
src.assets = dict(assets.assets)
src.assets_path = assets.path
src.assets = dict(asset_mgr.assets)
src.assets_path = asset_mgr.path
src.dump(dst_dir, include_details=True)
# test loading test case from test_info.json
with TestCase.load_single(dst_dir) as dst:
Expand Down Expand Up @@ -334,11 +334,11 @@ def test_testcase_14(tmp_path):
nested.mkdir()
asset_file = tmp_path / "example_asset"
asset_file.touch()
with AssetManager(base_path=tmp_path) as assets:
assets.add("example", asset_file)
with AssetManager(base_path=tmp_path) as asset_mgr:
asset_mgr.add("example", asset_file)
with TestCase("target.bin", "test-adapter") as src:
src.assets = assets.assets
src.assets_path = assets.path
src.assets = asset_mgr.assets
src.assets_path = asset_mgr.path
src.add_from_bytes(b"test", "target.bin")
src.dump(nested / "test-1", include_details=True)
src.dump(nested / "test-2", include_details=True)
Expand Down
2 changes: 1 addition & 1 deletion grizzly/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def main(args):
valgrind=args.valgrind,
)
# add specified assets
target.assets.add_batch(args.asset)
target.asset_mgr.add_batch(args.asset)
target.process_assets()
adapter.monitor = target.monitor

Expand Down
19 changes: 10 additions & 9 deletions grizzly/reduce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,14 @@ def run(self, repeat=1, launch_attempts=3, min_results=1, post_launch_delay=0):
for testcase in self.testcases:
testcase.cleanup()
# add target assets to test cases
if not self.target.assets.is_empty():
if not self.target.asset_mgr.is_empty():
for test in reduction:
test.assets = dict(
self.target.assets.assets
self.target.asset_mgr.assets
)
test.assets_path = (
self.target.asset_mgr.path
)
test.assets_path = self.target.assets.path
# add target environment variables
if self.target.filtered_environ():
for test in reduction:
Expand Down Expand Up @@ -783,7 +785,7 @@ def main(cls, args):
elif args.valgrind:
LOG.info("Running with Valgrind. This will be SLOW!")

assets = None
asset_mgr = None
certs = None
signature = None
signature_desc = None
Expand All @@ -798,7 +800,7 @@ def main(cls, args):
signature_desc = meta["shortDescription"]

try:
testcases, assets, env_vars = ReplayManager.load_testcases(
testcases, asset_mgr, env_vars = ReplayManager.load_testcases(
args.input, subset=args.test_index
)
except TestCaseLoadFailure as exc:
Expand Down Expand Up @@ -834,7 +836,6 @@ def main(cls, args):
args.launch_timeout,
args.log_limit,
args.memory,
assets=assets,
certs=certs,
headless=args.headless,
pernosco=args.pernosco,
Expand All @@ -848,7 +849,7 @@ def main(cls, args):
env_vars = None
# TODO: support overriding existing assets
# prioritize specified assets over included
target.assets.add_batch(args.asset)
target.asset_mgr.add_batch(args.asset)
target.process_assets()

if certs and not target.https():
Expand Down Expand Up @@ -919,8 +920,8 @@ def main(cls, args):
target.cleanup()
for testcase in testcases:
testcase.cleanup()
if assets:
assets.cleanup()
if asset_mgr:
asset_mgr.cleanup()
if certs is not None:
certs.cleanup()
LOG.info("Done.")
16 changes: 8 additions & 8 deletions grizzly/reduce/test_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ def replay_run(testcases, _time_limit, **kw):

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -650,7 +650,7 @@ def fake_iter():

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -722,7 +722,7 @@ def fake_iter():

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -783,7 +783,7 @@ def replay_run(testcases, _time_limit, **kw):
update_coll = mocker.patch("grizzly.common.fuzzmanager.Collector")
target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -855,10 +855,10 @@ def submit(test_cases, report, force=False):

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {"test": "abc"}
with AssetManager(base_path=str(tmp_path)) as assets:
with AssetManager(base_path=tmp_path) as asset_mgr:
(tmp_path / "example_asset").touch()
assets.add("example", tmp_path / "example_asset", copy=False)
target.assets = assets
asset_mgr.add("example", tmp_path / "example_asset", copy=False)
target.asset_mgr = asset_mgr
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -979,7 +979,7 @@ def replay_run(_testcases, _time_limit, **kw):

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
server = mocker.Mock(spec_set=Sapphire, timeout=iter_input)
try:
mgr = ReduceManager(
Expand Down
6 changes: 3 additions & 3 deletions grizzly/reduce/test_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def replay_run(testcases, _time_limit, **kw):

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -361,7 +361,7 @@ def replay_run(testcases, _time_limit, **kw):

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down Expand Up @@ -421,7 +421,7 @@ def replay_run(testcases, _time_limit, **kw):

target = mocker.Mock(spec_set=Target)
target.filtered_environ.return_value = {}
target.assets = mocker.Mock(spec_set=AssetManager)
target.asset_mgr = mocker.Mock(spec_set=AssetManager)
try:
mgr = ReduceManager(
[],
Expand Down
30 changes: 17 additions & 13 deletions grizzly/replay/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def load_testcases(cls, path, subset=None):
if not testcases:
raise TestCaseLoadFailure("Failed to load TestCases")
# load and remove assets and environment variables from test cases
assets = None
asset_mgr = None
env_vars = None
for test in testcases:
if assets is None and test.assets and test.assets_path:
assets = AssetManager.load(test.assets, test.root / test.assets_path)
if asset_mgr is None and test.assets and test.assets_path:
asset_mgr = AssetManager.load(test.assets, test.root / test.assets_path)
if not env_vars and test.env_vars:
env_vars = dict(test.env_vars)
test.env_vars.clear()
Expand All @@ -190,7 +190,7 @@ def load_testcases(cls, path, subset=None):
LOG.debug(
"loaded TestCase(s): %d, assets: %r, env vars: %r",
len(testcases),
assets is not None,
asset_mgr is not None,
env_vars is not None,
)
if subset:
Expand All @@ -205,7 +205,7 @@ def load_testcases(cls, path, subset=None):
for test in testcases:
test.cleanup()
testcases = selected
return testcases, assets, env_vars
return testcases, asset_mgr, env_vars

@staticmethod
def report_to_filesystem(path, results, tests=None):
Expand Down Expand Up @@ -604,7 +604,7 @@ def main(cls, args):
signature = CrashSignature.fromFile(args.sig) if args.sig else None

try:
testcases, assets, env_vars = cls.load_testcases(
testcases, asset_mgr, env_vars = cls.load_testcases(
args.input, subset=args.test_index
)
except TestCaseLoadFailure as exc:
Expand Down Expand Up @@ -645,7 +645,6 @@ def main(cls, args):
args.launch_timeout,
args.log_limit,
args.memory,
assets=assets,
certs=certs,
headless=args.headless,
pernosco=args.pernosco,
Expand All @@ -656,8 +655,13 @@ def main(cls, args):
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
# TODO: prioritize specified assets over included
target.assets.add_batch(args.asset)
target.asset_mgr.add_batch(args.asset)
target.process_assets()

if certs and not target.https():
Expand Down Expand Up @@ -699,10 +703,10 @@ def main(cls, args):
LOG.info("No results detected")
if results and (args.logs or args.fuzzmanager):
# add target assets to test cases
if not target.assets.is_empty():
if not target.asset_mgr.is_empty():
for test in testcases:
test.assets = dict(target.assets.assets)
test.assets_path = target.assets.path
test.assets = dict(target.asset_mgr.assets)
test.assets_path = target.asset_mgr.path
# add target environment variables
if target.filtered_environ():
for test in testcases:
Expand Down Expand Up @@ -740,8 +744,8 @@ def main(cls, args):
target.cleanup()
for testcase in testcases:
testcase.cleanup()
if assets:
assets.cleanup()
if asset_mgr:
asset_mgr.cleanup()
if certs is not None:
certs.cleanup()
LOG.info("Done.")
Loading

0 comments on commit 72b2bd1

Please sign in to comment.