Skip to content

Commit

Permalink
test: let do_crash return the test crash report filename
Browse files Browse the repository at this point in the history
The property `test_report` can be set or None, but will always be set
after calling `do_crash`. Let `do_crash` return the test crash report
filename to ease mypy's type check.
  • Loading branch information
bdrung committed Feb 19, 2025
1 parent 9975f02 commit 79033d8
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions tests/integration/test_signal_crashes.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,24 +235,24 @@ def test_empty_core_dump(self) -> None:

self._check_report(expect_report=False)

def test_crash_apport(self):
def test_crash_apport(self) -> None:

Check warning on line 238 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L238

Added line #L238 was not covered by tests
"""Report generation with apport."""
self.do_crash()
st = os.stat(self.test_report)
test_report = self.do_crash()
st = os.stat(test_report)

Check warning on line 241 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L240-L241

Added lines #L240 - L241 were not covered by tests

# a subsequent crash does not alter unseen report
self.do_crash()
st2 = os.stat(self.test_report)
test_report = self.do_crash()
st2 = os.stat(test_report)

Check warning on line 245 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L244-L245

Added lines #L244 - L245 were not covered by tests
self.assertEqual(st, st2, "original unseen report did not get overwritten")

# a subsequent crash alters seen report
apport.fileutils.mark_report_seen(self.test_report)
self.do_crash()
st2 = os.stat(self.test_report)
apport.fileutils.mark_report_seen(test_report)
test_report = self.do_crash()
st2 = os.stat(test_report)

Check warning on line 251 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L249-L251

Added lines #L249 - L251 were not covered by tests
self.assertNotEqual(st, st2, "original seen report gets overwritten")

pr = apport.Report()
with open(self.test_report, "rb") as f:
with open(test_report, "rb") as f:
pr.load(f)
self.assertTrue(
set(required_fields).issubset(set(pr.keys())), "report has required fields"
Expand Down Expand Up @@ -457,11 +457,11 @@ def test_flood_limit(self) -> None:
while count < 7:
sys.stderr.write(f"{count} ")
sys.stderr.flush()
self.do_crash()
test_report = self.do_crash()

Check warning on line 460 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L460

Added line #L460 was not covered by tests
reports = apport.fileutils.get_new_reports()
if not reports:
break
apport.fileutils.mark_report_seen(self.test_report)
apport.fileutils.mark_report_seen(test_report)

Check warning on line 464 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L464

Added line #L464 was not covered by tests
count += 1
self.assertGreater(count, 1, "gets at least 2 repeated crashes")
self.assertLess(count, 7, "stops flooding after less than 7 repeated crashes")
Expand Down Expand Up @@ -491,17 +491,17 @@ def test_core_dump_packaged(self) -> None:
for sig in (signal.SIGSEGV, signal.SIGABRT):
for kb, exp_file in core_ulimit_table:
resource.setrlimit(resource.RLIMIT_CORE, (kb, -1))
self.do_crash(
test_report = self.do_crash(

Check warning on line 494 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L494

Added line #L494 was not covered by tests
expect_corefile=exp_file,
expect_corefile_owner=os.geteuid(),
sig=sig,
)
self.check_report_coredump(self.test_report)
apport.fileutils.delete_report(self.test_report)
self.check_report_coredump(test_report)
apport.fileutils.delete_report(test_report)

Check warning on line 500 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L499-L500

Added lines #L499 - L500 were not covered by tests

# creates core file with existing crash report, too
self.do_crash(expect_corefile=True)
apport.fileutils.delete_report(self.test_report)
test_report = self.do_crash(expect_corefile=True)
apport.fileutils.delete_report(test_report)

Check warning on line 504 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L503-L504

Added lines #L503 - L504 were not covered by tests

def test_core_dump_packaged_sigquit(self) -> None:
"""Packaged executables create core files, no report for SIGQUIT."""
Expand Down Expand Up @@ -578,14 +578,14 @@ def inject_bogus_report():
# do_crash verifies that we get the original core, not the injected one
self.do_crash(expect_corefile=True, hook_before_apport=inject_bogus_report)

def test_ignore(self):
def test_ignore(self) -> None:

Check warning on line 581 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L581

Added line #L581 was not covered by tests
"""Ignore executables."""
self.do_crash()
test_report = self.do_crash()

Check warning on line 583 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L583

Added line #L583 was not covered by tests

pr = apport.Report()
with open(self.test_report, "rb") as f:
with open(test_report, "rb") as f:
pr.load(f)
os.unlink(self.test_report)
os.unlink(test_report)

Check warning on line 588 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L588

Added line #L588 was not covered by tests

pr.mark_ignore()

Expand Down Expand Up @@ -786,16 +786,16 @@ def test_crash_setuid_unpackaged(self) -> None:
suid_dumpable=2,
)

def test_coredump_from_socket(self):
def test_coredump_from_socket(self) -> None:

Check warning on line 789 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L789

Added line #L789 was not covered by tests
"""Forward a core dump through a socket.
This is being used in a container via systemd activation, where the
core dump gets read from /run/apport.socket.
"""
self.do_crash(via_socket=True)
test_report = self.do_crash(via_socket=True)

Check warning on line 795 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L795

Added line #L795 was not covered by tests

pr = apport.Report()
with open(self.test_report, "rb") as f:
with open(test_report, "rb") as f:
pr.load(f)
self.assertEqual(pr["Signal"], "11")
self.assertEqual(pr["ExecutablePath"], self.TEST_EXECUTABLE)
Expand All @@ -811,17 +811,17 @@ def test_core_dump_packaged_sigquit_via_socket(self) -> None:
)

@unittest.skipIf(os.geteuid() != 0, "this test needs to be run as root")
def test_crash_setuid_drop_via_socket(self):
def test_crash_setuid_drop_via_socket(self) -> None:
"""Report generation via socket for setuid program which drops root."""
with create_dropsuid() as dropsuid:
resource.setrlimit(resource.RLIMIT_CORE, (-1, -1))
self.do_crash(
test_report = self.do_crash(

Check warning on line 818 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L818

Added line #L818 was not covered by tests
command=dropsuid, uid=MAIL_UID, suid_dumpable=2, via_socket=True
)

# check crash report
report = apport.Report()
with open(self.test_report, "rb") as report_file:
with open(test_report, "rb") as report_file:
report.load(report_file)
self.assertEqual(report["Signal"], "11")
self.assertEqual(report["ExecutablePath"], dropsuid)
Expand Down Expand Up @@ -1120,7 +1120,7 @@ def do_crash(
via_socket: bool = False,
cwd: str | None = None,
**kwargs: typing.Any,
) -> None:
) -> str:
# TODO: Split into smaller functions/methods
# pylint: disable=too-many-branches,too-many-locals,too-many-statements
"""Generate a test crash.
Expand Down Expand Up @@ -1239,6 +1239,7 @@ def do_crash(
expect_report=expect_report,
expected_owner=0 if suid_dumpable == 2 else os.geteuid(),
)
return self.test_report

Check warning on line 1242 in tests/integration/test_signal_crashes.py

View check run for this annotation

Codecov / codecov/patch

tests/integration/test_signal_crashes.py#L1242

Added line #L1242 was not covered by tests

@staticmethod
def gdb_command(
Expand Down

0 comments on commit 79033d8

Please sign in to comment.