From c14be5651b797deca86310efef1fac7f4a8eceef Mon Sep 17 00:00:00 2001 From: Tyson Smith Date: Thu, 14 Apr 2022 12:56:12 -0700 Subject: [PATCH] [adb] Update use of process_minidumps() --- grizzly/target/adb_device/adb_process.py | 17 ++++++++----- grizzly/target/adb_device/test_adb_process.py | 24 +++++++++++-------- grizzly/target/adb_device/test_adb_session.py | 21 +++++----------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/grizzly/target/adb_device/adb_process.py b/grizzly/target/adb_device/adb_process.py index f7cbb69b..83985885 100644 --- a/grizzly/target/adb_device/adb_process.py +++ b/grizzly/target/adb_device/adb_process.py @@ -2,6 +2,7 @@ import re from enum import Enum, unique from logging import getLogger +from pathlib import Path from random import getrandbits from shutil import copy, rmtree from tempfile import NamedTemporaryFile, mkdtemp @@ -328,10 +329,10 @@ def prefs_to_dict(prefs_file): def _process_logs(self, crash_reports): assert self.logs is None self.logs = mkdtemp(prefix="logs_", dir=grz_tmp("logs")) - unprocessed = os.path.join(self.logs, "unprocessed") - os.mkdir(unprocessed) + unprocessed = Path(self.logs) / "unprocessed" + unprocessed.mkdir(exist_ok=True) - with open(os.path.join(self.logs, "log_logcat.txt"), "wb") as log_fp: + with (Path(self.logs) / "log_logcat.txt").open("wb") as log_fp: # TODO: should this filter by pid or not? log_fp.write(self._session.collect_logs()) # log_fp.write(self._session.collect_logs(pid=self._pid)) @@ -341,11 +342,15 @@ def _process_logs(self, crash_reports): # copy crash logs from the device for fname in crash_reports: - self._session.pull(fname, unprocessed) + self._session.pull(fname, str(unprocessed)) with PuppetLogger() as logger: - syms_path = self._session.symbols_path(self._package) - process_minidumps(unprocessed, syms_path, logger.add_log) + if any(unprocessed.glob("*.dmp")): + process_minidumps( + unprocessed, + Path(self._session.symbols_path(self._package)), + logger.add_log, + ) logger.close() logger.save_logs(self.logs) diff --git a/grizzly/target/adb_device/test_adb_process.py b/grizzly/target/adb_device/test_adb_process.py index 7404ca8b..30148e6e 100644 --- a/grizzly/target/adb_device/test_adb_process.py +++ b/grizzly/target/adb_device/test_adb_process.py @@ -183,33 +183,37 @@ def test_adb_process_10(mocker, tmp_path): assert any(dmp_path.glob("fake.txt")) -def test_adb_process_11(mocker): +def test_adb_process_11(mocker, tmp_path): """test ADBProcess._process_logs()""" mocker.patch("grizzly.target.adb_device.adb_process.Bootstrapper", autospec=True) + mocker.patch( + "grizzly.target.adb_device.adb_process.mkdtemp", + autospec=True, + return_value=str(tmp_path), + ) mocker.patch("grizzly.target.adb_device.adb_process.PuppetLogger", autospec=True) fake_proc_md = mocker.patch( "grizzly.target.adb_device.adb_process.process_minidumps", autospec=True ) fake_session = mocker.Mock(spec_set=ADBSession) fake_session.collect_logs.return_value = b"fake logcat data" + fake_session.symbols_path.return_value = "foo" with ADBProcess("org.some.app", fake_session) as proc: # no extra logs proc._process_logs([]) - assert isdir(proc.logs) - try: - assert "log_logcat.txt" in listdir(proc.logs) - finally: - rmtree(proc.logs) + assert "log_logcat.txt" in listdir(proc.logs) + rmtree(proc.logs) proc.logs = None assert fake_proc_md.call_count == 0 assert fake_session.pull.call_count == 0 # other logs available + (tmp_path / "unprocessed").mkdir(parents=True) + (tmp_path / "unprocessed" / "log.dmp").touch() + (tmp_path / "unprocessed" / "asan_log.dmp").touch() proc._process_logs(["log.dmp", "asan_log.txt"]) assert isdir(proc.logs) - try: - assert "log_logcat.txt" in listdir(proc.logs) - finally: - rmtree(proc.logs) + assert "log_logcat.txt" in listdir(proc.logs) + rmtree(proc.logs) assert fake_proc_md.call_count == 1 assert fake_session.pull.call_count == 2 diff --git a/grizzly/target/adb_device/test_adb_session.py b/grizzly/target/adb_device/test_adb_session.py index e7abebf7..07982998 100644 --- a/grizzly/target/adb_device/test_adb_session.py +++ b/grizzly/target/adb_device/test_adb_session.py @@ -652,15 +652,10 @@ def fake_adb_call(cmd, timeout=None): # strip shell args cmd.remove("-T") cmd.remove("-n") - if cmd[2] == "ps": - return ( - 0, - "header... this should be skipped\n" - "1337 1772 1024 org.test.preinstalled\n" - "5847 1 315992 /sbin/adbd\n" - "9990 1772 1221212 org.mozilla.fennec_aurora\n" - "5944 5847 6280 ps\n\n", - ) + if cmd[2] == "pidof": + if cmd[3] == "org.test.preinstalled": + return 0, "1337" + return 1, "" raise AssertionError("unexpected command %r" % (cmd,)) mocker.patch( @@ -1046,12 +1041,8 @@ def fake_adb_call(cmd, timeout=None): cmd.remove("-T") cmd.remove("-n") if cmd[2] == "ps": - assert cmd[-1] == "9990" - return ( - 0, - "PID PPID RSS NAME\n" - "9990 1772 128064 org.mozilla.fennec_aurora\n\n", - ) + assert "9990" in cmd + return 0, "PID\n9990\n\n" raise AssertionError("unexpected command %r" % (cmd,)) mocker.patch(