Skip to content

Commit

Permalink
[adb] Update use of process_minidumps()
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Aug 12, 2022
1 parent 241862d commit feb6cdb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
17 changes: 11 additions & 6 deletions grizzly/target/adb_device/adb_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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)

Expand Down
24 changes: 14 additions & 10 deletions grizzly/target/adb_device/test_adb_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 6 additions & 15 deletions grizzly/target/adb_device/test_adb_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit feb6cdb

Please sign in to comment.