Skip to content

Commit b52cd2b

Browse files
committed
Merge branch 'zypper-error'
2 parents e820656 + d94167f commit b52cd2b

5 files changed

Lines changed: 65 additions & 19 deletions

File tree

‎CHANGES.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ Misc
1313
Bug fixes and minor changes
1414
---------------------------
1515

16+
+ `#15`_, `#18`_: Send a report also in the case of certain errors
17+
from zypper.
1618
+ `#16`_, `#17`_: Review of the test framework.
1719

1820
.. _#14: https://github.com/RKrahl/auto-patch/pull/14
21+
.. _#15: https://github.com/RKrahl/auto-patch/issues/15
1922
.. _#16: https://github.com/RKrahl/auto-patch/issues/16
2023
.. _#17: https://github.com/RKrahl/auto-patch/pull/17
24+
.. _#18: https://github.com/RKrahl/auto-patch/pull/18
2125

2226

2327
1.1.0 (2022-10-03)

‎scripts/auto-patch.py‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def logging_add_report(cfg, stream):
7070
root = logging.getLogger()
7171
report_hdlr = logging.StreamHandler(stream=stream)
7272
report_hdlr.setLevel(cfg.get('report_level'))
73-
report_hdlr.setFormatter(logging.Formatter(fmt="\n%(message)s"))
73+
fmt = "\n%(levelname)s: %(message)s"
74+
report_hdlr.setFormatter(logging.Formatter(fmt=fmt))
7475
root.addHandler(report_hdlr)
7576
try:
7677
yield None
@@ -292,31 +293,40 @@ def patch(stdout=None):
292293
err.Message += (". Giving up after %d tries." % try_count)
293294
raise err
294295

296+
def make_report(logfile):
297+
logfile.seek(0)
298+
report = logfile.read()
299+
log.debug(report)
300+
if config['mailreport'].getboolean('report'):
301+
msg = EmailMessage()
302+
msg.set_content(report)
303+
msg['From'] = config['mailreport'].get('mailfrom')
304+
msg['To'] = config['mailreport'].get('mailto')
305+
msg['Subject'] = config['mailreport'].get('subject')
306+
mailhost = config['mailreport'].get('mailhost')
307+
with smtplib.SMTP(mailhost) as smtp:
308+
smtp.send_message(msg)
309+
295310
def main():
296311
setup_logging(config['logging'])
297312
with tempfile.TemporaryFile(mode='w+t') as tmpf:
313+
exit_code = 0
298314
with logging_add_report(config['logging'], tmpf):
299-
have_patches = patch(stdout=tmpf)
300-
if have_patches:
301-
tmpf.seek(0)
302-
report = tmpf.read()
303-
log.debug(report)
304-
if config['mailreport'].getboolean('report'):
305-
msg = EmailMessage()
306-
msg.set_content(report)
307-
msg['From'] = config['mailreport'].get('mailfrom')
308-
msg['To'] = config['mailreport'].get('mailto')
309-
msg['Subject'] = config['mailreport'].get('subject')
310-
mailhost = config['mailreport'].get('mailhost')
311-
with smtplib.SMTP(mailhost) as smtp:
312-
smtp.send_message(msg)
315+
try:
316+
have_patches = patch(stdout=tmpf)
317+
except (ZypperCommitError, ZypperRPMScriptfailed,
318+
ZypperSignal) as err:
319+
log.error(err)
320+
exit_code = err.ExitCode
321+
if exit_code or have_patches:
322+
make_report(tmpf)
323+
return exit_code
313324

314325
if __name__ == "__main__":
315326
try:
316-
main()
327+
exit_code = main()
317328
except (ZypperPrivilegesError, ZypperNoReposError, ZypperLockedError,
318-
ZypperCommitError, ZypperSignal, ZypperReposSkipped,
319-
ZypperRPMScriptfailed) as err:
329+
ZypperReposSkipped) as err:
320330
log.error(err)
321331
sys.exit(err.ExitCode)
322332
except ZypperExitException as err:
@@ -327,3 +337,4 @@ def main():
327337
log.critical("Internal error %s: %s", type(err).__name__, err,
328338
exc_info=err)
329339
sys.exit(-1)
340+
sys.exit(exit_code)

‎tests/conftest.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def run(self, exitcode=0):
122122
p.join()
123123
assert p.exitcode == exitcode
124124

125-
def check_report(self):
125+
def check_report(self, extra_msg=None):
126126
with open("report.pickle", "rb") as f:
127127
host = pickle.load(f)
128128
msg = pickle.load(f)
@@ -132,4 +132,7 @@ def check_report(self):
132132
idx = body.find(res.stdout, idx)
133133
assert idx >= 0
134134
idx += len(res.stdout)
135+
if extra_msg:
136+
idx = body.find(extra_msg, idx)
137+
assert idx >= 0
135138
return host, msg

‎tests/test_02_exitcode.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ def test_error_permission(tmpdir):
3434
# assert that no mail report has been sent:
3535
with pytest.raises(FileNotFoundError):
3636
caller.check_report()
37+
38+
39+
def test_error_scripterr(tmpdir):
40+
"""A %post() scriptlet from one of the packages failed.
41+
42+
This may happen, though rarely. The auto-patch should report the
43+
error, but still deliver a report.
44+
"""
45+
with tmpdir.as_cwd():
46+
caller = AutoPatchCaller.get_caller("err_scripterr")
47+
caller.run(exitcode=107)
48+
caller.check_report(extra_msg="ERROR:")

‎tests/zypper-result-data.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,5 +435,21 @@
435435
"returncode": 5,
436436
"stderr": "Root privileges are required to run this command.\n"
437437
}
438+
],
439+
"err_scripterr": [
440+
{
441+
"cmd": "patch-check",
442+
"returncode": 100,
443+
"stdout": "\nCategory | Patches\n------------+--------\nrecommended | 1\n\n1 patch needed (0 security patches)\n"
444+
},
445+
{
446+
"cmd": "list-patches",
447+
"stdout": "\nRepository | Name | Category | Severity | Interactive | Status | Since | Summary\n-------------------------------------------------------------+-----------------------------+-------------+----------+-------------+--------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nUpdate repository with updates from SUSE Linux Enterprise 15 | openSUSE-SLE-15.6-2025-2574 | recommended | moderate | --- | needed | 2025-08-08 | Recommended update for python3-PyNaCl, python3-atomicwrites, python3-cryptography, python3-cryptography-vectors, python3-more-itertools, python3-paramiko, python3-pip, python3-pyOpenSSL, python3-pytest, python3-setuptools\n\n1 patch needed (0 security patches)\n\n"
448+
},
449+
{
450+
"cmd": "patch",
451+
"returncode": 107,
452+
"stdout": "\nThe following 2 packages are going to be upgraded:\n python3-pip python3-pytest\n\nThe following NEW patch is going to be installed:\n openSUSE-SLE-15.6-2025-2574\n\n2 packages to upgrade.\n\nPackage download size: 2.2 MiB\n\nPackage install size change:\n | 10.2 MiB required by packages that will be installed\n -1.8 MiB | - 12.0 MiB released by packages that will be removed\n\nBackend: classic_rpmtrans\nContinue? [y/n/v/...? shows all options] (y): y\n\n"
453+
}
438454
]
439455
}

0 commit comments

Comments
 (0)